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 1861 2007-02-27 01:56:16Z rvelices $ |
---|
7 | // | last update : $Date: 2007-02-27 01:56:16 +0000 (Tue, 27 Feb 2007) $ |
---|
8 | // | last modifier : $Author: rvelices $ |
---|
9 | // | revision : $Revision: 1861 $ |
---|
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 | |
---|
26 | define('CAL_VIEW_LIST', 'list'); |
---|
27 | define('CAL_VIEW_CALENDAR', 'calendar'); |
---|
28 | |
---|
29 | function 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 .= ' |
---|
40 | INNER 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 .= ' |
---|
54 | WHERE 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 .= ' |
---|
87 | WHERE 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 | foreach ($styles as $style => $style_data) |
---|
205 | { |
---|
206 | foreach ($views as $view) |
---|
207 | { |
---|
208 | if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR) |
---|
209 | { |
---|
210 | $selected = ''; |
---|
211 | |
---|
212 | if ($style!=$cal_style) |
---|
213 | { |
---|
214 | $chronology_date = array(); |
---|
215 | if ( isset($page['chronology_date'][0]) ) |
---|
216 | { |
---|
217 | array_push($chronology_date, $page['chronology_date'][0]); |
---|
218 | } |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | $chronology_date = $page['chronology_date']; |
---|
223 | } |
---|
224 | $url = duplicate_index_url( |
---|
225 | array( |
---|
226 | 'chronology_style' => $style, |
---|
227 | 'chronology_view' => $view, |
---|
228 | 'chronology_date' => $chronology_date, |
---|
229 | ) |
---|
230 | ); |
---|
231 | |
---|
232 | if ($style==$cal_style and $view==$page['chronology_view'] ) |
---|
233 | { |
---|
234 | $selected = 'SELECTED'; |
---|
235 | } |
---|
236 | |
---|
237 | $template->assign_block_vars( |
---|
238 | 'calendar.views.view', |
---|
239 | array( |
---|
240 | 'VALUE' => $url, |
---|
241 | 'CONTENT' => l10n('chronology_'.$style.'_'.$view), |
---|
242 | 'SELECTED' => $selected, |
---|
243 | ) |
---|
244 | ); |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | $url = duplicate_index_url( |
---|
249 | array(), array('start', 'chronology_date') |
---|
250 | ); |
---|
251 | $calendar_title = '<a href="'.$url.'">' |
---|
252 | .$fields[$page['chronology_field']]['label'].'</a>'; |
---|
253 | $calendar_title.= $calendar->get_display_name(); |
---|
254 | $template->merge_block_vars('calendar', |
---|
255 | array( |
---|
256 | 'TITLE' => $calendar_title |
---|
257 | ) |
---|
258 | ); |
---|
259 | } // end category calling |
---|
260 | |
---|
261 | if ($must_show_list) |
---|
262 | { |
---|
263 | $query = 'SELECT DISTINCT(id)'; |
---|
264 | $query .= $calendar->inner_sql.' |
---|
265 | '.$calendar->get_date_where(); |
---|
266 | if ( isset($page['super_order_by']) ) |
---|
267 | { |
---|
268 | $query .= ' |
---|
269 | '.$conf['order_by']; |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | if ( count($page['chronology_date'])==0 |
---|
274 | or in_array('any', $page['chronology_date']) ) |
---|
275 | {// selected period is very big so we show newest first |
---|
276 | $order = ' DESC, '; |
---|
277 | } |
---|
278 | else |
---|
279 | {// selected period is small (month,week) so we show oldest first |
---|
280 | $order = ' ASC, '; |
---|
281 | } |
---|
282 | $order_by = str_replace( |
---|
283 | 'ORDER BY ', |
---|
284 | 'ORDER BY '.$calendar->date_field.$order, $conf['order_by'] |
---|
285 | ); |
---|
286 | $query .= ' |
---|
287 | '.$order_by; |
---|
288 | } |
---|
289 | $page['items'] = array_from_query($query, 'id'); |
---|
290 | } |
---|
291 | pwg_debug('end initialize_calendar'); |
---|
292 | } |
---|
293 | |
---|
294 | ?> |
---|