source: branches/1.6/include/calendar_base.class.php @ 27569

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

fix: calendar prev/next links not working properly when 'any' (All/Tout)
was selected

fix: calendar 'any' (All/Tout) not shown on for the day selection
(last calendar level) - it was meaningless

fix: calendar image ordering is by date descending for large periods (no year
selected) or ascending for small periods (week,month...)

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