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

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

merge r1162 from branch-1_6 into trunk

fix: calendar prev/next links not working properly when 'any' (All/Tout)
was selected

fix: calendar 'any' (All/Tout) not shown on for the day selection
(last calendar level) - it was meaningless

fix: calendar image ordering is by date descending for large periods (no year
selected) or ascending for small periods (week,month...)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 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-04-14 01:42:24 +0000 (Fri, 14 Apr 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1163 $
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['items'] = array();
41    $inner_sql .= '
42INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
43
44    if (isset($page['category']) and is_numeric($page['category']))
45    {
46      $sub_ids = array_diff(
47        get_subcat_ids(array($page['category'])),
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  $fields = array(
78    // Created
79    'created' => array(
80      'label'          => l10n('Creation date'),
81      ),
82    // Posted
83    'posted' => array(
84      'label'          => l10n('Post date'),
85      ),
86    );
87
88  $styles = array(
89    // Monthly style
90    'monthly' => array(
91      'include'        => 'calendar_monthly.class.php',
92      'view_calendar'  => true,
93      ),
94    // Weekly style
95    'weekly' => array(
96      'include'        => 'calendar_weekly.class.php',
97      'view_calendar'  => false,
98      ),
99    );
100
101  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
102
103  // Retrieve calendar field
104  if ( !isset( $fields[ $page['chronology_field'] ] ) )
105  {
106    die('bad chronology field');
107  }
108
109  // Retrieve style
110  if ( !isset( $styles[ $page['chronology_style'] ] ) )
111  {
112    $page['chronology_style'] = 'monthly';
113  }
114  $cal_style = $page['chronology_style'];
115  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
116  $calendar = new Calendar();
117
118  // Retrieve view
119
120  if ( !isset($page['chronology_view']) or
121       !in_array( $page['chronology_view'], $views ) )
122  {
123    $page['chronology_view'] = CAL_VIEW_LIST;
124  }
125
126  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
127        !$styles[$cal_style]['view_calendar'] )
128  {
129
130    $page['chronology_view'] = CAL_VIEW_LIST;
131  }
132
133  // perform a sanity check on $requested
134  if (!isset($page['chronology_date']))
135  {
136    $page['chronology_date'] = array();
137  }
138  while ( count($page['chronology_date']) > 3)
139  {
140    array_pop($page['chronology_date']);
141  }
142
143  $any_count = 0;
144  for ($i = 0; $i < count($page['chronology_date']); $i++)
145  {
146    if ($page['chronology_date'][$i] == 'any')
147    {
148      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
149      {// we dont allow any in calendar view
150        while ($i < count($page['chronology_date']))
151        {
152          array_pop($page['chronology_date']);
153        }
154        break;
155      }
156      $any_count++;
157    }
158    elseif ($page['chronology_date'][$i] == '')
159    {
160      while ($i < count($page['chronology_date']))
161      {
162        array_pop($page['chronology_date']);
163      }
164    }
165    else
166    {
167      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
168    }
169  }
170  if ($any_count == 3)
171  {
172    array_pop($page['chronology_date']);
173  }
174
175  $calendar->initialize($inner_sql);
176
177  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
178
179  $must_show_list = true; // true until calendar generates its own display
180  if (basename($_SERVER['SCRIPT_FILENAME']) != 'picture.php')
181  {
182    $template->assign_block_vars('calendar', array());
183
184    if ($calendar->generate_category_content())
185    {
186      unset(
187        $page['thumbnails_include'],
188        $page['items']
189        );
190
191      $must_show_list = false;
192    }
193
194    $template->assign_block_vars( 'calendar.views', array() );
195    foreach ($styles as $style => $style_data)
196    {
197      foreach ($views as $view)
198      {
199        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
200        {
201          $selected = '';
202
203          if ($style!=$cal_style)
204          {
205            $chronology_date = array();
206            if ( isset($page['chronology_date'][0]) )
207            {
208              array_push($chronology_date, $page['chronology_date'][0]);
209            }
210          }
211          else
212          {
213            $chronology_date = $page['chronology_date'];
214          }
215          $url = duplicate_index_url(
216              array(
217                'chronology_style' => $style,
218                'chronology_view' => $view,
219                'chronology_date' => $chronology_date,
220                )
221             );
222
223          if ($style==$cal_style and $view==$page['chronology_view'] )
224          {
225            $selected = 'SELECTED';
226          }
227
228          $template->assign_block_vars(
229            'calendar.views.view',
230            array(
231              'VALUE' => $url,
232              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
233              'SELECTED' => $selected,
234              )
235            );
236        }
237      }
238    }
239    $url = duplicate_index_url(
240              array(), array('start', 'chronology_date')
241            );
242    $calendar_title = '<a href="'.$url.'">'
243        .$fields[$page['chronology_field']]['label'].'</a>';
244    $calendar_title.= $calendar->get_display_name();
245    //this should be an assign_block_vars, but I need to assign 'calendar'
246    //above and at that point I don't have the title yet.
247    $template->_tpldata['calendar.'][0]['TITLE'] = $calendar_title;
248  } // end category calling
249
250  if ($must_show_list)
251  {
252    $query = 'SELECT DISTINCT(id)';
253    $query .= $calendar->inner_sql.'
254  '.$calendar->get_date_where();
255    if ( isset($page['super_order_by']) )
256    {
257      $query .= '
258  '.$conf['order_by'];
259    }
260    else
261    {
262      if ( count($page['chronology_date'])==0
263           or in_array('any', $page['chronology_date']) )
264      {// selected period is very big so we show newest first
265        $order = ' DESC, ';
266      }
267      else
268      {// selected period is small (month,week) so we show oldest first
269        $order = ' ASC, ';
270      }
271      $order_by = str_replace(
272        'ORDER BY ',
273        'ORDER BY '.$calendar->date_field.$order, $conf['order_by']
274        );
275      $query .= '
276  '.$order_by;
277    }
278
279    $page['items']              = array_from_query($query, 'id');
280    $page['thumbnails_include'] = 'include/category_default.inc.php';
281  }
282  pwg_debug('end initialize_calendar');
283}
284
285?>
Note: See TracBrowser for help on using the repository browser.