source: branches/branch-1_5/include/category_calendar.inc.php @ 3214

Last change on this file since 3214 was 1054, checked in by plg, 18 years ago

bug 278 fixed: calendar on date_available could not work because
date_available has a datetime MySQL column format while date_creation has a
date MySQL column format. We use DATE_FORMAT MySQL function to resolv this
issue.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-02-23 21:38:00 +0000 (Thu, 23 Feb 2006) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1054 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * This file is included by category.php to show thumbnails for the category
30 * calendar
31 *
32 */
33
34// years of image availability
35$query = '
36SELECT YEAR('.$conf['calendar_datefield'].') AS year, COUNT(id) AS count
37  FROM '.IMAGES_TABLE.', '.IMAGE_CATEGORY_TABLE.'
38  '.$page['where'].'
39    AND id = image_id
40  GROUP BY year
41;';
42$result = pwg_query($query);
43$calendar_years = array();
44while ($row = mysql_fetch_array($result))
45{
46  $calendar_years[$row['year']] = $row['count'];
47}
48
49// if the year requested is not among the available years, we unset the
50// variable
51if (isset($page['calendar_year'])
52    and !isset($calendar_years[$page['calendar_year']]))
53{
54  unset($page['calendar_year']);
55}
56
57// years navigation bar creation
58$years_nav_bar = '';
59foreach ($calendar_years as $calendar_year => $nb_picture_year)
60{
61  if (isset($page['calendar_year'])
62      and $calendar_year == $page['calendar_year'])
63  {
64    $years_nav_bar.= ' <span class="dateSelected">'.$calendar_year.'</span>';
65  }
66  else
67  {
68    $url = PHPWG_ROOT_PATH.'category.php?cat=calendar';
69    $url.= '&amp;year='.$calendar_year;
70    $url = add_session_id($url);
71    $years_nav_bar.= ' <a href="'.$url.'">'.$calendar_year.'</a>';
72  }
73}
74
75$template->assign_block_vars(
76  'calendar',
77  array('YEARS_NAV_BAR' => $years_nav_bar)
78  );
79
80// months are calculated (to know which months are available, and how many
81// pictures per month we can find) only if a year is requested.
82if (isset($page['calendar_year']))
83{
84  // creation of hash associating the number of the month in the year with
85  // the number of picture for this month : $calendar_months
86  $query = '
87SELECT DISTINCT(MONTH('.$conf['calendar_datefield'].')) AS month
88     , COUNT(id) AS count
89  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
90  '.$page['where'].'
91    AND '.$conf['calendar_datefield'].'
92      BETWEEN \''.$page['calendar_year'].'-1-1\'
93      AND \''.$page['calendar_year'].'-12-31\'
94  GROUP BY MONTH('.$conf['calendar_datefield'].')
95;';
96  $result = pwg_query($query);
97  $calendar_months = array();
98  while ($row = mysql_fetch_array($result))
99  {
100    $calendar_months[$row['month']] = $row['count'];
101  }
102
103  // if a month is requested and is not among the available months, we unset
104  // the requested month
105  if (isset($page['calendar_month'])
106      and !isset($calendar_months[$page['calendar_month']]))
107  {
108    unset($page['calendar_month']);
109  }
110
111  // months navigation bar creation
112  $months_nav_bar = '';
113  foreach ($calendar_months as $calendar_month => $nb_picture_month)
114  {
115    if (isset($page['calendar_month'])
116        and $calendar_month == $page['calendar_month'])
117    {
118      $months_nav_bar.= ' <span class="dateSelected">';
119      $months_nav_bar.= $lang['month'][(int)$calendar_month];
120      $months_nav_bar.= '</span>';
121    }
122    else
123    {
124      $url = PHPWG_ROOT_PATH.'category.php?cat=calendar&amp;month=';
125      $url.= $page['calendar_year'].'.'.sprintf('%02s', $calendar_month);
126      $months_nav_bar.= ' ';
127      $months_nav_bar.= '<a href="'.add_session_id($url).'">';
128      $months_nav_bar.= $lang['month'][(int)$calendar_month];
129      $months_nav_bar.= '</a>';
130    }
131  }
132  $template->assign_block_vars(
133    'calendar',
134    array('MONTHS_NAV_BAR' => $months_nav_bar));
135}
136
137/**
138 * 4 sub-cases are possibles for the calendar category :
139 *
140 *  1. show years if no year is requested
141 *  2. show months of the requested year if no month is requested
142 *  3. show days of the {year,month} requested if no day requested
143 *  4. show categories of the requested day (+ a special category gathering
144 *     all categories)
145 */
146
147if (!isset($page['calendar_year']))
148{
149  $nb_pics = count($calendar_years);
150}
151elseif (!isset($page['calendar_month']))
152{
153  $nb_pics = count($calendar_months);
154}
155elseif (!isset($page['calendar_day']))
156{
157  // creation of hash associating the number of the day in the month with
158  // the number of picture for this day : $calendar_days
159  $query = '
160SELECT
161    DISTINCT DATE_FORMAT('.$conf['calendar_datefield'].', \'%Y-%m-%d\') AS day
162  , COUNT(id) AS count
163  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
164  '.$page['where'].'
165    AND '.$conf['calendar_datefield'].'
166      BETWEEN \''.$page['calendar_year'].'-'.$page['calendar_month'].'-1\'
167      AND \''.$page['calendar_year'].'-'.$page['calendar_month'].'-31\'
168  GROUP BY day
169;';
170  $result = pwg_query($query);
171  $calendar_days = array();
172  while ($row = mysql_fetch_array($result))
173  {
174    $calendar_days[$row['day']] = $row['count'];
175  }
176  $nb_pics = count($calendar_days);
177}
178elseif (isset($page['calendar_day']))
179{
180  // $page['calendar_date'] is the concatenation of year-month-day. simplier
181  // to use in SQ queries
182  $page['calendar_date'] = $page['calendar_year'];
183  $page['calendar_date'].= '-'.$page['calendar_month'];
184  $page['calendar_date'].= '-'.$page['calendar_day'];
185 
186  $query = '
187SELECT category_id AS category, COUNT(id) AS count
188  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
189  '.$page['where'].'
190    AND DATE_FORMAT('.$conf['calendar_datefield'].', \'%Y-%c-%e\')'
191    .' = \''.$page['calendar_date'].'\'
192  GROUP BY category_id
193;';
194  $result = pwg_query($query);
195  $calendar_categories = array();
196  // special category 0 : gathering all available categories (0 cannot be a
197  // oregular category identifier)
198  $calendar_categories[0] = 0;
199  while ($row = mysql_fetch_array($result))
200  {
201    $calendar_categories[$row['category']] = $row['count'];
202  }
203  // update the total number of pictures for this day
204  $calendar_categories[0] = array_sum($calendar_categories);
205 
206  $nb_pics = count($calendar_categories);
207}
208
209// template thumbnail initialization
210if ($nb_pics > 0)
211{
212  $template->assign_block_vars('thumbnails', array());
213  // first line
214  $template->assign_block_vars('thumbnails.line', array());
215  // current row displayed
216  $row_number = 0;
217}
218
219if (!isset($page['calendar_year']))
220{
221  // for each month of this year, display a random picture
222  foreach ($calendar_years as $calendar_year => $nb_pics)
223  {
224    $query = '
225SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
226  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
227  '.$page['where'].'
228    AND '.$conf['calendar_datefield'].'
229      BETWEEN \''.$calendar_year.'-1-1\'
230      AND \''.$calendar_year.'-12-31\'
231  ORDER BY RAND()
232  LIMIT 0,1
233;';
234    $row = mysql_fetch_array(pwg_query($query));
235   
236    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
237   
238    $name = $calendar_year.' ('.$nb_pics.')';
239
240    $thumbnail_title = $lang['calendar_picture_hint'].$name;
241     
242    $url_link = PHPWG_ROOT_PATH.'category.php?cat=calendar';
243    $url_link.= '&amp;year='.$calendar_year;
244   
245    $template->assign_block_vars(
246      'thumbnails.line.thumbnail',
247      array(
248        'IMAGE'=>$thumbnail_src,
249        'IMAGE_ALT'=>$row['file'],
250        'IMAGE_TITLE'=>$thumbnail_title,
251         
252        'U_IMG_LINK'=>add_session_id($url_link)
253       )
254     );
255
256    $template->assign_block_vars(
257      'thumbnails.line.thumbnail.category_name',
258      array(
259        'NAME' => $name
260        )
261      );
262
263    // create a new line ?
264    if (++$row_number == $user['nb_image_line'])
265    {
266      $template->assign_block_vars('thumbnails.line', array());
267      $row_number = 0;
268    }
269  }
270}
271elseif (!isset($page['calendar_month']))
272{
273  // for each month of this year, display a random picture
274  foreach ($calendar_months as $calendar_month => $nb_pics)
275  {
276    $query = '
277SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
278  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
279  '.$page['where'].'
280    AND '.$conf['calendar_datefield'].'
281      BETWEEN \''.$page['calendar_year'].'-'.$calendar_month.'-1\'
282      AND \''.$page['calendar_year'].'-'.$calendar_month.'-31\'
283  ORDER BY RAND()
284  LIMIT 0,1
285;';
286    $row = mysql_fetch_array(pwg_query($query));
287   
288    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
289   
290    $name = $lang['month'][$calendar_month];
291    $name.= ' '.$page['calendar_year'];
292    $name.= ' ('.$nb_pics.')';
293
294    $thumbnail_title = $lang['calendar_picture_hint'].$name;
295     
296    $url_link = PHPWG_ROOT_PATH.'category.php?cat=calendar';
297    $url_link.= '&amp;month='.$page['calendar_year'].'.';
298    if ($calendar_month < 10)
299    {
300      // adding leading zero
301      $url_link.= '0';
302    }
303    $url_link.= $calendar_month;
304   
305    $template->assign_block_vars(
306      'thumbnails.line.thumbnail',
307      array(
308        'IMAGE'=>$thumbnail_src,
309        'IMAGE_ALT'=>$row['file'],
310        'IMAGE_TITLE'=>$thumbnail_title,
311         
312        'U_IMG_LINK'=>add_session_id($url_link)
313       )
314     );
315
316    $template->assign_block_vars(
317      'thumbnails.line.thumbnail.category_name',
318      array(
319        'NAME' => $name
320        )
321      );
322
323    // create a new line ?
324    if (++$row_number == $user['nb_image_line'])
325    {
326      $template->assign_block_vars('thumbnails.line', array());
327      $row_number = 0;
328    }
329  }
330}
331elseif (!isset($page['calendar_day']))
332{
333  // for each day of the requested month, display a random picture
334  foreach ($calendar_days as $calendar_day => $nb_pics)
335  {
336    $query = '
337SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
338       , DAYOFWEEK(\''.$calendar_day.'\') AS dow
339  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
340  '.$page['where'].'
341    AND DATE_FORMAT('.$conf['calendar_datefield'].', \'%Y-%m-%d\')'
342      .' = \''.$calendar_day.'\'
343  ORDER BY RAND()
344  LIMIT 0,1
345;';
346    $row = mysql_fetch_array(pwg_query($query));
347   
348    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
349
350    list($year,$month,$day) = explode('-', $calendar_day);
351    $name = $lang['day'][$row['dow']-1];
352    $name.= ' '.$day;
353    $name.= ' ('.$nb_pics.')';
354     
355    $thumbnail_title = $lang['calendar_picture_hint'].$name;
356
357    $url_link = PHPWG_ROOT_PATH.'category.php';
358    $url_link.= '?cat=calendar&amp;day='.str_replace('-', '.', $calendar_day);
359   
360    $template->assign_block_vars(
361      'thumbnails.line.thumbnail',
362      array(
363        'IMAGE'=>$thumbnail_src,
364        'IMAGE_ALT'=>$row['file'],
365        'IMAGE_TITLE'=>$thumbnail_title,
366         
367        'U_IMG_LINK'=>add_session_id($url_link)
368         )
369       );
370
371    $template->assign_block_vars(
372      'thumbnails.line.thumbnail.category_name',
373      array(
374        'NAME' => $name
375        )
376      );
377
378    // create a new line ?
379    if (++$row_number == $user['nb_image_line'])
380    {
381      $template->assign_block_vars('thumbnails.line', array());
382      $row_number = 0;
383    }
384  }
385}
386elseif (isset($page['calendar_day']))
387{
388  $old_level_separator = $conf['level_separator'];
389  $conf['level_separator'] = '<br />';
390  // for each category of this day, display a random picture
391  foreach ($calendar_categories as $calendar_category => $nb_pics)
392  {
393    if ($calendar_category == 0)
394    {
395      $name = '['.$lang['all_categories'].']';
396    }
397    else
398    {
399      $cat_infos = get_cat_info( $calendar_category );
400     
401      $name = get_cat_display_name($cat_infos['name'],'',false);
402      $name = '['.$name.']';
403    }
404    $name.= ' ('.$nb_pics.')';
405   
406    $query = '
407SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
408  FROM '.IMAGES_TABLE.', '.IMAGE_CATEGORY_TABLE.'
409  '.$page['where'].'
410    AND DATE_FORMAT('.$conf['calendar_datefield'].', \'%Y-%c-%e\')'
411      .' = \''.$page['calendar_date'].'\'';
412    if ($calendar_category != 0)
413    {
414      $query.= '
415    AND category_id = '.$calendar_category;
416    }
417    $query.= '
418    AND id = image_id
419  ORDER BY RAND()
420  LIMIT 0,1
421;';
422    $row = mysql_fetch_array(pwg_query($query));
423   
424    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
425   
426    $thumbnail_title = $lang['calendar_picture_hint'].$name;
427
428    $url_link = PHPWG_ROOT_PATH.'category.php?cat=search';
429    $url_link.= '&amp;search='.$conf['calendar_datefield'].':'.$_GET['day'];
430    if ($calendar_category != 0)
431    {
432      $url_link.= '--cat:'.$calendar_category.'|AND';
433    }
434   
435    $template->assign_block_vars(
436      'thumbnails.line.thumbnail',
437      array(
438        'IMAGE'=>$thumbnail_src,
439        'IMAGE_ALT'=>$row['file'],
440        'IMAGE_TITLE'=>$thumbnail_title,
441         
442        'U_IMG_LINK'=>add_session_id($url_link)
443         )
444       );
445
446    $template->assign_block_vars(
447      'thumbnails.line.thumbnail.category_name',
448      array(
449        'NAME' => $name
450        )
451      );
452   
453    // create a new line ?
454    if (++$row_number == $user['nb_image_line'])
455    {
456      $template->assign_block_vars('thumbnails.line', array());
457      $row_number = 0;
458    }
459  }
460  $conf['level_separator'] = $old_level_separator;
461}
462?>
Note: See TracBrowser for help on using the repository browser.