source: trunk/include/calendar_monthly.class.php @ 1061

Last change on this file since 1061 was 1061, checked in by rvelices, 18 years ago

calendar improvement: month calendar view a la flickr

fix: html 4.01 compliant in rating.tpl

fix: issue with IE from version 1052 (redirect on access denied)

  • Property svn:eol-style set to native
File size: 16.3 KB
Line 
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-01-27 02:11:43 +0100 (ven, 27 jan 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1014 $
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
27include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php');
28
29define ('CYEAR',  0);
30define ('CMONTH', 1);
31define ('CDAY',   2);
32
33/**
34 * Monthly calendar style (composed of years/months and days)
35 */
36class Calendar extends CalendarBase
37{
38
39  /**
40   * Initialize the calendar
41   * @param string date_field db column on which this calendar works
42   * @param string inner_sql used for queries (INNER JOIN or normal)
43   * @param array date_components
44   */
45  function initialize($date_field, $inner_sql, $date_components)
46  {
47    parent::initialize($date_field, $inner_sql, $date_components);
48    global $lang;
49    $this->calendar_levels = array(
50      array(
51          'sql'=> 'YEAR('.$this->date_field.')',
52          'labels' => null
53        ),
54      array(
55          'sql'=> 'MONTH('.$this->date_field.')',
56          'labels' => $lang['month']
57        ),
58      array(
59          'sql'=> 'DAYOFMONTH('.$this->date_field.')',
60          'labels' => null
61        ),
62     );
63  }
64
65/**
66 * Generate navigation bars for category page
67 * @return boolean false to indicate that thumbnails
68 * where not included here, true otherwise
69 */
70function generate_category_content($url_base, $view_type)
71{
72  global $conf;
73
74  $this->url_base = $url_base;
75
76  if ($view_type==CAL_VIEW_CALENDAR)
77  {
78    if ( count($this->date_components)==0 )
79    {//case A: no year given - display all years+months
80      if ($this->build_global_calendar())
81        return true;
82    }
83
84    if ( count($this->date_components)==1 )
85    {//case B: year given - display all days in given year
86      if ($this->build_year_calendar())
87      {
88        $this->build_nav_bar(CYEAR); // years
89        return true;
90      }
91    }
92
93    if ( count($this->date_components)==2 )
94    {//case C: year+month given - display a nice month calendar
95      $this->build_month_calendar();
96      $this->build_nav_bar(CYEAR); // years
97      $this->build_nav_bar(CMONTH); // month
98      return true;
99    }
100  }
101
102  if ($view_type==CAL_VIEW_LIST or count($this->date_components)==3)
103  {
104    if ( count($this->date_components)>=0 )
105    {
106      $this->build_nav_bar(CYEAR); // years
107    }
108    if ( count($this->date_components)>=1)
109    {
110      $this->build_nav_bar(CMONTH); // month
111    }
112    if ( count($this->date_components)>=2 )
113    {
114      $this->build_nav_bar(
115          CDAY,
116          range( 1, $this->get_all_days_in_month(
117              $this->date_components[CYEAR] ,$this->date_components[CMONTH] )
118              )
119        ); // days
120    }
121  }
122  return false;
123}
124
125
126/**
127 * Returns a sql where subquery for the date field
128 * @param int max_levels return the where up to this level
129 * (e.g. 2=only year and month)
130 * @return string
131 */
132function get_date_where($max_levels=3)
133{
134  $date = $this->date_components;
135  while (count($date)>$max_levels)
136  {
137    array_pop($date);
138  }
139  $res = '';
140  if (isset($date[CYEAR]) and $date[CYEAR]!='any')
141  {
142    $b = $date[CYEAR] . '-';
143    $e = $date[CYEAR] . '-';
144    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
145    {
146      $b .= $date[CMONTH] . '-';
147      $e .= $date[CMONTH] . '-';
148      if (isset($date[CDAY]) and $date[CDAY]!='any')
149      {
150        $b .= $date[CDAY];
151        $e .= $date[CDAY];
152      }
153      else
154      {
155        $b .= '01';
156        $e .= '31';
157      }
158    }
159    else
160    {
161      $b .= '01-01';
162      $e .= '12-31';
163      if (isset($date[CMONTH]) and $date[CMONTH]!='any')
164      {
165        $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
166      }
167      if (isset($date[CDAY]) and $date[CDAY]!='any')
168      {
169        $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
170      }
171    }
172    $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res;
173  }
174  else
175  {
176    $res = ' AND '.$this->date_field.' IS NOT NULL';
177    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
178    {
179      $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
180    }
181    if (isset($date[CDAY]) and $date[CDAY]!='any')
182    {
183      $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
184    }
185  }
186  return $res;
187}
188
189
190
191//--------------------------------------------------------- private members ---
192
193// returns an array with alll the days in a given month
194function get_all_days_in_month($year, $month)
195{
196  $md= array(1=>31,28,31,30,31,30,31,31,30,31,30,31);
197
198  if ( is_numeric($year) and $month==2)
199  {
200    $nb_days = $md[2];
201    if ( ($year%4==0)  and ( ($year%100!=0) or ($year%400!=0) ) )
202    {
203      $nb_days++;
204    }
205  }
206  elseif ( is_numeric($month) )
207  {
208    $nb_days = $md[ $month ];
209  }
210  else
211  {
212    $nb_days = 31;
213  }
214  return $nb_days;
215}
216
217function build_global_calendar()
218{
219  assert( count($this->date_components) == 0 );
220  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%Y%m")) as period,
221            COUNT( DISTINCT(id) ) as count';
222  $query.= $this->inner_sql;
223  $query.= $this->get_date_where();
224  $query.= '
225  GROUP BY period';
226
227  $result = pwg_query($query);
228  $items=array();
229  while ($row = mysql_fetch_array($result))
230  {
231    $y = substr($row['period'], 0, 4);
232    $m = (int)substr($row['period'], 4, 2);
233    if ( ! isset($items[$y]) )
234    {
235      $items[$y] = array('nb_images'=>0, 'children'=>array() );
236    }
237    $items[$y]['children'][$m] = $row['count'];
238    $items[$y]['nb_images'] += $row['count'];
239  }
240  //echo ('<pre>'. var_export($items, true) . '</pre>');
241  if (count($items)==1)
242  {// only one year exists so bail out to year view
243    list($y) = array_keys($items);
244    $this->date_components[CYEAR] = $y;
245    return false;
246  }
247
248  global $lang, $template;
249  foreach ( $items as $year=>$year_data)
250  {
251    $url_base = $this->url_base.$year;
252
253    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">'.$year.'</a>';
254    $nav_bar .= ' ('.$year_data['nb_images'].')';
255    $nav_bar .= '</span><br>';
256
257    $url_base .= '-';
258    $nav_bar .= $this->get_nav_bar_from_items( $url_base,
259            $year_data['children'], null, 'calCal', false, false, $lang['month'] );
260
261    $template->assign_block_vars( 'calendar.calbar',
262         array( 'BAR' => $nav_bar)
263         );
264  }
265  return true;
266}
267
268function build_year_calendar()
269{
270  assert( count($this->date_components) == 1 );
271  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%m%d")) as period,
272            COUNT( DISTINCT(id) ) as count';
273  $query.= $this->inner_sql;
274  $query.= $this->get_date_where();
275  $query.= '
276  GROUP BY period';
277
278  $result = pwg_query($query);
279  $items=array();
280  while ($row = mysql_fetch_array($result))
281  {
282    $m = (int)substr($row['period'], 0, 2);
283    $d = substr($row['period'], 2, 2);
284    if ( ! isset($items[$m]) )
285    {
286      $items[$m] = array('nb_images'=>0, 'children'=>array() );
287    }
288    $items[$m]['children'][$d] = $row['count'];
289    $items[$m]['nb_images'] += $row['count'];
290  }
291  //echo ('<pre>'. var_export($items, true) . '</pre>');
292  if (count($items)==1)
293  { // only one month exists so bail out to month view
294    list($m) = array_keys($items);
295    $this->date_components[CMONTH] = $m;
296    if (count($items[$m]['children'])==1)
297    { // or even to day view if everything occured in one day
298      list($d) = array_keys($items[$m]['children']);
299      $this->date_components[CDAY] = $d;
300    }
301    return false;
302  }
303  global $lang, $template;
304  foreach ( $items as $month=>$month_data)
305  {
306    $url_base = $this->url_base.$this->date_components[CYEAR].'-'.$month;
307
308    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">';
309    $nav_bar .= $lang['month'][$month].'</a>';
310    $nav_bar .= ' ('.$month_data['nb_images'].')';
311    $nav_bar .= '</span><br>';
312
313    $url_base .= '-';
314    $nav_bar .= $this->get_nav_bar_from_items( $url_base,
315                     $month_data['children'], null, 'calCal', false );
316
317    $template->assign_block_vars( 'calendar.calbar',
318         array( 'BAR' => $nav_bar)
319         );
320  }
321  return true;
322
323}
324
325function build_month_calendar()
326{
327  $query='SELECT DISTINCT(DAYOFMONTH('.$this->date_field.')) as period,
328            COUNT( DISTINCT(id) ) as count';
329  $query.= $this->inner_sql;
330  $query.= $this->get_date_where($this->date_components);
331  $query.= '
332  GROUP BY period';
333
334  $result = pwg_query($query);
335  while ($row = mysql_fetch_array($result))
336  {
337    $d = (int)$row['period'];
338    $items[$d] = array('nb_images'=>$row['count']);
339  }
340
341  foreach ( $items as $day=>$data)
342  {
343    $this->date_components[CDAY]=$day;
344    $query = '
345SELECT file,tn_ext,path, width, height, DAYOFWEEK('.$this->date_field.')-1 as dow';
346    $query.= $this->inner_sql;
347    $query.= $this->get_date_where();
348    $query.= '
349  ORDER BY RAND()
350  LIMIT 0,1';
351    unset ( $this->date_components[CDAY] );
352
353    $row = mysql_fetch_array(pwg_query($query));
354    $items[$day]['tn_path'] = get_thumbnail_src($row['path'], @$row['tn_ext']);
355    $items[$day]['tn_file'] = $row['file'];
356    $items[$day]['width'] = $row['width'];
357    $items[$day]['height'] = $row['height'];
358    $items[$day]['dow'] = $row['dow'];
359  }
360
361  global $lang, $template, $conf;
362
363  if ( !empty($items)
364      and $conf['calendar_month_cell_width']>0
365      and $conf['calendar_month_cell_height']>0)
366  {
367    list($known_day) = array_keys($items);
368    $known_dow = $items[$known_day]['dow'];
369    $first_day_dow = ($known_dow-($known_day-1))%7;
370    if ($first_day_dow<0)
371    {
372      $first_day_dow += 7;
373    }
374    //first_day_dow = week day corresponding to the first day of this month
375    $wday_labels = $lang['day'];
376
377    // BEGIN - pass now in week starting Monday
378    if ($first_day_dow==0)
379    {
380      $first_day_dow = 6;
381    }
382    else
383    {
384      $first_day_dow -= 1;
385    }
386    array_push( $wday_labels, array_shift($wday_labels) );
387    // END - pass now in week starting Monday
388
389    $cell_width = $conf['calendar_month_cell_width'];
390    $cell_height = $conf['calendar_month_cell_height'];
391
392    $template->set_filenames(
393      array(
394        'month_calendar'=>'month_calendar.tpl',
395        )
396      );
397
398    $template->assign_block_vars('calendar.thumbnails',
399        array(
400           'WIDTH'=>$cell_width,
401           'HEIGHT'=>$cell_height,
402          )
403      );
404
405    //fill the heading with day names
406    $template->assign_block_vars('calendar.thumbnails.head', array());
407    foreach( $wday_labels as $d => $label)
408    {
409      $template->assign_block_vars('calendar.thumbnails.head.col',
410                    array('LABEL'=>$label)
411                  );
412    }
413
414    $template->assign_block_vars('calendar.thumbnails.row', array());
415
416    //fill the empty days in the week before first day of this month
417    for ($i=0; $i<$first_day_dow; $i++)
418    {
419      $template->assign_block_vars('calendar.thumbnails.row.col', array());
420      $template->assign_block_vars('calendar.thumbnails.row.col.blank', array());
421    }
422    for ($day=1; $day<=$this->get_all_days_in_month(
423              $this->date_components[CYEAR] ,$this->date_components[CMONTH]); $day++)
424    {
425      $dow = ($first_day_dow + $day-1)%7;
426      if ($dow==0)
427      {
428        $template->assign_block_vars('calendar.thumbnails.row', array());
429      }
430      $template->assign_block_vars('calendar.thumbnails.row.col', array());
431      if ( !isset($items[$day]) )
432      {
433        $template->assign_block_vars('calendar.thumbnails.row.col.empty',
434              array('LABEL'=>$day));
435      }
436      else
437      {
438        // first try to guess thumbnail size
439        if ( !empty($items[$day]['width']) )
440        {
441          $tn_size = get_picture_size(
442                 $items[$day]['width'], $items[$day]['height'],
443                 $conf['tn_width'], $conf['tn_height'] );
444        }
445        else
446        {// item not an image (tn is either mime type or an image)
447          $tn_size = @getimagesize($items[$day]['tn_path']);
448        }
449        $tn_width = $tn_size[0];
450        $tn_height = $tn_size[1];
451
452        // now need to fit the thumbnail of size tn_size within
453        // a cell of size cell_size by playing with CSS position (left/top)
454        // and the width and height of <img>.
455        $ratio_w = $tn_width/$cell_width;
456        $ratio_h = $tn_height/$cell_height;
457
458        $pos_top=$pos_left=0;
459        $img_width=$img_height='';
460        if ( $ratio_w>1 and $ratio_h>1)
461        {// cell completely smaller than the thumbnail so we will let the browser
462         // resize the thumbnail
463          if ($ratio_w > $ratio_h )
464          {// thumbnail ratio compared to cell -> wide format
465            $img_height = 'height="'.$cell_height.'"';
466            $browser_img_width = $cell_height*$tn_width/$tn_height;
467            $pos_left = ($tn_width-$browser_img_width)/2;
468          }
469          else
470          {
471            $img_width = 'width="'.$cell_width.'"';
472            $browser_img_height = $cell_width*$tn_height/$tn_width;
473            $pos_top = ($tn_height-$browser_img_height)/2;
474          }
475        }
476        else
477        {
478          $pos_left = ($tn_width-$cell_width)/2;
479          $pos_top = ($tn_height-$cell_height)/2;
480        }
481
482        $css_style = '';
483        if ( round($pos_left)!=0)
484        {
485          $css_style.='left:'.round(-$pos_left).'px;';
486        }
487        if ( round($pos_top)!=0)
488        {
489          $css_style.='top:'.round(-$pos_top).'px;';
490        }
491        $url = $this->url_base.
492              $this->date_components[CYEAR].'-'.
493              $this->date_components[CMONTH].'-'.$day;
494        $alt = $wday_labels[$dow] . ' ' . $day.
495               ' ('.$items[$day]['nb_images'].')';
496        $template->assign_block_vars('calendar.thumbnails.row.col.full',
497              array(
498                'LABEL'     => $day,
499                'IMAGE'     => $items[$day]['tn_path'],
500                'U_IMG_LINK'=> $url,
501                'STYLE'     => $css_style,
502                'IMG_WIDTH' => $img_width,
503                'IMG_HEIGHT'=> $img_height,
504                'IMAGE_ALT' => $alt,
505              )
506            );
507      }
508    }
509    //fill the empty days in the week after the last day of this month
510    while ( $dow<6 )
511    {
512      $template->assign_block_vars('calendar.thumbnails.row.col', array());
513      $template->assign_block_vars('calendar.thumbnails.row.col.blank', array());
514      $dow++;
515    }
516    $template->assign_var_from_handle('MONTH_CALENDAR', 'month_calendar');
517  }
518  else
519  {
520    $template->assign_block_vars('thumbnails', array());
521    $template->assign_block_vars('thumbnails.line', array());
522    foreach ( $items as $day=>$data)
523    {
524      $url = $this->url_base.
525            $this->date_components[CYEAR].'-'.
526            $this->date_components[CMONTH].'-'.$day;
527
528      $thumbnail_title = $lang['day'][$data['dow']] . ' ' . $day;
529      $name = $thumbnail_title .' ('.$data['nb_images'].')';
530
531      $template->assign_block_vars(
532          'thumbnails.line.thumbnail',
533          array(
534            'IMAGE'=>$data['tn_path'],
535            'IMAGE_ALT'=>$data['tn_file'],
536            'IMAGE_TITLE'=>$thumbnail_title,
537            'U_IMG_LINK'=>$url
538           )
539          );
540      $template->assign_block_vars(
541          'thumbnails.line.thumbnail.category_name',
542          array(
543            'NAME' => $name
544            )
545          );
546    }
547  }
548
549  return true;
550}
551
552}
553?>
Note: See TracBrowser for help on using the repository browser.