1: <?php
2: /**
3: * The Ansel_View_Abstract:: Parent class for the various Ansel_View classes
4: *
5: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (GPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/gpl.
9: *
10: * @author Chuck Hagenbuch <chuck@horde.org>
11: * @author Michael J. Rubinsky <mrubinsk@horde.org>
12: * @package Ansel
13: */
14: abstract class Ansel_View_Base
15: {
16: protected $_params = array();
17:
18: /**
19: * Const'r
20: *
21: * Any javascript files needed by the (non-api) view should be included
22: * within this method. Additionally, any redirects need to be done in the
23: * cont'r since when ::html() is called, headers will have already been
24: * sent.
25: *
26: * @param array $params Any parameters that the view might need.
27: * <pre>
28: * gallery_id The gallery id this view is for. If omitted, it
29: * looks for a query parameter called 'gallery'
30: *
31: * gallery_slug Same as above, but a slug
32: *
33: * gallery_view_url If set, this is used as the link to a gallery
34: * view. %g is replaced by the gallery_id and %s is
35: * replaced by the gallery_slug.
36: *
37: * gallery_view The specific Renderer to use, if needed.
38: * (GalleryLightbox, Gallery etc...).
39: *
40: * image_view_url If this is set, the image tiles will use this url
41: * for the image view link. %i and %g will be
42: * replaced by image_id and gallery_id respectively.
43: * %s will be replaced by the gallery_slug
44: *
45: * image_view_src If this is set to true, the image view link will go
46: * directly to the actual image. This overrides any
47: * setting of image_view_url.
48: *
49: * image_view_attributes An optional array of attribute => value pairs
50: * that are used as attributes of the image view
51: * link.
52: *
53: * image_view_title Specifies which property of the image object
54: * to use as the image caption.
55: *
56: * image_onclick Specifies a onclick handler for the image tile
57: * links.
58: *
59: * style Force the use of this named style.
60: *
61: * api If set, we are being called from the external api
62: *
63: * page The gallery page number to display if not the
64: * default value of the first page (page = 0)
65: *
66: * day, month, year Numeric date part values to describe the gallery
67: * date grouping to view in date mode.
68: *
69: * force_date_grouping Do not auto navigate to the first date grouping
70: * with more then one resource. Used from the api
71: * when clicking on breadcrumb links, for example.
72: * </pre>
73: */
74: public function __construct($params = array())
75: {
76: $this->_params = $params;
77: }
78:
79: public function __get($property)
80: {
81: if (isset($this->_params[$property])) {
82: return $this->_params[$property];
83: }
84:
85: return null;
86: }
87:
88: public function __set($property, $value)
89: {
90: $this->_params[$property] = $value;
91: }
92:
93: public function __isset($property)
94: {
95: return isset($this->_params[$property]);
96: }
97:
98: /**
99: * Getter for the view parameters.
100: *
101: * @return unknown_type
102: */
103: public function getParams()
104: {
105: return $this->_params;
106: }
107:
108: /**
109: * Todo
110: *
111: * @param integer $galleryId The gallery id
112: * @param string $slug The gallery slug
113: *
114: * @return Ansel_Gallery The requested Ansel_Gallery object
115: * @throws Horde_Exception
116: * @throws InvalidArgumentException
117: *
118: */
119: protected function _getGallery($galleryId = null, $slug = '')
120: {
121: if (is_null($galleryId) && empty($slug)) {
122: $galleryId = !empty($this->_params['gallery_id']) ? $this->_params['gallery_id'] : null;
123: $slug = !empty($this->_params['gallery_slug']) ? $this->_params['gallery_slug'] : null;
124: }
125:
126: if (empty($galleryId) && empty($slug)) {
127: throw new Ansel_Exception(_("No gallery specified"));
128: }
129:
130: // If we have a slug, use it.
131: try {
132: if (!empty($slug)) {
133: $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGalleryBySlug($slug);
134: } else {
135: $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($galleryId);
136: }
137: } catch (Ansel_Exception $e) {
138: throw new Horde_Exception_NotFound($e->getmessage());
139: }
140:
141: if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
142: throw new Horde_Exception(sprintf(_("Access denied to gallery \"%s\"."), $gallery->get('name')));
143: }
144:
145: /* Set any date info we might have */
146: if (!empty($this->_params['year'])) {
147: $date = Ansel::getDateParameter(
148: array('year' => $this->_params['year'],
149: 'month' => $this->_params['month'],
150: 'day' => $this->_params['day']));
151: } else {
152: $date = array();
153: }
154: $gallery->setDate($date);
155:
156: return $gallery;
157: }
158:
159:
160: /**
161: * JSON representation of this gallery's images. We don't use
162: * Ansel_Gallery::toJson() on purpose since that is a general jsonification
163: * of the gallery data. This method is specific to the view, paging, links
164: * etc...
165: *
166: * @param Ansel_Gallery $gallery The gallery to represent in this view
167: * @param array $params An array of parameters for this method:
168: * <pre>
169: * full - Should a full URL be generated? [false]
170: * from - Starting image count [0]
171: * count - The number of images to include (starting at from) [0]
172: * image_view - The type of ImageGenerator to obtain the src url for. [screen]
173: * view_links - Should the JSON include links to the Image and/or Gallery View? [false]
174: * perpage - Number of images per page [from user prefs]
175: * </pre>
176: *
177: * @return string A serialized JSON array.
178: */
179: static public function json(Ansel_Gallery $gallery, $params = array())
180: {
181: global $conf, $prefs;
182:
183: $default = array(
184: 'full' => false,
185: 'from' => 0,
186: 'count' => 0,
187: 'image_view' => 'screen',
188: 'view_links' => false,
189: 'perpage' => $prefs->getValue('tilesperpage', $conf['thumbnail']['perpage'])
190: );
191:
192: $params = array_merge($default, $params);
193: $json = array();
194: $curimage = 0;
195: $curpage = 0;
196: if (empty($params['images'])) {
197: $images = $gallery->getImages($params['from'], $params['count']);
198: }
199: $style = $gallery->getStyle();
200: if ($params['image_view'] == 'thumb' && !empty($params['generator'])) {
201: $style->thumbstyle = $params['generator'];
202: }
203: foreach ($images as $image) {
204: // Calculate the page this image will appear on in the gallery view.
205: if (++$curimage > $params['perpage']) {
206: ++$curpage;
207: $curimage = 0;
208: }
209:
210: $data = array(
211: (string)Ansel::getImageUrl($image->id, $params['image_view'], $params['full'], $style),
212: htmlspecialchars($image->filename),
213: $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)),
214: $image->id,
215: $curpage
216: );
217: if ($params['view_links']) {
218: $data[] = (string)Ansel::getUrlFor('view',
219: array('gallery' => $gallery->id,
220: 'slug' => $gallery->get('slug'),
221: 'image' => $image->id,
222: 'view' => 'Image',
223: 'page' => $curpage),
224: true);
225: $data[] = (string)Ansel::getUrlFor('view',
226: array('gallery' => $image->gallery,
227: 'slug' => $gallery->get('slug'),
228: 'view' => 'Gallery'),
229: true);
230: }
231: // Source, Width, Height, Name, Caption, Image Id, Gallery Page
232: $json[] = $data;
233: }
234:
235: return Horde_Serialize::serialize($json, Horde_Serialize::JSON);
236: }
237:
238: }
239: