1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-11-26 21:15:50 +0100 (sam., 26 nov. 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 958 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | // +-----------------------------------------------------------------------+ |
---|
29 | // | functions | |
---|
30 | // +-----------------------------------------------------------------------+ |
---|
31 | |
---|
32 | /* |
---|
33 | * get standard sql where in order to |
---|
34 | * restict an filter caregories and images |
---|
35 | * |
---|
36 | * IMAGE_CATEGORY_TABLE muste named ic in the query |
---|
37 | * |
---|
38 | * @param none |
---|
39 | * |
---|
40 | * @return string sql where |
---|
41 | */ |
---|
42 | function get_std_sql_where_restrict_filter($prefix_condition, $force_one_condition = false) |
---|
43 | { |
---|
44 | return get_sql_condition_FandF |
---|
45 | ( |
---|
46 | array |
---|
47 | ( |
---|
48 | 'forbidden_categories' => 'ic.category_id', |
---|
49 | 'visible_categories' => 'ic.category_id', |
---|
50 | 'visible_images' => 'ic.image_id' |
---|
51 | ), |
---|
52 | $prefix_condition, |
---|
53 | $force_one_condition |
---|
54 | ); |
---|
55 | } |
---|
56 | |
---|
57 | /* |
---|
58 | * Execute custom notification query |
---|
59 | * |
---|
60 | * @param string action ('count' or 'info') |
---|
61 | * @param string type of query ('new_comments', 'unvalidated_comments', 'new_elements', 'updated_categories', 'new_users', 'waiting_elements') |
---|
62 | * @param string start (mysql datetime format) |
---|
63 | * @param string end (mysql datetime format) |
---|
64 | * |
---|
65 | * @return integer for action count |
---|
66 | * array for info |
---|
67 | */ |
---|
68 | function custom_notification_query($action, $type, $start, $end) |
---|
69 | { |
---|
70 | global $user; |
---|
71 | |
---|
72 | switch($type) |
---|
73 | { |
---|
74 | case 'new_comments': |
---|
75 | $query = ' |
---|
76 | FROM '.COMMENTS_TABLE.' AS c |
---|
77 | , '.IMAGE_CATEGORY_TABLE.' AS ic |
---|
78 | WHERE c.image_id = ic.image_id |
---|
79 | AND c.validation_date > \''.$start.'\' |
---|
80 | AND c.validation_date <= \''.$end.'\' |
---|
81 | '.get_std_sql_where_restrict_filter('AND').' |
---|
82 | ;'; |
---|
83 | break; |
---|
84 | case 'unvalidated_comments': |
---|
85 | $query = ' |
---|
86 | FROM '.COMMENTS_TABLE.' |
---|
87 | WHERE date <= \''.$end.'\' |
---|
88 | AND (validated = \'false\' |
---|
89 | OR validation_date > \''.$end.'\') |
---|
90 | ;'; |
---|
91 | break; |
---|
92 | case 'new_elements': |
---|
93 | $query = ' |
---|
94 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id |
---|
95 | WHERE date_available > \''.$start.'\' |
---|
96 | AND date_available <= \''.$end.'\' |
---|
97 | '.get_std_sql_where_restrict_filter('AND').' |
---|
98 | ;'; |
---|
99 | break; |
---|
100 | case 'updated_categories': |
---|
101 | $query = ' |
---|
102 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id |
---|
103 | WHERE date_available > \''.$start.'\' |
---|
104 | AND date_available <= \''.$end.'\' |
---|
105 | '.get_std_sql_where_restrict_filter('AND').' |
---|
106 | ;'; |
---|
107 | break; |
---|
108 | case 'new_users': |
---|
109 | $query = ' |
---|
110 | FROM '.USER_INFOS_TABLE.' |
---|
111 | WHERE registration_date > \''.$start.'\' |
---|
112 | AND registration_date <= \''.$end.'\' |
---|
113 | ;'; |
---|
114 | break; |
---|
115 | case 'waiting_elements': |
---|
116 | $query = ' |
---|
117 | FROM '.WAITING_TABLE.' |
---|
118 | WHERE validated = \'false\' |
---|
119 | ;'; |
---|
120 | break; |
---|
121 | default: |
---|
122 | // stop this function and return nothing |
---|
123 | return; |
---|
124 | break; |
---|
125 | } |
---|
126 | |
---|
127 | switch($action) |
---|
128 | { |
---|
129 | case 'count': |
---|
130 | switch($type) |
---|
131 | { |
---|
132 | case 'new_comments': |
---|
133 | $field_id = 'c.id'; |
---|
134 | break; |
---|
135 | case 'unvalidated_comments': |
---|
136 | $field_id = 'id'; |
---|
137 | break; |
---|
138 | case 'new_elements': |
---|
139 | $field_id = 'image_id'; |
---|
140 | break; |
---|
141 | case 'updated_categories': |
---|
142 | $field_id = 'category_id'; |
---|
143 | break; |
---|
144 | case 'new_users': |
---|
145 | $field_id = 'user_id'; |
---|
146 | break; |
---|
147 | case 'waiting_elements': |
---|
148 | $field_id = 'id'; |
---|
149 | break; |
---|
150 | } |
---|
151 | $query = 'SELECT count(distinct '.$field_id.') as CountId |
---|
152 | '.$query; |
---|
153 | list($count) = mysql_fetch_array(pwg_query($query)); |
---|
154 | return $count; |
---|
155 | |
---|
156 | break; |
---|
157 | case 'info': |
---|
158 | switch($type) |
---|
159 | { |
---|
160 | case 'new_comments': |
---|
161 | $fields = array('c.id'); |
---|
162 | break; |
---|
163 | case 'unvalidated_comments': |
---|
164 | $fields = array('id'); |
---|
165 | break; |
---|
166 | case 'new_elements': |
---|
167 | $fields = array('image_id'); |
---|
168 | break; |
---|
169 | case 'updated_categories': |
---|
170 | $fields = array('category_id'); |
---|
171 | break; |
---|
172 | case 'new_users': |
---|
173 | $fields = array('user_id'); |
---|
174 | break; |
---|
175 | case 'waiting_elements': |
---|
176 | $fields = array('id'); |
---|
177 | break; |
---|
178 | } |
---|
179 | |
---|
180 | $query = 'SELECT distinct '.implode(', ', $fields).' |
---|
181 | '.$query; |
---|
182 | $result = pwg_query($query); |
---|
183 | |
---|
184 | $infos = array(); |
---|
185 | |
---|
186 | while ($row = mysql_fetch_array($result)) |
---|
187 | { |
---|
188 | array_push($infos, $row); |
---|
189 | } |
---|
190 | |
---|
191 | return $infos; |
---|
192 | |
---|
193 | break; |
---|
194 | } |
---|
195 | |
---|
196 | //return is done on previous switch($action) |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * new comments between two dates, according to authorized categories |
---|
201 | * |
---|
202 | * @param string start (mysql datetime format) |
---|
203 | * @param string end (mysql datetime format) |
---|
204 | * @param string forbidden categories (comma separated) |
---|
205 | * @return count comment ids |
---|
206 | */ |
---|
207 | function nb_new_comments($start, $end) |
---|
208 | { |
---|
209 | return custom_notification_query('count', 'new_comments', $start, $end); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * new comments between two dates, according to authorized categories |
---|
214 | * |
---|
215 | * @param string start (mysql datetime format) |
---|
216 | * @param string end (mysql datetime format) |
---|
217 | * @param string forbidden categories (comma separated) |
---|
218 | * @return array comment ids |
---|
219 | */ |
---|
220 | function new_comments($start, $end) |
---|
221 | { |
---|
222 | return custom_notification_query('info', 'new_comments', $start, $end); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * unvalidated at a precise date |
---|
227 | * |
---|
228 | * Comments that are registered and not validated yet on a precise date |
---|
229 | * |
---|
230 | * @param string date (mysql datetime format) |
---|
231 | * @return count comment ids |
---|
232 | */ |
---|
233 | function nb_unvalidated_comments($date) |
---|
234 | { |
---|
235 | return custom_notification_query('count', 'unvalidated_comments', $date, $date); |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * unvalidated at a precise date |
---|
240 | * |
---|
241 | * Comments that are registered and not validated yet on a precise date |
---|
242 | * |
---|
243 | * @param string date (mysql datetime format) |
---|
244 | * @return array comment ids |
---|
245 | */ |
---|
246 | function unvalidated_comments($date) |
---|
247 | { |
---|
248 | return custom_notification_query('info', 'unvalidated_comments', $start, $end); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * new elements between two dates, according to authorized categories |
---|
253 | * |
---|
254 | * @param string start (mysql datetime format) |
---|
255 | * @param string end (mysql datetime format) |
---|
256 | * @param string forbidden categories (comma separated) |
---|
257 | * @return count element ids |
---|
258 | */ |
---|
259 | function nb_new_elements($start, $end) |
---|
260 | { |
---|
261 | return custom_notification_query('count', 'new_elements', $start, $end); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * new elements between two dates, according to authorized categories |
---|
266 | * |
---|
267 | * @param string start (mysql datetime format) |
---|
268 | * @param string end (mysql datetime format) |
---|
269 | * @param string forbidden categories (comma separated) |
---|
270 | * @return array element ids |
---|
271 | */ |
---|
272 | function new_elements($start, $end) |
---|
273 | { |
---|
274 | return custom_notification_query('info', 'new_elements', $start, $end); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * updated categories between two dates, according to authorized categories |
---|
279 | * |
---|
280 | * @param string start (mysql datetime format) |
---|
281 | * @param string end (mysql datetime format) |
---|
282 | * @param string forbidden categories (comma separated) |
---|
283 | * @return count element ids |
---|
284 | */ |
---|
285 | function nb_updated_categories($start, $end) |
---|
286 | { |
---|
287 | return custom_notification_query('count', 'updated_categories', $start, $end); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * updated categories between two dates, according to authorized categories |
---|
292 | * |
---|
293 | * @param string start (mysql datetime format) |
---|
294 | * @param string end (mysql datetime format) |
---|
295 | * @param string forbidden categories (comma separated) |
---|
296 | * @return array element ids |
---|
297 | */ |
---|
298 | function updated_categories($start, $end) |
---|
299 | { |
---|
300 | return custom_notification_query('info', 'updated_categories', $start, $end); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * new registered users between two dates |
---|
305 | * |
---|
306 | * @param string start (mysql datetime format) |
---|
307 | * @param string end (mysql datetime format) |
---|
308 | * @return count user ids |
---|
309 | */ |
---|
310 | function nb_new_users($start, $end) |
---|
311 | { |
---|
312 | return custom_notification_query('count', 'new_users', $start, $end); |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * new registered users between two dates |
---|
317 | * |
---|
318 | * @param string start (mysql datetime format) |
---|
319 | * @param string end (mysql datetime format) |
---|
320 | * @return array user ids |
---|
321 | */ |
---|
322 | function new_users($start, $end) |
---|
323 | { |
---|
324 | return custom_notification_query('info', 'new_users', $start, $end); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * currently waiting pictures |
---|
329 | * |
---|
330 | * @return count waiting ids |
---|
331 | */ |
---|
332 | function nb_waiting_elements() |
---|
333 | { |
---|
334 | return custom_notification_query('count', 'waiting_elements', '', ''); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * currently waiting pictures |
---|
339 | * |
---|
340 | * @return array waiting ids |
---|
341 | */ |
---|
342 | function waiting_elements() |
---|
343 | { |
---|
344 | return custom_notification_query('info', 'waiting_elements', $start, $end); |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * There are new between two dates ? |
---|
349 | * |
---|
350 | * Informations : number of new comments, number of new elements, number of |
---|
351 | * updated categories. Administrators are also informed about : number of |
---|
352 | * unvalidated comments, number of new users (TODO : number of unvalidated |
---|
353 | * elements) |
---|
354 | * |
---|
355 | * @param string start date (mysql datetime format) |
---|
356 | * @param string end date (mysql datetime format) |
---|
357 | * |
---|
358 | * @return boolean : true if exist news else false |
---|
359 | */ |
---|
360 | function news_exists($start, $end) |
---|
361 | { |
---|
362 | return ( |
---|
363 | (nb_new_comments($start, $end) > 0) or |
---|
364 | (nb_new_elements($start, $end) > 0) or |
---|
365 | (nb_updated_categories($start, $end) > 0) or |
---|
366 | ((is_admin()) and (nb_unvalidated_comments($end) > 0)) or |
---|
367 | ((is_admin()) and (nb_new_users($start, $end) > 0)) or |
---|
368 | ((is_admin()) and (nb_waiting_elements() > 0)) |
---|
369 | ); |
---|
370 | } |
---|
371 | |
---|
372 | /** |
---|
373 | * Formats a news line and adds it to the array (e.g. '5 new elements') |
---|
374 | */ |
---|
375 | function add_news_line(&$news, $count, $singular_fmt_key, $plural_fmt_key, $url='', $add_url=false) |
---|
376 | { |
---|
377 | if ($count > 0) |
---|
378 | { |
---|
379 | $line = l10n_dec($singular_fmt_key, $plural_fmt_key, $count); |
---|
380 | if ($add_url and !empty($url) ) |
---|
381 | { |
---|
382 | $line = '<a href="'.$url.'">'.$line.'</a>'; |
---|
383 | } |
---|
384 | array_push($news, $line); |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * What's new between two dates ? |
---|
390 | * |
---|
391 | * Informations : number of new comments, number of new elements, number of |
---|
392 | * updated categories. Administrators are also informed about : number of |
---|
393 | * unvalidated comments, number of new users (TODO : number of unvalidated |
---|
394 | * elements) |
---|
395 | * |
---|
396 | * @param string start date (mysql datetime format) |
---|
397 | * @param string end date (mysql datetime format) |
---|
398 | * @param bool exclude_img_cats if true, no info about new images/categories |
---|
399 | * @param bool add_url add html A link around news |
---|
400 | * |
---|
401 | * @return array of news |
---|
402 | */ |
---|
403 | function news($start, $end, $exclude_img_cats=false, $add_url=false) |
---|
404 | { |
---|
405 | $news = array(); |
---|
406 | |
---|
407 | if (!$exclude_img_cats) |
---|
408 | { |
---|
409 | add_news_line( $news, |
---|
410 | nb_new_elements($start, $end), '%d new element', '%d new elements'); |
---|
411 | } |
---|
412 | |
---|
413 | if (!$exclude_img_cats) |
---|
414 | { |
---|
415 | add_news_line( $news, |
---|
416 | nb_updated_categories($start, $end), '%d category updated', '%d categories updated'); |
---|
417 | } |
---|
418 | |
---|
419 | add_news_line( $news, |
---|
420 | nb_new_comments($start, $end), '%d new comment', '%d new comments', |
---|
421 | get_root_url().'comments.php', $add_url ); |
---|
422 | |
---|
423 | if (is_admin()) |
---|
424 | { |
---|
425 | add_news_line( $news, |
---|
426 | nb_unvalidated_comments($end), '%d comment to validate', '%d comments to validate', |
---|
427 | get_root_url().'admin.php?page=comments', $add_url ); |
---|
428 | |
---|
429 | add_news_line( $news, |
---|
430 | nb_new_users($start, $end), '%d new user', '%d new users', |
---|
431 | get_root_url().'admin.php?page=user_list', $add_url ); |
---|
432 | |
---|
433 | add_news_line( $news, |
---|
434 | nb_waiting_elements(), '%d waiting element', '%d waiting elements', |
---|
435 | get_root_url().'admin.php?page=waiting', $add_url ); |
---|
436 | } |
---|
437 | |
---|
438 | return $news; |
---|
439 | } |
---|
440 | |
---|
441 | /** |
---|
442 | * returns information about recently published elements grouped by post date |
---|
443 | * @param int max_dates maximum returned number of recent dates |
---|
444 | * @param int max_elements maximum returned number of elements per date |
---|
445 | * @param int max_cats maximum returned number of categories per date |
---|
446 | */ |
---|
447 | function get_recent_post_dates($max_dates, $max_elements, $max_cats) |
---|
448 | { |
---|
449 | global $conf, $user; |
---|
450 | |
---|
451 | $where_sql = get_std_sql_where_restrict_filter('WHERE', true); |
---|
452 | |
---|
453 | $query = ' |
---|
454 | SELECT date_available, |
---|
455 | COUNT(DISTINCT id) nb_elements, |
---|
456 | COUNT(DISTINCT category_id) nb_cats |
---|
457 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id |
---|
458 | '.$where_sql.' |
---|
459 | GROUP BY date_available |
---|
460 | ORDER BY date_available DESC |
---|
461 | LIMIT 0,'.$max_dates.' |
---|
462 | ;'; |
---|
463 | $result = pwg_query($query); |
---|
464 | $dates = array(); |
---|
465 | while ($row = mysql_fetch_assoc($result)) |
---|
466 | { |
---|
467 | array_push($dates, $row); |
---|
468 | } |
---|
469 | |
---|
470 | for ($i=0; $i<count($dates); $i++) |
---|
471 | { |
---|
472 | if ($max_elements>0) |
---|
473 | { // get some thumbnails ... |
---|
474 | $query = ' |
---|
475 | SELECT DISTINCT id, path, name, tn_ext |
---|
476 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id |
---|
477 | '.$where_sql.' |
---|
478 | AND date_available="'.$dates[$i]['date_available'].'" |
---|
479 | AND tn_ext IS NOT NULL |
---|
480 | LIMIT 0,'.$max_elements.' |
---|
481 | ;'; |
---|
482 | $dates[$i]['elements'] = array(); |
---|
483 | $result = pwg_query($query); |
---|
484 | while ($row = mysql_fetch_assoc($result)) |
---|
485 | { |
---|
486 | array_push($dates[$i]['elements'], $row); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | if ($max_cats>0) |
---|
491 | {// get some categories ... |
---|
492 | $query = ' |
---|
493 | SELECT DISTINCT c.uppercats, COUNT(DISTINCT i.id) img_count |
---|
494 | FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id=image_id |
---|
495 | INNER JOIN '.CATEGORIES_TABLE.' c ON c.id=category_id |
---|
496 | '.$where_sql.' |
---|
497 | AND date_available="'.$dates[$i]['date_available'].'" |
---|
498 | GROUP BY category_id |
---|
499 | ORDER BY img_count DESC |
---|
500 | LIMIT 0,'.$max_cats.' |
---|
501 | ;'; |
---|
502 | $dates[$i]['categories'] = array(); |
---|
503 | $result = pwg_query($query); |
---|
504 | while ($row = mysql_fetch_assoc($result)) |
---|
505 | { |
---|
506 | array_push($dates[$i]['categories'], $row); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | return $dates; |
---|
511 | } |
---|
512 | ?> |
---|