1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php'); |
---|
25 | |
---|
26 | define ('CYEAR', 0); |
---|
27 | define ('CMONTH', 1); |
---|
28 | define ('CDAY', 2); |
---|
29 | |
---|
30 | /** |
---|
31 | * Monthly calendar style (composed of years/months and days) |
---|
32 | */ |
---|
33 | class Calendar extends CalendarBase |
---|
34 | { |
---|
35 | |
---|
36 | /** |
---|
37 | * Initialize the calendar |
---|
38 | * @param string inner_sql used for queries (INNER JOIN or normal) |
---|
39 | */ |
---|
40 | function initialize($inner_sql) |
---|
41 | { |
---|
42 | parent::initialize($inner_sql); |
---|
43 | global $lang; |
---|
44 | $this->calendar_levels = array( |
---|
45 | array( |
---|
46 | 'sql'=> pwg_db_get_year($this->date_field), |
---|
47 | 'labels' => null |
---|
48 | ), |
---|
49 | array( |
---|
50 | 'sql'=> pwg_db_get_month($this->date_field), |
---|
51 | 'labels' => $lang['month'] |
---|
52 | ), |
---|
53 | array( |
---|
54 | 'sql'=> pwg_db_get_dayofmonth($this->date_field), |
---|
55 | 'labels' => null |
---|
56 | ), |
---|
57 | ); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Generate navigation bars for category page |
---|
62 | * @return boolean false to indicate that thumbnails |
---|
63 | * where not included here, true otherwise |
---|
64 | */ |
---|
65 | function generate_category_content() |
---|
66 | { |
---|
67 | global $conf, $page; |
---|
68 | |
---|
69 | $view_type = $page['chronology_view']; |
---|
70 | if ($view_type==CAL_VIEW_CALENDAR) |
---|
71 | { |
---|
72 | global $template; |
---|
73 | $tpl_var = array(); |
---|
74 | if ( count($page['chronology_date'])==0 ) |
---|
75 | {//case A: no year given - display all years+months |
---|
76 | if ($this->build_global_calendar($tpl_var)) |
---|
77 | { |
---|
78 | $template->assign('chronology_calendar', $tpl_var); |
---|
79 | return true; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | if ( count($page['chronology_date'])==1 ) |
---|
84 | {//case B: year given - display all days in given year |
---|
85 | if ($this->build_year_calendar($tpl_var)) |
---|
86 | { |
---|
87 | $template->assign('chronology_calendar', $tpl_var); |
---|
88 | $this->build_nav_bar(CYEAR); // years |
---|
89 | return true; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | if ( count($page['chronology_date'])==2 ) |
---|
94 | {//case C: year+month given - display a nice month calendar |
---|
95 | if ( $this->build_month_calendar($tpl_var) ) |
---|
96 | { |
---|
97 | $template->assign('chronology_calendar', $tpl_var); |
---|
98 | } |
---|
99 | $this->build_next_prev(); |
---|
100 | return true; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | if ($view_type==CAL_VIEW_LIST or count($page['chronology_date'])==3) |
---|
105 | { |
---|
106 | if ( count($page['chronology_date'])==0 ) |
---|
107 | { |
---|
108 | $this->build_nav_bar(CYEAR); // years |
---|
109 | } |
---|
110 | if ( count($page['chronology_date'])==1) |
---|
111 | { |
---|
112 | $this->build_nav_bar(CMONTH); // month |
---|
113 | } |
---|
114 | if ( count($page['chronology_date'])==2 ) |
---|
115 | { |
---|
116 | $day_labels = range( 1, $this->get_all_days_in_month( |
---|
117 | $page['chronology_date'][CYEAR] ,$page['chronology_date'][CMONTH] ) ); |
---|
118 | array_unshift($day_labels, 0); |
---|
119 | unset( $day_labels[0] ); |
---|
120 | $this->build_nav_bar( CDAY, $day_labels ); // days |
---|
121 | } |
---|
122 | $this->build_next_prev(); |
---|
123 | } |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns a sql where subquery for the date field |
---|
130 | * @param int max_levels return the where up to this level |
---|
131 | * (e.g. 2=only year and month) |
---|
132 | * @return string |
---|
133 | */ |
---|
134 | function get_date_where($max_levels=3) |
---|
135 | { |
---|
136 | global $page; |
---|
137 | |
---|
138 | $date = $page['chronology_date']; |
---|
139 | while (count($date)>$max_levels) |
---|
140 | { |
---|
141 | array_pop($date); |
---|
142 | } |
---|
143 | $res = ''; |
---|
144 | if (isset($date[CYEAR]) and $date[CYEAR]!=='any') |
---|
145 | { |
---|
146 | $b = $date[CYEAR] . '-'; |
---|
147 | $e = $date[CYEAR] . '-'; |
---|
148 | if (isset($date[CMONTH]) and $date[CMONTH]!=='any') |
---|
149 | { |
---|
150 | $b .= sprintf('%02d-', $date[CMONTH]); |
---|
151 | $e .= sprintf('%02d-', $date[CMONTH]); |
---|
152 | if (isset($date[CDAY]) and $date[CDAY]!=='any') |
---|
153 | { |
---|
154 | $b .= sprintf('%02d', $date[CDAY]); |
---|
155 | $e .= sprintf('%02d', $date[CDAY]); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | $b .= '01'; |
---|
160 | $e .= $this->get_all_days_in_month($date[CYEAR], $date[CMONTH]); |
---|
161 | } |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | $b .= '01-01'; |
---|
166 | $e .= '12-31'; |
---|
167 | if (isset($date[CMONTH]) and $date[CMONTH]!=='any') |
---|
168 | { |
---|
169 | $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH]; |
---|
170 | } |
---|
171 | if (isset($date[CDAY]) and $date[CDAY]!=='any') |
---|
172 | { |
---|
173 | $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY]; |
---|
174 | } |
---|
175 | } |
---|
176 | $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res; |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | $res = ' AND '.$this->date_field.' IS NOT NULL'; |
---|
181 | if (isset($date[CMONTH]) and $date[CMONTH]!=='any') |
---|
182 | { |
---|
183 | $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH]; |
---|
184 | } |
---|
185 | if (isset($date[CDAY]) and $date[CDAY]!=='any') |
---|
186 | { |
---|
187 | $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY]; |
---|
188 | } |
---|
189 | } |
---|
190 | return $res; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | //--------------------------------------------------------- private members --- |
---|
196 | |
---|
197 | // returns an array with all the days in a given month |
---|
198 | function get_all_days_in_month($year, $month) |
---|
199 | { |
---|
200 | $md= array(1=>31,28,31,30,31,30,31,31,30,31,30,31); |
---|
201 | |
---|
202 | if ( is_numeric($year) and $month==2) |
---|
203 | { |
---|
204 | $nb_days = $md[2]; |
---|
205 | if ( ($year%4==0) and ( ($year%100!=0) or ($year%400!=0) ) ) |
---|
206 | { |
---|
207 | $nb_days++; |
---|
208 | } |
---|
209 | } |
---|
210 | elseif ( is_numeric($month) ) |
---|
211 | { |
---|
212 | $nb_days = $md[ $month ]; |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | $nb_days = 31; |
---|
217 | } |
---|
218 | return $nb_days; |
---|
219 | } |
---|
220 | |
---|
221 | function build_global_calendar(&$tpl_var) |
---|
222 | { |
---|
223 | global $page; |
---|
224 | |
---|
225 | assert( count($page['chronology_date']) == 0 ); |
---|
226 | $query=' |
---|
227 | SELECT '.pwg_db_get_date_YYYYMM($this->date_field).' as period,' |
---|
228 | .pwg_db_get_year($this->date_field).' as year, ' |
---|
229 | .pwg_db_get_month($this->date_field).' as month, |
---|
230 | count(distinct id) as count'; |
---|
231 | $query.= $this->inner_sql; |
---|
232 | $query.= $this->get_date_where(); |
---|
233 | $query.= ' |
---|
234 | GROUP BY period, year, month |
---|
235 | ORDER BY year DESC, month ASC'; |
---|
236 | |
---|
237 | $result = pwg_query($query); |
---|
238 | $items=array(); |
---|
239 | while ($row = pwg_db_fetch_assoc($result)) |
---|
240 | { |
---|
241 | $y = substr($row['period'], 0, 4); |
---|
242 | $m = (int)substr($row['period'], 4, 2); |
---|
243 | if ( ! isset($items[$y]) ) |
---|
244 | { |
---|
245 | $items[$y] = array('nb_images'=>0, 'children'=>array() ); |
---|
246 | } |
---|
247 | $items[$y]['children'][$m] = $row['count']; |
---|
248 | $items[$y]['nb_images'] += $row['count']; |
---|
249 | } |
---|
250 | //echo ('<pre>'. var_export($items, true) . '</pre>'); |
---|
251 | if (count($items)==1) |
---|
252 | {// only one year exists so bail out to year view |
---|
253 | list($y) = array_keys($items); |
---|
254 | $page['chronology_date'][CYEAR] = $y; |
---|
255 | return false; |
---|
256 | } |
---|
257 | |
---|
258 | global $lang; |
---|
259 | foreach ( $items as $year=>$year_data) |
---|
260 | { |
---|
261 | $chronology_date = array( $year ); |
---|
262 | $url = duplicate_index_url( array('chronology_date'=>$chronology_date) ); |
---|
263 | |
---|
264 | $nav_bar = $this->get_nav_bar_from_items( $chronology_date, |
---|
265 | $year_data['children'], false, false, $lang['month'] ); |
---|
266 | |
---|
267 | $tpl_var['calendar_bars'][] = |
---|
268 | array( |
---|
269 | 'U_HEAD' => $url, |
---|
270 | 'NB_IMAGES' => $year_data['nb_images'], |
---|
271 | 'HEAD_LABEL' => $year, |
---|
272 | 'items' => $nav_bar, |
---|
273 | ); |
---|
274 | } |
---|
275 | return true; |
---|
276 | } |
---|
277 | |
---|
278 | function build_year_calendar(&$tpl_var) |
---|
279 | { |
---|
280 | global $page; |
---|
281 | |
---|
282 | assert( count($page['chronology_date']) == 1 ); |
---|
283 | $query='SELECT '.pwg_db_get_date_MMDD($this->date_field).' as period, |
---|
284 | COUNT(DISTINCT id) as count'; |
---|
285 | $query.= $this->inner_sql; |
---|
286 | $query.= $this->get_date_where(); |
---|
287 | $query.= ' |
---|
288 | GROUP BY period |
---|
289 | ORDER BY period ASC'; |
---|
290 | |
---|
291 | $result = pwg_query($query); |
---|
292 | $items=array(); |
---|
293 | while ($row = pwg_db_fetch_assoc($result)) |
---|
294 | { |
---|
295 | $m = (int)substr($row['period'], 0, 2); |
---|
296 | $d = substr($row['period'], 2, 2); |
---|
297 | if ( ! isset($items[$m]) ) |
---|
298 | { |
---|
299 | $items[$m] = array('nb_images'=>0, 'children'=>array() ); |
---|
300 | } |
---|
301 | $items[$m]['children'][$d] = $row['count']; |
---|
302 | $items[$m]['nb_images'] += $row['count']; |
---|
303 | } |
---|
304 | if (count($items)==1) |
---|
305 | { // only one month exists so bail out to month view |
---|
306 | list($m) = array_keys($items); |
---|
307 | $page['chronology_date'][CMONTH] = $m; |
---|
308 | return false; |
---|
309 | } |
---|
310 | global $lang; |
---|
311 | foreach ( $items as $month=>$month_data) |
---|
312 | { |
---|
313 | $chronology_date = array( $page['chronology_date'][CYEAR], $month ); |
---|
314 | $url = duplicate_index_url( array('chronology_date'=>$chronology_date) ); |
---|
315 | |
---|
316 | $nav_bar = $this->get_nav_bar_from_items( $chronology_date, |
---|
317 | $month_data['children'], false ); |
---|
318 | |
---|
319 | $tpl_var['calendar_bars'][] = |
---|
320 | array( |
---|
321 | 'U_HEAD' => $url, |
---|
322 | 'NB_IMAGES' => $month_data['nb_images'], |
---|
323 | 'HEAD_LABEL' => $lang['month'][$month], |
---|
324 | 'items' => $nav_bar, |
---|
325 | ); |
---|
326 | } |
---|
327 | return true; |
---|
328 | |
---|
329 | } |
---|
330 | |
---|
331 | function build_month_calendar(&$tpl_var) |
---|
332 | { |
---|
333 | global $page; |
---|
334 | |
---|
335 | $query='SELECT '.pwg_db_get_dayofmonth($this->date_field).' as period, |
---|
336 | COUNT(DISTINCT id) as count'; |
---|
337 | $query.= $this->inner_sql; |
---|
338 | $query.= $this->get_date_where(); |
---|
339 | $query.= ' |
---|
340 | GROUP BY period |
---|
341 | ORDER BY period ASC'; |
---|
342 | |
---|
343 | $items=array(); |
---|
344 | $result = pwg_query($query); |
---|
345 | while ($row = pwg_db_fetch_assoc($result)) |
---|
346 | { |
---|
347 | $d = (int)$row['period']; |
---|
348 | $items[$d] = array('nb_images'=>$row['count']); |
---|
349 | } |
---|
350 | |
---|
351 | foreach ( $items as $day=>$data) |
---|
352 | { |
---|
353 | $page['chronology_date'][CDAY]=$day; |
---|
354 | $query = ' |
---|
355 | SELECT id, file,tn_ext,path, width, height, '.pwg_db_get_dayofweek($this->date_field).'-1 as dow'; |
---|
356 | $query.= $this->inner_sql; |
---|
357 | $query.= $this->get_date_where(); |
---|
358 | $query.= ' |
---|
359 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
360 | LIMIT 1'; |
---|
361 | unset ( $page['chronology_date'][CDAY] ); |
---|
362 | |
---|
363 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
364 | $items[$day]['tn_url'] = get_thumbnail_url($row); |
---|
365 | $items[$day]['file'] = $row['file']; |
---|
366 | $items[$day]['path'] = $row['path']; |
---|
367 | $items[$day]['tn_ext'] = @$row['tn_ext']; |
---|
368 | $items[$day]['width'] = $row['width']; |
---|
369 | $items[$day]['height'] = $row['height']; |
---|
370 | $items[$day]['dow'] = $row['dow']; |
---|
371 | } |
---|
372 | |
---|
373 | global $lang, $conf; |
---|
374 | |
---|
375 | if ( !empty($items) |
---|
376 | and $conf['calendar_month_cell_width']>0 |
---|
377 | and $conf['calendar_month_cell_height']>0) |
---|
378 | { |
---|
379 | list($known_day) = array_keys($items); |
---|
380 | $known_dow = $items[$known_day]['dow']; |
---|
381 | $first_day_dow = ($known_dow-($known_day-1))%7; |
---|
382 | if ($first_day_dow<0) |
---|
383 | { |
---|
384 | $first_day_dow += 7; |
---|
385 | } |
---|
386 | //first_day_dow = week day corresponding to the first day of this month |
---|
387 | $wday_labels = $lang['day']; |
---|
388 | |
---|
389 | // BEGIN - pass now in week starting Monday |
---|
390 | if ($first_day_dow==0) |
---|
391 | { |
---|
392 | $first_day_dow = 6; |
---|
393 | } |
---|
394 | else |
---|
395 | { |
---|
396 | $first_day_dow -= 1; |
---|
397 | } |
---|
398 | array_push( $wday_labels, array_shift($wday_labels) ); |
---|
399 | // END - pass now in week starting Monday |
---|
400 | |
---|
401 | $cell_width = $conf['calendar_month_cell_width']; |
---|
402 | $cell_height = $conf['calendar_month_cell_height']; |
---|
403 | |
---|
404 | $tpl_weeks = array(); |
---|
405 | $tpl_crt_week = array(); |
---|
406 | |
---|
407 | //fill the empty days in the week before first day of this month |
---|
408 | for ($i=0; $i<$first_day_dow; $i++) |
---|
409 | { |
---|
410 | $tpl_crt_week[] = array(); |
---|
411 | } |
---|
412 | |
---|
413 | for ( $day = 1; |
---|
414 | $day <= $this->get_all_days_in_month( |
---|
415 | $page['chronology_date'][CYEAR], $page['chronology_date'][CMONTH] |
---|
416 | ); |
---|
417 | $day++) |
---|
418 | { |
---|
419 | $dow = ($first_day_dow + $day-1)%7; |
---|
420 | if ($dow==0 and $day!=1) |
---|
421 | { |
---|
422 | $tpl_weeks[] = $tpl_crt_week; // add finished week to week list |
---|
423 | $tpl_crt_week = array(); // start new week |
---|
424 | } |
---|
425 | |
---|
426 | if ( !isset($items[$day]) ) |
---|
427 | {// empty day |
---|
428 | $tpl_crt_week[] = |
---|
429 | array( |
---|
430 | 'DAY' => $day |
---|
431 | ); |
---|
432 | } |
---|
433 | else |
---|
434 | { |
---|
435 | $thumb = get_thumbnail_path($items[$day]); |
---|
436 | $tn_size = @getimagesize($thumb); |
---|
437 | |
---|
438 | $tn_width = $tn_size[0]; |
---|
439 | $tn_height = $tn_size[1]; |
---|
440 | |
---|
441 | // now need to fit the thumbnail of size tn_size within |
---|
442 | // a cell of size cell_size by playing with CSS position (left/top) |
---|
443 | // and the width and height of <img>. |
---|
444 | $ratio_w = $tn_width/$cell_width; |
---|
445 | $ratio_h = $tn_height/$cell_height; |
---|
446 | |
---|
447 | $pos_top=$pos_left=0; |
---|
448 | $css_style = ''; |
---|
449 | |
---|
450 | if ( $ratio_w>1 and $ratio_h>1) |
---|
451 | {// cell completely smaller than the thumbnail so we will let the browser |
---|
452 | // resize the thumbnail |
---|
453 | if ($ratio_w > $ratio_h ) |
---|
454 | {// thumbnail ratio compared to cell -> wide format |
---|
455 | $css_style = 'height:'.$cell_height.'px;'; |
---|
456 | $browser_img_width = $cell_height*$tn_width/$tn_height; |
---|
457 | $pos_left = ($browser_img_width-$cell_width)/2; |
---|
458 | } |
---|
459 | else |
---|
460 | { |
---|
461 | $css_style = 'width:'.$cell_width.'px;'; |
---|
462 | $browser_img_height = $cell_width*$tn_height/$tn_width; |
---|
463 | $pos_top = ($browser_img_height-$cell_height)/2; |
---|
464 | } |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | $pos_left = ($tn_width-$cell_width)/2; |
---|
469 | $pos_top = ($tn_height-$cell_height)/2; |
---|
470 | } |
---|
471 | |
---|
472 | if ( round($pos_left)!=0) |
---|
473 | { |
---|
474 | $css_style.='left:'.round(-$pos_left).'px;'; |
---|
475 | } |
---|
476 | if ( round($pos_top)!=0) |
---|
477 | { |
---|
478 | $css_style.='top:'.round(-$pos_top).'px;'; |
---|
479 | } |
---|
480 | $url = duplicate_index_url( |
---|
481 | array( |
---|
482 | 'chronology_date' => |
---|
483 | array( |
---|
484 | $page['chronology_date'][CYEAR], |
---|
485 | $page['chronology_date'][CMONTH], |
---|
486 | $day |
---|
487 | ) |
---|
488 | ) |
---|
489 | ); |
---|
490 | |
---|
491 | $tpl_crt_week[] = |
---|
492 | array( |
---|
493 | 'DAY' => $day, |
---|
494 | 'DOW' => $dow, |
---|
495 | 'NB_ELEMENTS' => $items[$day]['nb_images'], |
---|
496 | 'IMAGE' => $items[$day]['tn_url'], |
---|
497 | 'U_IMG_LINK' => $url, |
---|
498 | 'IMAGE_STYLE' => $css_style, |
---|
499 | 'IMAGE_ALT' => $items[$day]['file'], |
---|
500 | ); |
---|
501 | } |
---|
502 | } |
---|
503 | //fill the empty days in the week after the last day of this month |
---|
504 | while ( $dow<6 ) |
---|
505 | { |
---|
506 | $tpl_crt_week[] = array(); |
---|
507 | $dow++; |
---|
508 | } |
---|
509 | $tpl_weeks[] = $tpl_crt_week; |
---|
510 | |
---|
511 | $tpl_var['month_view'] = |
---|
512 | array( |
---|
513 | 'CELL_WIDTH' => $cell_width, |
---|
514 | 'CELL_HEIGHT' => $cell_height, |
---|
515 | 'wday_labels' => $wday_labels, |
---|
516 | 'weeks' => $tpl_weeks, |
---|
517 | ); |
---|
518 | } |
---|
519 | |
---|
520 | return true; |
---|
521 | } |
---|
522 | |
---|
523 | } |
---|
524 | ?> |
---|