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

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