source: trunk/include/functions_calendar.inc.php @ 28913

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

use persistent cache for calendar list in root flat mode (same perf improvement as category flat view)

  • Property svn:eol-style set to LF
File size: 9.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23
24/**
25 * @package functions\calendar
26 */
27
28/** URL keyword for list view */
29define('CAL_VIEW_LIST',     'list');
30/** URL keyword for calendar view */
31define('CAL_VIEW_CALENDAR', 'calendar');
32
33
34/**
35 * Initialize _$page_ and _$template_ vars for calendar view.
36 */
37function initialize_calendar()
38{
39  global $page, $conf, $user, $template, $persistent_cache, $filter;
40
41//------------------ initialize the condition on items to take into account ---
42  $inner_sql = ' FROM ' . IMAGES_TABLE;
43
44  if ($page['section']=='categories')
45  { // we will regenerate the items by including subcats elements
46    $page['items'] = array();
47    $inner_sql .= '
48INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
49
50    if ( isset($page['category']) )
51    {
52      $sub_ids = array_diff(
53        get_subcat_ids(array($page['category']['id'])),
54        explode(',', $user['forbidden_categories'])
55        );
56
57      if (empty($sub_ids))
58      {
59        return; // nothing to do
60      }
61      $inner_sql .= '
62WHERE category_id IN ('.implode(',',$sub_ids).')';
63      $inner_sql .= '
64    '.get_sql_condition_FandF
65      (
66        array
67          (
68            'visible_images' => 'id'
69          ),
70        'AND', false
71      );
72    }
73    else
74    {
75      $inner_sql .= '
76    '.get_sql_condition_FandF
77      (
78        array
79          (
80            'forbidden_categories' => 'category_id',
81            'visible_categories' => 'category_id',
82            'visible_images' => 'id'
83          ),
84        'WHERE', true
85      );
86    }
87  }
88  else
89  {
90    if ( empty($page['items']) )
91    {
92      return; // nothing to do
93    }
94    $inner_sql .= '
95WHERE id IN (' . implode(',',$page['items']) .')';
96  }
97
98//-------------------------------------- initialize the calendar parameters ---
99  pwg_debug('start initialize_calendar');
100
101  $fields = array(
102    // Created
103    'created' => array(
104      'label'          => l10n('Creation date'),
105      ),
106    // Posted
107    'posted' => array(
108      'label'          => l10n('Post date'),
109      ),
110    );
111
112  $styles = array(
113    // Monthly style
114    'monthly' => array(
115      'include'        => 'calendar_monthly.class.php',
116      'view_calendar'  => true,
117      ),
118    // Weekly style
119    'weekly' => array(
120      'include'        => 'calendar_weekly.class.php',
121      'view_calendar'  => false,
122      ),
123    );
124
125  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
126
127  // Retrieve calendar field
128  isset( $fields[ $page['chronology_field'] ] ) or fatal_error('bad chronology field');
129
130  // Retrieve style
131  if ( !isset( $styles[ $page['chronology_style'] ] ) )
132  {
133    $page['chronology_style'] = 'monthly';
134  }
135  $cal_style = $page['chronology_style'];
136  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
137  // TODO : class name overlap, rename them in CalendarMonth and CalendarWeek
138  $calendar = new Calendar(); 
139
140  // Retrieve view
141
142  if ( !isset($page['chronology_view']) or
143       !in_array( $page['chronology_view'], $views ) )
144  {
145    $page['chronology_view'] = CAL_VIEW_LIST;
146  }
147
148  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
149        !$styles[$cal_style]['view_calendar'] )
150  {
151
152    $page['chronology_view'] = CAL_VIEW_LIST;
153  }
154
155  // perform a sanity check on $requested
156  if (!isset($page['chronology_date']))
157  {
158    $page['chronology_date'] = array();
159  }
160  while ( count($page['chronology_date']) > 3)
161  {
162    array_pop($page['chronology_date']);
163  }
164
165  $any_count = 0;
166  for ($i = 0; $i < count($page['chronology_date']); $i++)
167  {
168    if ($page['chronology_date'][$i] == 'any')
169    {
170      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
171      {// we dont allow any in calendar view
172        while ($i < count($page['chronology_date']))
173        {
174          array_pop($page['chronology_date']);
175        }
176        break;
177      }
178      $any_count++;
179    }
180    elseif ($page['chronology_date'][$i] == '')
181    {
182      while ($i < count($page['chronology_date']))
183      {
184        array_pop($page['chronology_date']);
185      }
186    }
187    else
188    {
189      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
190    }
191  }
192  if ($any_count == 3)
193  {
194    array_pop($page['chronology_date']);
195  }
196
197  $calendar->initialize($inner_sql);
198
199  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
200
201  $must_show_list = true; // true until calendar generates its own display
202  if (script_basename() != 'picture') // basename without file extention
203  {
204    if ($calendar->generate_category_content())
205    {
206      $page['items'] = array();
207      $must_show_list = false;
208    }
209
210    $page['comment'] = '';
211    $template->assign('FILE_CHRONOLOGY_VIEW', 'month_calendar.tpl');
212
213    foreach ($styles as $style => $style_data)
214    {
215      foreach ($views as $view)
216      {
217        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
218        {
219          $selected = false;
220
221          if ($style!=$cal_style)
222          {
223            $chronology_date = array();
224            if ( isset($page['chronology_date'][0]) )
225            {
226              $chronology_date[] = $page['chronology_date'][0];
227            }
228          }
229          else
230          {
231            $chronology_date = $page['chronology_date'];
232          }
233          $url = duplicate_index_url(
234              array(
235                'chronology_style' => $style,
236                'chronology_view' => $view,
237                'chronology_date' => $chronology_date,
238                )
239             );
240
241          if ($style==$cal_style and $view==$page['chronology_view'] )
242          {
243            $selected = true;
244          }
245
246          $template->append(
247            'chronology_views',
248            array(
249              'VALUE' => $url,
250              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
251              'SELECTED' => $selected,
252              )
253            );
254        }
255      }
256    }
257    $url = duplicate_index_url(
258              array(), array('start', 'chronology_date')
259            );
260    $calendar_title = '<a href="'.$url.'">'
261        .$fields[$page['chronology_field']]['label'].'</a>';
262    $calendar_title.= $calendar->get_display_name();
263    $template->assign('chronology',
264        array(
265          'TITLE' => $calendar_title
266        )
267      );
268  } // end category calling
269
270  if ($must_show_list)
271  {
272    if ( isset($page['super_order_by']) )
273    {
274      $order_by = $conf['order_by'];
275    }
276    else
277    {
278      if ( count($page['chronology_date'])==0
279           or in_array('any', $page['chronology_date']) )
280      {// selected period is very big so we show newest first
281        $order = ' DESC, ';
282      }
283      else
284      {// selected period is small (month,week) so we show oldest first
285        $order = ' ASC, ';
286      }
287      $order_by = str_replace(
288        'ORDER BY ',
289        'ORDER BY '.$calendar->date_field.$order, $conf['order_by']
290        );
291    }
292   
293    if ('categories'==$page['section'] && !isset($page['category'])
294      && ( count($page['chronology_date'])==0
295            OR ($page['chronology_date'][0]=='any' && count($page['chronology_date'])==1) )
296      )
297    {
298      $cache_key = $persistent_cache->make_key($user['id'].$user['cache_update_time']
299        .$calendar->date_field.$order_by);
300    }
301
302    if ( !isset($cache_key) || !$persistent_cache->get($cache_key, $page['items']))
303    {
304      $query = 'SELECT DISTINCT id '
305        .$calendar->inner_sql.'
306  '.$calendar->get_date_where().'
307  '.$order_by;
308      $page['items'] = array_from_query($query, 'id');
309      if ( isset($cache_key) )
310        $persistent_cache->set($cache_key, $page['items']);
311    }
312  }
313  pwg_debug('end initialize_calendar');
314}
315
316?>
Note: See TracBrowser for help on using the repository browser.