source: trunk/include/category_calendar.inc.php @ 896

Last change on this file since 896 was 896, checked in by plg, 19 years ago
  • bug 154 fixed: "Calendar category, too precise dates". Bug came from the bigger precision of creation_date and available_date in images tables. Need to retrieve only the year-month-day information.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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: 2005-10-17 21:11:19 +0000 (Mon, 17 Oct 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 896 $
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 '.$conf['calendar_datefield'].' = \''.$page['calendar_date'].'\'
191  GROUP BY category_id
192;';
193  $result = pwg_query($query);
194  $calendar_categories = array();
195  // special category 0 : gathering all available categories (0 cannot be a
196  // oregular category identifier)
197  $calendar_categories[0] = 0;
198  while ($row = mysql_fetch_array($result))
199  {
200    $calendar_categories[$row['category']] = $row['count'];
201  }
202  // update the total number of pictures for this day
203  $calendar_categories[0] = array_sum($calendar_categories);
204 
205  $nb_pics = count($calendar_categories);
206}
207
208// template thumbnail initialization
209if ($nb_pics > 0)
210{
211  $template->assign_block_vars('thumbnails', array());
212  // first line
213  $template->assign_block_vars('thumbnails.line', array());
214  // current row displayed
215  $row_number = 0;
216}
217
218if (!isset($page['calendar_year']))
219{
220  // for each month of this year, display a random picture
221  foreach ($calendar_years as $calendar_year => $nb_pics)
222  {
223    $query = '
224SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
225  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
226  '.$page['where'].'
227    AND '.$conf['calendar_datefield'].'
228      BETWEEN \''.$calendar_year.'-1-1\'
229      AND \''.$calendar_year.'-12-31\'
230  ORDER BY RAND()
231  LIMIT 0,1
232;';
233    $row = mysql_fetch_array(pwg_query($query));
234   
235    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
236   
237    $name = $calendar_year.' ('.$nb_pics.')';
238
239    $thumbnail_title = $lang['calendar_picture_hint'].$name;
240     
241    $url_link = PHPWG_ROOT_PATH.'category.php?cat=calendar';
242    $url_link.= '&amp;year='.$calendar_year;
243   
244    $template->assign_block_vars(
245      'thumbnails.line.thumbnail',
246      array(
247        'IMAGE'=>$thumbnail_src,
248        'IMAGE_ALT'=>$row['file'],
249        'IMAGE_TITLE'=>$thumbnail_title,
250         
251        'U_IMG_LINK'=>add_session_id($url_link)
252       )
253     );
254
255    $template->assign_block_vars(
256      'thumbnails.line.thumbnail.category_name',
257      array(
258        'NAME' => $name
259        )
260      );
261
262    // create a new line ?
263    if (++$row_number == $user['nb_image_line'])
264    {
265      $template->assign_block_vars('thumbnails.line', array());
266      $row_number = 0;
267    }
268  }
269}
270elseif (!isset($page['calendar_month']))
271{
272  // for each month of this year, display a random picture
273  foreach ($calendar_months as $calendar_month => $nb_pics)
274  {
275    $query = '
276SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
277  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
278  '.$page['where'].'
279    AND '.$conf['calendar_datefield'].'
280      BETWEEN \''.$page['calendar_year'].'-'.$calendar_month.'-1\'
281      AND \''.$page['calendar_year'].'-'.$calendar_month.'-31\'
282  ORDER BY RAND()
283  LIMIT 0,1
284;';
285    $row = mysql_fetch_array(pwg_query($query));
286   
287    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
288   
289    $name = $lang['month'][$calendar_month];
290    $name.= ' '.$page['calendar_year'];
291    $name.= ' ('.$nb_pics.')';
292
293    $thumbnail_title = $lang['calendar_picture_hint'].$name;
294     
295    $url_link = PHPWG_ROOT_PATH.'category.php?cat=calendar';
296    $url_link.= '&amp;month='.$page['calendar_year'].'.';
297    if ($calendar_month < 10)
298    {
299      // adding leading zero
300      $url_link.= '0';
301    }
302    $url_link.= $calendar_month;
303   
304    $template->assign_block_vars(
305      'thumbnails.line.thumbnail',
306      array(
307        'IMAGE'=>$thumbnail_src,
308        'IMAGE_ALT'=>$row['file'],
309        'IMAGE_TITLE'=>$thumbnail_title,
310         
311        'U_IMG_LINK'=>add_session_id($url_link)
312       )
313     );
314
315    $template->assign_block_vars(
316      'thumbnails.line.thumbnail.category_name',
317      array(
318        'NAME' => $name
319        )
320      );
321
322    // create a new line ?
323    if (++$row_number == $user['nb_image_line'])
324    {
325      $template->assign_block_vars('thumbnails.line', array());
326      $row_number = 0;
327    }
328  }
329}
330elseif (!isset($page['calendar_day']))
331{
332  // for each day of the requested month, display a random picture
333  foreach ($calendar_days as $calendar_day => $nb_pics)
334  {
335    $query = '
336SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
337       , DAYOFWEEK(\''.$calendar_day.'\') AS dow
338  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
339  '.$page['where'].'
340    AND '.$conf['calendar_datefield'].' = \''.$calendar_day.'\'
341  ORDER BY RAND()
342  LIMIT 0,1
343;';
344    $row = mysql_fetch_array(pwg_query($query));
345   
346    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
347
348    list($year,$month,$day) = explode('-', $calendar_day);
349    $name = $lang['day'][$row['dow']-1];
350    $name.= ' '.$day;
351    $name.= ' ('.$nb_pics.')';
352     
353    $thumbnail_title = $lang['calendar_picture_hint'].$name;
354
355    $url_link = PHPWG_ROOT_PATH.'category.php';
356    $url_link.= '?cat=calendar&amp;day='.str_replace('-', '.', $calendar_day);
357   
358    $template->assign_block_vars(
359      'thumbnails.line.thumbnail',
360      array(
361        'IMAGE'=>$thumbnail_src,
362        'IMAGE_ALT'=>$row['file'],
363        'IMAGE_TITLE'=>$thumbnail_title,
364         
365        'U_IMG_LINK'=>add_session_id($url_link)
366         )
367       );
368
369    $template->assign_block_vars(
370      'thumbnails.line.thumbnail.category_name',
371      array(
372        'NAME' => $name
373        )
374      );
375
376    // create a new line ?
377    if (++$row_number == $user['nb_image_line'])
378    {
379      $template->assign_block_vars('thumbnails.line', array());
380      $row_number = 0;
381    }
382  }
383}
384elseif (isset($page['calendar_day']))
385{
386  $old_level_separator = $conf['level_separator'];
387  $conf['level_separator'] = '<br />';
388  // for each category of this day, display a random picture
389  foreach ($calendar_categories as $calendar_category => $nb_pics)
390  {
391    if ($calendar_category == 0)
392    {
393      $name = '['.$lang['all_categories'].']';
394    }
395    else
396    {
397      $cat_infos = get_cat_info( $calendar_category );
398     
399      $name = get_cat_display_name($cat_infos['name'],'',false);
400      $name = '['.$name.']';
401    }
402    $name.= ' ('.$nb_pics.')';
403   
404    $query = '
405SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
406  FROM '.IMAGES_TABLE.', '.IMAGE_CATEGORY_TABLE.'
407  '.$page['where'].'
408    AND '.$conf['calendar_datefield'].' = \''.$page['calendar_date'].'\'';
409    if ($calendar_category != 0)
410    {
411      $query.= '
412    AND category_id = '.$calendar_category;
413    }
414    $query.= '
415    AND id = image_id
416  ORDER BY RAND()
417  LIMIT 0,1
418;';
419    $row = mysql_fetch_array(pwg_query($query));
420   
421    $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
422   
423    $thumbnail_title = $lang['calendar_picture_hint'].$name;
424
425    $url_link = PHPWG_ROOT_PATH.'category.php?cat=search';
426    $url_link.= '&amp;search='.$conf['calendar_datefield'].':'.$_GET['day'];
427    if ($calendar_category != 0)
428    {
429      $url_link.= '--cat:'.$calendar_category.'|AND';
430    }
431   
432    $template->assign_block_vars(
433      'thumbnails.line.thumbnail',
434      array(
435        'IMAGE'=>$thumbnail_src,
436        'IMAGE_ALT'=>$row['file'],
437        'IMAGE_TITLE'=>$thumbnail_title,
438         
439        'U_IMG_LINK'=>add_session_id($url_link)
440         )
441       );
442
443    $template->assign_block_vars(
444      'thumbnails.line.thumbnail.category_name',
445      array(
446        'NAME' => $name
447        )
448      );
449   
450    // create a new line ?
451    if (++$row_number == $user['nb_image_line'])
452    {
453      $template->assign_block_vars('thumbnails.line', array());
454      $row_number = 0;
455    }
456  }
457  $conf['level_separator'] = $old_level_separator;
458}
459?>
Note: See TracBrowser for help on using the repository browser.