eZ Community » Blogs » Thiago Campos Viana » Tip: Zip images under a node with...

By

Tip: Zip images under a node with Zeta Components (eZ components)

Friday 17 February 2012 1:03:05 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

In this post I will show how to create a simple module to create a zip file containing all images under a content node, like an image gallery.

Here's the basic structure under the extension folder:

  • zip
    • settings
      • module.ini.append.php
    • modules
      • zip
        • module.php
        • images.php

module.ini.append.php

<?php /* #?ini charset="utf-8"?
 
[ModuleSettings]
ExtensionRepositories[]=zip
ModuleList[]=zip
 
*/ ?>

module.php

<?php
 
$Module = array(
    'name' => 'Zip'
);
 
$ViewList = array();
 
$ViewList['images'] = array(
    'functions' => array( 'images' ),
    'script' => 'images.php',
    'params' => array( 'ParentNodeID' )
);
 
 
$FunctionList[ 'images' ] = array();

images.php

<?php
 
set_time_limit( 0 );
 
function zipImages( $filename, $images = array() )
{
    $zip = ezcArchive::open( $filename, ezcArchive::ZIP );
    foreach($images as $image){
        if(file_exists($image)){
            $pathinfo=pathinfo($image);
            $zip->append($image, $pathinfo['dirname'] );
        }
 
    }
 
 
    $zip->close();
    return $zip;
}
 
$module = $Params['Module'];
$ParentNodeID = (int) $Params['ParentNodeID'];
 
 
$filename = 'var/storage/'.$ParentNodeID.'.zip';
 
 
 
$parent=eZContentObjectTreeNode::fetch($ParentNodeID);
 
// check if we need to create a new file
if (!file_exists($filename) || filemtime($filename) < $parent->ModifiedSubNode ) {
    $nodes = $parent->subTree();
    $images = array();
    if ($nodes) {
        foreach ($nodes as $subNode) {
            $dataMap = $subNode->attribute('data_map');
            // check if the children has an image attribute
            if(isset($dataMap['image'])){
                $original = $dataMap['image']->attribute('content')->attribute('original');
                $images[]=$original['full_path'];
            
            }
        }
    }
 
    if(file_exists($filename)) unlink($filename);
    zipImages($filename, $images);
}
 
$file = new eZFile();
 
$file->download( $filename, true, false );
 
eZExecution::cleanExit();

Now, to call our module you will need to put this line in your gallery full view:

<div class="attribute-download-all">
     <a href={concat('zip/images/',$node.node_id)|ezurl}>{"Download images"|i18n( "design/common" )}</a>
 </div>

Note that we pass the parent node id as a parameter to our module.

Proudly Developed with from