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

Last change on this file since 27369 was 27369, checked in by rvelices, 10 years ago

more query2array

  • Property svn:eol-style set to LF
File size: 10.2 KB
RevLine 
[1053]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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/**
[25507]25 * @package functions\calendar
26 */
27
28
29/**
[1053]30 * Base class for monthly and weekly calendar styles
31 */
[25507]32abstract class CalendarBase
[1053]33{
[25507]34  /** db column on which this calendar works */
[1053]35  var $date_field;
[25507]36  /** used for queries (INNER JOIN or normal) */
[1053]37  var $inner_sql;
[25507]38  /** used to store db fields */
[1059]39  var $calendar_levels;
[1053]40
41  /**
[25507]42   * Generate navigation bars for category page.
43   *
44   * @return boolean false indicates that thumbnails where not included
[1053]45   */
[25507]46  abstract function generate_category_content();
47
48  /**
49   * Returns a sql WHERE subquery for the date field.
50   *
51   * @param int $max_levels (e.g. 2=only year and month)
52   * @return string
53   */
[26865]54  abstract function get_date_where($max_levels=3);
[25507]55
56  /**
57   * Initialize the calendar.
58   *
59   * @param string $inner_sql
60   */
[1086]61  function initialize($inner_sql)
[1053]62  {
[1086]63    global $page;
[1090]64    if ($page['chronology_field']=='posted')
[1086]65    {
66      $this->date_field = 'date_available';
67    }
68    else
69    {
70      $this->date_field = 'date_creation';
71    }
[1053]72    $this->inner_sql = $inner_sql;
73  }
74
[25507]75  /**
76   * Returns the calendar title (with HTML).
77   *
78   * @return string
79   */
[1059]80  function get_display_name()
81  {
[1086]82    global $conf, $page;
[1059]83    $res = '';
84
[1086]85    for ($i=0; $i<count($page['chronology_date']); $i++)
[1059]86    {
87      $res .= $conf['level_separator'];
[1086]88      if ( isset($page['chronology_date'][$i+1]) )
[1069]89      {
[1086]90        $chronology_date = array_slice($page['chronology_date'],0, $i+1);
91        $url = duplicate_index_url(
92            array( 'chronology_date'=>$chronology_date ),
93            array( 'start' )
94            );
[1059]95        $res .=
96          '<a href="'.$url.'">'
[1086]97          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]98          .'</a>';
99      }
100      else
101      {
102        $res .=
103          '<span class="calInHere">'
[1086]104          .$this->get_date_component_label($i, $page['chronology_date'][$i])
[1059]105          .'</span>';
106      }
107    }
108    return $res;
109  }
110
[1053]111  /**
[25507]112   * Returns a display name for a date component optionally using labels.
113   *
114   * @return string
115   */
116  protected function get_date_component_label($level, $date_component)
[1057]117  {
118    $label = $date_component;
[1059]119    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
[1057]120    {
[1059]121      $label = $this->calendar_levels[$level]['labels'][$date_component];
[1057]122    }
[1069]123    elseif ('any' === $date_component )
[1057]124    {
[5021]125      $label = l10n('All');
[1057]126    }
127    return $label;
128  }
[1059]129
[1057]130  /**
[25507]131   * Gets a nice display name for a date to be shown in previous/next links
132   *
133   * @param string $date
134   * @return string
[1062]135   */
[25507]136  protected function get_date_nice_name($date)
[1062]137  {
138    $date_components = explode('-', $date);
139    $res = '';
140    for ($i=count($date_components)-1; $i>=0; $i--)
141    {
[1069]142      if ('any' !== $date_components[$i])
[1062]143      {
[1069]144        $label = $this->get_date_component_label($i, $date_components[$i] );
145        if ( $res!='' )
[1062]146        {
[1069]147          $res .= ' ';
[1062]148        }
[1069]149        $res .= $label;
[1062]150      }
151    }
152    return $res;
153  }
154
155  /**
[1053]156   * Creates a calendar navigation bar.
157   *
[25507]158   * @param array $date_components
159   * @param array $items - hash of items to put in the bar (e.g. 2005,2006)
160   * @param bool $show_any - adds any link to the end of the bar
161   * @param bool $show_empty - shows all labels even those without items
162   * @param array $labels - optional labels for items (e.g. Jan,Feb,...)
163   * @return string
[1053]164   */
[25507]165  protected function get_nav_bar_from_items($date_components, $items,
[3168]166                                  $show_any,
[1059]167                                  $show_empty=false, $labels=null)
[1053]168  {
[2469]169    global $conf, $page, $template;
[1059]170
[2469]171    $nav_bar_datas=array();
[1059]172
173    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
174    {
175      foreach ($labels as $item => $label)
176      {
177        if ( ! isset($items[$item]) )
178        {
179          $items[$item] = -1;
180        }
181      }
182      ksort($items);
183    }
184
[1053]185    foreach ($items as $item => $nb_images)
186    {
187      $label = $item;
188      if (isset($labels[$item]))
189      {
190        $label = $labels[$item];
191      }
[2101]192      if ($nb_images==-1)
[1053]193      {
[2469]194        $tmp_datas=array(
[3168]195          'LABEL'=> $label
[2469]196        );
[1059]197      }
[1053]198      else
199      {
[1086]200        $url = duplicate_index_url(
[1107]201          array('chronology_date'=>array_merge($date_components,array($item))),
[1086]202          array( 'start' )
203            );
[2469]204        $tmp_datas=array(
[3168]205          'LABEL'=> $label,
206          'URL' => $url
[2469]207        );
[1053]208      }
209      if ($nb_images > 0)
210      {
[3168]211        $tmp_datas['NB_IMAGES']=$nb_images;
[1053]212      }
[2469]213      $nav_bar_datas[]=$tmp_datas;
[3168]214
[1053]215    }
[1059]216
[1163]217    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
218          count($date_components)<count($this->calendar_levels)-1 )
[1053]219    {
[2101]220      $url = duplicate_index_url(
221        array('chronology_date'=>array_merge($date_components,array('any'))),
222        array( 'start' )
223          );
[2469]224      $nav_bar_datas[]=array(
[5021]225        'LABEL' => l10n('All'),
[3168]226        'URL' => $url
[2469]227      );
[1053]228    }
[2469]229
[3168]230    return $nav_bar_datas;
[1053]231  }
232
233  /**
234   * Creates a calendar navigation bar for a given level.
235   *
[25507]236   * @param int $level - 0-year, 1-month/week, 2-day
[1053]237   */
[25507]238  protected function build_nav_bar($level, $labels=null)
[1053]239  {
[1086]240    global $template, $conf, $page;
[1059]241
[1053]242    $query = '
[3168]243SELECT DISTINCT('.$this->calendar_levels[$level]['sql'].') as period,
244  COUNT(DISTINCT id) as nb_images'.
245$this->inner_sql.
246$this->get_date_where($level).'
[12624]247  GROUP BY period;';
[1053]248
[27369]249    $level_items = query2array($query, 'period', 'nb_images');
[1053]250
[1062]251    if ( count($level_items)==1 and
[1086]252         count($page['chronology_date'])<count($this->calendar_levels)-1)
[1059]253    {
[1086]254      if ( ! isset($page['chronology_date'][$level]) )
[1059]255      {
256        list($key) = array_keys($level_items);
[1086]257        $page['chronology_date'][$level] = (int)$key;
[1059]258
[1086]259        if ( $level<count($page['chronology_date']) and
[1062]260             $level!=count($this->calendar_levels)-1 )
261        {
262          return;
263        }
[1059]264      }
265    }
266
[1092]267    $dates = $page['chronology_date'];
268    while ($level<count($dates))
269    {
270      array_pop($dates);
271    }
272
[1053]273    $nav_bar = $this->get_nav_bar_from_items(
[1092]274      $dates,
[1053]275      $level_items,
276      true,
[1059]277      true,
278      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
[1053]279      );
280
[2231]281    $template->append(
282      'chronology_navigation_bars',
[1053]283      array(
[3168]284        'items' => $nav_bar,
[1053]285        )
286      );
287  }
[1062]288
289  /**
290   * Assigns the next/previous link to the template with regards to
291   * the currently choosen date.
292   */
[25507]293  protected function build_next_prev()
[1062]294  {
[1086]295    global $template, $page;
[4398]296
[1062]297    $prev = $next =null;
[1086]298    if ( empty($page['chronology_date']) )
[1062]299      return;
[4398]300   
[4781]301    $sub_queries = array();
[4398]302    $nb_elements = count($page['chronology_date']);
303    for ($i=0; $i<$nb_elements; $i++)
[1062]304    {
[1163]305      if ( 'any' === $page['chronology_date'][$i] )
[1062]306      {
[4781]307        $sub_queries[] = '\'any\'';
[1062]308      }
309      else
310      {
[4781]311        $sub_queries[] = pwg_db_cast_to_text($this->calendar_levels[$i]['sql']);
[1062]312      }
313    }
[4781]314    $query = 'SELECT '.pwg_db_concat_ws($sub_queries, '-').' AS period';
[4398]315    $query .= $this->inner_sql .'
[1062]316AND ' . $this->date_field . ' IS NOT NULL
317GROUP BY period';
[4398]318   
319    $current = implode('-', $page['chronology_date'] );
[27369]320    $upper_items = query2array($query,null, 'period');
[1069]321
[4833]322    usort($upper_items, 'version_compare');
[1062]323    $upper_items_rank = array_flip($upper_items);
[1069]324    if ( !isset($upper_items_rank[$current]) )
325    {
[23618]326      $upper_items[] = $current;// just in case (external link)
[4833]327      usort($upper_items, 'version_compare');
[1069]328      $upper_items_rank = array_flip($upper_items);
329    }
[1062]330    $current_rank = $upper_items_rank[$current];
[1069]331
[2231]332    $tpl_var = array();
[3168]333
[1062]334    if ( $current_rank>0 )
335    { // has previous
336      $prev = $upper_items[$current_rank-1];
[1086]337      $chronology_date = explode('-', $prev);
[2231]338      $tpl_var['previous'] =
[1062]339        array(
340          'LABEL' => $this->get_date_nice_name($prev),
[1086]341          'URL' => duplicate_index_url(
342                array('chronology_date'=>$chronology_date), array('start')
343                )
[1062]344        );
345    }
[3168]346
[1062]347    if ( $current_rank < count($upper_items)-1 )
[1086]348    { // has next
[1062]349      $next = $upper_items[$current_rank+1];
[1086]350      $chronology_date = explode('-', $next);
[2231]351      $tpl_var['next'] =
[1062]352        array(
353          'LABEL' => $this->get_date_nice_name($next),
[1086]354          'URL' => duplicate_index_url(
355                array('chronology_date'=>$chronology_date), array('start')
356                )
[1062]357        );
358    }
[3168]359
[2231]360    if ( !empty($tpl_var) )
361    {
[23618]362      $existing = $template->smarty->getVariable('chronology_navigation_bars');
363      if (! ($existing instanceof Undefined_Smarty_Variable))
[2231]364      {
[23618]365        $existing->value[ sizeof($existing->value)-1 ] =
366          array_merge( $existing->value[ sizeof($existing->value)-1 ], $tpl_var);
[2231]367      }
368      else
369      {
370        $template->append( 'chronology_navigation_bars', $tpl_var );
371      }
372    }
[1062]373  }
[1053]374}
[25507]375
[12118]376?>
Note: See TracBrowser for help on using the repository browser.