Exemple #1
0
 /**
  * Get the children of this gallery.
  *
  * @param integer $perm    The permissions to limit to.
  * @param integer $from    The child to start at.
  * @param integer $to      The child to end with.
  * @param boolean $noauto  Whether or not to automatically drill down to the
  *                         first grouping with more then one group.
  *
  * @return array A mixed array of Ansel_Gallery_Decorator_Date and
  *               Ansel_Image objects.
  */
 public function getGalleryChildren($perm = Horde_Perms::SHOW, $from = 0, $to = 0, $noauto = false)
 {
     // Cache the results
     static $children = array();
     $fullkey = md5($noauto . $perm . $this->_gallery->id . serialize($this->_date) . 0 . 0);
     $cache_key = md5($noauto . $perm . $this->_gallery->id . serialize($this->_date) . $from . $to);
     if (!empty($children[$cache_key])) {
         return $children[$cache_key];
     } elseif (!empty($children[$fullkey])) {
         return $this->_getArraySlice($children[$fullkey], $from, $to, true);
     }
     $ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage');
     // Get a list of all the subgalleries
     $this->_loadSubGalleries();
     $params = array('fields' => array('image_id', 'image_original_date'));
     if (count($this->_subGalleries)) {
         $params['gallery_id'] = array_merge($this->_subGalleries, array($this->_gallery->id));
     } else {
         $params['gallery_id'] = $this->_gallery->id;
     }
     $sorted_dates = array();
     // See how specific the date is
     if (!count($this->_date) || empty($this->_date['year'])) {
         // All available images - grouped by year
         $images = $ansel_storage->listImages($params);
         $dates = array();
         foreach ($images as $key => $image) {
             $dates[date('Y', $image['image_original_date'])][] = $key;
         }
         $keys = array_keys($dates);
         // Drill down further if we only have a single group
         if (!$noauto && count($keys) == 1) {
             $this->_date['year'] = array_pop($keys);
             return $this->getGalleryChildren($perm, $from, $to, $noauto);
         }
         sort($keys, SORT_NUMERIC);
         foreach ($keys as $key) {
             $sorted_dates[$key] = $dates[$key];
         }
         $display_unit = 'year';
     } elseif (empty($this->_date['month'])) {
         // Specific year - grouped by month
         $start = new Horde_Date(array('year' => $this->_date['year'], 'month' => 1, 'day' => 1));
         // Last second of the year
         $end = new Horde_Date($start);
         $end->mday = 31;
         $end->month = 12;
         $end->hour = 23;
         $end->min = 59;
         $end->sec = 59;
         // Get the image ids and dates
         $params['filter'] = array(array('property' => 'originalDate', 'op' => '<=', 'value' => (int) $end->timestamp()), array('property' => 'originalDate', 'op' => '>=', 'value' => (int) $start->timestamp()));
         $images = $ansel_storage->listImages($params);
         $dates = array();
         foreach ($images as $key => $image) {
             $dates[date('n', $image['image_original_date'])][] = $key;
         }
         $keys = array_keys($dates);
         // Only 1 date grouping here, automatically drill down
         if (!$noauto && count($keys) == 1) {
             $this->_date['month'] = array_pop($keys);
             return $this->getGalleryChildren($perm, $from, $to, $noauto);
         }
         sort($keys, SORT_NUMERIC);
         foreach ($keys as $key) {
             $sorted_dates[$key] = $dates[$key];
         }
         $display_unit = 'month';
     } elseif (empty($this->_date['day'])) {
         // A single month - group by day
         $start = new Horde_Date(array('year' => $this->_date['year'], 'month' => $this->_date['month'], 'day' => 1));
         // Last second of the month
         $end = new Horde_Date($start);
         $end->mday = Horde_Date_Utils::daysInMonth($end->month, $end->year);
         $end->hour = 23;
         $end->min = 59;
         $end->sec = 59;
         $params['filter'] = array(array('property' => 'originalDate', 'op' => '<=', 'value' => (int) $end->timestamp()), array('property' => 'originalDate', 'op' => '>=', 'value' => (int) $start->timestamp()));
         $images = $ansel_storage->listImages($params);
         $dates = array();
         foreach ($images as $key => $image) {
             $dates[date('d', $image['image_original_date'])][] = $key;
         }
         $keys = array_keys($dates);
         // Only a single grouping, go deeper
         if (!$noauto && count($keys) == 1) {
             $this->_date['day'] = array_pop($keys);
             return $this->getGalleryChildren($perm, $from, $to, $noauto);
         }
         sort($keys, SORT_NUMERIC);
         foreach ($keys as $key) {
             $sorted_dates[$key] = $dates[$key];
         }
         $dates = $sorted_dates;
         $display_unit = 'day';
     } else {
         // We are down to a specific day
         $start = new Horde_Date($this->_date);
         // Last second of this day
         $end = new Horde_Date($start->timestamp());
         $end->hour = 23;
         $end->min = 59;
         $end->sec = 59;
         // Filter for this day
         $params['filter'] = array(array('property' => 'originalDate', 'op' => '<=', 'value' => (int) $end->timestamp()), array('property' => 'originalDate', 'op' => '>=', 'value' => (int) $start->timestamp()));
         // Only get what we need
         $params['offset'] = $from;
         $params['limit'] = $to;
         // Default to asking for just image_ids
         unset($params['fields']);
         // Get the image list
         $images = $ansel_storage->listImages($params);
         if ($images) {
             $results = $ansel_storage->getImages(array('ids' => $images, 'preserve' => true));
         } else {
             $results = array();
         }
         if ($this->_gallery->get('has_subgalleries')) {
             $images = array();
             foreach ($results as $id => $image) {
                 $image->gallery = $this->_gallery->id;
                 $images[$id] = $image;
             }
             $children[$cache_key] = $images;
         } else {
             $children[$cache_key] = $results;
         }
         return $children[$cache_key];
     }
     $results = array();
     foreach ($sorted_dates as $key => $images) {
         /* Get the new date parameter */
         switch ($display_unit) {
             case 'year':
                 $date = array('year' => $key);
                 break;
             case 'month':
                 $date = array('year' => $this->_date['year'], 'month' => (int) $key);
                 break;
             case 'day':
                 $date = array('year' => (int) $this->_date['year'], 'month' => (int) $this->_date['month'], 'day' => (int) $key);
         }
         $obj = new Ansel_Gallery_Decorator_Date($this->_gallery, $images);
         $obj->setDate($date);
         $results[$key] = $obj;
     }
     $children[$cache_key] = $results;
     if ($from > 0 || $to > 0) {
         return $this->_getArraySlice($results, $from, $to, true);
     }
     return $results;
 }