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

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

merge r1162 from branch-1_6 into trunk

fix: calendar prev/next links not working properly when 'any' (All/Tout)
was selected

fix: calendar 'any' (All/Tout) not shown on for the day selection
(last calendar level) - it was meaningless

fix: calendar image ordering is by date descending for large periods (no year
selected) or ascending for small periods (week,month...)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
RevLine 
[1053]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$
[1107]8// | last update   : $Date: 2006-04-14 01:42:24 +0000 (Fri, 14 Apr 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1163 $
[1053]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;
[1059]36  //
37  var $calendar_levels;
[1053]38
[1062]39  var $has_nav_bar;
40
[1053]41  /**
42   * Initialize the calendar
43   * @param string inner_sql used for queries (INNER JOIN or normal)
44   */
[1086]45  function initialize($inner_sql)
[1053]46  {
[1086]47    global $page;
[1090]48    if ($page['chronology_field']=='posted')
[1086]49    {
50      $this->date_field = 'date_available';
51    }
52    else
53    {
54      $this->date_field = 'date_creation';
55    }
[1053]56    $this->inner_sql = $inner_sql;
[1062]57    $this->has_nav_bar = false;
[1053]58  }
59
[1059]60  function get_display_name()
61  {
[1086]62    global $conf, $page;
[1059]63    $res = '';
64
[1086]65    for ($i=0; $i<count($page['chronology_date']); $i++)
[1059]66    {
67      $res .= $conf['level_separator'];
[1086]68      if ( isset($page['chronology_date'][$i+1]) )
[1069]69      {
[1086]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            );
[1059]75        $res .=
76          '<a href="'.$url.'">'
[1086]77          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]78          .'</a>';
79      }
80      else
81      {
82        $res .=
83          '<span class="calInHere">'
[1086]84          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]85          .'</span>';
86      }
87    }
88    return $res;
89  }
90
[1053]91//--------------------------------------------------------- private members ---
92  /**
[1057]93   * Returns a display name for a date component optionally using labels
94  */
[1059]95  function get_date_component_label($level, $date_component)
[1057]96  {
97    $label = $date_component;
[1059]98    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
[1057]99    {
[1059]100      $label = $this->calendar_levels[$level]['labels'][$date_component];
[1057]101    }
[1069]102    elseif ('any' === $date_component )
[1057]103    {
104      $label = l10n('calendar_any');
105    }
106    return $label;
107  }
[1059]108
[1057]109  /**
[1062]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    {
[1069]118      if ('any' !== $date_components[$i])
[1062]119      {
[1069]120        $label = $this->get_date_component_label($i, $date_components[$i] );
121        if ( $res!='' )
[1062]122        {
[1069]123          $res .= ' ';
[1062]124        }
[1069]125        $res .= $label;
[1062]126      }
127    }
128    return $res;
129  }
130
131  /**
[1053]132   * Creates a calendar navigation bar.
133   *
[1086]134   * @param array date_components
[1053]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
[1059]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
[1053]140   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
141   * @return string the navigation bar
142   */
[1086]143  function get_nav_bar_from_items($date_components, $items, $selected_item,
[1059]144                                  $class_prefix, $show_any,
145                                  $show_empty=false, $labels=null)
[1053]146  {
[1086]147    global $conf, $page;
[1059]148
[1053]149    $nav_bar = '';
[1059]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
[1053]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      }
[1059]175      elseif ($nb_images==-1)
176      {
177        $nav_bar .= '<span class="'.$class_prefix.'Empty">';
178        $nav_bar .= $label;
179      }
[1053]180      else
181      {
182        $nav_bar .= '<span class="'.$class_prefix.'">';
[1086]183        $url = duplicate_index_url(
[1107]184          array('chronology_date'=>array_merge($date_components,array($item))),
[1086]185          array( 'start' )
186            );
[1053]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    }
[1059]197
[1163]198    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
199          count($date_components)<count($this->calendar_levels)-1 )
[1053]200    {
201      $label = l10n('calendar_any');
[1069]202      if (isset($selected_item) and 'any' === $selected_item)
[1053]203      {
204        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
205        $nav_bar .= $label;
206      }
207      else
208      {
209        $nav_bar .= '<span class="'.$class_prefix.'">';
[1086]210        $url = duplicate_index_url(
[1107]211          array('chronology_date'=>array_merge($date_components,array('any'))),
[1086]212          array( 'start' )
213            );
[1053]214        $nav_bar .= '<a href="'.$url.'">';
215        $nav_bar .= $label;
216        $nav_bar .= '</a>';
217      }
218      $nav_bar.= '</span>';
219    }
220    return $nav_bar;
221  }
222
223  /**
224   * Creates a calendar navigation bar for a given level.
225   *
[1059]226   * @param int level - the level (0-year,1-month/week,2-day)
[1053]227   * @return void
228   */
[1059]229  function build_nav_bar($level, $labels=null)
[1053]230  {
[1086]231    global $template, $conf, $page;
[1059]232
[1053]233    $query = '
[1059]234SELECT DISTINCT('.$this->calendar_levels[$level]['sql']
[1053]235      .') as period';
236    $query.= $this->inner_sql;
[1057]237    $query.= $this->get_date_where($level);
[1053]238    $query.= '
239  GROUP BY period
240;';
241
242    $level_items = array();
243    $result = pwg_query($query);
244    while ($row = mysql_fetch_array($result))
245    {
246      $level_items[$row['period']] = 0;
247    }
248
[1062]249    if ( count($level_items)==1 and
[1086]250         count($page['chronology_date'])<count($this->calendar_levels)-1)
[1059]251    {
[1086]252      if ( ! isset($page['chronology_date'][$level]) )
[1059]253      {
254        list($key) = array_keys($level_items);
[1086]255        $page['chronology_date'][$level] = (int)$key;
[1059]256
[1086]257        if ( $level<count($page['chronology_date']) and
[1062]258             $level!=count($this->calendar_levels)-1 )
259        {
260          return;
261        }
[1059]262      }
263    }
264
[1092]265    $dates = $page['chronology_date'];
266    while ($level<count($dates))
267    {
268      array_pop($dates);
269    }
270
[1053]271    $nav_bar = $this->get_nav_bar_from_items(
[1092]272      $dates,
[1053]273      $level_items,
[1062]274      null,
[1059]275      'calItem',
[1053]276      true,
[1059]277      true,
278      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
[1053]279      );
280
281    $template->assign_block_vars(
282      'calendar.navbar',
283      array(
[1062]284        'BAR' => $nav_bar,
[1053]285        )
286      );
[1062]287    $this->has_nav_bar = true;
[1053]288  }
[1062]289
290  /**
291   * Assigns the next/previous link to the template with regards to
292   * the currently choosen date.
293   */
294  function build_next_prev()
295  {
[1086]296    global $template, $page;
[1062]297    $prev = $next =null;
[1086]298    if ( empty($page['chronology_date']) )
[1062]299      return;
300    $query = 'SELECT CONCAT_WS("-"';
[1086]301    for ($i=0; $i<count($page['chronology_date']); $i++)
[1062]302    {
[1163]303      if ( 'any' === $page['chronology_date'][$i] )
[1062]304      {
[1069]305        $query .= ','.'"any"';
[1062]306      }
307      else
308      {
[1069]309        $query .= ','.$this->calendar_levels[$i]['sql'];
[1062]310      }
311    }
[1086]312    $current = implode('-', $page['chronology_date'] );
[1062]313
314    $query.=') as period' . $this->inner_sql .'
315AND ' . $this->date_field . ' IS NOT NULL
316GROUP BY period';
[1069]317
[1062]318    $upper_items = array_from_query( $query, 'period');
[1069]319
[1062]320    usort($upper_items, 'version_compare');
321    $upper_items_rank = array_flip($upper_items);
[1069]322    if ( !isset($upper_items_rank[$current]) )
323    {
324      array_push($upper_items, $current);// just in case (external link)
325      usort($upper_items, 'version_compare');
326      $upper_items_rank = array_flip($upper_items);
327    }
[1062]328    $current_rank = $upper_items_rank[$current];
[1069]329
[1062]330    if (!$this->has_nav_bar and
331        ($current_rank>0 or $current_rank < count($upper_items)-1 ) )
332    {
333      $template->assign_block_vars( 'calendar.navbar', array() );
334    }
[1086]335
[1062]336    if ( $current_rank>0 )
337    { // has previous
338      $prev = $upper_items[$current_rank-1];
[1086]339      $chronology_date = explode('-', $prev);
[1062]340      $template->assign_block_vars(
341        'calendar.navbar.prev',
342        array(
343          'LABEL' => $this->get_date_nice_name($prev),
[1086]344          'URL' => duplicate_index_url(
345                array('chronology_date'=>$chronology_date), array('start')
346                )
[1062]347          )
348        );
349    }
350    if ( $current_rank < count($upper_items)-1 )
[1086]351    { // has next
[1062]352      $next = $upper_items[$current_rank+1];
[1086]353      $chronology_date = explode('-', $next);
[1062]354      $template->assign_block_vars(
355        'calendar.navbar.next',
356        array(
357          'LABEL' => $this->get_date_nice_name($next),
[1086]358          'URL' => duplicate_index_url(
359                array('chronology_date'=>$chronology_date), array('start')
360                )
[1062]361          )
362        );
363    }
364  }
[1053]365}
[1050]366?>
Note: See TracBrowser for help on using the repository browser.