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