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