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

Last change on this file since 1064 was 1062, checked in by rvelices, 19 years ago

improvement: calendar navigation now uses at maximum one navigation bar
together with a next/previous date links

improvement: calendar view styles now shown in DIV.titrePage (I still need
to move padding from H2 to DIV.titrePage ...)

fix: moved all calendar css colors from template to the theme

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