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

Last change on this file since 4781 was 4781, checked in by nikrou, 14 years ago

Feature 511 : add support for sqlite database engine

Using session_write_close function when session handler use database because write is called after object destruction.

  • Property svn:eol-style set to LF
File size: 9.7 KB
RevLine 
[1053]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3049]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1053]23
24/**
25 * Base class for monthly and weekly calendar styles
26 */
27class CalendarBase
28{
29  // db column on which this calendar works
30  var $date_field;
31  // used for queries (INNER JOIN or normal)
32  var $inner_sql;
[1059]33  //
34  var $calendar_levels;
[1053]35
36  /**
37   * Initialize the calendar
38   * @param string inner_sql used for queries (INNER JOIN or normal)
39   */
[1086]40  function initialize($inner_sql)
[1053]41  {
[1086]42    global $page;
[1090]43    if ($page['chronology_field']=='posted')
[1086]44    {
45      $this->date_field = 'date_available';
46    }
47    else
48    {
49      $this->date_field = 'date_creation';
50    }
[1053]51    $this->inner_sql = $inner_sql;
52  }
53
[1059]54  function get_display_name()
55  {
[1086]56    global $conf, $page;
[1059]57    $res = '';
58
[1086]59    for ($i=0; $i<count($page['chronology_date']); $i++)
[1059]60    {
61      $res .= $conf['level_separator'];
[1086]62      if ( isset($page['chronology_date'][$i+1]) )
[1069]63      {
[1086]64        $chronology_date = array_slice($page['chronology_date'],0, $i+1);
65        $url = duplicate_index_url(
66            array( 'chronology_date'=>$chronology_date ),
67            array( 'start' )
68            );
[1059]69        $res .=
70          '<a href="'.$url.'">'
[1086]71          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]72          .'</a>';
73      }
74      else
75      {
76        $res .=
77          '<span class="calInHere">'
[1086]78          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]79          .'</span>';
80      }
81    }
82    return $res;
83  }
84
[1053]85//--------------------------------------------------------- private members ---
86  /**
[1057]87   * Returns a display name for a date component optionally using labels
88  */
[1059]89  function get_date_component_label($level, $date_component)
[1057]90  {
91    $label = $date_component;
[1059]92    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
[1057]93    {
[1059]94      $label = $this->calendar_levels[$level]['labels'][$date_component];
[1057]95    }
[1069]96    elseif ('any' === $date_component )
[1057]97    {
98      $label = l10n('calendar_any');
99    }
100    return $label;
101  }
[1059]102
[1057]103  /**
[1062]104   * Gets a nice display name for a date to be shown in previos/next links.
105   */
106  function get_date_nice_name($date)
107  {
108    $date_components = explode('-', $date);
109    $res = '';
110    for ($i=count($date_components)-1; $i>=0; $i--)
111    {
[1069]112      if ('any' !== $date_components[$i])
[1062]113      {
[1069]114        $label = $this->get_date_component_label($i, $date_components[$i] );
115        if ( $res!='' )
[1062]116        {
[1069]117          $res .= ' ';
[1062]118        }
[1069]119        $res .= $label;
[1062]120      }
121    }
122    return $res;
123  }
124
125  /**
[1053]126   * Creates a calendar navigation bar.
127   *
[1086]128   * @param array date_components
[1053]129   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
[1059]130   * @param bool show_any - adds any link to the end of the bar
131   * @param bool show_empty - shows all labels even those without items
[1053]132   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
133   * @return string the navigation bar
134   */
[2101]135  function get_nav_bar_from_items($date_components, $items,
[3168]136                                  $show_any,
[1059]137                                  $show_empty=false, $labels=null)
[1053]138  {
[2469]139    global $conf, $page, $template;
[1059]140
[2469]141    $nav_bar_datas=array();
[1059]142
143    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
144    {
145      foreach ($labels as $item => $label)
146      {
147        if ( ! isset($items[$item]) )
148        {
149          $items[$item] = -1;
150        }
151      }
152      ksort($items);
153    }
154
[1053]155    foreach ($items as $item => $nb_images)
156    {
157      $label = $item;
158      if (isset($labels[$item]))
159      {
160        $label = $labels[$item];
161      }
[2101]162      if ($nb_images==-1)
[1053]163      {
[2469]164        $tmp_datas=array(
[3168]165          'LABEL'=> $label
[2469]166        );
[1059]167      }
[1053]168      else
169      {
[1086]170        $url = duplicate_index_url(
[1107]171          array('chronology_date'=>array_merge($date_components,array($item))),
[1086]172          array( 'start' )
173            );
[2469]174        $tmp_datas=array(
[3168]175          'LABEL'=> $label,
176          'URL' => $url
[2469]177        );
[1053]178      }
179      if ($nb_images > 0)
180      {
[3168]181        $tmp_datas['NB_IMAGES']=$nb_images;
[1053]182      }
[2469]183      $nav_bar_datas[]=$tmp_datas;
[3168]184
[1053]185    }
[1059]186
[1163]187    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
188          count($date_components)<count($this->calendar_levels)-1 )
[1053]189    {
[2101]190      $url = duplicate_index_url(
191        array('chronology_date'=>array_merge($date_components,array('any'))),
192        array( 'start' )
193          );
[2469]194      $nav_bar_datas[]=array(
[3168]195        'LABEL' => l10n('calendar_any'),
196        'URL' => $url
[2469]197      );
[1053]198    }
[2469]199
[3168]200    return $nav_bar_datas;
[1053]201  }
202
203  /**
204   * Creates a calendar navigation bar for a given level.
205   *
[1059]206   * @param int level - the level (0-year,1-month/week,2-day)
[1053]207   * @return void
208   */
[1059]209  function build_nav_bar($level, $labels=null)
[1053]210  {
[1086]211    global $template, $conf, $page;
[1059]212
[1053]213    $query = '
[3168]214SELECT DISTINCT('.$this->calendar_levels[$level]['sql'].') as period,
215  COUNT(DISTINCT id) as nb_images'.
216$this->inner_sql.
217$this->get_date_where($level).'
[4398]218  GROUP BY period
219  ORDER BY period ASC
[1053]220;';
221
[3168]222    $level_items = simple_hash_from_query($query, 'period', 'nb_images');
[1053]223
[1062]224    if ( count($level_items)==1 and
[1086]225         count($page['chronology_date'])<count($this->calendar_levels)-1)
[1059]226    {
[1086]227      if ( ! isset($page['chronology_date'][$level]) )
[1059]228      {
229        list($key) = array_keys($level_items);
[1086]230        $page['chronology_date'][$level] = (int)$key;
[1059]231
[1086]232        if ( $level<count($page['chronology_date']) and
[1062]233             $level!=count($this->calendar_levels)-1 )
234        {
235          return;
236        }
[1059]237      }
238    }
239
[1092]240    $dates = $page['chronology_date'];
241    while ($level<count($dates))
242    {
243      array_pop($dates);
244    }
245
[1053]246    $nav_bar = $this->get_nav_bar_from_items(
[1092]247      $dates,
[1053]248      $level_items,
249      true,
[1059]250      true,
251      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
[1053]252      );
253
[2231]254    $template->append(
255      'chronology_navigation_bars',
[1053]256      array(
[3168]257        'items' => $nav_bar,
[1053]258        )
259      );
260  }
[1062]261
262  /**
263   * Assigns the next/previous link to the template with regards to
264   * the currently choosen date.
265   */
266  function build_next_prev()
267  {
[1086]268    global $template, $page;
[4398]269
[1062]270    $prev = $next =null;
[1086]271    if ( empty($page['chronology_date']) )
[1062]272      return;
[4398]273   
[4781]274    $sub_queries = array();
[4398]275    $nb_elements = count($page['chronology_date']);
276    for ($i=0; $i<$nb_elements; $i++)
[1062]277    {
[1163]278      if ( 'any' === $page['chronology_date'][$i] )
[1062]279      {
[4781]280        $sub_queries[] = '\'any\'';
[1062]281      }
282      else
283      {
[4781]284        $sub_queries[] = pwg_db_cast_to_text($this->calendar_levels[$i]['sql']);
[1062]285      }
286    }
[4781]287    $query = 'SELECT '.pwg_db_concat_ws($sub_queries, '-').' AS period';
[4398]288    $query .= $this->inner_sql .'
[1062]289AND ' . $this->date_field . ' IS NOT NULL
290GROUP BY period';
[4398]291   
292    $current = implode('-', $page['chronology_date'] );
[1062]293    $upper_items = array_from_query( $query, 'period');
[1069]294
[4781]295    usort($upper_items, 'date_compare');
[1062]296    $upper_items_rank = array_flip($upper_items);
[1069]297    if ( !isset($upper_items_rank[$current]) )
298    {
299      array_push($upper_items, $current);// just in case (external link)
[4781]300      usort($upper_items, 'date_compare');
[1069]301      $upper_items_rank = array_flip($upper_items);
302    }
[1062]303    $current_rank = $upper_items_rank[$current];
[1069]304
[2231]305    $tpl_var = array();
[3168]306
[1062]307    if ( $current_rank>0 )
308    { // has previous
309      $prev = $upper_items[$current_rank-1];
[1086]310      $chronology_date = explode('-', $prev);
[2231]311      $tpl_var['previous'] =
[1062]312        array(
313          'LABEL' => $this->get_date_nice_name($prev),
[1086]314          'URL' => duplicate_index_url(
315                array('chronology_date'=>$chronology_date), array('start')
316                )
[1062]317        );
318    }
[3168]319
[1062]320    if ( $current_rank < count($upper_items)-1 )
[1086]321    { // has next
[1062]322      $next = $upper_items[$current_rank+1];
[1086]323      $chronology_date = explode('-', $next);
[2231]324      $tpl_var['next'] =
[1062]325        array(
326          'LABEL' => $this->get_date_nice_name($next),
[1086]327          'URL' => duplicate_index_url(
328                array('chronology_date'=>$chronology_date), array('start')
329                )
[1062]330        );
331    }
[3168]332
[2231]333    if ( !empty($tpl_var) )
334    {
335      $existing = & $template->get_template_vars('chronology_navigation_bars');
336      if ( !empty($existing) )
337      {
338        $existing[ sizeof($existing)-1 ] =
339          array_merge( $existing[ sizeof($existing)-1 ], $tpl_var);
340      }
341      else
342      {
343        $template->append( 'chronology_navigation_bars', $tpl_var );
344      }
345    }
[1062]346  }
[1053]347}
[1903]348?>
Note: See TracBrowser for help on using the repository browser.