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

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

calendar: added posted/created chronology

calendar: added a where are we bar (like: created/2005/august)

calendar: possibility to hide the All/Any buttons ($confcalendar_show_any)

calendar: possibility to display a single navigation bar instead of
several navigation bars ($confcalendar_multi_bar)

calendar: tried to simplify code and improve readability
(still requires a review)

  • Property svn:eol-style set to native
File size: 6.1 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
42  /**
43   * Initialize the calendar
44   * @param string date_field db column on which this calendar works
45   * @param string inner_sql used for queries (INNER JOIN or normal)
46   * @param array date_components
47   */
48  function initialize($date_field, $inner_sql, $date_components)
49  {
50    $this->date_field = $date_field;
51    $this->inner_sql = $inner_sql;
52    $this->date_components = $date_components;
53  }
54
55//--------------------------------------------------------- private members ---
56  /**
57   * Returns a display name for a date component optionally using labels
58  */
59  function get_date_component_label($date_component, $labels=null)
60  {
61    $label = $date_component;
62    if (isset($labels[$date_component]))
63    {
64      $label = $labels[$date_component];
65    }
66    elseif ($date_component == 'any' )
67    {
68      $label = l10n('calendar_any');
69    }
70    return $label;
71  }
72 
73  /**
74   * Creates a calendar navigation bar.
75   *
76   * @param string url_base - links start with this root
77   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
78   * @param array selected_item - item currently selected (e.g. 2005)
79   * @param string class_prefix - html class attribute prefix for span elements
80   * @param bool allow_any - adds any to the end of the bar
81   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
82   * @return string the navigation bar
83   */
84  function get_nav_bar_from_items($url_base, $items, $selected_item,
85                                  $class_prefix, $allow_any, $labels=null)
86  {
87    $nav_bar = '';
88     
89    foreach ($items as $item => $nb_images)
90    {
91      $label = $item;
92      if (isset($labels[$item]))
93      {
94        $label = $labels[$item];
95      }
96      if (isset($selected_item) and $item == $selected_item)
97      {
98        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
99        $nav_bar .= $label;
100      }
101      else
102      {
103        $nav_bar .= '<span class="'.$class_prefix.'">';
104        $url = $url_base . $item;
105        $nav_bar .= '<a href="'.$url.'">';
106        $nav_bar .= $label;
107        $nav_bar .= '</a>';
108      }
109      if ($nb_images > 0)
110      {
111        $nav_bar .= '('.$nb_images.')';
112      }
113      $nav_bar.= '</span>';
114    }
115    global $conf;
116    if ($conf['calendar_show_any'] and $allow_any and count($items) > 1)
117    {
118      $label = l10n('calendar_any');
119      if (isset($selected_item) and 'any' == $selected_item)
120      {
121        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
122        $nav_bar .= $label;
123      }
124      else
125      {
126        $nav_bar .= '<span class="'.$class_prefix.'">';
127        $url = $url_base . 'any';
128        $nav_bar .= '<a href="'.$url.'">';
129        $nav_bar .= $label;
130        $nav_bar .= '</a>';
131      }
132      $nav_bar.= '</span>';
133    }
134    return $nav_bar;
135  }
136
137  /**
138   * Creates a calendar navigation bar for a given level.
139   *
140   * @param string sql_func - YEAR/MONTH/DAY/WEEK/DAYOFWEEK ...
141   * @param string sql_offset - (e.g. +1 for WEEK - first in year is 1)
142   * @param array labels - optional labels to show in the navigation bar
143   * @return void
144   */
145  function build_nav_bar($level, $sql_func,
146                         $sql_offset='', $labels=null)
147  {
148    global $template;
149   
150    $query = '
151SELECT DISTINCT('.$sql_func.'('.$this->date_field.')'.$sql_offset
152      .') as period';
153    $query.= $this->inner_sql;
154    $query.= $this->get_date_where($level);
155    $query.= '
156  GROUP BY period
157;';
158
159    $level_items = array();
160    $result = pwg_query($query);
161    while ($row = mysql_fetch_array($result))
162    {
163      $level_items[$row['period']] = 0;
164    }
165
166    $url_base = $this->url_base;
167    for ($i=0; $i<$level; $i++)
168    {
169      if (isset($this->date_components[$i]))
170      {
171        $url_base .= $this->date_components[$i].'-';
172      }
173    }
174    $selected = null;
175    if ( isset($this->date_components[$level]) )
176    {
177      $selected = $this->date_components[$level];
178    }
179    $nav_bar = $this->get_nav_bar_from_items(
180      $url_base,
181      $level_items,
182      $selected,
183      'cal',
184      true,
185      $labels
186      );
187
188    $template->assign_block_vars(
189      'calendar.navbar',
190      array(
191        'BAR' => $nav_bar
192        )
193      );
194  }
195}
196?>
Note: See TracBrowser for help on using the repository browser.