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

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

calendar: added posted/created chronology

calendar: added a where are we bar (like: created/2005/august)

calendar: possibility to hide the All/Any buttons ($confcalendar_show_any)

calendar: possibility to display a single navigation bar instead of
several navigation bars ($confcalendar_multi_bar)

calendar: tried to simplify code and improve readability
(still requires a review)

  • 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 * Generate navigation bars for category page
41 * @return boolean false to indicate that thumbnails
42 * where not included here, true otherwise
43 */
44function generate_category_content($url_base, $view_type)
45{
46  global $lang, $conf;
47
48  $this->url_base = $url_base;
49
50  if ($view_type==CAL_VIEW_CALENDAR)
51  {
52    if ( count($this->date_components)==0 )
53    {//case A: no year given - display all years+months
54      if ($this->build_global_calendar())
55        return true;
56    }
57
58    if ( count($this->date_components)==1 )
59    {//case B: year given - display all days in given year
60      if ($this->build_year_calendar())
61      {
62        if ( $conf['calendar_multi_bar'] )
63          $this->build_nav_bar2(CYEAR, 'YEAR'); // years
64        return true;
65      }
66    }
67
68    if ( count($this->date_components)==2 )
69    {//case C: year+month given - display a nice month calendar
70      $this->build_month_calendar();
71      if ( $conf['calendar_multi_bar'] )
72      {
73        $this->build_nav_bar2(CYEAR, 'YEAR'); // years
74      }
75      if (count($this->date_components)>=1 and
76          ( $conf['calendar_multi_bar'] or count($this->date_components)==1 ) )
77      {
78        $this->build_nav_bar2(CMONTH, 'MONTH', $lang['month']); // month
79      }
80      return true;
81    }
82  }
83
84  if ($view_type==CAL_VIEW_LIST or count($this->date_components)==3)
85  {
86    if ( $conf['calendar_multi_bar'] or count($this->date_components)==0 )
87    {
88      $this->build_nav_bar2(CYEAR, 'YEAR'); // years
89    }
90    if ( count($this->date_components)>=1 and
91        ( $conf['calendar_multi_bar'] or count($this->date_components)==1 )
92       )
93    {
94      $this->build_nav_bar2(CMONTH, 'MONTH', $lang['month']); // month
95    }
96    if ( count($this->date_components)>=2 )
97    {
98      $this->build_nav_bar2(CDAY, 'DAYOFMONTH' ); // days
99    }
100  }
101  return false;
102}
103
104
105/**
106 * Returns a sql where subquery for the date field
107 * @param int max_levels return the where up to this level
108 * (e.g. 2=only year and month)
109 * @return string
110 */
111function get_date_where($max_levels=3)
112{
113  $date = $this->date_components;
114  while (count($date)>$max_levels)
115  {
116    array_pop($date);
117  }
118  $res = '';
119  if (isset($date[CYEAR]) and $date[CYEAR]!='any')
120  {
121    $b = $date[CYEAR] . '-';
122    $e = $date[CYEAR] . '-';
123    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
124    {
125      $b .= $date[CMONTH] . '-';
126      $e .= $date[CMONTH] . '-';
127      if (isset($date[CDAY]) and $date[CDAY]!='any')
128      {
129        $b .= $date[CDAY];
130        $e .= $date[CDAY];
131      }
132      else
133      {
134        $b .= '01';
135        $e .= '31';
136      }
137    }
138    else
139    {
140      $b .= '01-01';
141      $e .= '12-31';
142      if (isset($date[CMONTH]) and $date[CMONTH]!='any')
143      {
144        $res .= ' AND MONTH('.$this->date_field.')='.$date[CMONTH];
145      }
146      if (isset($date[2]) and $date[2]!='any')
147      {
148        $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$date[CDAY];
149      }
150    }
151    $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res;
152  }
153  else
154  {
155    $res = ' AND '.$this->date_field.' IS NOT NULL';
156    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
157    {
158      $res .= ' AND MONTH('.$this->date_field.')='.$date[CMONTH];
159    }
160    if (isset($date[CDAY]) and $date[CDAY]!='any')
161    {
162      $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$date[CDAY];
163    }
164  }
165  return $res;
166}
167
168
169function get_display_name()
170{
171  global $conf, $lang;
172  $res = '';
173  $url = $this->url_base;
174  if ( isset($this->date_components[CYEAR]) )
175  {
176    $res .= $conf['level_separator'];
177    $url .= $this->date_components[CYEAR].'-';
178    $res .= 
179      '<a href="'.$url.'">'
180      .$this->get_date_component_label($this->date_components[CYEAR])
181      .'</a>';
182  }
183  if ( isset($this->date_components[CMONTH]) )
184  {
185    $res .= $conf['level_separator'];
186    $url .= $this->date_components[CMONTH].'-';
187    $res .= 
188      '<a href="'.$url.'">'
189      .$this->get_date_component_label(
190                     $this->date_components[CMONTH], 
191                     $lang['month']
192                    )
193      .'</a>';
194  }
195  if ( isset($this->date_components[CDAY]) )
196  {
197    $res .= $conf['level_separator'];
198    $url .= $this->date_components[CDAY].'-';
199    $res .= 
200      '<a href="'.$url.'">'
201      .$this->get_date_component_label($this->date_components[CDAY])
202      .'</a>';
203  }
204
205  return $res;
206}
207
208
209//--------------------------------------------------------- private members ---
210function build_nav_bar2($level, $sql_func, $labels=null)
211{
212  parent::build_nav_bar($level, $sql_func, '', $labels);
213}
214
215function build_global_calendar()
216{
217  assert( count($this->date_components) == 0 );
218  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%Y%m")) as period,
219            COUNT(id) as count';
220  $query.= $this->inner_sql;
221  $query.= $this->get_date_where();
222  $query.= '
223  GROUP BY period';
224
225  $result = pwg_query($query);
226  while ($row = mysql_fetch_array($result))
227  {
228    $y = substr($row['period'], 0, 4);
229    $m = (int)substr($row['period'], 4, 2);
230    if ( ! isset($items[$y]) )
231    {
232      $items[$y] = array('nb_images'=>0, 'children'=>array() );
233    }
234    $items[$y]['children'][$m] = $row['count'];
235    $items[$y]['nb_images'] += $row['count'];
236  }
237  //echo ('<pre>'. var_export($items, true) . '</pre>');
238  if (count($items)==1)
239  {// only one year exists so bail out to year view
240    list($y) = array_keys($items);
241    $this->date_components[CYEAR] = $y;
242    return false;
243  }
244
245  global $lang, $template;
246  foreach ( $items as $year=>$year_data)
247  {
248    $url_base = $this->url_base.$year;
249
250    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">'.$year.'</a>';
251    $nav_bar .= ' ('.$year_data['nb_images'].')';
252    $nav_bar .= '</span><br>';
253
254    $url_base .= '-';
255    $nav_bar .= $this->get_nav_bar_from_items( $url_base, 
256            $year_data['children'], null, 'calCal', false, $lang['month'] );
257
258    $template->assign_block_vars( 'calendar.calbar',
259         array( 'BAR' => $nav_bar)
260         );
261  }
262  return true;
263}
264
265function build_year_calendar()
266{
267  assert( count($this->date_components) == 1 );
268  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%m%d")) as period,
269            COUNT(id) as count';
270  $query.= $this->inner_sql;
271  $query.= $this->get_date_where();
272  $query.= '
273  GROUP BY period';
274
275  $result = pwg_query($query);
276  while ($row = mysql_fetch_array($result))
277  {
278    $m = (int)substr($row['period'], 0, 2);
279    $d = substr($row['period'], 2, 2);
280    if ( ! isset($items[$m]) )
281    {
282      $items[$m] = array('nb_images'=>0, 'children'=>array() );
283    }
284    $items[$m]['children'][$d] = $row['count'];
285    $items[$m]['nb_images'] += $row['count'];
286  }
287  //echo ('<pre>'. var_export($items, true) . '</pre>');
288  if (count($items)==1)
289  { // only one month exists so bail out to month view
290    list($m) = array_keys($items);
291    $this->date_components[CMONTH] = $m;
292    if (count($items[$m]['children'])==1)
293    { // or even to day view if everything occured in one day
294      list($d) = array_keys($items[$m]['children']);
295      $this->date_components[CDAY] = $d;
296    }
297    return false;
298  }
299  global $lang, $template;
300  foreach ( $items as $month=>$month_data)
301  {
302    $url_base = $this->url_base.$this->date_components[CYEAR].'-'.$month;
303
304    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">';
305    $nav_bar .= $lang['month'][$month].'</a>';
306    $nav_bar .= ' ('.$month_data['nb_images'].')';
307    $nav_bar .= '</span><br>';
308
309    $url_base .= '-';
310    $nav_bar .= $this->get_nav_bar_from_items( $url_base, 
311                     $month_data['children'], null, 'calCal', false );
312
313    $template->assign_block_vars( 'calendar.calbar',
314         array( 'BAR' => $nav_bar)
315         );
316  }
317  return true;
318
319}
320
321function build_month_calendar()
322{
323  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%d")) as period,
324            COUNT(id) as count';
325  $query.= $this->inner_sql;
326  $query.= $this->get_date_where($this->date_components);
327  $query.= '
328  GROUP BY period';
329
330  $result = pwg_query($query);
331  while ($row = mysql_fetch_array($result))
332  {
333    $d = $row['period'];
334    $items[$d] = $row['count'];
335  }
336
337  global $lang, $template;
338
339  $template->assign_block_vars('thumbnails', array());
340  $template->assign_block_vars('thumbnails.line', array());
341  foreach ( $items as $day=>$nb_images)
342  {
343    $url_base = $this->url_base.
344          $this->date_components[CYEAR].'-'.
345          $this->date_components[CMONTH].'-'.$day;
346    $this->date_components[CDAY]=$day;
347    $query = '
348SELECT file,tn_ext,path, DAYOFWEEK('.$this->date_field.')-1 as dw';
349    $query.= $this->inner_sql;
350    $query.= $this->get_date_where();
351    $query.= '
352  ORDER BY RAND()
353  LIMIT 0,1';
354    unset ( $this->date_components[CDAY] );
355
356    $row = mysql_fetch_array(pwg_query($query));
357
358    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
359    $thumbnail_title = $lang['day'][$row['dw']] . ' ' . $day;
360    $name = $thumbnail_title .' ('.$nb_images.')';
361
362    $template->assign_block_vars(
363        'thumbnails.line.thumbnail',
364        array(
365          'IMAGE'=>$thumbnail_src,
366          'IMAGE_ALT'=>$row['file'],
367          'IMAGE_TITLE'=>$thumbnail_title,
368          'U_IMG_LINK'=>$url_base
369         )
370        );
371    $template->assign_block_vars(
372        'thumbnails.line.thumbnail.category_name',
373        array(
374          'NAME' => $name
375          )
376        );
377  }
378  return true;
379}
380
381}
382?>
Note: See TracBrowser for help on using the repository browser.