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

Last change on this file since 1053 was 1053, checked in by plg, 18 years ago

modification: DAY() MySQL function replaced by DAYOFMONTH() to improve
backward compatibility (this function was added in MySQL 4.1)

bug fixed: with chronology mode, PWG displays thumbnails on main page if
even if no category (which will soon be called "section") is set. This was
producing warnings on category.php from include/category_default.inc.php.

refactoring: on include/calendar_base.class.php and
include/functions_calendar.inc.php. Unix file format, coding guidelines,
etc. While trying to understand the code, I've made some presentation
modification to clarify variable names and so on.

File size: 7.9 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 initialize_calendar()
31{
32  global $page, $conf, $user, $template;
33
34//------------------ initialize the condition on items to take into account ---
35  $inner_sql = ' FROM ' . IMAGES_TABLE;
36 
37  if (!isset($page['cat']) or is_numeric($page['cat']))
38  { // we will regenerate the items by including subcats elements
39    $page['cat_nb_images'] = 0;
40    $page['items'] = array();
41    $inner_sql .= '
42INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
43   
44    if (isset($page['cat']) and is_numeric($page['cat']))
45    {
46      $sub_ids = array_diff(
47        get_subcat_ids(array($page['cat'])),
48        explode(',', $user['forbidden_categories'])
49        );
50     
51      if (empty($sub_ids))
52      {
53        return; // nothing to do
54      }
55      $inner_sql .= '
56WHERE category_id IN ('.implode(',',$sub_ids).')';
57    }
58    else
59    {
60      $inner_sql .= '
61WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
62    }
63  }
64  else
65  {
66    if ( empty($page['items']) )
67    {
68      return; // nothing to do
69    }
70    $inner_sql .= '
71WHERE id IN (' . implode(',',$page['items']) .')';
72  }
73
74//-------------------------------------- initialize the calendar parameters ---
75  pwg_debug('start initialize_calendar');
76 
77  $cal_styles = array(
78    // Weekly style
79    array(
80      'link'           => 'm',
81      'default_link'   => '',
82      'name'           => l10n('Monthly'),
83      'include'        => 'calendar_monthly.class.php',
84      'view_calendar'  => true,
85      ),
86    // Monthly style
87    array(
88      'link'           => 'w',
89      'default_link'   => 'w-',
90      'name'           => l10n('Weekly'),
91      'include'        => 'calendar_weekly.class.php',
92      ),
93    );
94
95  $requested = explode('-', $_GET['calendar']);
96  $calendar = null;
97  foreach ($cal_styles as $cal_style)
98  {
99    if ($requested[0] == $cal_style['link'])
100    {
101      include(PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
102      $calendar = new Calendar();
103      array_shift($requested);
104      break;
105    }
106  }
107 
108  if (!isset($calendar))
109  {
110    foreach($cal_styles as $cal_style)
111    {
112      if ('' == $cal_style['default_link'])
113      {
114        break;
115      }
116    }
117    include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
118    $calendar = new Calendar();
119  }
120
121  $view_type = CAL_VIEW_LIST;
122  if ($requested[0] == CAL_VIEW_LIST)
123  {
124    array_shift($requested);
125  }
126  elseif ($requested[0] == CAL_VIEW_CALENDAR)
127  {
128    if ($cal_style['view_calendar'])
129    {
130      $view_type = CAL_VIEW_CALENDAR;
131    }
132    array_shift($requested);
133  }
134  // perform a sanity check on $requested
135  while (count($requested) > 3)
136  {
137    array_pop($requested);
138  }
139
140  $any_count = 0;
141  for ($i = 0; $i < count($requested); $i++)
142  {
143    if ($requested[$i] == 'any')
144    {
145      if ($view_type == CAL_VIEW_CALENDAR)
146      {// we dont allow any in calendar view
147        while ($i < count($requested))
148        {
149          array_pop($requested);
150        }
151        break;
152      }
153      $any_count++;
154    }
155    elseif ($requested[$i] == '')
156    {
157      while ($i < count($requested))
158      {
159        array_pop($requested);
160      }
161    }
162  }
163  if ($any_count == 3)
164  {
165    array_pop($requested);
166  }
167
168  $calendar->initialize($conf['calendar_datefield'], $inner_sql);
169  //echo ('<pre>'. var_export($requested, true) . '</pre>');
170  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
171
172  // TODO: what makes the list view required?
173  $must_show_list = true;
174 
175  if (basename($_SERVER["PHP_SELF"]) == 'category.php')
176  {
177    $template->assign_block_vars('calendar', array());
178
179    $url_base =
180      PHPWG_ROOT_PATH.'category.php'
181      .get_query_string_diff(array('start', 'calendar'))
182      .(empty($url_base) ? '?' : '&')
183      .'calendar='
184      ;
185
186    if ($calendar->generate_category_content(
187          $url_base.$cal_style['default_link'],
188          $view_type,
189          $requested
190          )
191       )
192    {
193      unset(
194        $page['thumbnails_include'],
195        $page['items'],
196        $page['cat_nb_images']
197        );
198     
199      $must_show_list = false;
200    }
201
202    if ($cal_style['view_calendar'])
203    { // Build bar for views (List/Calendar)
204      $views = array(
205        // list view
206        array(
207          'type'  => CAL_VIEW_LIST,
208          'label' => l10n('List')
209          ),
210        // calendar view
211        array(
212          'type'  => CAL_VIEW_CALENDAR,
213          'label' => l10n('calendar')
214          ),
215        );
216     
217      $views_bar = '';
218
219      foreach ($views as $view)
220      {
221        if ($view_type != $view['type'])
222        {
223          $views_bar.=
224            '<a href="'
225            .$url_base.$cal_style['default_link'].$view['type'].'-'
226            .implode('-', $requested)
227            .'">'.$view['label'].'</a> ';
228        }
229        else
230        {
231          $views_bar.= $view['label'].' ';
232        }
233       
234        $views_bar.= ' ';
235      }
236     
237      $template->assign_block_vars(
238        'calendar.views',
239        array(
240          'BAR' => $views_bar,
241          )
242        );
243    }
244
245    // Build bar for calendar styles (Monthly, Weekly)
246    $styles_bar = '';
247    foreach ($cal_styles as $style)
248    {
249      if ($cal_style['link'] != $style['link'])
250      {
251        $url = $url_base.$style['default_link'];
252        $url .= $view_type;
253        if (isset($requested[0]))
254        {
255          $url .= '-' . $requested[0];
256        }
257        $styles_bar .= '<a href="'. $url . '">'.$style['name'].'</a> ';
258      }
259      else
260      {
261        $styles_bar .=  $style['name'].' ';
262      }
263    }
264    $template->assign_block_vars(
265      'calendar.styles',
266      array(
267        'BAR' => $styles_bar,
268        )
269      );
270  } // end category calling
271
272  if ($must_show_list)
273  {
274    $query = 'SELECT DISTINCT(id)';
275    $query .= $calendar->inner_sql;
276    $query .= $calendar->get_date_where($requested);
277    if ( isset($page['super_order_by']) )
278    {
279      $query .= '
280  '.$conf['order_by'];
281    }
282    else
283    {
284      $order_by = str_replace(
285        'ORDER BY ',
286        'ORDER BY '.$calendar->date_field.',', $conf['order_by']
287        );
288      $query .= $order_by;
289    }
290
291    $page['items']              = array_from_query($query, 'id');
292    $page['cat_nb_images']      = count($page['items']);
293    $page['thumbnails_include'] = 'include/category_default.inc.php';
294  }
295  pwg_debug('end initialize_calendar');
296}
297
298?>
Note: See TracBrowser for help on using the repository browser.