Ansel::downloadImagesAsZip PHP Method

downloadImagesAsZip() public static method

Downloads all requested images as a zip file. Assumes all permissions have been checked on the requested resource. Can request either a single gallery of images, OR an array of individual image ids.
public static downloadImagesAsZip ( Ansel_Gallery $gallery = null, array $images = [] )
$gallery Ansel_Gallery The galleries to download
$images array The images to download
    public static function downloadImagesAsZip($gallery = null, $images = array())
    {
        global $session;
        if (empty($GLOBALS['conf']['gallery']['downloadzip'])) {
            $GLOBALS['notification']->push(_("Downloading zip files is not enabled. Talk to your server administrator."));
            Horde::url('view.php?view=List', true)->redirect();
            exit;
        }
        // Requested a gallery
        if (!is_null($gallery)) {
            // We can name the zip file with the slug if we have it
            $slug = $gallery->get('slug');
            // Set the date in case we are viewing in date mode
            $gallery->setDate(Ansel::getDateParameter());
            $images = $gallery->listImages();
        }
        // At this point, we should always have a list of images
        if (!count($images)) {
            $notification->push(sprintf(_("There are no photos in %s to download."), $gallery->get('name')), 'horde.message');
            Horde::url('view.php?view=List', true)->redirect();
            exit;
        }
        // Try to close off the current session to avoid locking it while the
        // gallery is downloading.
        $session->close();
        if (!is_null($gallery)) {
            // Check full photo permissions
            if ($gallery->canDownload()) {
                $view = 'full';
            } else {
                $view = 'screen';
            }
        }
        $zipfiles = array();
        foreach ($images as $id) {
            $image = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImage($id);
            // If we didn't select an entire gallery, check the download
            // size for each image.
            if (!isset($view)) {
                $g = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($image->gallery);
                $v = $g->canDownload() ? 'full' : 'screen';
            } else {
                $v = $view;
            }
            $zipfiles[] = array('data' => $image->raw($v), 'name' => $image->filename);
        }
        $zip = Horde_Compress::factory('zip');
        $body = $zip->compress($zipfiles);
        if (!empty($gallery)) {
            $filename = (!empty($slug) ? $slug : $gallery->id) . '.zip';
        } else {
            $filename = 'Ansel.zip';
        }
        $GLOBALS['browser']->downloadHeaders($filename, 'application/zip', false, strlen($body));
        echo $body;
        exit;
    }

Usage Example

