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

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

URL rewriting: fix some old links, calendar simplification and prepare code
for urls without ? (added functions get_root_url and add_url_param)

  • Property svn:eol-style set to native
File size: 8.1 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',     'list');
28define('CAL_VIEW_CALENDAR', 'calendar');
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 ($page['section']=='categories' or
38      ( isset($page['category']) and is_numeric($page['category']) ) )
39  { // we will regenerate the items by including subcats elements
40    $page['cat_nb_images'] = 0;
41    $page['items'] = array();
42    $inner_sql .= '
43INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
44
45    if (isset($page['category']) and is_numeric($page['category']))
46    {
47      $sub_ids = array_diff(
48        get_subcat_ids(array($page['category'])),
49        explode(',', $user['forbidden_categories'])
50        );
51
52      if (empty($sub_ids))
53      {
54        return; // nothing to do
55      }
56      $inner_sql .= '
57WHERE category_id IN ('.implode(',',$sub_ids).')';
58    }
59    else
60    {
61      $inner_sql .= '
62WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
63    }
64  }
65  else
66  {
67    if ( empty($page['items']) )
68    {
69      return; // nothing to do
70    }
71    $inner_sql .= '
72WHERE id IN (' . implode(',',$page['items']) .')';
73  }
74
75//-------------------------------------- initialize the calendar parameters ---
76  pwg_debug('start initialize_calendar');
77
78  $fields = array(
79    // Created
80    'created' => array(
81      'label'          => l10n('Creation date'),
82      ),
83    // Posted
84    'posted' => array(
85      'label'          => l10n('Post date'),
86      ),
87    );
88
89  $styles = array(
90    // Monthly style
91    'monthly' => array(
92      'include'        => 'calendar_monthly.class.php',
93      'view_calendar'  => true,
94      ),
95    // Weekly style
96    'weekly' => array(
97      'include'        => 'calendar_weekly.class.php',
98      'view_calendar'  => false,
99      ),
100    );
101
102  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
103
104  // Retrieve calendar field
105  if ( !isset( $fields[ $page['chronology_field'] ] ) )
106  {
107    die('bad chronology field');
108  }
109
110  // Retrieve style
111  if ( !isset( $styles[ $page['chronology_style'] ] ) )
112  {
113    $page['chronology_style'] = 'monthly';
114  }
115  $cal_style = $page['chronology_style'];
116  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
117  $calendar = new Calendar();
118
119  // Retrieve view
120
121  if ( !isset($page['chronology_view']) or
122       !in_array( $page['chronology_view'], $views ) )
123  {
124    $page['chronology_view'] = CAL_VIEW_LIST;
125  }
126
127  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
128        !$styles[$cal_style]['view_calendar'] )
129  {
130
131    $page['chronology_view'] = CAL_VIEW_LIST;
132  }
133
134  // perform a sanity check on $requested
135  if (!isset($page['chronology_date']))
136  {
137    $page['chronology_date'] = array();
138  }
139  while ( count($page['chronology_date']) > 3)
140  {
141    array_pop($page['chronology_date']);
142  }
143
144  $any_count = 0;
145  for ($i = 0; $i < count($page['chronology_date']); $i++)
146  {
147    if ($page['chronology_date'][$i] == 'any')
148    {
149      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
150      {// we dont allow any in calendar view
151        while ($i < count($page['chronology_date']))
152        {
153          array_pop($page['chronology_date']);
154        }
155        break;
156      }
157      $any_count++;
158    }
159    elseif ($page['chronology_date'][$i] == '')
160    {
161      while ($i < count($page['chronology_date']))
162      {
163        array_pop($page['chronology_date']);
164      }
165    }
166    else
167    {
168      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
169    }
170  }
171  if ($any_count == 3)
172  {
173    array_pop($page['chronology_date']);
174  }
175
176  $calendar->initialize($inner_sql);
177
178  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
179
180  $must_show_list = true; // true until calendar generates its own display
181  if (basename($_SERVER['SCRIPT_NAME']) == 'category.php')
182  {
183    $template->assign_block_vars('calendar', array());
184
185    if ($calendar->generate_category_content())
186    {
187      unset(
188        $page['thumbnails_include'],
189        $page['items'],
190        $page['cat_nb_images']
191        );
192
193      $must_show_list = false;
194    }
195
196    $template->assign_block_vars( 'calendar.views', array() );
197    foreach ($styles as $style => $style_data)
198    {
199      foreach ($views as $view)
200      {
201        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
202        {
203          $selected = '';
204
205          if ($style!=$cal_style)
206          {
207            $chronology_date = array();
208            if ( isset($page['chronology_date'][0]) )
209            {
210              array_push($chronology_date, $page['chronology_date'][0]);
211            }
212          }
213          else
214          {
215            $chronology_date = $page['chronology_date'];
216          }
217          $url = duplicate_index_url(
218              array(
219                'chronology_style' => $style,
220                'chronology_view' => $view,
221                'chronology_date' => $chronology_date,
222                )
223             );
224
225          if ($style==$cal_style and $view==$page['chronology_view'] )
226          {
227            $selected = 'SELECTED';
228          }
229
230          $template->assign_block_vars(
231            'calendar.views.view',
232            array(
233              'VALUE' => $url,
234              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
235              'SELECTED' => $selected,
236              )
237            );
238        }
239      }
240    }
241    $url = duplicate_index_url(
242              array(), array('start', 'chronology_date')
243            );
244    $calendar_title = '<a href="'.$url.'">'
245        .$fields[$page['chronology_field']]['label'].'</a>';
246    $calendar_title.= $calendar->get_display_name();
247    //this should be an assign_block_vars, but I need to assign 'calendar'
248    //above and at that point I don't have the title yet.
249    $template->_tpldata['calendar.'][0]['TITLE'] = $calendar_title;
250  } // end category calling
251
252  if ($must_show_list)
253  {
254    $query = 'SELECT DISTINCT(id)';
255    $query .= $calendar->inner_sql.'
256  '.$calendar->get_date_where();
257    if ( isset($page['super_order_by']) )
258    {
259      $query .= '
260  '.$conf['order_by'];
261    }
262    else
263    {
264      $order_by = str_replace(
265        'ORDER BY ',
266        'ORDER BY '.$calendar->date_field.' DESC,', $conf['order_by']
267        );
268      $query .= '
269  '.$order_by;
270    }
271
272    $page['items']              = array_from_query($query, 'id');
273    $page['cat_nb_images']      = count($page['items']);
274    $page['thumbnails_include'] = 'include/category_default.inc.php';
275  }
276  pwg_debug('end initialize_calendar');
277}
278
279?>
Note: See TracBrowser for help on using the repository browser.