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

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

fix: php 5 errors and warnings

improve: put back update remote site from local listing.xml functionality
(allow_url_fopen is false on several ISPs)

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