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

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

calendar improvements: week on weekly list starts on Monday,
ability to show grayed months/weeks/days (without any picture in it),
added icons for created/posted fields
language uniformization

calendar fixes: correct number of pictures in calendar view,
code simplification (I hope so)

  • Property svn:eol-style set to native
File size: 7.6 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  /**
44   * Initialize the calendar
45   * @param string date_field db column on which this calendar works
46   * @param string inner_sql used for queries (INNER JOIN or normal)
47   * @param array date_components
48   */
49  function initialize($date_field, $inner_sql, $date_components)
50  {
51    $this->date_field = $date_field;
52    $this->inner_sql = $inner_sql;
53    $this->date_components = $date_components;
54  }
55
56  function get_display_name()
57  {
58    global $conf;
59    $res = '';
60    $url = $this->url_base;
61
62    for ($i=0; $i<count($this->date_components); $i++)
63    {
64      $res .= $conf['level_separator'];
65
66      $url .= $this->date_components[$i].'-';
67      if ( isset($this->date_components[$i+1]) )
68      {
69        $res .=
70          '<a href="'.$url.'">'
71          .$this->get_date_component_label($i, $this->date_components[$i])
72          .'</a>';
73      }
74      else
75      {
76        $res .=
77          '<span class="calInHere">'
78          .$this->get_date_component_label($i, $this->date_components[$i])
79          .'</span>';
80      }
81    }
82    return $res;
83  }
84
85//--------------------------------------------------------- private members ---
86  /**
87   * Returns a display name for a date component optionally using labels
88  */
89  function get_date_component_label($level, $date_component)
90  {
91    $label = $date_component;
92    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
93    {
94      $label = $this->calendar_levels[$level]['labels'][$date_component];
95    }
96    elseif ($date_component == 'any' )
97    {
98      $label = l10n('calendar_any');
99    }
100    return $label;
101  }
102
103  /**
104   * Creates a calendar navigation bar.
105   *
106   * @param string url_base - links start with this root
107   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
108   * @param array selected_item - item currently selected (e.g. 2005)
109   * @param string class_prefix - html class attribute prefix for span elements
110   * @param bool show_any - adds any link to the end of the bar
111   * @param bool show_empty - shows all labels even those without items
112   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
113   * @return string the navigation bar
114   */
115  function get_nav_bar_from_items($url_base, $items, $selected_item,
116                                  $class_prefix, $show_any,
117                                  $show_empty=false, $labels=null)
118  {
119    global $conf;
120
121    $nav_bar = '';
122
123    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
124    {
125      foreach ($labels as $item => $label)
126      {
127        if ( ! isset($items[$item]) )
128        {
129          $items[$item] = -1;
130        }
131      }
132      ksort($items);
133    }
134
135    foreach ($items as $item => $nb_images)
136    {
137      $label = $item;
138      if (isset($labels[$item]))
139      {
140        $label = $labels[$item];
141      }
142      if (isset($selected_item) and $item == $selected_item)
143      {
144        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
145        $nav_bar .= $label;
146      }
147      elseif ($nb_images==-1)
148      {
149        $nav_bar .= '<span class="'.$class_prefix.'Empty">';
150        $nav_bar .= $label;
151      }
152      else
153      {
154        $nav_bar .= '<span class="'.$class_prefix.'">';
155        $url = $url_base . $item;
156        $nav_bar .= '<a href="'.$url.'">';
157        $nav_bar .= $label;
158        $nav_bar .= '</a>';
159      }
160      if ($nb_images > 0)
161      {
162        $nav_bar .= '('.$nb_images.')';
163      }
164      $nav_bar.= '</span>';
165    }
166
167    if ($conf['calendar_show_any'] and $show_any and count($items) > 1)
168    {
169      $label = l10n('calendar_any');
170      if (isset($selected_item) and 'any' == $selected_item)
171      {
172        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
173        $nav_bar .= $label;
174      }
175      else
176      {
177        $nav_bar .= '<span class="'.$class_prefix.'">';
178        $url = $url_base . 'any';
179        $nav_bar .= '<a href="'.$url.'">';
180        $nav_bar .= $label;
181        $nav_bar .= '</a>';
182      }
183      $nav_bar.= '</span>';
184    }
185    return $nav_bar;
186  }
187
188  /**
189   * Creates a calendar navigation bar for a given level.
190   *
191   * @param int level - the level (0-year,1-month/week,2-day)
192   * @return void
193   */
194  function build_nav_bar($level, $labels=null)
195  {
196    global $template, $conf;
197
198    $query = '
199SELECT DISTINCT('.$this->calendar_levels[$level]['sql']
200      .') as period';
201    $query.= $this->inner_sql;
202    $query.= $this->get_date_where($level);
203    $query.= '
204  GROUP BY period
205;';
206
207    $level_items = array();
208    $result = pwg_query($query);
209    while ($row = mysql_fetch_array($result))
210    {
211      $level_items[$row['period']] = 0;
212    }
213
214    if ( count($level_items)==1 )
215    {
216      if ( ! isset($this->date_components[$level]) )
217      {
218        list($key) = array_keys($level_items);
219        $this->date_components[$level] = (int)$key;
220      }
221    }
222
223    if ( $conf['calendar_multi_bar']==false )
224    {
225      if ( $level<count($this->date_components) and
226           $level!=count($this->calendar_levels)-1 )
227      {
228        return;
229      }
230    }
231
232    $url_base = $this->url_base;
233    for ($i=0; $i<$level; $i++)
234    {
235      if (isset($this->date_components[$i]))
236      {
237        $url_base .= $this->date_components[$i].'-';
238      }
239    }
240    $selected = null;
241    if ( isset($this->date_components[$level]) )
242    {
243      $selected = $this->date_components[$level];
244    }
245    $nav_bar = $this->get_nav_bar_from_items(
246      $url_base,
247      $level_items,
248      $selected,
249      'calItem',
250      true,
251      true,
252      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
253      );
254
255    $template->assign_block_vars(
256      'calendar.navbar',
257      array(
258        'BAR' => $nav_bar
259        )
260      );
261  }
262}
263?>
Note: See TracBrowser for help on using the repository browser.