1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | branch : BSF (Best So Far) |
---|
7 | // | file : $RCSfile$ |
---|
8 | // | last update : $Date: 2006-01-27 02:11:43 +0100 (ven, 27 jan 2006) $ |
---|
9 | // | last modifier : $Author: rvelices $ |
---|
10 | // | revision : $Revision: 1014 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php'); |
---|
28 | |
---|
29 | /** |
---|
30 | * Monthly calendar style (composed of years/months and days) |
---|
31 | */ |
---|
32 | class Calendar extends CalendarBase |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * Generate navigation bars for category page |
---|
37 | * @return boolean false to indicate that thumbnails |
---|
38 | * where not included here, true otherwise |
---|
39 | */ |
---|
40 | function generate_category_content($url_base, $view_type, &$requested) |
---|
41 | { |
---|
42 | global $lang; |
---|
43 | |
---|
44 | $this->url_base = $url_base; |
---|
45 | |
---|
46 | if ($view_type==CAL_VIEW_CALENDAR and count($requested)==0) |
---|
47 | {//case A: no year given - display all years+months |
---|
48 | if ($this->build_global_calendar($requested)) |
---|
49 | return true; |
---|
50 | } |
---|
51 | |
---|
52 | if ($view_type==CAL_VIEW_CALENDAR and count($requested)==1) |
---|
53 | {//case B: year given - display all days in given year |
---|
54 | if ($this->build_year_calendar($requested)) |
---|
55 | { |
---|
56 | $this->build_nav_bar2($view_type, $requested, 0, 'YEAR'); // years |
---|
57 | return true; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | if ($view_type==CAL_VIEW_CALENDAR and count($requested)==2) |
---|
62 | {//case C: year+month given - display a nice month calendar |
---|
63 | $this->build_month_calendar($requested); |
---|
64 | $this->build_nav_bar2(CAL_VIEW_CALENDAR, $requested, 0, 'YEAR'); // years |
---|
65 | if (count($requested)>0) |
---|
66 | $this->build_nav_bar2(CAL_VIEW_CALENDAR, $requested, 1, 'MONTH', $lang['month']); // month |
---|
67 | return true; |
---|
68 | } |
---|
69 | |
---|
70 | if ($view_type==CAL_VIEW_LIST or count($requested)==3) |
---|
71 | { |
---|
72 | $this->build_nav_bar2($view_type, $requested, 0, 'YEAR'); // years |
---|
73 | if (count($requested)>0) |
---|
74 | $this->build_nav_bar2($view_type, $requested, 1, 'MONTH', $lang['month']); // month |
---|
75 | if (count($requested)>1) |
---|
76 | $this->build_nav_bar2($view_type, $requested, 2, 'DAYOFMONTH' ); // days |
---|
77 | } |
---|
78 | return false; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * Returns a sql where subquery for the date field |
---|
84 | * @param array requested selected levels for this calendar |
---|
85 | * (e.g. 2005,11,5 for 5th of November 2005) |
---|
86 | * @param int max_levels return the where up to this level |
---|
87 | * (e.g. 2=only year and month) |
---|
88 | * @return string |
---|
89 | */ |
---|
90 | function get_date_where($requested, $max_levels=3) |
---|
91 | { |
---|
92 | while (count($requested)>$max_levels) |
---|
93 | { |
---|
94 | array_pop($requested); |
---|
95 | } |
---|
96 | $res = ''; |
---|
97 | if (isset($requested[0]) and $requested[0]!='any') |
---|
98 | { |
---|
99 | $b = $requested[0] . '-'; |
---|
100 | $e = $requested[0] . '-'; |
---|
101 | if (isset($requested[1]) and $requested[1]!='any') |
---|
102 | { |
---|
103 | $b .= $requested[1] . '-'; |
---|
104 | $e .= $requested[1] . '-'; |
---|
105 | if (isset($requested[2]) and $requested[2]!='any') |
---|
106 | { |
---|
107 | $b .= $requested[2]; |
---|
108 | $e .= $requested[2]; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | $b .= '01'; |
---|
113 | $e .= '31'; |
---|
114 | } |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | $b .= '01-01'; |
---|
119 | $e .= '12-31'; |
---|
120 | if (isset($requested[1]) and $requested[1]!='any') |
---|
121 | { |
---|
122 | $res .= ' AND MONTH('.$this->date_field.')='.$requested[1]; |
---|
123 | } |
---|
124 | if (isset($requested[2]) and $requested[2]!='any') |
---|
125 | { |
---|
126 | $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2]; |
---|
127 | } |
---|
128 | } |
---|
129 | $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res; |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | $res = ' AND '.$this->date_field.' IS NOT NULL'; |
---|
134 | if (isset($requested[1]) and $requested[1]!='any') |
---|
135 | { |
---|
136 | $res .= ' AND MONTH('.$this->date_field.')='.$requested[1]; |
---|
137 | } |
---|
138 | if (isset($requested[2]) and $requested[2]!='any') |
---|
139 | { |
---|
140 | $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2]; |
---|
141 | } |
---|
142 | } |
---|
143 | return $res; |
---|
144 | } |
---|
145 | |
---|
146 | //--------------------------------------------------------- private members --- |
---|
147 | function build_nav_bar2($view_type, $requested, $level, $sql_func, $labels=null) |
---|
148 | { |
---|
149 | parent::build_nav_bar($view_type, $requested, $level, $sql_func, '', $labels); |
---|
150 | } |
---|
151 | |
---|
152 | function build_global_calendar(&$requested) |
---|
153 | { |
---|
154 | assert( count($requested) == 0 ); |
---|
155 | $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%Y%m")) as period, |
---|
156 | COUNT(id) as count'; |
---|
157 | $query.= $this->inner_sql; |
---|
158 | $query.= $this->get_date_where($requested); |
---|
159 | $query.= ' |
---|
160 | GROUP BY period'; |
---|
161 | |
---|
162 | $result = pwg_query($query); |
---|
163 | while ($row = mysql_fetch_array($result)) |
---|
164 | { |
---|
165 | $y = substr($row['period'], 0, 4); |
---|
166 | $m = (int)substr($row['period'], 4, 2); |
---|
167 | if ( ! isset($items[$y]) ) |
---|
168 | { |
---|
169 | $items[$y] = array('nb_images'=>0, 'children'=>array() ); |
---|
170 | } |
---|
171 | $items[$y]['children'][$m] = $row['count']; |
---|
172 | $items[$y]['nb_images'] += $row['count']; |
---|
173 | } |
---|
174 | //echo ('<pre>'. var_export($items, true) . '</pre>'); |
---|
175 | if (count($items)==1) |
---|
176 | {// only one year exists so bail out to year view |
---|
177 | list($y) = array_keys($items); |
---|
178 | array_push($requested, $y ); |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | global $lang, $template; |
---|
183 | foreach ( $items as $year=>$year_data) |
---|
184 | { |
---|
185 | $url_base = $this->url_base .'c-'.$year; |
---|
186 | |
---|
187 | $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">'.$year.'</a>'; |
---|
188 | $nav_bar .= ' ('.$year_data['nb_images'].')'; |
---|
189 | $nav_bar .= '</span><br>'; |
---|
190 | |
---|
191 | $url_base .= '-'; |
---|
192 | $nav_bar .= $this->get_nav_bar_from_items( $url_base, |
---|
193 | $year_data['children'], null, 'calCal', false, $lang['month'] ); |
---|
194 | |
---|
195 | $template->assign_block_vars( 'calendar.calbar', |
---|
196 | array( 'BAR' => $nav_bar) |
---|
197 | ); |
---|
198 | } |
---|
199 | return true; |
---|
200 | } |
---|
201 | |
---|
202 | function build_year_calendar(&$requested) |
---|
203 | { |
---|
204 | assert( count($requested) == 1 ); |
---|
205 | $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%m%d")) as period, |
---|
206 | COUNT(id) as count'; |
---|
207 | $query.= $this->inner_sql; |
---|
208 | $query.= $this->get_date_where($requested); |
---|
209 | $query.= ' |
---|
210 | GROUP BY period'; |
---|
211 | |
---|
212 | $result = pwg_query($query); |
---|
213 | while ($row = mysql_fetch_array($result)) |
---|
214 | { |
---|
215 | $m = (int)substr($row['period'], 0, 2); |
---|
216 | $d = substr($row['period'], 2, 2); |
---|
217 | if ( ! isset($items[$m]) ) |
---|
218 | { |
---|
219 | $items[$m] = array('nb_images'=>0, 'children'=>array() ); |
---|
220 | } |
---|
221 | $items[$m]['children'][$d] = $row['count']; |
---|
222 | $items[$m]['nb_images'] += $row['count']; |
---|
223 | } |
---|
224 | //echo ('<pre>'. var_export($items, true) . '</pre>'); |
---|
225 | if (count($items)==1) |
---|
226 | { // only one month exists so bail out to month view |
---|
227 | list($m) = array_keys($items); |
---|
228 | array_push($requested, $m ); |
---|
229 | if (count($items[$m]['children'])==1) |
---|
230 | { // or even to day view if everything occured in one day |
---|
231 | list($d) = array_keys($items[$m]['children']); |
---|
232 | array_push($requested, $d); |
---|
233 | } |
---|
234 | return false; |
---|
235 | } |
---|
236 | global $lang, $template; |
---|
237 | foreach ( $items as $month=>$month_data) |
---|
238 | { |
---|
239 | $url_base = $this->url_base.'c-'.$requested[0].'-'.$month; |
---|
240 | |
---|
241 | $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">'; |
---|
242 | $nav_bar .= $lang['month'][$month].'</a>'; |
---|
243 | $nav_bar .= ' ('.$month_data['nb_images'].')'; |
---|
244 | $nav_bar .= '</span><br>'; |
---|
245 | |
---|
246 | $url_base .= '-'; |
---|
247 | $nav_bar .= $this->get_nav_bar_from_items( $url_base, |
---|
248 | $month_data['children'], null, 'calCal', false ); |
---|
249 | |
---|
250 | $template->assign_block_vars( 'calendar.calbar', |
---|
251 | array( 'BAR' => $nav_bar) |
---|
252 | ); |
---|
253 | } |
---|
254 | return true; |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | function build_month_calendar($requested) |
---|
259 | { |
---|
260 | $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%d")) as period, |
---|
261 | COUNT(id) as count'; |
---|
262 | $query.= $this->inner_sql; |
---|
263 | $query.= $this->get_date_where($requested, 2); |
---|
264 | $query.= ' |
---|
265 | GROUP BY period'; |
---|
266 | |
---|
267 | $result = pwg_query($query); |
---|
268 | while ($row = mysql_fetch_array($result)) |
---|
269 | { |
---|
270 | $d = $row['period']; |
---|
271 | $items[$d] = $row['count']; |
---|
272 | } |
---|
273 | |
---|
274 | global $lang, $template; |
---|
275 | |
---|
276 | $template->assign_block_vars('thumbnails', array()); |
---|
277 | $template->assign_block_vars('thumbnails.line', array()); |
---|
278 | foreach ( $items as $day=>$nb_images) |
---|
279 | { |
---|
280 | $url_base = $this->url_base.'c-'.$requested[0].'-'.$requested[1].'-'.$day; |
---|
281 | $requested[2]=$day; |
---|
282 | $query = ' |
---|
283 | SELECT file,tn_ext,path, DAYOFWEEK('.$this->date_field.')-1 as dw'; |
---|
284 | $query.= $this->inner_sql; |
---|
285 | $query.= $this->get_date_where($requested); |
---|
286 | $query.= ' |
---|
287 | ORDER BY RAND() |
---|
288 | LIMIT 0,1'; |
---|
289 | |
---|
290 | $row = mysql_fetch_array(pwg_query($query)); |
---|
291 | |
---|
292 | $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']); |
---|
293 | $thumbnail_title = $lang['day'][$row['dw']] . ' ' . $day; |
---|
294 | $name = $thumbnail_title .' ('.$nb_images.')'; |
---|
295 | |
---|
296 | $template->assign_block_vars( |
---|
297 | 'thumbnails.line.thumbnail', |
---|
298 | array( |
---|
299 | 'IMAGE'=>$thumbnail_src, |
---|
300 | 'IMAGE_ALT'=>$row['file'], |
---|
301 | 'IMAGE_TITLE'=>$thumbnail_title, |
---|
302 | 'U_IMG_LINK'=>$url_base |
---|
303 | ) |
---|
304 | ); |
---|
305 | $template->assign_block_vars( |
---|
306 | 'thumbnails.line.thumbnail.category_name', |
---|
307 | array( |
---|
308 | 'NAME' => $name |
---|
309 | ) |
---|
310 | ); |
---|
311 | } |
---|
312 | return true; |
---|
313 | } |
---|
314 | |
---|
315 | } |
---|
316 | ?> |
---|