source: branches/2.0/include/calendar_base.class.php @ 5014

Last change on this file since 5014 was 3046, checked in by plg, 15 years ago

Administration: happy new year 2009, all PHP headers updated.

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