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

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

calendar: removed some warnings, improved display for weekly style and
worked on css (still not perfect due to IE)

  • Property svn:eol-style set to native
File size: 10.0 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      if ($i>0)
69      {
70        $url .= '-';
71      }
72      $url .= $this->date_components[$i];
73      if ( isset($this->date_components[$i+1]) )
74      {
75        $res .=
76          '<a href="'.$url.'">'
77          .$this->get_date_component_label($i, $this->date_components[$i])
78          .'</a>';
79      }
80      else
81      {
82        $res .=
83          '<span class="calInHere">'
84          .$this->get_date_component_label($i, $this->date_components[$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 string url_base - links start with this root
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($url_base, $items, $selected_item,
144                                  $class_prefix, $show_any,
145                                  $show_empty=false, $labels=null)
146  {
147    global $conf;
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 = $url_base . $item;
184        $nav_bar .= '<a href="'.$url.'">';
185        $nav_bar .= $label;
186        $nav_bar .= '</a>';
187      }
188      if ($nb_images > 0)
189      {
190        $nav_bar .= '('.$nb_images.')';
191      }
192      $nav_bar.= '</span>';
193    }
194
195    if ($conf['calendar_show_any'] and $show_any and count($items) > 1)
196    {
197      $label = l10n('calendar_any');
198      if (isset($selected_item) and 'any' === $selected_item)
199      {
200        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
201        $nav_bar .= $label;
202      }
203      else
204      {
205        $nav_bar .= '<span class="'.$class_prefix.'">';
206        $url = $url_base . 'any';
207        $nav_bar .= '<a href="'.$url.'">';
208        $nav_bar .= $label;
209        $nav_bar .= '</a>';
210      }
211      $nav_bar.= '</span>';
212    }
213    return $nav_bar;
214  }
215
216  /**
217   * Creates a calendar navigation bar for a given level.
218   *
219   * @param int level - the level (0-year,1-month/week,2-day)
220   * @return void
221   */
222  function build_nav_bar($level, $labels=null)
223  {
224    global $template, $conf;
225
226    $query = '
227SELECT DISTINCT('.$this->calendar_levels[$level]['sql']
228      .') as period';
229    $query.= $this->inner_sql;
230    $query.= $this->get_date_where($level);
231    $query.= '
232  GROUP BY period
233;';
234
235    $level_items = array();
236    $result = pwg_query($query);
237    while ($row = mysql_fetch_array($result))
238    {
239      $level_items[$row['period']] = 0;
240    }
241
242    if ( count($level_items)==1 and
243         count($this->date_components)<count($this->calendar_levels)-1)
244    {
245      if ( ! isset($this->date_components[$level]) )
246      {
247        list($key) = array_keys($level_items);
248        $this->date_components[$level] = (int)$key;
249
250        if ( $level<count($this->date_components) and
251             $level!=count($this->calendar_levels)-1 )
252        {
253          return;
254        }
255      }
256    }
257
258    $url_base = $this->url_base;
259    for ($i=0; $i<$level; $i++)
260    {
261      if (isset($this->date_components[$i]))
262      {
263        $url_base .= $this->date_components[$i].'-';
264      }
265    }
266    $nav_bar = $this->get_nav_bar_from_items(
267      $url_base,
268      $level_items,
269      null,
270      'calItem',
271      true,
272      true,
273      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
274      );
275
276    $template->assign_block_vars(
277      'calendar.navbar',
278      array(
279        'BAR' => $nav_bar,
280        )
281      );
282    $this->has_nav_bar = true;
283  }
284
285  /**
286   * Assigns the next/previous link to the template with regards to
287   * the currently choosen date.
288   */
289  function build_next_prev()
290  {
291    global $template;
292    $prev = $next =null;
293    if ( empty($this->date_components) )
294      return;
295    $query = 'SELECT CONCAT_WS("-"';
296    for ($i=0; $i<count($this->date_components); $i++)
297    {
298      if ( 'any' === $this->date_components[$i] )
299      {
300        $query .= ','.'"any"';
301      }
302      else
303      {
304        $query .= ','.$this->calendar_levels[$i]['sql'];
305      }
306    }
307    $current = implode('-', $this->date_components );
308
309    $query.=') as period' . $this->inner_sql .'
310AND ' . $this->date_field . ' IS NOT NULL
311GROUP BY period';
312
313    $upper_items = array_from_query( $query, 'period');
314
315    usort($upper_items, 'version_compare');
316    $upper_items_rank = array_flip($upper_items);
317    if ( !isset($upper_items_rank[$current]) )
318    {
319      array_push($upper_items, $current);// just in case (external link)
320      usort($upper_items, 'version_compare');
321      $upper_items_rank = array_flip($upper_items);
322    }
323    $current_rank = $upper_items_rank[$current];
324
325    if (!$this->has_nav_bar and
326        ($current_rank>0 or $current_rank < count($upper_items)-1 ) )
327    {
328      $template->assign_block_vars( 'calendar.navbar', array() );
329    }
330    if ( $current_rank>0 )
331    { // has previous
332      $prev = $upper_items[$current_rank-1];
333      $template->assign_block_vars(
334        'calendar.navbar.prev',
335        array(
336          'LABEL' => $this->get_date_nice_name($prev),
337          'URL' => $this->url_base . $prev,
338          )
339        );
340    }
341    if ( $current_rank < count($upper_items)-1 )
342    {
343      // has next
344      $next = $upper_items[$current_rank+1];
345      $template->assign_block_vars(
346        'calendar.navbar.next',
347        array(
348          'LABEL' => $this->get_date_nice_name($next),
349          'URL' => $this->url_base . $next,
350          )
351        );
352    }
353  }
354}
355?>
Note: See TracBrowser for help on using the repository browser.