Tuesday, 24 May 2011

PHP - Unzip an uploaded file using php

$zip = new ZipArchive;
     $res = $zip->open(’my_zip_file.zip’);
     if ($res === TRUE) {
         $zip->extractTo(’my_extract_to_dir/’);
         $zip->close();
         echo ‘ok’;
     } else {
         echo ‘failed’;
     }

Views3 view slideshow fixing

Well... I got it to work using the following:

<?phpfunction phptemplate_views_slideshow_thumbnailhover_no_display_section($view, $rows, $id, $options, $teaser = TRUE) {
 
// Add the slideshow elements.
 
$attributes['id'] = "views_slideshow_thumbnailhover_teaser_section_$id";
 
$attributes['class'] = 'views_slideshow_thumbnailhover_teaser_section views_slideshow_teaser_section';
 
$attributes = drupal_attributes($attributes);

 
$output = "<div$attributes>";
  foreach (
$view->result as $count => $node) {
    if (
$view->display_handler->uses_fields()) {
     
$rendered = '';
      foreach (
$options['views_slideshow_thumbnailhover']['main_fields'] as $field => $use) {
        if (
is_object($view->field[$field])) {
         
$rendered_field = $view->field[$field]->theme($node);
         
         
// If the field is excluded then don't display it.
          // It must be rendered first though so that Custom Text can use it.
         
if (!$view->field[$field]->options['exclude']) {
            if ((
$use !== 0) && !empty($rendered_field)) {
             
$rendered .= '<div class="views-field-'. views_css_safe($view->field[$field]->field) .'">';
              if (
$view->field[$field]->label()) {
               
$rendered .= '<label class="view-label-'. views_css_safe($view->field[$field]->field) . '">';
               
$rendered .= $view->field[$field]->label() . ':';
               
$rendered .= '</label>';
              }
             
$rendered .= '<div class="views-content-'. views_css_safe($view->field[$field]->field) .'">';
             
// !custom $rendered .=  $rendered_field;
             
if ($use == 'nid' && is_numeric($rendered_field)) {
               
$rendered .= node_view(node_load($rendered_field),true);
              } else {
               
$rendered .= $rendered_field;
              }
             
// end custom
             
$rendered .= '</div>';
             
$rendered .= '</div>';
            }
          }
        }
      }
    }
    else {
     
$rendered = node_view(node_load($node->nid), $teaser, FALSE, FALSE);
    }
   
$output .= theme('views_slideshow_thumbnailhover_no_display_teaser', $rendered, $id, $count);
  }
 
$output .= "</div>";
  return
$output;
}
?>
But it does seem a bit roundabout just to get the tpl-overridden teaser to display correctly.
Just to summarize what I did for reference:
* Create a view (Views Slideshow)
* Add set it to Thumbnail Hover
* Add two fields - Node:Nid and whatever other you want to use for breakouts
* Slideshow settings: set Node:Nid as the main frame and the other field as breakouts
* Override your node teaser display using node.tpl.php
* Use the above function to convert Node:Nid field into an overridden teaser display
Now the teaser shows up as it's overridden by node.tpl.php

and so on

Prerequisite

- 1 content type dedicated to this slideshow that I called Customer.
- This content type has 1 cck image field to store the ad.
- Mandatory Assigment of customer nodes to an Industry taxonomy
- Imagecache preset to display my node images with fixed dimensions
This tutorial is based on a page view using slideshow as style for the left side of the page, and an attachment view renders the right side of the screen (this is not a block, just a floated div).

View setup

Let's first build the view without the attachment which is only used to filter by industry
create a new view and under

Default basic settings

under Style choose slideshow:
mode ThumbnailHover
List type: ordered list
no grouping
Hover breakout: full node
Timer delay:5000
Initial fade delay:-2000
speed:400
Pause: yes when hovering the node
Pager event:hover
control:none
image counter:none
effect:fade
Row style: fields (will only apply to our node title in our case)
Items to display: unlimited
pager:none
ajax:yes
Header and page path: whatever

Fields and sorting

node:title (no option, just remove label)
node:title ascending

Filters

the usual filters: node published, node type Customer
=> you should now have a working view.

Layout the page

2 more things to do:

Layout the thumbnail

this is not done with the view templates but with the customer node template (because the thumbnailHover slideshow will show you the node just like you would see it from a direct link. I basically defined a template that displays the image with fixed dimensions (thanks to imagecache) above a background image for the frame.

adjust the CSS for the view to display the node list nicely

Main CSS characteristics: 3 main divs to layout (#views_slideshow_thumbnailhover_main_1 on the left, #views_slideshow_thumbnailhover_breakout_teasers_1 in the middle, div.attachment on the right)
- give a %width for each of them, float left the first 2 and right the attachment
- change the cursor to pointer for the middle div so that visitors understand they can mouse over the node titles to show a different ad

part2: add an ATTACHMENT VIEW for the Industries

change the PAGE display to add a view argument

-add new argument for field: taxonomy term
-Action to take if argument is not present: Provide default argument
-default argument type: fixed entry, default value: Apparel & Accessories (my first Industry term)
-Optional: Under validator, add the following pHP code:
$pattern = "/%26/";
   $handler -> argument = preg_replace($pattern,"&", $argument);
   return true;

==> This is to handle the special character & in the taxonomy terms - if you don't do that, then the link on taxonomy term such as Media & Movies doesn't work
-Action to take if argument doesn't validate: display empty text (just in case..)

add a new ATTACHMENT display

-Fields, filter and sort: no change
-Attachment settings:
-inherit argument:YES (inherit its arguments from the parent display ie the PAGE view)
-Inherit exposed filter:NO (we don't have exposed filter)
-position:after
-Attach to:page
-ARGUMENTS:OVERRIDE taxonomy:term settings
and here is the trick:
-Under action to take if argument is not present,choose: summary, sort ascending
-under validator option, choose PHP with the following code: return FALSE;
-Action to take if argument doesn't validate: summary, sort ascending
-under style, choose LIST
==> when there is no argument, we still want to display all industries and count them (there is no argument when the page is first displayed)
==> also, even if there is an argument (the user clicked on an Industry) we still want the list of Industry to stay as is so we force the argument to not validate using return code FALSE) - the view will still refresh the left side of the screen (the PAGE view) thanks to the argument settings defined at the PAGE level

Wednesday, 18 May 2011

Simple date differene

<?php
$start
= new DateTime('08-06-1995'); // DD-MM-YYYY
$end = new DateTime('22-11-1968');

if (
$start < $end) {
    echo
"Correct order";
} else {
    echo
"Incorrect order, end date is before the starting date";
}
?>

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...