Example #1
0
 public static function galleryActions($actionID)
 {
     global $registry, $notification, $page_output;
     if (self::download($actionID)) {
         return true;
     }
     $ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage');
     switch ($actionID) {
         case 'add':
         case 'addchild':
         case 'save':
         case 'modify':
             $view = new Ansel_View_GalleryProperties(array('actionID' => $actionID, 'url' => new Horde_Url(Horde_Util::getFormData('url')), 'gallery' => Horde_Util::getFormData('gallery')));
             $view->run();
             exit;
         case 'downloadzip':
             $galleryId = Horde_Util::getFormData('gallery');
             try {
                 $gallery = $ansel_storage->getGallery($galleryId);
                 if (!$registry->getAuth() || !$gallery->hasPermission($registry->getAuth(), Horde_Perms::READ)) {
                     $notification->push(_("Access denied downloading photos from this gallery."), 'horde.error');
                     Horde::url('view.php?view=List', true)->redirect();
                     exit;
                 }
                 Ansel::downloadImagesAsZip($gallery);
             } catch (Ansel_Exception $e) {
                 $notification->push($gallery->getMessage(), 'horde.error');
                 Horde::url('view.php?view=List', true)->redirect();
                 exit;
             }
             exit;
         case 'delete':
         case 'empty':
             // Print the confirmation screen.
             $galleryId = Horde_Util::getFormData('gallery');
             if ($galleryId) {
                 try {
                     $gallery = $ansel_storage->getGallery($galleryId);
                     $page_output->header();
                     $notification->notify(array('listeners' => 'status'));
                     require ANSEL_TEMPLATES . '/gallery/delete_confirmation.inc';
                     $page_output->footer();
                     exit;
                 } catch (Ansel_Exception $e) {
                     $notification->push($gallery->getMessage(), 'horde.error');
                 }
             }
             // Return to the gallery list.
             Horde::url(Ansel::getUrlFor('view', array('view' => 'List'), true))->redirect();
             exit;
         case 'do_delete':
         case 'do_empty':
             $ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage');
             $galleryId = Horde_Util::getPost('gallery');
             try {
                 $gallery = $ansel_storage->getGallery($galleryId);
             } catch (Ansel_Exception $e) {
                 $notification->push($e->getMessage(), 'horde.error');
                 Ansel::getUrlFor('default_view', array())->redirect();
                 exit;
             }
             switch ($actionID) {
                 case 'do_delete':
                     if (!$gallery->hasPermission($registry->getAuth(), Horde_Perms::DELETE)) {
                         $notification->push(_("Access denied deleting this gallery."), 'horde.error');
                     } else {
                         try {
                             $ansel_storage->removeGallery($gallery);
                             $notification->push(sprintf(_("Successfully deleted %s."), $gallery->get('name')), 'horde.success');
                         } catch (Ansel_Exception $e) {
                             $notification->push(sprintf(_("There was a problem deleting %s: %s"), $gallery->get('name'), $e->getMessage()), 'horde.error');
                         } catch (Horde_Exception_NotFound $e) {
                             Horde::log($e, 'err');
                         }
                     }
                     // Return to the default view.
                     Ansel::getUrlFor('default_view', array())->redirect();
                     exit;
                 case 'do_empty':
                     if (!$gallery->hasPermission($registry->getAuth(), Horde_Perms::DELETE)) {
                         $notification->push(_("Access denied deleting this gallery."), 'horde.error');
                     } else {
                         $ansel_storage->emptyGallery($gallery);
                         $notification->push(sprintf(_("Successfully emptied \"%s\""), $gallery->get('name')), 'horde.success');
                     }
                     Ansel::getUrlFor('view', array('view' => 'Gallery', 'gallery' => $galleryId, 'slug' => $gallery->get('slug')), true)->redirect();
                     exit;
                 default:
                     Ansel::getUrlFor('view', array('view' => 'Gallery', 'gallery' => $galleryId, 'slug' => $gallery->get('slug')), true)->redirect();
                     exit;
             }
         case 'generateDefault':
             // Re-generate the default pretty gallery image.
             $galleryId = Horde_Util::getFormData('gallery');
             try {
                 $gallery = $ansel_storage->getGallery($galleryId);
                 $gallery->clearStacks();
                 $notification->push(_("The gallery's default photo has successfully been reset."), 'horde.success');
                 Horde::url('view.php', true)->add('gallery', $galleryId)->redirect();
                 exit;
             } catch (Ansel_Exception $e) {
                 $notification->push($e->getMessage(), 'horde.error');
                 Horde::url('index.php', true)->redirect();
                 exit;
             }
         case 'generateThumbs':
             // Re-generate all of this gallery's prettythumbs.
             $galleryId = Horde_Util::getFormData('gallery');
             try {
                 $gallery = $ansel_storage->getGallery($galleryId);
             } catch (Ansel_Exception $e) {
                 $notification->push($gallery->getMessage(), 'horde.error');
                 Horde::url('index.php', true)->redirect();
                 exit;
             }
             $gallery->clearThumbs();
             $notification->push(_("The gallery's thumbnails have successfully been reset."), 'horde.success');
             Horde::url('view.php', true)->add('gallery', $galleryId)->redirect();
             exit;
         case 'deleteCache':
             // Delete all cached image views.
             $galleryId = Horde_Util::getFormData('gallery');
             try {
                 $gallery = $ansel_storage->getGallery($galleryId);
             } catch (Ansel_Exception $e) {
                 $notification->push($gallery->getMessage(), 'horde.error');
                 Horde::url('index.php', true)->redirect();
                 exit;
             }
             $gallery->clearViews();
             $notification->push(_("The gallery's views have successfully been reset."), 'horde.success');
             Horde::url('view.php', true)->add('gallery', $galleryId)->redirect();
             exit;
     }
     return false;
 }