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