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
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
[25507]24/**
25 * @package functions\calendar
26 */
27
28/** URL keyword for list view */
[1086]29define('CAL_VIEW_LIST',     'list');
[25507]30/** URL keyword for calendar view */
[1086]31define('CAL_VIEW_CALENDAR', 'calendar');
[1053]32
[25507]33
34/**
35 * Initialize _$page_ and _$template_ vars for calendar view.
36 */
[1053]37function initialize_calendar()
38{
[28491]39  global $page, $conf, $user, $template, $persistent_cache, $filter;
[1053]40
41//------------------ initialize the condition on items to take into account ---
42  $inner_sql = ' FROM ' . IMAGES_TABLE;
[1059]43
[1861]44  if ($page['section']=='categories')
[1053]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';
[1059]49
[1861]50    if ( isset($page['category']) )
[1053]51    {
52      $sub_ids = array_diff(
[1861]53        get_subcat_ids(array($page['category']['id'])),
[1053]54        explode(',', $user['forbidden_categories'])
55        );
[1059]56
[1053]57      if (empty($sub_ids))
58      {
59        return; // nothing to do
60      }
61      $inner_sql .= '
62WHERE category_id IN ('.implode(',',$sub_ids).')';
[1759]63      $inner_sql .= '
64    '.get_sql_condition_FandF
65      (
66        array
67          (
68            'visible_images' => 'id'
69          ),
70        'AND', false
71      );
[1053]72    }
73    else
74    {
75      $inner_sql .= '
[1677]76    '.get_sql_condition_FandF
77      (
78        array
79          (
80            'forbidden_categories' => 'category_id',
81            'visible_categories' => 'category_id',
[1759]82            'visible_images' => 'id'
[1677]83          ),
84        'WHERE', true
85      );
[1053]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');
[1057]100
101  $fields = array(
102    // Created
103    'created' => array(
104      'label'          => l10n('Creation date'),
105      ),
106    // Posted
107    'posted' => array(
[1059]108      'label'          => l10n('Post date'),
[1057]109      ),
110    );
111
112  $styles = array(
[1056]113    // Monthly style
[1057]114    'monthly' => array(
[1053]115      'include'        => 'calendar_monthly.class.php',
116      'view_calendar'  => true,
[28926]117      'classname'      => 'CalendarMonthly',
[1053]118      ),
[1059]119    // Weekly style
[1057]120    'weekly' => array(
[1053]121      'include'        => 'calendar_weekly.class.php',
[1056]122      'view_calendar'  => false,
[28926]123      'classname'      => 'CalendarWeekly',
[1053]124      ),
125    );
126
[1086]127  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
[1057]128
129  // Retrieve calendar field
[2502]130  isset( $fields[ $page['chronology_field'] ] ) or fatal_error('bad chronology field');
[1059]131
[1057]132  // Retrieve style
[1090]133  if ( !isset( $styles[ $page['chronology_style'] ] ) )
[1086]134  {
[1090]135    $page['chronology_style'] = 'monthly';
[1086]136  }
[1090]137  $cal_style = $page['chronology_style'];
[28926]138  $classname = $styles[$cal_style]['classname'];
139
[1057]140  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
[28926]141  $calendar = new $classname();
[1057]142
143  // Retrieve view
[1086]144
[1090]145  if ( !isset($page['chronology_view']) or
146       !in_array( $page['chronology_view'], $views ) )
[1053]147  {
[1090]148    $page['chronology_view'] = CAL_VIEW_LIST;
[1053]149  }
150
[1090]151  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
[1086]152        !$styles[$cal_style]['view_calendar'] )
153  {
154
[1090]155    $page['chronology_view'] = CAL_VIEW_LIST;
[1086]156  }
157
[1053]158  // perform a sanity check on $requested
[1086]159  if (!isset($page['chronology_date']))
[1053]160  {
[1086]161    $page['chronology_date'] = array();
[1053]162  }
[1086]163  while ( count($page['chronology_date']) > 3)
164  {
165    array_pop($page['chronology_date']);
166  }
[1053]167
168  $any_count = 0;
[1086]169  for ($i = 0; $i < count($page['chronology_date']); $i++)
[1053]170  {
[1086]171    if ($page['chronology_date'][$i] == 'any')
[1053]172    {
[1090]173      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
[1053]174      {// we dont allow any in calendar view
[1086]175        while ($i < count($page['chronology_date']))
[1053]176        {
[1086]177          array_pop($page['chronology_date']);
[1053]178        }
179        break;
180      }
181      $any_count++;
182    }
[1086]183    elseif ($page['chronology_date'][$i] == '')
[1053]184    {
[1086]185      while ($i < count($page['chronology_date']))
[1053]186      {
[1086]187        array_pop($page['chronology_date']);
[1053]188      }
189    }
[1059]190    else
191    {
[1086]192      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
[1059]193    }
[1053]194  }
195  if ($any_count == 3)
196  {
[1086]197    array_pop($page['chronology_date']);
[1053]198  }
[1059]199
[1086]200  $calendar->initialize($inner_sql);
[1053]201
[1059]202  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
203
[1057]204  $must_show_list = true; // true until calendar generates its own display
[1690]205  if (script_basename() != 'picture') // basename without file extention
[1053]206  {
[1086]207    if ($calendar->generate_category_content())
[1053]208    {
[1820]209      $page['items'] = array();
[1053]210      $must_show_list = false;
211    }
[2502]212
[2231]213    $page['comment'] = '';
214    $template->assign('FILE_CHRONOLOGY_VIEW', 'month_calendar.tpl');
[1059]215
[1057]216    foreach ($styles as $style => $style_data)
[1053]217    {
[1086]218      foreach ($views as $view)
[1053]219      {
[1057]220        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
[1053]221        {
[2231]222          $selected = false;
[1086]223
224          if ($style!=$cal_style)
[1057]225          {
[1086]226            $chronology_date = array();
227            if ( isset($page['chronology_date'][0]) )
[1057]228            {
[25018]229              $chronology_date[] = $page['chronology_date'][0];
[1057]230            }
231          }
232          else
233          {
[1086]234            $chronology_date = $page['chronology_date'];
[1057]235          }
[1086]236          $url = duplicate_index_url(
237              array(
[1090]238                'chronology_style' => $style,
239                'chronology_view' => $view,
[1086]240                'chronology_date' => $chronology_date,
241                )
242             );
243
[1090]244          if ($style==$cal_style and $view==$page['chronology_view'] )
[1086]245          {
[2231]246            $selected = true;
[1086]247          }
248
[2231]249          $template->append(
250            'chronology_views',
[1057]251            array(
252              'VALUE' => $url,
[1090]253              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
[1057]254              'SELECTED' => $selected,
255              )
256            );
[1053]257        }
258      }
259    }
[1086]260    $url = duplicate_index_url(
[1090]261              array(), array('start', 'chronology_date')
[1086]262            );
263    $calendar_title = '<a href="'.$url.'">'
[1090]264        .$fields[$page['chronology_field']]['label'].'</a>';
[1062]265    $calendar_title.= $calendar->get_display_name();
[2231]266    $template->assign('chronology',
[1820]267        array(
268          'TITLE' => $calendar_title
269        )
270      );
[1053]271  } // end category calling
272
273  if ($must_show_list)
274  {
275    if ( isset($page['super_order_by']) )
276    {
[28491]277      $order_by = $conf['order_by'];
[1053]278    }
279    else
280    {
[1163]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      }
[1053]290      $order_by = str_replace(
291        'ORDER BY ',
[1163]292        'ORDER BY '.$calendar->date_field.$order, $conf['order_by']
[1053]293        );
[28491]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().'
[1062]310  '.$order_by;
[28491]311      $page['items'] = array_from_query($query, 'id');
312      if ( isset($cache_key) )
313        $persistent_cache->set($cache_key, $page['items']);
[1053]314    }
315  }
316  pwg_debug('end initialize_calendar');
317}
[25507]318
[1047]319?>
Note: See TracBrowser for help on using the repository browser.