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