1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 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 | /** |
---|
25 | * @package functions\calendar |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * Base class for monthly and weekly calendar styles |
---|
31 | */ |
---|
32 | abstract class CalendarBase |
---|
33 | { |
---|
34 | /** db column on which this calendar works */ |
---|
35 | var $date_field; |
---|
36 | /** used for queries (INNER JOIN or normal) */ |
---|
37 | var $inner_sql; |
---|
38 | /** used to store db fields */ |
---|
39 | var $calendar_levels; |
---|
40 | |
---|
41 | /** |
---|
42 | * Generate navigation bars for category page. |
---|
43 | * |
---|
44 | * @return boolean false indicates that thumbnails where not included |
---|
45 | */ |
---|
46 | abstract function generate_category_content(); |
---|
47 | |
---|
48 | /** |
---|
49 | * Returns a sql WHERE subquery for the date field. |
---|
50 | * |
---|
51 | * @param int $max_levels (e.g. 2=only year and month) |
---|
52 | * @return string |
---|
53 | */ |
---|
54 | abstract function get_date_where($max_levels); |
---|
55 | |
---|
56 | /** |
---|
57 | * Initialize the calendar. |
---|
58 | * |
---|
59 | * @param string $inner_sql |
---|
60 | */ |
---|
61 | function initialize($inner_sql) |
---|
62 | { |
---|
63 | global $page; |
---|
64 | if ($page['chronology_field']=='posted') |
---|
65 | { |
---|
66 | $this->date_field = 'date_available'; |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | $this->date_field = 'date_creation'; |
---|
71 | } |
---|
72 | $this->inner_sql = $inner_sql; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Returns the calendar title (with HTML). |
---|
77 | * |
---|
78 | * @return string |
---|
79 | */ |
---|
80 | function get_display_name() |
---|
81 | { |
---|
82 | global $conf, $page; |
---|
83 | $res = ''; |
---|
84 | |
---|
85 | for ($i=0; $i<count($page['chronology_date']); $i++) |
---|
86 | { |
---|
87 | $res .= $conf['level_separator']; |
---|
88 | if ( isset($page['chronology_date'][$i+1]) ) |
---|
89 | { |
---|
90 | $chronology_date = array_slice($page['chronology_date'],0, $i+1); |
---|
91 | $url = duplicate_index_url( |
---|
92 | array( 'chronology_date'=>$chronology_date ), |
---|
93 | array( 'start' ) |
---|
94 | ); |
---|
95 | $res .= |
---|
96 | '<a href="'.$url.'">' |
---|
97 | .$this->get_date_component_label($i, $page['chronology_date'][$i]) |
---|
98 | .'</a>'; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | $res .= |
---|
103 | '<span class="calInHere">' |
---|
104 | .$this->get_date_component_label($i, $page['chronology_date'][$i]) |
---|
105 | .'</span>'; |
---|
106 | } |
---|
107 | } |
---|
108 | return $res; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Returns a display name for a date component optionally using labels. |
---|
113 | * |
---|
114 | * @return string |
---|
115 | */ |
---|
116 | protected function get_date_component_label($level, $date_component) |
---|
117 | { |
---|
118 | $label = $date_component; |
---|
119 | if (isset($this->calendar_levels[$level]['labels'][$date_component])) |
---|
120 | { |
---|
121 | $label = $this->calendar_levels[$level]['labels'][$date_component]; |
---|
122 | } |
---|
123 | elseif ('any' === $date_component ) |
---|
124 | { |
---|
125 | $label = l10n('All'); |
---|
126 | } |
---|
127 | return $label; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Gets a nice display name for a date to be shown in previous/next links |
---|
132 | * |
---|
133 | * @param string $date |
---|
134 | * @return string |
---|
135 | */ |
---|
136 | protected function get_date_nice_name($date) |
---|
137 | { |
---|
138 | $date_components = explode('-', $date); |
---|
139 | $res = ''; |
---|
140 | for ($i=count($date_components)-1; $i>=0; $i--) |
---|
141 | { |
---|
142 | if ('any' !== $date_components[$i]) |
---|
143 | { |
---|
144 | $label = $this->get_date_component_label($i, $date_components[$i] ); |
---|
145 | if ( $res!='' ) |
---|
146 | { |
---|
147 | $res .= ' '; |
---|
148 | } |
---|
149 | $res .= $label; |
---|
150 | } |
---|
151 | } |
---|
152 | return $res; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Creates a calendar navigation bar. |
---|
157 | * |
---|
158 | * @param array $date_components |
---|
159 | * @param array $items - hash of items to put in the bar (e.g. 2005,2006) |
---|
160 | * @param bool $show_any - adds any link to the end of the bar |
---|
161 | * @param bool $show_empty - shows all labels even those without items |
---|
162 | * @param array $labels - optional labels for items (e.g. Jan,Feb,...) |
---|
163 | * @return string |
---|
164 | */ |
---|
165 | protected function get_nav_bar_from_items($date_components, $items, |
---|
166 | $show_any, |
---|
167 | $show_empty=false, $labels=null) |
---|
168 | { |
---|
169 | global $conf, $page, $template; |
---|
170 | |
---|
171 | $nav_bar_datas=array(); |
---|
172 | |
---|
173 | if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) ) |
---|
174 | { |
---|
175 | foreach ($labels as $item => $label) |
---|
176 | { |
---|
177 | if ( ! isset($items[$item]) ) |
---|
178 | { |
---|
179 | $items[$item] = -1; |
---|
180 | } |
---|
181 | } |
---|
182 | ksort($items); |
---|
183 | } |
---|
184 | |
---|
185 | foreach ($items as $item => $nb_images) |
---|
186 | { |
---|
187 | $label = $item; |
---|
188 | if (isset($labels[$item])) |
---|
189 | { |
---|
190 | $label = $labels[$item]; |
---|
191 | } |
---|
192 | if ($nb_images==-1) |
---|
193 | { |
---|
194 | $tmp_datas=array( |
---|
195 | 'LABEL'=> $label |
---|
196 | ); |
---|
197 | } |
---|
198 | else |
---|
199 | { |
---|
200 | $url = duplicate_index_url( |
---|
201 | array('chronology_date'=>array_merge($date_components,array($item))), |
---|
202 | array( 'start' ) |
---|
203 | ); |
---|
204 | $tmp_datas=array( |
---|
205 | 'LABEL'=> $label, |
---|
206 | 'URL' => $url |
---|
207 | ); |
---|
208 | } |
---|
209 | if ($nb_images > 0) |
---|
210 | { |
---|
211 | $tmp_datas['NB_IMAGES']=$nb_images; |
---|
212 | } |
---|
213 | $nav_bar_datas[]=$tmp_datas; |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | if ($conf['calendar_show_any'] and $show_any and count($items)>1 and |
---|
218 | count($date_components)<count($this->calendar_levels)-1 ) |
---|
219 | { |
---|
220 | $url = duplicate_index_url( |
---|
221 | array('chronology_date'=>array_merge($date_components,array('any'))), |
---|
222 | array( 'start' ) |
---|
223 | ); |
---|
224 | $nav_bar_datas[]=array( |
---|
225 | 'LABEL' => l10n('All'), |
---|
226 | 'URL' => $url |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | return $nav_bar_datas; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Creates a calendar navigation bar for a given level. |
---|
235 | * |
---|
236 | * @param int $level - 0-year, 1-month/week, 2-day |
---|
237 | */ |
---|
238 | protected function build_nav_bar($level, $labels=null) |
---|
239 | { |
---|
240 | global $template, $conf, $page; |
---|
241 | |
---|
242 | $query = ' |
---|
243 | SELECT DISTINCT('.$this->calendar_levels[$level]['sql'].') as period, |
---|
244 | COUNT(DISTINCT id) as nb_images'. |
---|
245 | $this->inner_sql. |
---|
246 | $this->get_date_where($level).' |
---|
247 | GROUP BY period;'; |
---|
248 | |
---|
249 | $level_items = simple_hash_from_query($query, 'period', 'nb_images'); |
---|
250 | |
---|
251 | if ( count($level_items)==1 and |
---|
252 | count($page['chronology_date'])<count($this->calendar_levels)-1) |
---|
253 | { |
---|
254 | if ( ! isset($page['chronology_date'][$level]) ) |
---|
255 | { |
---|
256 | list($key) = array_keys($level_items); |
---|
257 | $page['chronology_date'][$level] = (int)$key; |
---|
258 | |
---|
259 | if ( $level<count($page['chronology_date']) and |
---|
260 | $level!=count($this->calendar_levels)-1 ) |
---|
261 | { |
---|
262 | return; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | $dates = $page['chronology_date']; |
---|
268 | while ($level<count($dates)) |
---|
269 | { |
---|
270 | array_pop($dates); |
---|
271 | } |
---|
272 | |
---|
273 | $nav_bar = $this->get_nav_bar_from_items( |
---|
274 | $dates, |
---|
275 | $level_items, |
---|
276 | true, |
---|
277 | true, |
---|
278 | isset($labels) ? $labels : $this->calendar_levels[$level]['labels'] |
---|
279 | ); |
---|
280 | |
---|
281 | $template->append( |
---|
282 | 'chronology_navigation_bars', |
---|
283 | array( |
---|
284 | 'items' => $nav_bar, |
---|
285 | ) |
---|
286 | ); |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * Assigns the next/previous link to the template with regards to |
---|
291 | * the currently choosen date. |
---|
292 | */ |
---|
293 | protected function build_next_prev() |
---|
294 | { |
---|
295 | global $template, $page; |
---|
296 | |
---|
297 | $prev = $next =null; |
---|
298 | if ( empty($page['chronology_date']) ) |
---|
299 | return; |
---|
300 | |
---|
301 | $sub_queries = array(); |
---|
302 | $nb_elements = count($page['chronology_date']); |
---|
303 | for ($i=0; $i<$nb_elements; $i++) |
---|
304 | { |
---|
305 | if ( 'any' === $page['chronology_date'][$i] ) |
---|
306 | { |
---|
307 | $sub_queries[] = '\'any\''; |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | $sub_queries[] = pwg_db_cast_to_text($this->calendar_levels[$i]['sql']); |
---|
312 | } |
---|
313 | } |
---|
314 | $query = 'SELECT '.pwg_db_concat_ws($sub_queries, '-').' AS period'; |
---|
315 | $query .= $this->inner_sql .' |
---|
316 | AND ' . $this->date_field . ' IS NOT NULL |
---|
317 | GROUP BY period'; |
---|
318 | |
---|
319 | $current = implode('-', $page['chronology_date'] ); |
---|
320 | $upper_items = array_from_query( $query, 'period'); |
---|
321 | |
---|
322 | usort($upper_items, 'version_compare'); |
---|
323 | $upper_items_rank = array_flip($upper_items); |
---|
324 | if ( !isset($upper_items_rank[$current]) ) |
---|
325 | { |
---|
326 | $upper_items[] = $current;// just in case (external link) |
---|
327 | usort($upper_items, 'version_compare'); |
---|
328 | $upper_items_rank = array_flip($upper_items); |
---|
329 | } |
---|
330 | $current_rank = $upper_items_rank[$current]; |
---|
331 | |
---|
332 | $tpl_var = array(); |
---|
333 | |
---|
334 | if ( $current_rank>0 ) |
---|
335 | { // has previous |
---|
336 | $prev = $upper_items[$current_rank-1]; |
---|
337 | $chronology_date = explode('-', $prev); |
---|
338 | $tpl_var['previous'] = |
---|
339 | array( |
---|
340 | 'LABEL' => $this->get_date_nice_name($prev), |
---|
341 | 'URL' => duplicate_index_url( |
---|
342 | array('chronology_date'=>$chronology_date), array('start') |
---|
343 | ) |
---|
344 | ); |
---|
345 | } |
---|
346 | |
---|
347 | if ( $current_rank < count($upper_items)-1 ) |
---|
348 | { // has next |
---|
349 | $next = $upper_items[$current_rank+1]; |
---|
350 | $chronology_date = explode('-', $next); |
---|
351 | $tpl_var['next'] = |
---|
352 | array( |
---|
353 | 'LABEL' => $this->get_date_nice_name($next), |
---|
354 | 'URL' => duplicate_index_url( |
---|
355 | array('chronology_date'=>$chronology_date), array('start') |
---|
356 | ) |
---|
357 | ); |
---|
358 | } |
---|
359 | |
---|
360 | if ( !empty($tpl_var) ) |
---|
361 | { |
---|
362 | $existing = $template->smarty->getVariable('chronology_navigation_bars'); |
---|
363 | if (! ($existing instanceof Undefined_Smarty_Variable)) |
---|
364 | { |
---|
365 | $existing->value[ sizeof($existing->value)-1 ] = |
---|
366 | array_merge( $existing->value[ sizeof($existing->value)-1 ], $tpl_var); |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | $template->append( 'chronology_navigation_bars', $tpl_var ); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | ?> |
---|