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

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

calendar improvements: week on weekly list starts on Monday,
ability to show grayed months/weeks/days (without any picture in it),
added icons for created/posted fields
language uniformization

calendar fixes: correct number of pictures in calendar view,
code simplification (I hope so)

  • Property svn:eol-style set to native
File size: 11.1 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          $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 range(1, $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(DATE_FORMAT('.$this->date_field.',"%d")) as period,
328            COUNT(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 = $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, DAYOFWEEK('.$this->date_field.')-1 as dw';
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]['tn_dw'] = $row['dw'];
357  }
358
359  global $lang, $template;
360  $template->assign_block_vars('thumbnails', array());
361  $template->assign_block_vars('thumbnails.line', array());
362  foreach ( $items as $day=>$data)
363  {
364    $url_base = $this->url_base.
365          $this->date_components[CYEAR].'-'.
366          $this->date_components[CMONTH].'-'.$day;
367
368    $thumbnail_title = $lang['day'][$data['tn_dw']] . ' ' . $day;
369    $name = $thumbnail_title .' ('.$data['nb_images'].')';
370
371    $template->assign_block_vars(
372        'thumbnails.line.thumbnail',
373        array(
374          'IMAGE'=>$data['tn_path'],
375          'IMAGE_ALT'=>$data['tn_file'],
376          'IMAGE_TITLE'=>$thumbnail_title,
377          'U_IMG_LINK'=>$url_base
378         )
379        );
380    $template->assign_block_vars(
381        'thumbnails.line.thumbnail.category_name',
382        array(
383          'NAME' => $name
384          )
385        );
386  }
387
388  return true;
389}
390
391}
392?>
Note: See TracBrowser for help on using the repository browser.