1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | branch : BSF (Best So Far) |
---|
7 | // | file : $RCSfile$ |
---|
8 | // | last update : $Date: 2006-02-12 16:52:16 -0500 (Sun, 12 Feb 2006) $ |
---|
9 | // | last modifier : $Author: plg $ |
---|
10 | // | revision : $Revision: 1036 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | class BaseCalendarLevel |
---|
28 | { |
---|
29 | function BaseCalendarLevel($sql, $allow_any=true, $labels=null) |
---|
30 | { |
---|
31 | $this->sql = $sql; |
---|
32 | $this->allow_any = $allow_any; |
---|
33 | $this->labels = $labels; |
---|
34 | } |
---|
35 | |
---|
36 | function sql() |
---|
37 | { |
---|
38 | return $this->sql; |
---|
39 | } |
---|
40 | function sql_equal($item) |
---|
41 | { |
---|
42 | return $this->sql.'='.$item; |
---|
43 | } |
---|
44 | function allow_any() |
---|
45 | { |
---|
46 | return $this->allow_any; |
---|
47 | } |
---|
48 | function get_label($item) |
---|
49 | { |
---|
50 | if ( isset($this->labels[$item]) ) |
---|
51 | { |
---|
52 | return $this->labels[$item]; |
---|
53 | } |
---|
54 | return $item; |
---|
55 | } |
---|
56 | |
---|
57 | var $sql; |
---|
58 | var $allow_any; |
---|
59 | var $labels; |
---|
60 | } |
---|
61 | |
---|
62 | class YearMonthCalendarLevel extends BaseCalendarLevel |
---|
63 | { |
---|
64 | function YearMonthCalendarLevel() |
---|
65 | { |
---|
66 | global $conf; |
---|
67 | parent::BaseCalendarLevel('DATE_FORMAT('.$conf['calendar_datefield'].',"%Y%m")', false); |
---|
68 | } |
---|
69 | |
---|
70 | function sql_equal($item) |
---|
71 | { |
---|
72 | global $conf; |
---|
73 | $y = (int)($item/100); |
---|
74 | $m = (int)$item%100; |
---|
75 | // There seems to be much difference in performance between these: |
---|
76 | return $conf['calendar_datefield']." BETWEEN '$y-$m-01' AND '$y-$m-31'"; |
---|
77 | /* return '(YEAR('.$conf['calendar_datefield'].')='.$y.' |
---|
78 | AND MONTH('.$conf['calendar_datefield'].')='.$m.')';*/ |
---|
79 | // return parent::sql_equal($item); |
---|
80 | } |
---|
81 | |
---|
82 | function get_label($item) |
---|
83 | { |
---|
84 | global $lang; |
---|
85 | if ( preg_match( '/(\d{4})(\d{2})/', $item, $matches) ) |
---|
86 | { |
---|
87 | return $lang['month'][(int)$matches[2]].' '.$matches[1]; |
---|
88 | } |
---|
89 | return $item; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | // just to optimize MySql query so that it uses the index |
---|
94 | class YearCalendarLevel extends BaseCalendarLevel |
---|
95 | { |
---|
96 | function YearCalendarLevel($sql, $allow_any=true) |
---|
97 | { |
---|
98 | parent::BaseCalendarLevel($sql, $allow_any); |
---|
99 | } |
---|
100 | |
---|
101 | function sql_equal($item) |
---|
102 | { |
---|
103 | global $conf; |
---|
104 | return $conf['calendar_datefield']." BETWEEN '$item-01-01' AND '$item-12-31'"; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Parses $param and returns an array of calendar levels |
---|
110 | * @param requested array of requested items for each calendar level |
---|
111 | * @param cal_type is the requested calendar type |
---|
112 | */ |
---|
113 | function get_calendar_params($param, &$requested, &$cal_type) |
---|
114 | { |
---|
115 | global $conf, $lang; |
---|
116 | $requested = explode('-', $param); |
---|
117 | $cal_struct = array(); |
---|
118 | if ($requested[0]=='ywd') |
---|
119 | { |
---|
120 | array_push($cal_struct, new YearCalendarLevel( |
---|
121 | 'YEAR('.$conf['calendar_datefield'].')' ) ); |
---|
122 | array_push($cal_struct, new BaseCalendarLevel( |
---|
123 | 'WEEK('.$conf['calendar_datefield'].')+1' ) ); |
---|
124 | array_push($cal_struct, new BaseCalendarLevel( |
---|
125 | 'DAYOFWEEK('.$conf['calendar_datefield'].')-1', true, $lang['day'] ) ); |
---|
126 | $cal_type=array_shift($requested); |
---|
127 | } |
---|
128 | else if ($requested[0]=='md') |
---|
129 | { |
---|
130 | array_push($cal_struct, new YearMonthCalendarLevel() ); |
---|
131 | array_push($cal_struct, new BaseCalendarLevel( |
---|
132 | 'DAY('.$conf['calendar_datefield'].')' ) ); |
---|
133 | $cal_type=array_shift($requested); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | array_push($cal_struct, new YearCalendarLevel( |
---|
138 | 'YEAR('.$conf['calendar_datefield'].')' ) ); |
---|
139 | array_push($cal_struct, new BaseCalendarLevel( |
---|
140 | 'MONTH('.$conf['calendar_datefield'].')', true, $lang['month'] ) ); |
---|
141 | array_push($cal_struct, new BaseCalendarLevel( |
---|
142 | 'DAY('.$conf['calendar_datefield'].')' ) ); |
---|
143 | |
---|
144 | if ($requested[0]=='ymd') |
---|
145 | { |
---|
146 | $cal_type=array_shift($requested); |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | $cal_type=''; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | // perform a sanity check on $requested |
---|
155 | while (count($requested)>count($cal_struct)) |
---|
156 | { |
---|
157 | array_pop($requested); |
---|
158 | } |
---|
159 | |
---|
160 | $any_count = 0; |
---|
161 | for ($i=0; $i<count($requested); $i++) |
---|
162 | { |
---|
163 | if ($requested[$i]=='any') |
---|
164 | { |
---|
165 | if (! $cal_struct[$i]->allow_any() ) |
---|
166 | { |
---|
167 | while ($i<count($requested)) |
---|
168 | { |
---|
169 | array_pop( $requested ); |
---|
170 | } |
---|
171 | break; |
---|
172 | } |
---|
173 | $any_count++; |
---|
174 | } |
---|
175 | elseif ( empty($requested[$i]) ) |
---|
176 | { |
---|
177 | while ($i<count($requested)) |
---|
178 | { |
---|
179 | array_pop( $requested ); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | if ($any_count==count($cal_struct)) |
---|
184 | { |
---|
185 | array_pop($requested); |
---|
186 | } |
---|
187 | return $cal_struct; |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | function initialize_calendar() |
---|
193 | { |
---|
194 | global $page, $conf, $user, $template; |
---|
195 | |
---|
196 | if ( !isset($page['cat']) or is_numeric($page['cat']) ) |
---|
197 | { // we will regenerate the items by including subcats elements |
---|
198 | $page['cat_nb_images']=0; |
---|
199 | $page['items']=array(); |
---|
200 | if ( is_numeric($page['cat']) ) |
---|
201 | { |
---|
202 | $sub_ids = get_subcat_ids(array($page['cat'])); |
---|
203 | $sub_ids = array_diff($sub_ids, |
---|
204 | explode(',', $user['forbidden_categories']) ); |
---|
205 | if (empty($sub_ids)) |
---|
206 | { |
---|
207 | return; // nothing to do |
---|
208 | } |
---|
209 | $category_restriction .= ' IN ('.implode(',',$sub_ids).')'; |
---|
210 | } |
---|
211 | else |
---|
212 | { |
---|
213 | $category_restriction = ' NOT IN ('.$user['forbidden_categories'].')'; |
---|
214 | } |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | if ( empty($page['items']) ) |
---|
219 | { |
---|
220 | return; // nothing to do |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | pwg_debug('start initialize_calendar'); |
---|
225 | |
---|
226 | $cal_struct = get_calendar_params($_GET['calendar'], $requested, $cal_type); |
---|
227 | |
---|
228 | //echo ('<pre>'. var_export($cal_struct, true) . '</pre>'); |
---|
229 | //echo ('<pre>'. var_export($requested, true) . '</pre>'); |
---|
230 | |
---|
231 | $category_calling = false; |
---|
232 | if (basename($_SERVER["PHP_SELF"]) == 'category.php') |
---|
233 | { |
---|
234 | $category_calling = true; |
---|
235 | } |
---|
236 | |
---|
237 | if ($category_calling) |
---|
238 | { |
---|
239 | $template->assign_block_vars('calendar', array()); |
---|
240 | |
---|
241 | $url_base = get_query_string_diff(array('start','calendar')); |
---|
242 | $url_base .= empty($url_base) ? '?' : '&'; |
---|
243 | $url_base .= 'calendar='; |
---|
244 | $url_base = PHPWG_ROOT_PATH.'category.php'.$url_base; |
---|
245 | |
---|
246 | // Build navigation bar for calendar styles |
---|
247 | $nav_bar = 'Styles: '; |
---|
248 | foreach ( array('ymd','md','ywd') as $type) |
---|
249 | { |
---|
250 | if ( $type==$cal_type or ($cal_type=='' and $type=='ymd') ) |
---|
251 | { |
---|
252 | $nav_bar .= $type.' '; |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | $nav_bar .= '<a href="'. $url_base.$type . '">'.$type.'</a> '; |
---|
257 | } |
---|
258 | } |
---|
259 | $template->assign_block_vars( 'calendar.navbar', |
---|
260 | array( 'BAR' => $nav_bar) |
---|
261 | ); |
---|
262 | |
---|
263 | $url_base .= $cal_type; |
---|
264 | if ($cal_type!='') |
---|
265 | { |
---|
266 | $url_base .= '-'; |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | $prev_level_query=' |
---|
271 | AND '.$conf['calendar_datefield'].' IS NOT NULL '; |
---|
272 | |
---|
273 | for ($i=0; $i<count($cal_struct); $i++) |
---|
274 | { |
---|
275 | $crt_cal_level = $cal_struct[$i]; |
---|
276 | $query = ' |
---|
277 | SELECT DISTINCT('.$crt_cal_level->sql().') AS period, COUNT(id) as count |
---|
278 | FROM '.IMAGES_TABLE; |
---|
279 | if ( isset($category_restriction) ) |
---|
280 | { |
---|
281 | $query.= ' |
---|
282 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
283 | WHERE category_id' . $category_restriction; |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | $query.= ' |
---|
288 | WHERE id IN (' . implode(',',$page['items']) .')'; |
---|
289 | } |
---|
290 | $query.= $prev_level_query; |
---|
291 | $query.= ' |
---|
292 | GROUP BY period'; |
---|
293 | |
---|
294 | $level_items=array(); |
---|
295 | $result = pwg_query($query); |
---|
296 | $total_pics = 0; |
---|
297 | while ($row = mysql_fetch_array($result)) |
---|
298 | { |
---|
299 | $level_items[$row['period']] = (int)$row['count']; |
---|
300 | $total_pics += $row['count']; |
---|
301 | } |
---|
302 | //echo ('<pre>'. var_export($level_items, true) . '</pre>'); |
---|
303 | |
---|
304 | if ( $requested[$i] == 'any' and ! $crt_cal_level->allow_any() ) |
---|
305 | { |
---|
306 | unset($requested[$i]); |
---|
307 | } |
---|
308 | |
---|
309 | // --- Build the navigation bar |
---|
310 | if ( $crt_cal_level->allow_any() ) |
---|
311 | { |
---|
312 | $level_items['any'] = $total_pics; |
---|
313 | } |
---|
314 | $nav_bar=''; |
---|
315 | foreach ($level_items as $item => $nb_images) |
---|
316 | { |
---|
317 | $label = $crt_cal_level->get_label($item); |
---|
318 | if ( $item==$requested[$i] ) |
---|
319 | { |
---|
320 | $nav_bar .= ' <span class="dateSelected">'; |
---|
321 | $nav_bar .= $label; |
---|
322 | $nav_bar.= '</span>'; |
---|
323 | } |
---|
324 | else |
---|
325 | { |
---|
326 | $url = $url_base . $item; |
---|
327 | $nav_bar .= '<a href="'.$url.'">'; |
---|
328 | $nav_bar .= $label; |
---|
329 | $nav_bar .= '</a>'; |
---|
330 | } |
---|
331 | $nav_bar .= ' '; |
---|
332 | } |
---|
333 | $template->assign_block_vars( 'calendar.navbar', |
---|
334 | array( 'BAR' => $nav_bar) |
---|
335 | ); |
---|
336 | |
---|
337 | if ( !isset($requested[$i]) ) |
---|
338 | break; |
---|
339 | if ($requested[$i]!='any') |
---|
340 | { |
---|
341 | $prev_level_query.= ' AND '.$crt_cal_level->sql_equal($requested[$i]); |
---|
342 | } |
---|
343 | $url_base .= $requested[$i].'-'; |
---|
344 | } // end for each calendar level |
---|
345 | |
---|
346 | |
---|
347 | if ( $i < count($cal_struct) ) |
---|
348 | { |
---|
349 | $template->assign_block_vars('thumbnails', array()); |
---|
350 | $template->assign_block_vars('thumbnails.line', array()); |
---|
351 | foreach ($level_items as $level_item => $nb_pics) |
---|
352 | { |
---|
353 | if ($level_item=='any') |
---|
354 | continue; |
---|
355 | $query = ' |
---|
356 | SELECT file,tn_ext,'.$conf['calendar_datefield'].',path |
---|
357 | FROM '.IMAGES_TABLE; |
---|
358 | if ( isset($category_restriction) ) |
---|
359 | { |
---|
360 | $query.= ' |
---|
361 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
362 | WHERE category_id' . $category_restriction; |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | $query.= ' |
---|
367 | WHERE id IN (' . implode(',',$page['items']) .')'; |
---|
368 | } |
---|
369 | $query.= $prev_level_query; |
---|
370 | $query.= ' AND '.$crt_cal_level->sql_equal($level_item); |
---|
371 | $query.= ' |
---|
372 | ORDER BY RAND() |
---|
373 | LIMIT 0,1'; |
---|
374 | $row = mysql_fetch_array(pwg_query($query)); |
---|
375 | |
---|
376 | $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']); |
---|
377 | $thumbnail_title = $crt_cal_level->get_label($level_item); |
---|
378 | $name = $thumbnail_title .' ('.$nb_pics.')'; |
---|
379 | |
---|
380 | $template->assign_block_vars( |
---|
381 | 'thumbnails.line.thumbnail', |
---|
382 | array( |
---|
383 | 'IMAGE'=>$thumbnail_src, |
---|
384 | 'IMAGE_ALT'=>$row['file'], |
---|
385 | 'IMAGE_TITLE'=>$thumbnail_title, |
---|
386 | 'U_IMG_LINK'=>$url_base.$level_item |
---|
387 | ) |
---|
388 | ); |
---|
389 | $template->assign_block_vars( |
---|
390 | 'thumbnails.line.thumbnail.category_name', |
---|
391 | array( |
---|
392 | 'NAME' => $name |
---|
393 | ) |
---|
394 | ); |
---|
395 | } |
---|
396 | unset( $page['thumbnails_include'] ); // maybe move everything to a new include file ? |
---|
397 | pwg_debug('end initialize_calendar for thumbs'); |
---|
398 | return; |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | if (!$category_calling or $i==count($cal_struct) ) |
---|
403 | { |
---|
404 | $query = 'SELECT DISTINCT(id) FROM '.IMAGES_TABLE; |
---|
405 | if ( isset($category_restriction) ) |
---|
406 | { |
---|
407 | $query.= ' |
---|
408 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
409 | WHERE category_id' . $category_restriction; |
---|
410 | } |
---|
411 | else |
---|
412 | { |
---|
413 | $query.= ' |
---|
414 | WHERE id IN ('.implode(',',$page['items']).')'; |
---|
415 | } |
---|
416 | $query.= ' |
---|
417 | AND '.$conf['calendar_datefield'].' IS NOT NULL '; |
---|
418 | |
---|
419 | for ($i=0; $i<count($cal_struct); $i++) |
---|
420 | { |
---|
421 | assert( isset($requested[$i]) ); // otherwise we should not be here |
---|
422 | if ($requested[$i]!='any') |
---|
423 | { |
---|
424 | $query.= ' |
---|
425 | AND '. $cal_struct[$i]->sql_equal($requested[$i]); |
---|
426 | } |
---|
427 | |
---|
428 | } |
---|
429 | |
---|
430 | $page['items'] = array_from_query($query, 'id'); |
---|
431 | $page['cat_nb_images'] = count($page['items']); |
---|
432 | $page['thumbnails_include'] = 'include/category_default.inc.php'; |
---|
433 | } |
---|
434 | pwg_debug('end initialize_calendar'); |
---|
435 | } |
---|
436 | |
---|
437 | ?> |
---|