1<?php
2/**
3 * A decorator around an Ansel_Gallery to allow multiple date groupings
4 * to access the same Ansel_Gallery instance.
5 *
6 * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
7 *
8 * See the enclosed file COPYING for license information (GPL). If you
9 * did not receive this file, see http://www.horde.org/licenses/gpl.
10 *
11 * @author  Michael J. Rubinsky <mrubinsk@horde.org>
12 * @package Ansel
13 */
14class Ansel_Gallery_Decorator_Date
15{
16    /**
17     * The gallery mode helper
18     *
19     * @var Ansel_GalleryMode_Base object
20     */
21    protected $_modeHelper;
22
23    /**
24     * The gallery we are decorating
25     *
26     * @var Ansel_Gallery
27     */
28    protected $_gallery;
29
30    /**
31     * An array of image ids that this gallery contains
32     *
33     * @var array
34     */
35    protected $_images;
36
37    /**
38     * The Ansel_Gallery_Date constructor.
39     *
40     * The client code (Ansel_GalleryMode_Date) needs to call the setDate()
41     * method on the new GalleryMode_Date object before it's used.
42     *
43     * @param Ansel_Gallery $gallery  The gallery we are decorating.
44     * @param array $images           An array of image ids that this grouping
45     *                                contains.
46     */
47    public function __construct(Ansel_Gallery $gallery, $images = array())
48    {
49        $this->_gallery = $gallery;
50        $this->_modeHelper = new Ansel_GalleryMode_Date($this);
51        $this->data = $this->_gallery->data;
52        $this->_images = $images;
53    }
54
55    /**
56     * Magic method - pass thru methods to the wrapped Ansel_Gallery:: or to
57     * the Ansel_GalleryMode_Base:: handler.
58     *
59     * @param string $method
60     * @param array $args
61     *
62     * @return mixed
63     */
64    public function __call($method, $args)
65    {
66        switch ($method) {
67        case 'getGalleryChildren':
68        case 'countGalleryChildren':
69        case 'listImages':
70        case 'getImages':
71        case 'hasSubGalleries':
72        case 'getDate':
73        case 'setDate':
74            return call_user_func_array(array($this->_modeHelper, $method), $args);
75        default:
76            return call_user_func_array(array($this->_gallery, $method), $args);
77        }
78    }
79
80    public function __get($property)
81    {
82        switch ($property) {
83        case 'id':
84            return $this->_gallery->id;
85        }
86    }
87
88    /**
89     * Output the HTML for this gallery's tile.
90     *
91     * @param Ansel_Gallery $parent  The parent Ansel_Gallery object
92     * @param Ansel_Style $style     A gallery style to use.
93     * @param boolean $mini          Force the use of a mini thumbnail?
94     * @param array $params          Any additional parameters the Ansel_Tile
95     *                               object may need.
96     */
97    public function getTile($parent = null, $style = null, $mini = false, $params = array())
98    {
99        if (!is_null($parent) && is_null($style)) {
100            $style = $parent->getStyle();
101        }
102
103        return Ansel_Tile_DateGallery::getTile($this, $style, $mini, $params);
104    }
105
106    /**
107     * Return the most recently added images in this gallery.
108     *
109     * @param integer $limit  The maximum number of images to return.
110     *
111     * @return array  An array of Ansel_Image objects
112     */
113    public function getRecentImages($limit = 10)
114    {
115        return $GLOBALS['injector']->getInstance('Ansel_Storage')
116            ->getRecentImages(array($this->_gallery->id), $limit);
117    }
118
119    /**
120     * Returns the image in this gallery corresponding to the given id.
121     *
122     * @param integer $id  The ID of the image to retrieve.
123     *
124     * @return Ansel_Image  The image object corresponding to the given id.
125     */
126    public function getImage($id)
127    {
128        return $GLOBALS['injector']->getInstance('Ansel_Storage')->getImage($id);
129    }
130
131    /**
132     * Returns the number of images in this gallery and, optionally, all
133     * sub-galleries.
134     *
135     * @param boolean $subgalleries  Determines whether subgalleries should
136     *                               be counted or not.
137     *
138     * @return integer number of images in this gallery
139     */
140    public function countImages($subgalleries = false)
141    {
142        return count($this->_images);
143    }
144
145    /**
146     * Returns the key image for this gallery.
147     *
148     * @param Ansel_Style $style  Force the use of this style, if it's available
149     *
150     * @return mixed  The image_id of the key image or false.
151     */
152    public function getKeyImage($style = null)
153    {
154        if (count($this->_images)) {
155            return reset($this->_images);
156        } else {
157            return 0;
158        }
159    }
160
161    /**
162     * Return a count of the number of children this share has
163     *
164     * @param string  $user       The user to use for checking perms
165     * @param integer $perm       A Horde_Perms::* constant
166     * @param boolean $allLevels  Count grandchildren or just children
167     *
168     * @return integer  The number of child shares
169     */
170    public function countChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true)
171    {
172        return $this->_gallery->getShareOb()->countShares($user, $perm, null, $this, $allLevels);
173    }
174
175    /**
176     * Returns a child's direct parent
177     *
178     * @return Ansel_Gallery  The direct parent Horde_Share_Object
179     */
180    public function getParent()
181    {
182        return $this->_gallery->getParent($this);
183    }
184
185    /**
186     * Returns all image ids that this grouping contains.
187     *
188     * @array
189     */
190    public function getImagesByGrouping()
191    {
192        return $this->_images;
193    }
194}
195