source: trunk/include/functions_notification.inc.php @ 1789

Last change on this file since 1789 was 1789, checked in by rvelices, 18 years ago
  • correct php warnings (section_init and mass_inserts)
  • flat category view url is now 'flat' instead of 'flat_cat' (it is nicer and shorter)
  • small html validation (double class attribute)
  • removed unnecessary class=""
  • changed 2 times hard coded index.php?recent_pics with make_index_url(...)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
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-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: functions_notification.inc.php 1789 2007-02-08 01:31:05Z rvelices $
9// | last update   : $Date: 2007-02-08 01:31:05 +0000 (Thu, 08 Feb 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1789 $
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 */
42function 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 */
68function 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 */
207function 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 */
220function 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 */
233function 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 */
246function 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 */
259function 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 */
272function 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 */
285function 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 */
298function 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 */
310function 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 */
322function 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 */
332function 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 */
342function 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 */
360function 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 */
375function 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 */
403function 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      make_index_url(array('section'=>'recent_pics')), $add_url );
412  }
413
414  if (!$exclude_img_cats)
415  {
416    add_news_line( $news,
417      nb_updated_categories($start, $end), '%d category updated', '%d categories updated',
418      make_index_url(array('section'=>'recent_cats')), $add_url );
419  }
420
421  add_news_line( $news,
422      nb_new_comments($start, $end), '%d new comment', '%d new comments',
423      get_root_url().'comments.php', $add_url );
424
425  if (is_admin())
426  {
427    add_news_line( $news,
428        nb_unvalidated_comments($end), '%d comment to validate', '%d comments to validate',
429        get_root_url().'admin.php?page=comments', $add_url );
430
431    add_news_line( $news,
432        nb_new_users($start, $end), '%d new user', '%d new users',
433        get_root_url().'admin.php?page=user_list', $add_url );
434
435    add_news_line( $news,
436        nb_waiting_elements(), '%d waiting element', '%d waiting elements',
437        get_root_url().'admin.php?page=waiting', $add_url );
438  }
439
440  return $news;
441}
442
443/**
444 * returns information about recently published elements grouped by post date
445 * @param int max_dates maximum returned number of recent dates
446 * @param int max_elements maximum returned number of elements per date
447 * @param int max_cats maximum returned number of categories per date
448 */
449function get_recent_post_dates($max_dates, $max_elements, $max_cats)
450{
451  global $conf, $user;
452
453  $where_sql = get_std_sql_where_restrict_filter('WHERE', true);
454
455  $query = '
456SELECT date_available,
457      COUNT(DISTINCT id) nb_elements,
458      COUNT(DISTINCT category_id) nb_cats
459  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
460  '.$where_sql.'
461  GROUP BY date_available
462  ORDER BY date_available DESC
463  LIMIT 0,'.$max_dates.'
464;';
465  $result = pwg_query($query);
466  $dates = array();
467  while ($row = mysql_fetch_assoc($result))
468  {
469    array_push($dates, $row);
470  }
471
472  for ($i=0; $i<count($dates); $i++)
473  {
474    if ($max_elements>0)
475    { // get some thumbnails ...
476      $query = '
477SELECT DISTINCT id, path, name, tn_ext
478  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
479  '.$where_sql.'
480    AND date_available="'.$dates[$i]['date_available'].'"
481    AND tn_ext IS NOT NULL
482  LIMIT 0,'.$max_elements.'
483;';
484      $dates[$i]['elements'] = array();
485      $result = pwg_query($query);
486      while ($row = mysql_fetch_assoc($result))
487      {
488        array_push($dates[$i]['elements'], $row);
489      }
490    }
491
492    if ($max_cats>0)
493    {// get some categories ...
494      $query = '
495SELECT DISTINCT c.uppercats, COUNT(DISTINCT i.id) img_count
496  FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id=image_id
497    INNER JOIN '.CATEGORIES_TABLE.' c ON c.id=category_id
498  '.$where_sql.'
499    AND date_available="'.$dates[$i]['date_available'].'"
500  GROUP BY category_id
501  ORDER BY img_count DESC
502  LIMIT 0,'.$max_cats.'
503;';
504      $dates[$i]['categories'] = array();
505      $result = pwg_query($query);
506      while ($row = mysql_fetch_assoc($result))
507      {
508        array_push($dates[$i]['categories'], $row);
509      }
510    }
511  }
512  return $dates;
513}
514
515/**
516 * returns html description about recently published elements grouped by post date
517 * @param $date_detail: selected date computed by get_recent_post_dates function
518 */
519function get_html_description_recent_post_date($date_detail)
520{
521  global $conf;
522
523  $description = '';
524 
525  $description .=
526        '<li>'
527        .l10n_dec('%d new element', '%d new elements', $date_detail['nb_elements'])
528        .' ('
529        .'<a href="'.make_index_url(array('section'=>'recent_pics')).'">'
530          .l10n('recent_pics_cat').'</a>'
531        .')'
532        .'</li><br/>';
533
534  foreach($date_detail['elements'] as $element)
535  {
536    $tn_src = get_thumbnail_url($element);
537    $description .= '<img src="'.$tn_src.'"/>';
538  }
539  $description .= '...<br/>';
540
541  $description .=
542        '<li>'
543        .l10n_dec('%d category updated', '%d categories updated',
544                  $date_detail['nb_cats'])
545        .'</li>';
546
547  $description .= '<ul>';
548  foreach($date_detail['categories'] as $cat)
549  {
550    $description .=
551          '<li>'
552          .get_cat_display_name_cache($cat['uppercats'])
553          .' ('.
554          l10n_dec('%d new element',
555                   '%d new elements', $cat['img_count']).')'
556          .'</li>';
557  }
558  $description .= '</ul>';
559
560  return $description;
561}
562
563/**
564 * explodes a MySQL datetime format (2005-07-14 23:01:37) in fields "year",
565 * "month", "day", "hour", "minute", "second".
566 *
567 * @param string mysql datetime format
568 * @return array
569 */
570function explode_mysqldt($mysqldt)
571{
572  $date = array();
573  list($date['year'],
574       $date['month'],
575       $date['day'],
576       $date['hour'],
577       $date['minute'],
578       $date['second'])
579    = preg_split('/[-: ]/', $mysqldt);
580
581  return $date;
582}
583
584/**
585 * returns title about recently published elements grouped by post date
586 * @param $date_detail: selected date computed by get_recent_post_dates function
587 */
588function get_title_recent_post_date($date_detail)
589{
590  global $lang;
591
592  $date = $date_detail['date_available'];
593  $exploded_date = explode_mysqldt($date);
594
595  $title = l10n_dec('%d new element', '%d new elements', $date_detail['nb_elements']);
596  $title .= ' ('.$lang['month'][(int)$exploded_date['month']].' '.$exploded_date['day'].')';
597
598  return $title;
599}
600
601?>
Note: See TracBrowser for help on using the repository browser.