source: trunk/include/calendar_base.class.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
26// +-----------------------------------------------------------------------+
27// | file          : $Id: calendar_base.class.php 2297 2008-04-04 22:57:23Z plg $
28// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
29// | last modifier : $Author: plg $
30// | revision      : $Revision: 2297 $
31// +-----------------------------------------------------------------------+
32// | This program is free software; you can redistribute it and/or modify  |
33// | it under the terms of the GNU General Public License as published by  |
34// | the Free Software Foundation                                          |
35// |                                                                       |
36// | This program is distributed in the hope that it will be useful, but   |
37// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
38// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
39// | General Public License for more details.                              |
40// |                                                                       |
41// | You should have received a copy of the GNU General Public License     |
42// | along with this program; if not, write to the Free Software           |
43// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
44// | USA.                                                                  |
45// +-----------------------------------------------------------------------+
46
47/**
48 * Base class for monthly and weekly calendar styles
49 */
50class CalendarBase
51{
52  // db column on which this calendar works
53  var $date_field;
54  // used for queries (INNER JOIN or normal)
55  var $inner_sql;
56  //
57  var $calendar_levels;
58
59  /**
60   * Initialize the calendar
61   * @param string inner_sql used for queries (INNER JOIN or normal)
62   */
63  function initialize($inner_sql)
64  {
65    global $page;
66    if ($page['chronology_field']=='posted')
67    {
68      $this->date_field = 'date_available';
69    }
70    else
71    {
72      $this->date_field = 'date_creation';
73    }
74    $this->inner_sql = $inner_sql;
75  }
76
77  function get_display_name()
78  {
79    global $conf, $page;
80    $res = '';
81
82    for ($i=0; $i<count($page['chronology_date']); $i++)
83    {
84      $res .= $conf['level_separator'];
85      if ( isset($page['chronology_date'][$i+1]) )
86      {
87        $chronology_date = array_slice($page['chronology_date'],0, $i+1);
88        $url = duplicate_index_url(
89            array( 'chronology_date'=>$chronology_date ),
90            array( 'start' )
91            );
92        $res .=
93          '<a href="'.$url.'">'
94          .$this->get_date_component_label($i, $page['chronology_date'][$i])
95          .'</a>';
96      }
97      else
98      {
99        $res .=
100          '<span class="calInHere">'
101          .$this->get_date_component_label($i, $page['chronology_date'][$i])
102          .'</span>';
103      }
104    }
105    return $res;
106  }
107
108//--------------------------------------------------------- private members ---
109  /**
110   * Returns a display name for a date component optionally using labels
111  */
112  function get_date_component_label($level, $date_component)
113  {
114    $label = $date_component;
115    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
116    {
117      $label = $this->calendar_levels[$level]['labels'][$date_component];
118    }
119    elseif ('any' === $date_component )
120    {
121      $label = l10n('calendar_any');
122    }
123    return $label;
124  }
125
126  /**
127   * Gets a nice display name for a date to be shown in previos/next links.
128   */
129  function get_date_nice_name($date)
130  {
131    $date_components = explode('-', $date);
132    $res = '';
133    for ($i=count($date_components)-1; $i>=0; $i--)
134    {
135      if ('any' !== $date_components[$i])
136      {
137        $label = $this->get_date_component_label($i, $date_components[$i] );
138        if ( $res!='' )
139        {
140          $res .= ' ';
141        }
142        $res .= $label;
143      }
144    }
145    return $res;
146  }
147
148  /**
149   * Creates a calendar navigation bar.
150   *
151   * @param array date_components
152   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
153   * @param string class_prefix - html class attribute prefix for span elements
154   * @param bool show_any - adds any link to the end of the bar
155   * @param bool show_empty - shows all labels even those without items
156   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
157   * @return string the navigation bar
158   */
159  function get_nav_bar_from_items($date_components, $items,
160                                  $class_prefix, $show_any,
161                                  $show_empty=false, $labels=null)
162  {
163    global $conf, $page;
164
165    $nav_bar = '';
166
167    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
168    {
169      foreach ($labels as $item => $label)
170      {
171        if ( ! isset($items[$item]) )
172        {
173          $items[$item] = -1;
174        }
175      }
176      ksort($items);
177    }
178
179    foreach ($items as $item => $nb_images)
180    {
181      $label = $item;
182      if (isset($labels[$item]))
183      {
184        $label = $labels[$item];
185      }
186      if ($nb_images==-1)
187      {
188        $nav_bar .= '<span class="'.$class_prefix.'Empty">';
189        $nav_bar .= $label;
190      }
191      else
192      {
193        $nav_bar .= '<span class="'.$class_prefix.'">';
194        $url = duplicate_index_url(
195          array('chronology_date'=>array_merge($date_components,array($item))),
196          array( 'start' )
197            );
198        $nav_bar .= '<a href="'.$url.'">';
199        $nav_bar .= $label;
200        $nav_bar .= '</a>';
201      }
202      if ($nb_images > 0)
203      {
204        $nav_bar .= '('.$nb_images.')';
205      }
206      $nav_bar.= '</span>';
207    }
208
209    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
210          count($date_components)<count($this->calendar_levels)-1 )
211    {
212      $label = l10n('calendar_any');
213      $nav_bar .= '<span class="'.$class_prefix.'">';
214      $url = duplicate_index_url(
215        array('chronology_date'=>array_merge($date_components,array('any'))),
216        array( 'start' )
217          );
218      $nav_bar .= '<a href="'.$url.'">';
219      $nav_bar .= $label;
220      $nav_bar .= '</a>';
221      $nav_bar.= '</span>';
222    }
223    return $nav_bar;
224  }
225
226  /**
227   * Creates a calendar navigation bar for a given level.
228   *
229   * @param int level - the level (0-year,1-month/week,2-day)
230   * @return void
231   */
232  function build_nav_bar($level, $labels=null)
233  {
234    global $template, $conf, $page;
235
236    $query = '
237SELECT DISTINCT('.$this->calendar_levels[$level]['sql']
238      .') as period';
239    $query.= $this->inner_sql;
240    $query.= $this->get_date_where($level);
241    $query.= '
242  GROUP BY period
243;';
244
245    $level_items = array();
246    $result = pwg_query($query);
247    while ($row = mysql_fetch_array($result))
248    {
249      $level_items[$row['period']] = 0;
250    }
251
252    if ( count($level_items)==1 and
253         count($page['chronology_date'])<count($this->calendar_levels)-1)
254    {
255      if ( ! isset($page['chronology_date'][$level]) )
256      {
257        list($key) = array_keys($level_items);
258        $page['chronology_date'][$level] = (int)$key;
259
260        if ( $level<count($page['chronology_date']) and
261             $level!=count($this->calendar_levels)-1 )
262        {
263          return;
264        }
265      }
266    }
267
268    $dates = $page['chronology_date'];
269    while ($level<count($dates))
270    {
271      array_pop($dates);
272    }
273
274    $nav_bar = $this->get_nav_bar_from_items(
275      $dates,
276      $level_items,
277      'calItem',
278      true,
279      true,
280      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
281      );
282
283    $template->append(
284      'chronology_navigation_bars',
285      array(
286        'CONTENT' => $nav_bar,
287        )
288      );
289  }
290
291  /**
292   * Assigns the next/previous link to the template with regards to
293   * the currently choosen date.
294   */
295  function build_next_prev()
296  {
297    global $template, $page;
298    $prev = $next =null;
299    if ( empty($page['chronology_date']) )
300      return;
301    $query = 'SELECT CONCAT_WS("-"';
302    for ($i=0; $i<count($page['chronology_date']); $i++)
303    {
304      if ( 'any' === $page['chronology_date'][$i] )
305      {
306        $query .= ','.'"any"';
307      }
308      else
309      {
310        $query .= ','.$this->calendar_levels[$i]['sql'];
311      }
312    }
313    $current = implode('-', $page['chronology_date'] );
314
315    $query.=') as period' . $this->inner_sql .'
316AND ' . $this->date_field . ' IS NOT NULL
317GROUP BY period';
318
319    $upper_items = array_from_query( $query, 'period');
320
321    usort($upper_items, 'version_compare');
322    $upper_items_rank = array_flip($upper_items);
323    if ( !isset($upper_items_rank[$current]) )
324    {
325      array_push($upper_items, $current);// just in case (external link)
326      usort($upper_items, 'version_compare');
327      $upper_items_rank = array_flip($upper_items);
328    }
329    $current_rank = $upper_items_rank[$current];
330
331    $tpl_var = array();
332   
333    if ( $current_rank>0 )
334    { // has previous
335      $prev = $upper_items[$current_rank-1];
336      $chronology_date = explode('-', $prev);
337      $tpl_var['previous'] =
338        array(
339          'LABEL' => $this->get_date_nice_name($prev),
340          'URL' => duplicate_index_url(
341                array('chronology_date'=>$chronology_date), array('start')
342                )
343        );
344    }
345   
346    if ( $current_rank < count($upper_items)-1 )
347    { // has next
348      $next = $upper_items[$current_rank+1];
349      $chronology_date = explode('-', $next);
350      $tpl_var['next'] =
351        array(
352          'LABEL' => $this->get_date_nice_name($next),
353          'URL' => duplicate_index_url(
354                array('chronology_date'=>$chronology_date), array('start')
355                )
356        );
357    }
358   
359    if ( !empty($tpl_var) )
360    {
361      $existing = & $template->get_template_vars('chronology_navigation_bars');
362      if ( !empty($existing) )
363      {
364        $existing[ sizeof($existing)-1 ] =
365          array_merge( $existing[ sizeof($existing)-1 ], $tpl_var);
366      }
367      else
368      {
369        $template->append( 'chronology_navigation_bars', $tpl_var );
370      }
371    }
372  }
373}
374?>
Note: See TracBrowser for help on using the repository browser.