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

Last change on this file since 1057 was 1057, checked in by rvelices, 18 years ago

calendar: added posted/created chronology

calendar: added a where are we bar (like: created/2005/august)

calendar: possibility to hide the All/Any buttons ($confcalendar_show_any)

calendar: possibility to display a single navigation bar instead of
several navigation bars ($confcalendar_multi_bar)

calendar: tried to simplify code and improve readability
(still requires a review)

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | branch        : BSF (Best So Far)
7// | file          : $RCSfile$
8// | last update   : $Date: 2006-01-27 02:11:43 +0100 (ven, 27 jan 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1014 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27define('CAL_VIEW_LIST',     'l');
28define('CAL_VIEW_CALENDAR', 'c');
29
30function get_calendar_parameter($options, &$parameters )
31{
32  if ( count($parameters) and isset($options[$parameters[0]]) )
33  {
34    return array_shift($parameters);
35  }
36  else
37  {
38    foreach ($options as $option => $data)
39    {
40       if ( empty( $data['default_link'] ) )
41       {
42         break;
43       }
44    }
45    return $option;
46  }
47}
48
49function initialize_calendar()
50{
51  global $page, $conf, $user, $template;
52
53//------------------ initialize the condition on items to take into account ---
54  $inner_sql = ' FROM ' . IMAGES_TABLE;
55 
56  if (!isset($page['cat']) or is_numeric($page['cat']))
57  { // we will regenerate the items by including subcats elements
58    $page['cat_nb_images'] = 0;
59    $page['items'] = array();
60    $inner_sql .= '
61INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
62   
63    if (isset($page['cat']) and is_numeric($page['cat']))
64    {
65      $sub_ids = array_diff(
66        get_subcat_ids(array($page['cat'])),
67        explode(',', $user['forbidden_categories'])
68        );
69     
70      if (empty($sub_ids))
71      {
72        return; // nothing to do
73      }
74      $inner_sql .= '
75WHERE category_id IN ('.implode(',',$sub_ids).')';
76    }
77    else
78    {
79      $inner_sql .= '
80WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
81    }
82  }
83  else
84  {
85    if ( empty($page['items']) )
86    {
87      return; // nothing to do
88    }
89    $inner_sql .= '
90WHERE id IN (' . implode(',',$page['items']) .')';
91  }
92
93//-------------------------------------- initialize the calendar parameters ---
94  pwg_debug('start initialize_calendar');
95  // the parameters look like (FIELD)?(STYLE)?(VIEW)?(DATE COMPONENTS)?
96  // FIELD = (created-|posted-)
97  // STYLE = (m-|w-)
98  // VIEW  = (l-|c-)
99  // DATE COMPONENTS= YEAR(-MONTH/WEEK)?(-DAY)?
100
101  $fields = array(
102    // Created
103    'created' => array(
104       // TODO change next line when calendar_datefield disapears
105      'default_link'   => ( $conf['calendar_datefield']=='date_creation' ? '' : 'created-' ),
106      'label'          => l10n('Creation date'),
107      'db_field'       => 'date_creation',
108      ),
109    // Posted
110    'posted' => array(
111      // TODO change next line when calendar_datefield disapears
112      'default_link'   => ( $conf['calendar_datefield']=='date_available' ? '' : 'posted-' ),
113      'label'          => l10n('Availability date'),
114      'db_field'       => 'date_available',
115      ),
116    );
117
118  $styles = array(
119    // Monthly style
120    'monthly' => array(
121      'default_link'   => '',
122      'label'           => l10n('Monthly'),
123      'include'        => 'calendar_monthly.class.php',
124      'view_calendar'  => true,
125      ),
126    // Weekly style   
127    'weekly' => array(
128      'default_link'   => 'weekly-',
129      'label'           => l10n('Weekly'),
130      'include'        => 'calendar_weekly.class.php',
131      'view_calendar'  => false,
132      ),
133    );
134
135  $views = array(
136    // list view
137    CAL_VIEW_LIST => array(
138      'default_link'   => '',
139      'label' => l10n('List')
140      ),
141    // calendar view
142    CAL_VIEW_CALENDAR => array(
143      'default_link'   => CAL_VIEW_CALENDAR.'-',
144      'label' => l10n('calendar')
145      ),
146    );
147
148  $requested = explode('-', $_GET['calendar']);
149 
150  // Retrieve calendar field
151  $cal_field = get_calendar_parameter($fields, $requested);
152 
153  // Retrieve style
154  $cal_style = get_calendar_parameter($styles, $requested);
155  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
156  $calendar = new Calendar();
157
158  // Retrieve view
159  $cal_view = get_calendar_parameter($views, $requested);
160  if ( CAL_VIEW_CALENDAR==$cal_view and !$styles[$cal_style]['view_calendar'] )
161  {
162    $cal_view=CAL_VIEW_LIST;
163  }
164
165  // perform a sanity check on $requested
166  while (count($requested) > 3)
167  {
168    array_pop($requested);
169  }
170
171  $any_count = 0;
172  for ($i = 0; $i < count($requested); $i++)
173  {
174    if ($requested[$i] == 'any')
175    {
176      if ($cal_view == CAL_VIEW_CALENDAR)
177      {// we dont allow any in calendar view
178        while ($i < count($requested))
179        {
180          array_pop($requested);
181        }
182        break;
183      }
184      $any_count++;
185    }
186    elseif ($requested[$i] == '')
187    {
188      while ($i < count($requested))
189      {
190        array_pop($requested);
191      }
192    }
193  }
194  if ($any_count == 3)
195  {
196    array_pop($requested);
197  }
198 
199  $calendar->initialize($fields[$cal_field]['db_field'], $inner_sql, $requested);
200 
201  //echo ('<pre>'. var_export($fields, true) . '</pre>');
202
203  $url_base =
204    PHPWG_ROOT_PATH.'category.php'
205    .get_query_string_diff(array('start', 'calendar'))
206    .(empty($url_base) ? '?' : '&')
207    .'calendar='.$cal_field.'-'
208    ;
209
210  $must_show_list = true; // true until calendar generates its own display
211  if (basename($_SERVER["PHP_SELF"]) == 'category.php')
212  {
213    $template->assign_block_vars('calendar', array());
214
215    if ($calendar->generate_category_content(
216          $url_base.$cal_style.'-'.$cal_view.'-',
217          $cal_view
218          )
219       )
220    {
221      unset(
222        $page['thumbnails_include'],
223        $page['items'],
224        $page['cat_nb_images']
225        );
226     
227      $must_show_list = false;
228    }
229   
230    $template->assign_block_vars( 'calendar.views', array() );
231    foreach ($styles as $style => $style_data)
232    {
233      foreach ($views as $view => $view_data)
234      {
235        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
236        {
237          $selected = '';
238          $url = $url_base.$style.'-'.$view;
239          if ($style==$cal_style)
240          {
241            $url .= '-'.implode('-', $calendar->date_components);
242            if ( $view==$cal_view )
243            {
244              $selected = 'SELECTED';
245            }
246          }
247          else
248          {
249            if (isset($calendar->date_components[0]))
250            {
251              $url .= '-' . $calendar->date_components[0];
252            }
253          }
254          $template->assign_block_vars(
255            'calendar.views.view',
256            array(
257              'VALUE' => $url,
258              'CONTENT' => $style_data['label'].' ('.$view_data['label'].')',
259              'SELECTED' => $selected,
260              )
261            );
262        }
263      }
264    }
265  } // end category calling
266
267  $calendar_title = 
268      '<a href="'.$url_base.$cal_style.'-'.$cal_view.'">'
269      .$fields[$cal_field]['label'].'</a>';
270  $calendar_title.= $calendar->get_display_name();
271  $template->assign_block_vars(
272    'calendar',
273    array(
274      'TITLE' => $calendar_title,
275      )
276    );
277 
278  if ($must_show_list)
279  {
280    $query = 'SELECT DISTINCT(id)';
281    $query .= $calendar->inner_sql;
282    $query .= $calendar->get_date_where();
283    if ( isset($page['super_order_by']) )
284    {
285      $query .= '
286  '.$conf['order_by'];
287    }
288    else
289    {
290      $order_by = str_replace(
291        'ORDER BY ',
292        'ORDER BY '.$calendar->date_field.',', $conf['order_by']
293        );
294      $query .= $order_by;
295    }
296
297    $page['items']              = array_from_query($query, 'id');
298    $page['cat_nb_images']      = count($page['items']);
299    $page['thumbnails_include'] = 'include/category_default.inc.php';
300  }
301  pwg_debug('end initialize_calendar');
302}
303
304?>
Note: See TracBrowser for help on using the repository browser.