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

Last change on this file since 2231 was 2231, checked in by rvelices, 16 years ago
  • index.tpl, menubar.tpl, mainpage_categories.tpl and month_calendar.tpl go smarty
  • better template debugging tweak (the smarty console is shown only once at the end)
  • Property svn:eol-style set to LF
  • 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-2007 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | file          : $Id: functions_calendar.inc.php 2231 2008-03-01 13:12:07Z rvelices $
7// | last update   : $Date: 2008-03-01 13:12:07 +0000 (Sat, 01 Mar 2008) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 2231 $
10// +-----------------------------------------------------------------------+
11// | This program is free software; you can redistribute it and/or modify  |
12// | it under the terms of the GNU General Public License as published by  |
13// | the Free Software Foundation                                          |
14// |                                                                       |
15// | This program is distributed in the hope that it will be useful, but   |
16// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
17// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
18// | General Public License for more details.                              |
19// |                                                                       |
20// | You should have received a copy of the GNU General Public License     |
21// | along with this program; if not, write to the Free Software           |
22// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
23// | USA.                                                                  |
24// +-----------------------------------------------------------------------+
25
26define('CAL_VIEW_LIST',     'list');
27define('CAL_VIEW_CALENDAR', 'calendar');
28
29function initialize_calendar()
30{
31  global $page, $conf, $user, $template, $filter;
32
33//------------------ initialize the condition on items to take into account ---
34  $inner_sql = ' FROM ' . IMAGES_TABLE;
35
36  if ($page['section']=='categories')
37  { // we will regenerate the items by including subcats elements
38    $page['items'] = array();
39    $inner_sql .= '
40INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
41
42    if ( isset($page['category']) )
43    {
44      $sub_ids = array_diff(
45        get_subcat_ids(array($page['category']['id'])),
46        explode(',', $user['forbidden_categories'])
47        );
48
49      if (empty($sub_ids))
50      {
51        return; // nothing to do
52      }
53      $inner_sql .= '
54WHERE category_id IN ('.implode(',',$sub_ids).')';
55      $inner_sql .= '
56    '.get_sql_condition_FandF
57      (
58        array
59          (
60            'visible_images' => 'id'
61          ),
62        'AND', false
63      );
64    }
65    else
66    {
67      $inner_sql .= '
68    '.get_sql_condition_FandF
69      (
70        array
71          (
72            'forbidden_categories' => 'category_id',
73            'visible_categories' => 'category_id',
74            'visible_images' => 'id'
75          ),
76        'WHERE', true
77      );
78    }
79  }
80  else
81  {
82    if ( empty($page['items']) )
83    {
84      return; // nothing to do
85    }
86    $inner_sql .= '
87WHERE id IN (' . implode(',',$page['items']) .')';
88  }
89
90//-------------------------------------- initialize the calendar parameters ---
91  pwg_debug('start initialize_calendar');
92
93  $fields = array(
94    // Created
95    'created' => array(
96      'label'          => l10n('Creation date'),
97      ),
98    // Posted
99    'posted' => array(
100      'label'          => l10n('Post date'),
101      ),
102    );
103
104  $styles = array(
105    // Monthly style
106    'monthly' => array(
107      'include'        => 'calendar_monthly.class.php',
108      'view_calendar'  => true,
109      ),
110    // Weekly style
111    'weekly' => array(
112      'include'        => 'calendar_weekly.class.php',
113      'view_calendar'  => false,
114      ),
115    );
116
117  $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR);
118
119  // Retrieve calendar field
120  if ( !isset( $fields[ $page['chronology_field'] ] ) )
121  {
122    die('bad chronology field');
123  }
124
125  // Retrieve style
126  if ( !isset( $styles[ $page['chronology_style'] ] ) )
127  {
128    $page['chronology_style'] = 'monthly';
129  }
130  $cal_style = $page['chronology_style'];
131  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
132  $calendar = new Calendar();
133
134  // Retrieve view
135
136  if ( !isset($page['chronology_view']) or
137       !in_array( $page['chronology_view'], $views ) )
138  {
139    $page['chronology_view'] = CAL_VIEW_LIST;
140  }
141
142  if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and
143        !$styles[$cal_style]['view_calendar'] )
144  {
145
146    $page['chronology_view'] = CAL_VIEW_LIST;
147  }
148
149  // perform a sanity check on $requested
150  if (!isset($page['chronology_date']))
151  {
152    $page['chronology_date'] = array();
153  }
154  while ( count($page['chronology_date']) > 3)
155  {
156    array_pop($page['chronology_date']);
157  }
158
159  $any_count = 0;
160  for ($i = 0; $i < count($page['chronology_date']); $i++)
161  {
162    if ($page['chronology_date'][$i] == 'any')
163    {
164      if ($page['chronology_view'] == CAL_VIEW_CALENDAR)
165      {// we dont allow any in calendar view
166        while ($i < count($page['chronology_date']))
167        {
168          array_pop($page['chronology_date']);
169        }
170        break;
171      }
172      $any_count++;
173    }
174    elseif ($page['chronology_date'][$i] == '')
175    {
176      while ($i < count($page['chronology_date']))
177      {
178        array_pop($page['chronology_date']);
179      }
180    }
181    else
182    {
183      $page['chronology_date'][$i] = (int)$page['chronology_date'][$i];
184    }
185  }
186  if ($any_count == 3)
187  {
188    array_pop($page['chronology_date']);
189  }
190
191  $calendar->initialize($inner_sql);
192
193  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
194
195  $must_show_list = true; // true until calendar generates its own display
196  if (script_basename() != 'picture') // basename without file extention
197  {
198    if ($calendar->generate_category_content())
199    {
200      $page['items'] = array();
201      $must_show_list = false;
202    }
203   
204    $page['comment'] = '';
205    $template->assign('FILE_CHRONOLOGY_VIEW', 'month_calendar.tpl');
206
207    foreach ($styles as $style => $style_data)
208    {
209      foreach ($views as $view)
210      {
211        if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR)
212        {
213          $selected = false;
214
215          if ($style!=$cal_style)
216          {
217            $chronology_date = array();
218            if ( isset($page['chronology_date'][0]) )
219            {
220              array_push($chronology_date, $page['chronology_date'][0]);
221            }
222          }
223          else
224          {
225            $chronology_date = $page['chronology_date'];
226          }
227          $url = duplicate_index_url(
228              array(
229                'chronology_style' => $style,
230                'chronology_view' => $view,
231                'chronology_date' => $chronology_date,
232                )
233             );
234
235          if ($style==$cal_style and $view==$page['chronology_view'] )
236          {
237            $selected = true;
238          }
239
240          $template->append(
241            'chronology_views',
242            array(
243              'VALUE' => $url,
244              'CONTENT' => l10n('chronology_'.$style.'_'.$view),
245              'SELECTED' => $selected,
246              )
247            );
248        }
249      }
250    }
251    $url = duplicate_index_url(
252              array(), array('start', 'chronology_date')
253            );
254    $calendar_title = '<a href="'.$url.'">'
255        .$fields[$page['chronology_field']]['label'].'</a>';
256    $calendar_title.= $calendar->get_display_name();
257    $template->assign('chronology',
258        array(
259          'TITLE' => $calendar_title
260        )
261      );
262  } // end category calling
263
264  if ($must_show_list)
265  {
266    $query = 'SELECT DISTINCT(id)';
267    $query .= $calendar->inner_sql.'
268  '.$calendar->get_date_where();
269    if ( isset($page['super_order_by']) )
270    {
271      $query .= '
272  '.$conf['order_by'];
273    }
274    else
275    {
276      if ( count($page['chronology_date'])==0
277           or in_array('any', $page['chronology_date']) )
278      {// selected period is very big so we show newest first
279        $order = ' DESC, ';
280      }
281      else
282      {// selected period is small (month,week) so we show oldest first
283        $order = ' ASC, ';
284      }
285      $order_by = str_replace(
286        'ORDER BY ',
287        'ORDER BY '.$calendar->date_field.$order, $conf['order_by']
288        );
289      $query .= '
290  '.$order_by;
291    }
292    $page['items']              = array_from_query($query, 'id');
293  }
294  pwg_debug('end initialize_calendar');
295}
296
297?>
Note: See TracBrowser for help on using the repository browser.