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

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

new calendar completely integrated

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