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

Last change on this file since 2101 was 2101, checked in by rvelices, 17 years ago
  • removed unused code from the calendar
  • removed some unused css rules, simplified css a bit and made some rule grouping (nothing important)
  • 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-2007 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | file          : $Id: calendar_base.class.php 2101 2007-09-20 23:45:23Z rvelices $
7// | last update   : $Date: 2007-09-20 23:45:23 +0000 (Thu, 20 Sep 2007) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 2101 $
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  var $has_nav_bar;
39
40  /**
41   * Initialize the calendar
42   * @param string inner_sql used for queries (INNER JOIN or normal)
43   */
44  function initialize($inner_sql)
45  {
46    global $page;
47    if ($page['chronology_field']=='posted')
48    {
49      $this->date_field = 'date_available';
50    }
51    else
52    {
53      $this->date_field = 'date_creation';
54    }
55    $this->inner_sql = $inner_sql;
56    $this->has_nav_bar = false;
57  }
58
59  function get_display_name()
60  {
61    global $conf, $page;
62    $res = '';
63
64    for ($i=0; $i<count($page['chronology_date']); $i++)
65    {
66      $res .= $conf['level_separator'];
67      if ( isset($page['chronology_date'][$i+1]) )
68      {
69        $chronology_date = array_slice($page['chronology_date'],0, $i+1);
70        $url = duplicate_index_url(
71            array( 'chronology_date'=>$chronology_date ),
72            array( 'start' )
73            );
74        $res .=
75          '<a href="'.$url.'">'
76          .$this->get_date_component_label($i, $page['chronology_date'][$i])
77          .'</a>';
78      }
79      else
80      {
81        $res .=
82          '<span class="calInHere">'
83          .$this->get_date_component_label($i, $page['chronology_date'][$i])
84          .'</span>';
85      }
86    }
87    return $res;
88  }
89
90//--------------------------------------------------------- private members ---
91  /**
92   * Returns a display name for a date component optionally using labels
93  */
94  function get_date_component_label($level, $date_component)
95  {
96    $label = $date_component;
97    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
98    {
99      $label = $this->calendar_levels[$level]['labels'][$date_component];
100    }
101    elseif ('any' === $date_component )
102    {
103      $label = l10n('calendar_any');
104    }
105    return $label;
106  }
107
108  /**
109   * Gets a nice display name for a date to be shown in previos/next links.
110   */
111  function get_date_nice_name($date)
112  {
113    $date_components = explode('-', $date);
114    $res = '';
115    for ($i=count($date_components)-1; $i>=0; $i--)
116    {
117      if ('any' !== $date_components[$i])
118      {
119        $label = $this->get_date_component_label($i, $date_components[$i] );
120        if ( $res!='' )
121        {
122          $res .= ' ';
123        }
124        $res .= $label;
125      }
126    }
127    return $res;
128  }
129
130  /**
131   * Creates a calendar navigation bar.
132   *
133   * @param array date_components
134   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
135   * @param string class_prefix - html class attribute prefix for span elements
136   * @param bool show_any - adds any link to the end of the bar
137   * @param bool show_empty - shows all labels even those without items
138   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
139   * @return string the navigation bar
140   */
141  function get_nav_bar_from_items($date_components, $items,
142                                  $class_prefix, $show_any,
143                                  $show_empty=false, $labels=null)
144  {
145    global $conf, $page;
146
147    $nav_bar = '';
148
149    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
150    {
151      foreach ($labels as $item => $label)
152      {
153        if ( ! isset($items[$item]) )
154        {
155          $items[$item] = -1;
156        }
157      }
158      ksort($items);
159    }
160
161    foreach ($items as $item => $nb_images)
162    {
163      $label = $item;
164      if (isset($labels[$item]))
165      {
166        $label = $labels[$item];
167      }
168      if ($nb_images==-1)
169      {
170        $nav_bar .= '<span class="'.$class_prefix.'Empty">';
171        $nav_bar .= $label;
172      }
173      else
174      {
175        $nav_bar .= '<span class="'.$class_prefix.'">';
176        $url = duplicate_index_url(
177          array('chronology_date'=>array_merge($date_components,array($item))),
178          array( 'start' )
179            );
180        $nav_bar .= '<a href="'.$url.'">';
181        $nav_bar .= $label;
182        $nav_bar .= '</a>';
183      }
184      if ($nb_images > 0)
185      {
186        $nav_bar .= '('.$nb_images.')';
187      }
188      $nav_bar.= '</span>';
189    }
190
191    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
192          count($date_components)<count($this->calendar_levels)-1 )
193    {
194      $label = l10n('calendar_any');
195      $nav_bar .= '<span class="'.$class_prefix.'">';
196      $url = duplicate_index_url(
197        array('chronology_date'=>array_merge($date_components,array('any'))),
198        array( 'start' )
199          );
200      $nav_bar .= '<a href="'.$url.'">';
201      $nav_bar .= $label;
202      $nav_bar .= '</a>';
203      $nav_bar.= '</span>';
204    }
205    return $nav_bar;
206  }
207
208  /**
209   * Creates a calendar navigation bar for a given level.
210   *
211   * @param int level - the level (0-year,1-month/week,2-day)
212   * @return void
213   */
214  function build_nav_bar($level, $labels=null)
215  {
216    global $template, $conf, $page;
217
218    $query = '
219SELECT DISTINCT('.$this->calendar_levels[$level]['sql']
220      .') as period';
221    $query.= $this->inner_sql;
222    $query.= $this->get_date_where($level);
223    $query.= '
224  GROUP BY period
225;';
226
227    $level_items = array();
228    $result = pwg_query($query);
229    while ($row = mysql_fetch_array($result))
230    {
231      $level_items[$row['period']] = 0;
232    }
233
234    if ( count($level_items)==1 and
235         count($page['chronology_date'])<count($this->calendar_levels)-1)
236    {
237      if ( ! isset($page['chronology_date'][$level]) )
238      {
239        list($key) = array_keys($level_items);
240        $page['chronology_date'][$level] = (int)$key;
241
242        if ( $level<count($page['chronology_date']) and
243             $level!=count($this->calendar_levels)-1 )
244        {
245          return;
246        }
247      }
248    }
249
250    $dates = $page['chronology_date'];
251    while ($level<count($dates))
252    {
253      array_pop($dates);
254    }
255
256    $nav_bar = $this->get_nav_bar_from_items(
257      $dates,
258      $level_items,
259      'calItem',
260      true,
261      true,
262      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
263      );
264
265    $template->assign_block_vars(
266      'calendar.navbar',
267      array(
268        'BAR' => $nav_bar,
269        )
270      );
271    $this->has_nav_bar = true;
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    if (!$this->has_nav_bar and
315        ($current_rank>0 or $current_rank < count($upper_items)-1 ) )
316    {
317      $template->assign_block_vars( 'calendar.navbar', array() );
318    }
319
320    if ( $current_rank>0 )
321    { // has previous
322      $prev = $upper_items[$current_rank-1];
323      $chronology_date = explode('-', $prev);
324      $template->assign_block_vars(
325        'calendar.navbar.prev',
326        array(
327          'LABEL' => $this->get_date_nice_name($prev),
328          'URL' => duplicate_index_url(
329                array('chronology_date'=>$chronology_date), array('start')
330                )
331          )
332        );
333    }
334    if ( $current_rank < count($upper_items)-1 )
335    { // has next
336      $next = $upper_items[$current_rank+1];
337      $chronology_date = explode('-', $next);
338      $template->assign_block_vars(
339        'calendar.navbar.next',
340        array(
341          'LABEL' => $this->get_date_nice_name($next),
342          'URL' => duplicate_index_url(
343                array('chronology_date'=>$chronology_date), array('start')
344                )
345          )
346        );
347    }
348  }
349}
350?>
Note: See TracBrowser for help on using the repository browser.