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

Last change on this file since 29408 was 28926, checked in by mistic100, 10 years ago

bug 3002: Two classes with the same name : Calendar

  • 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      'classname'      => 'CalendarMonthly',
118      ),
119    // Weekly style
120    'weekly' => array(
121      'include'        => 'calendar_weekly.class.php',
122      'view_calendar'  => false,
123      'classname'      => 'CalendarWeekly',
124      ),
125    );
126
127  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
128
129  // Retrieve calendar field
130  isset( $fields[ $page['chronology_field'] ] ) or fatal_error('bad chronology field');
131
132  // Retrieve style
133  if ( !isset( $styles[ $page['chronology_style'] ] ) )
134  {
135    $page['chronology_style'] = 'monthly';
136  }
137  $cal_style = $page['chronology_style'];
138  $classname = $styles[$cal_style]['classname'];
139
140  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
141  $calendar = new $classname();
142
143  // Retrieve view
144
145  if ( !isset($page['chronology_view']) or
146       !in_array( $page['chronology_view'], $views ) )
147  {
148    $page['chronology_view'] = CAL_VIEW_LIST;
149  }
150
151  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
152        !$styles[$cal_style]['view_calendar'] )
153  {
154
155    $page['chronology_view'] = CAL_VIEW_LIST;
156  }
157
158  // perform a sanity check on $requested
159  if (!isset($page['chronology_date']))
160  {
161    $page['chronology_date'] = array();
162  }
163  while ( count($page['chronology_date']) > 3)
164  {
165    array_pop($page['chronology_date']);
166  }
167
168  $any_count = 0;
169  for ($i = 0; $i < count($page['chronology_date']); $i++)
170  {
171    if ($page['chronology_date'][$i] == 'any')
172    {
173      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
174      {// we dont allow any in calendar view
175        while ($i < count($page['chronology_date']))
176        {
177          array_pop($page['chronology_date']);
178        }
179        break;
180      }
181      $any_count++;
182    }
183    elseif ($page['chronology_date'][$i] == '')
184    {
185      while ($i < count($page['chronology_date']))
186      {
187        array_pop($page['chronology_date']);
188      }
189    }
190    else
191    {
192      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
193    }
194  }
195  if ($any_count == 3)
196  {
197    array_pop($page['chronology_date']);
198  }
199
200  $calendar->initialize($inner_sql);
201
202  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
203
204  $must_show_list = true; // true until calendar generates its own display
205  if (script_basename() != 'picture') // basename without file extention
206  {
207    if ($calendar->generate_category_content())
208    {
209      $page['items'] = array();
210      $must_show_list = false;
211    }
212
213    $page['comment'] = '';
214    $template->assign('FILE_CHRONOLOGY_VIEW', 'month_calendar.tpl');
215
216    foreach ($styles as $style => $style_data)
217    {
218      foreach ($views as $view)
219      {
220        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
221        {
222          $selected = false;
223
224          if ($style!=$cal_style)
225          {
226            $chronology_date = array();
227            if ( isset($page['chronology_date'][0]) )
228            {
229              $chronology_date[] = $page['chronology_date'][0];
230            }
231          }
232          else
233          {
234            $chronology_date = $page['chronology_date'];
235          }
236          $url = duplicate_index_url(
237              array(
238                'chronology_style' => $style,
239                'chronology_view' => $view,
240                'chronology_date' => $chronology_date,
241                )
242             );
243
244          if ($style==$cal_style and $view==$page['chronology_view'] )
245          {
246            $selected = true;
247          }
248
249          $template->append(
250            'chronology_views',
251            array(
252              'VALUE' => $url,
253              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
254              'SELECTED' => $selected,
255              )
256            );
257        }
258      }
259    }
260    $url = duplicate_index_url(
261              array(), array('start', 'chronology_date')
262            );
263    $calendar_title = '<a href="'.$url.'">'
264        .$fields[$page['chronology_field']]['label'].'</a>';
265    $calendar_title.= $calendar->get_display_name();
266    $template->assign('chronology',
267        array(
268          'TITLE' => $calendar_title
269        )
270      );
271  } // end category calling
272
273  if ($must_show_list)
274  {
275    if ( isset($page['super_order_by']) )
276    {
277      $order_by = $conf['order_by'];
278    }
279    else
280    {
281      if ( count($page['chronology_date'])==0
282           or in_array('any', $page['chronology_date']) )
283      {// selected period is very big so we show newest first
284        $order = ' DESC, ';
285      }
286      else
287      {// selected period is small (month,week) so we show oldest first
288        $order = ' ASC, ';
289      }
290      $order_by = str_replace(
291        'ORDER BY ',
292        'ORDER BY '.$calendar->date_field.$order, $conf['order_by']
293        );
294    }
295   
296    if ('categories'==$page['section'] && !isset($page['category'])
297      && ( count($page['chronology_date'])==0
298            OR ($page['chronology_date'][0]=='any' && count($page['chronology_date'])==1) )
299      )
300    {
301      $cache_key = $persistent_cache->make_key($user['id'].$user['cache_update_time']
302        .$calendar->date_field.$order_by);
303    }
304
305    if ( !isset($cache_key) || !$persistent_cache->get($cache_key, $page['items']))
306    {
307      $query = 'SELECT DISTINCT id '
308        .$calendar->inner_sql.'
309  '.$calendar->get_date_where().'
310  '.$order_by;
311      $page['items'] = array_from_query($query, 'id');
312      if ( isset($cache_key) )
313        $persistent_cache->set($cache_key, $page['items']);
314    }
315  }
316  pwg_debug('end initialize_calendar');
317}
318
319?>
Note: See TracBrowser for help on using the repository browser.