Rev 229 Rev 236
1 <?php 1 <?php
2   2  
3 /** 3 /**
4 * Gallery class. 4 * Gallery class.
5 * 5 *
6 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 6 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
7 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License 7 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
8 * @copyright (c)2003-2005 Tamlyn Rhodes 8 * @copyright (c)2003-2005 Tamlyn Rhodes
9 * @version $Id: gallery.class.php,v 1.19 2006/09/12 11:53:18 thepavian Exp $ 9 * @version $Id: gallery.class.php,v 1.19 2006/09/12 11:53:18 thepavian Exp $
10 */ 10 */
11   11  
12 //include the base class 12 //include the base class
13 require_once dirname(__FILE__)."/item.class.php"; 13 require_once dirname(__FILE__)."/item.class.php";
14 14
15 /** 15 /**
16 * Data-only class used to store gallery data. 16 * Data-only class used to store gallery data.
17 * 17 *
18 * @package singapore 18 * @package singapore
19 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 19 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
20 * @copyright (c)2003-2005 Tamlyn Rhodes 20 * @copyright (c)2003-2005 Tamlyn Rhodes
21 */ 21 */
22 class sgGallery extends sgItem 22 class sgGallery extends sgItem
23 { 23 {
24 /** 24 /**
25 * Filename of the image used to represent this gallery. 25 * Filename of the image used to represent this gallery.
26 * Special values: 26 * Special values:
27 * - __none__ no thumbnail is displayed 27 * - __none__ no thumbnail is displayed
28 * - __random__ a random image is chosen every time 28 * - __random__ a random image is chosen every time
29 * @var string 29 * @var string
30 */ 30 */
31 var $filename = "__none__"; 31 var $filename = "__none__";
32 32
33 /** 33 /**
34 * Short multiline summary of gallery contents 34 * Short multiline summary of gallery contents
35 * @var string 35 * @var string
36 */ 36 */
37 var $summary = ""; 37 var $summary = "";
38 38
39 /** 39 /**
40 * Array of {@link sgImage} objects 40 * Array of {@link sgImage} objects
41 * @var array 41 * @var array
42 */ 42 */
43 var $images = array(); 43 var $images = array();
44 44
45 /** 45 /**
46 * Array of {@link sgGallery} objects 46 * Array of {@link sgGallery} objects
47 * @var array 47 * @var array
48 */ 48 */
49 var $galleries = array(); 49 var $galleries = array();
50 50
51 /** 51 /**
52 * Constructor 52 * Constructor
53 * @param string gallery id 53 * @param string gallery id
54 * @param sgGallery reference to the parent gallery 54 * @param sgGallery reference to the parent gallery
55 */ 55 */
56 function sgGallery($id, &$parent) 56 function sgGallery($id, &$parent)
57 { 57 {
58 $this->id = $id; 58 $this->id = $id;
59 $this->parent =& $parent; 59 $this->parent =& $parent;
60 $this->config =& sgConfig::getInstance(); 60 $this->config =& sgConfig::getInstance();
61 $this->translator =& Translator::getInstance(); 61 $this->translator =& Translator::getInstance();
62 } 62 }
63 63
64 /** @return bool true if this is a non-album gallery; false otherwise */ 64 /** @return bool true if this is a non-album gallery; false otherwise */
65 function isGallery() { return $this->hasChildGalleries(); } 65 function isGallery() { return $this->hasChildGalleries(); }
66 66
67 /** @return bool true if this is an album; false otherwise */ 67 /** @return bool true if this is an album; false otherwise */
68 function isAlbum() { return !$this->isGallery(); } 68 function isAlbum() { return !$this->isGallery(); }
69 69
70 /** @return bool true if this is the root gallery; false otherwise */ 70 /** @return bool true if this is the root gallery; false otherwise */
71 function isRoot() { return $this->id == "."; } 71 function isRoot() { return $this->id == "."; }
72 72
73 /** @return bool true if this gallery has child galleries; false otherwise */ 73 /** @return bool true if this gallery has child galleries; false otherwise */
74 function hasChildGalleries() { return $this->galleryCount() != 0; } 74 function hasChildGalleries() { return $this->galleryCount() != 0; }
75 75
76 /** @return bool true if this gallery contains one or more images; false otherwise */ 76 /** @return bool true if this gallery contains one or more images; false otherwise */
77 function hasImages() { return $this->imageCount() != 0; } 77 function hasImages() { return $this->imageCount() != 0; }
78 78
79 function imageCount() { return count($this->images); } 79 function imageCount() { return count($this->images); }
80 function galleryCount() { return count($this->galleries); } 80 function galleryCount() { return count($this->galleries); }
81 81
82 function imageCountText() { return $this->translator->_ng("%s image", "%s images", $this->imageCount()); } 82 function imageCountText() { return $this->translator->_ng("%s image", "%s images", $this->imageCount()); }
83 function galleryCountText() { return $this->translator->_ng("%s gallery", "%s galleries", $this->galleryCount()); } 83 function galleryCountText() { return $this->translator->_ng("%s gallery", "%s galleries", $this->galleryCount()); }
84 84
85 /** 85 /**
86 * Caches returned value for use with repeat requests 86 * Caches returned value for use with repeat requests
87 * @return string the rawurlencoded version of the gallery id 87 * @return string the rawurlencoded version of the gallery id
88 */ 88 */
89 function idEncoded() 89 function idEncoded()
90 { 90 {
91 return isset($this->idEncoded) ? $this->idEncoded : $this->idEncoded = $this->encodeId($this->id); 91 return isset($this->idEncoded) ? $this->idEncoded : $this->idEncoded = $this->encodeId($this->id);
92 } 92 }
93 93
94 /** 94 /**
95 * rawurlencode() supplied string but preserve / character for cosmetic reasons. 95 * rawurlencode() supplied string but preserve / character for cosmetic reasons.
96 * @param string id to encode 96 * @param string id to encode
97 * @return string encoded id 97 * @return string encoded id
98 * @static 98 * @static
99 */ 99 */
100 function encodeId($id) 100 function encodeId($id)
101 { 101 {
102 $in = explode("/",$id); 102 $in = explode("/",$id);
103 $out = array(); 103 $out = array();
104 for($i=1;$i<count($in);$i++) 104 for($i=1;$i<count($in);$i++)
105 $out[$i-1] = rawurlencode($in[$i]); 105 $out[$i-1] = rawurlencode($in[$i]);
106 return $out ? implode("/",$out) : "."; 106 return $out ? implode("/",$out) : ".";
107 } 107 }
108 108
109 function nameForce() 109 function nameForce()
110 { 110 {
111 if($this->name) 111 if($this->name)
112 return $this->name; 112 return $this->name;
113 elseif($this->isRoot()) 113 elseif($this->isRoot())
114 return $this->config->gallery_name; 114 return $this->config->gallery_name;
115 else 115 else
116 return substr($this->id, strrpos($this->id,'/')+1); 116 return substr($this->id, strrpos($this->id,'/')+1);
117 } 117 }
118 118
119 /** 119 /**
120 * If the gallery is an album then it returns the number of 120 * If the gallery is an album then it returns the number of
121 * images contained otherwise the number of sub-galleries is returned 121 * images contained otherwise the number of sub-galleries is returned
122 * @return string the contents of the specified gallery 122 * @return string the contents of the specified gallery
123 */ 123 */
124 function itemCountText() 124 function itemCountText()
125 { 125 {
126 if($this->isAlbum()) 126 if($this->isAlbum())
127 return $this->imageCountText(); 127 return $this->imageCountText();
128 else 128 else
129 return $this->galleryCountText(); 129 return $this->galleryCountText();
130 } 130 }
131 131
132 /** 132 /**
133 * If the gallery is an album then it returns the number of 133 * If the gallery is an album then it returns the number of
134 * images contained otherwise the number of sub-galleries is returned 134 * images contained otherwise the number of sub-galleries is returned
135 * @return int the contents of the specified gallery 135 * @return int the contents of the specified gallery
136 */ 136 */
137 function itemCount() 137 function itemCount()
138 { 138 {
139 if($this->isAlbum()) 139 if($this->isAlbum())
140 return $this->imageCount(); 140 return $this->imageCount();
141 else 141 else
142 return $this->galleryCount(); 142 return $this->galleryCount();
143 } 143 }
144 144
145 /** 145 /**
146 * @return int number of galleries in current view 146 * @return int number of galleries in current view
147 */ 147 */
148 function galleryCountSelected() 148 function galleryCountSelected()
149 { 149 {
150 return min($this->galleryCount() - $this->startat, $this->config->thumb_number_gallery); 150 return min($this->galleryCount() - $this->startat, $this->config->thumb_number_gallery);
151 } 151 }
152 152
153 /** 153 /**
154 * @return int number of image in current view 154 * @return int number of image in current view
155 */ 155 */
156 function imageCountSelected() 156 function imageCountSelected()
157 { 157 {
158 return min($this->imageCount() - $this->startat, $this->config->thumb_number_album); 158 return min($this->imageCount() - $this->startat, $this->config->thumb_number_album);
159 } 159 }
160 160
161 161
162 /** 162 /**
163 * @return string the absolute, canonical system path to the image 163 * @return string the absolute, canonical system path to the image
164 */ 164 */
165 function realPath() 165 function realPath()
166 { 166 {
167 return realpath($this->config->base_path.$this->config->pathto_galleries.$this->id); 167 return realpath($this->config->base_path.$this->config->pathto_galleries.$this->id);
168 } 168 }
169 169
170 function thumbnailURL($type = "gallery") 170 function thumbnailURL($type = "gallery")
171 { 171 {
172 $thumb = $this->thumbnail($type); 172 $thumb = $this->thumbnail($type);
173 return $thumb->URL(); 173 return $thumb->URL();
174 } 174 }
175 175
176 function thumbnailHTML($class = "sgThumbGallery", $type = "gallery") 176 function thumbnailHTML($class = "sgThumbGallery", $type = "gallery")
177 { 177 {
178 $thumb = $this->thumbnail($type); 178 $thumb = $this->thumbnail($type);
179 if($thumb == null) { 179 if($thumb == null) {
180 $ret = nl2br($this->translator->_g("No\nthumbnail")); 180 $ret = nl2br($this->translator->_g("No\nthumbnail"));
181 } else { 181 } else {
182 $ret = '<img src="'.$thumb->URL().'" '; 182 $ret = '<img src="'.$thumb->URL().'" ';
183 $ret .= 'class="'.$class.'" '; 183 $ret .= 'class="'.$class.'" ';
184 $ret .= 'width="'.$thumb->width().'" height="'.$thumb->height().'" '; 184 $ret .= 'width="'.$thumb->width().'" height="'.$thumb->height().'" ';
185 $ret .= 'alt="'.$this->translator->_g("Sample image from gallery").'" />'; 185 $ret .= 'alt="'.$this->translator->_g("Sample image from gallery").'" />';
186 } 186 }
187 187
188 return $ret; 188 return $ret;
189 } 189 }
190 190
191 function thumbnailLink($class = "sgThumbGallery", $type = "gallery") 191 function thumbnailLink($class = "sgThumbGallery", $type = "gallery")
192 { 192 {
193 return '<a href="'.$this->URL().'">'.$this->thumbnailHTML($class, $type).'</a>'; 193 return '<a href="'.$this->URL().'">'.$this->thumbnailHTML($class, $type).'</a>';
194 } 194 }
195 195
196 /** 196 /**
197 * Removes script-generated HTML (BRs and URLs) but leaves any other HTML 197 * Removes script-generated HTML (BRs and URLs) but leaves any other HTML
198 * @return string the summary of the gallery 198 * @return string the summary of the gallery
199 */ 199 */
200 function summaryStripped() 200 function summaryStripped()
201 { 201 {
202 return str_replace("<br />","\n",$this->summary()); 202 return str_replace("<br />","\n",$this->summary());
203 } 203 }
204 204
205 function hasPrev() 205 function hasPrev()
206 { 206 {
207 return (bool) $this->index(); 207 return (bool) $this->index();
208 } 208 }
209 209
210 function hasNext() 210 function hasNext()
211 { 211 {
212 $index = $this->index(); 212 $index = $this->index();
213 return $index !== false && $index < $this->parent->galleryCount()-1; 213 return $index !== false && $index < $this->parent->galleryCount()-1;
214 } 214 }
215 215
216 function &prevGallery() 216 function &prevGallery()
217 { 217 {
218 $tmp =& new sgGallery($this->parent->id.'/'.$this->parent->galleries[$this->index()-1], $this->parent); 218 $tmp =& new sgGallery($this->parent->id.'/'.$this->parent->galleries[$this->index()-1], $this->parent);
219 return $tmp; 219 return $tmp;
220 } 220 }
221 221
222 function &nextGallery() 222 function &nextGallery()
223 { 223 {
224 $tmp =& new sgGallery($this->parent->id.'/'.$this->parent->galleries[$this->index()+1], $this->parent); 224 $tmp =& new sgGallery($this->parent->id.'/'.$this->parent->galleries[$this->index()+1], $this->parent);
225 return $tmp; 225 return $tmp;
226 } 226 }
227 227
228 function prevURL($action = null) 228 function prevURL($action = null)
229 { 229 {
230 $tmp =& $this->prevGallery(); 230 $tmp =& $this->prevGallery();
231 return $tmp->URL(null, $action); 231 return $tmp->URL(null, $action);
232 } 232 }
233 233
234 function nextURL($action = null) 234 function nextURL($action = null)
235 { 235 {
236 $tmp =& $this->nextGallery(); 236 $tmp =& $this->nextGallery();
237 return $tmp->URL(null, $action); 237 return $tmp->URL(null, $action);
238 } 238 }
239 239
240 function prevLink($action = null) 240 function prevLink($action = null)
241 { 241 {
242 return '<a href="'.$this->prevURL($action).'">'.$this->prevText().'</a>'; 242 return '<a href="'.$this->prevURL($action).'">'.$this->prevText().'</a>';
243 } 243 }
244 244
245 function nextLink($action = null) 245 function nextLink($action = null)
246 { 246 {
247 return '<a href="'.$this->nextURL($action).'">'.$this->nextText().'</a>'; 247 return '<a href="'.$this->nextURL($action).'">'.$this->nextText().'</a>';
248 } 248 }
249 249
250 function prevText() 250 function prevText()
251 { 251 {
252 return $this->translator->_g("gallery|Previous"); 252 return $this->translator->_g("gallery|Previous");
253 } 253 }
254 254
255 function nextText() 255 function nextText()
256 { 256 {
257 return $this->translator->_g("gallery|Next"); 257 return $this->translator->_g("gallery|Next");
258 } 258 }
259 259
260 /** 260 /**
261 * finds position of current gallery in parent array 261 * finds position of current gallery in parent array
262 */ 262 */
263 function index() 263 function index()
264 { 264 {
265 if(!$this->isRoot()) 265 if(!$this->isRoot())
266 foreach($this->parent->galleries as $key => $galleryId) 266 foreach($this->parent->galleries as $key => $galleryId)
267 if(basename($this->id) == $galleryId) 267 if(basename($this->id) == $galleryId)
268 return $key; 268 return $key;
269 269
270 return false; 270 return false;
271 } 271 }
272 272
273 /** Accessor methods */ 273 /** Accessor methods */
274 function summary() { return $this->summary; } 274 function summary() { return $this->summary; }
275 275
276 /** Private methods */ 276 /** Private methods */
277 function thumbnail($type) 277 function thumbnail($type)
278 { 278 {
279 //only create thumbnail if it doesn't already exist 279 //only create thumbnail if it doesn't already exist
280 if(!isset($this->thumbnails[$type])) { 280 if(!isset($this->thumbnails[$type])) {
281 281
282 if($this->filename == "__none__" || $this->imageCount() == 0) 282 if($this->filename == "__none__" || $this->imageCount() == 0)
283 return; 283 return;
284 elseif($this->filename == "__random__") { 284 elseif($this->filename == "__random__") {
285 srand(time()); //seed random number generator and select random image 285 srand(time()); //seed random number generator and select random image
286 $img =& $this->images[rand(0,count($this->images)-1)]; 286 $img =& $this->images[rand(0,count($this->images)-1)];
287 } else 287 } else
288 $img =& $this->findImage($this->filename); 288 $img =& $this->findImage($this->filename);
289 289
290 //create thumbnail 290 //create thumbnail
291 $this->thumbnails[$type] =& new sgThumbnail($img, $type); 291 $this->thumbnails[$type] =& new sgThumbnail($img, $type);
292 } 292 }
293 293
294 return $this->thumbnails[$type]; 294 return $this->thumbnails[$type];
295 } 295 }
296 296
297 /** 297 /**
298 * Finds an image from the current gallery 298 * Finds an image from the current gallery
299 * @param mixed either the filename of the image to select or the integer 299 * @param mixed either the filename of the image to select or the integer
300 * index of its position in the images array 300 * index of its position in the images array
301 * @return sgImage the image found 301 * @return sgImage the image found
302 */ 302 */
303 function &findImage($image) 303 function &findImage($image)
304 { 304 {
305 if(is_string($image)) 305 if(is_string($image))
306 foreach($this->images as $index => $img) 306 foreach($this->images as $index => $img)
307 if($img->id == $image) 307 if($img->id == $image)
308 return $this->images[$index]; 308 return $this->images[$index];
309 elseif(is_int($image) && $image >= 0 && $image < $this->imageCount()) 309 elseif(is_int($image) && $image >= 0 && $image < $this->imageCount())
310 return $this->images[$image]; 310 return $this->images[$image];
311 311
312 return null; 312 return null;
313 } 313 }
314 314
315 315
316 316
317 } 317 }
318   318  
319   319  
320 ?> 320 ?>