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

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: functions_notification.inc.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48// +-----------------------------------------------------------------------+
49// |                               functions                               |
50// +-----------------------------------------------------------------------+
51
52/*
53 * get standard sql where in order to
54 * restict an filter caregories and images
55 *
56 * IMAGE_CATEGORY_TABLE muste named ic in the query
57 *
58 * @param none
59 *
60 * @return string sql where
61 */
62function get_std_sql_where_restrict_filter($prefix_condition, $force_one_condition = false)
63{
64  return get_sql_condition_FandF
65          (
66            array
67              (
68                'forbidden_categories' => 'ic.category_id',
69                'visible_categories' => 'ic.category_id',
70                'visible_images' => 'ic.image_id'
71              ),
72            $prefix_condition,
73            $force_one_condition
74          );
75}
76
77/*
78 * Execute custom notification query
79 *
80 * @param string action ('count' or 'info')
81 * @param string type of query ('new_comments', 'unvalidated_comments', 'new_elements', 'updated_categories', 'new_users', 'waiting_elements')
82 * @param string start (mysql datetime format)
83 * @param string end (mysql datetime format)
84 *
85 * @return integer for action count
86 *         array for info
87 */
88function custom_notification_query($action, $type, $start, $end)
89{
90  global $user;
91
92  switch($type)
93  {
94    case 'new_comments':
95      $query = '
96  FROM '.COMMENTS_TABLE.' AS c
97     , '.IMAGE_CATEGORY_TABLE.' AS ic
98  WHERE c.image_id = ic.image_id
99    AND c.validation_date > \''.$start.'\'
100    AND c.validation_date <= \''.$end.'\'
101      '.get_std_sql_where_restrict_filter('AND').'
102;';
103      break;
104    case 'unvalidated_comments':
105      $query = '
106  FROM '.COMMENTS_TABLE.'
107  WHERE date> \''.$start.'\' AND date <= \''.$end.'\'
108    AND validated = \'false\'
109;';
110      break;
111    case 'new_elements':
112      $query = '
113  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id
114  WHERE date_available > \''.$start.'\'
115    AND date_available <= \''.$end.'\'
116      '.get_std_sql_where_restrict_filter('AND').'
117;';
118      break;
119    case 'updated_categories':
120      $query = '
121  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id
122  WHERE date_available > \''.$start.'\'
123    AND date_available <= \''.$end.'\'
124      '.get_std_sql_where_restrict_filter('AND').'
125;';
126      break;
127    case 'new_users':
128      $query = '
129  FROM '.USER_INFOS_TABLE.'
130  WHERE registration_date > \''.$start.'\'
131    AND registration_date <= \''.$end.'\'
132;';
133      break;
134    case 'waiting_elements':
135      $query = '
136  FROM '.WAITING_TABLE.'
137  WHERE validated = \'false\'
138;';
139      break;
140    default:
141      // stop this function and return nothing
142      return;
143      break;
144  }
145
146  switch($action)
147  {
148    case 'count':
149      switch($type)
150      {
151        case 'new_comments':
152          $field_id = 'c.id';
153          break;
154        case 'unvalidated_comments':
155          $field_id = 'id';
156          break;
157        case 'new_elements':
158          $field_id = 'image_id';
159          break;
160        case 'updated_categories':
161          $field_id = 'category_id';
162          break;
163        case 'new_users':
164          $field_id = 'user_id';
165          break;
166        case 'waiting_elements':
167          $field_id = 'id';
168          break;
169    }
170    $query = 'SELECT count(distinct '.$field_id.') as CountId
171'.$query;
172    list($count) = mysql_fetch_array(pwg_query($query));
173    return $count;
174
175    break;
176    case 'info':
177      switch($type)
178      {
179        case 'new_comments':
180          $fields = array('c.id');
181          break;
182        case 'unvalidated_comments':
183          $fields = array('id');
184          break;
185        case 'new_elements':
186          $fields = array('image_id');
187          break;
188        case 'updated_categories':
189          $fields = array('category_id');
190          break;
191        case 'new_users':
192          $fields = array('user_id');
193          break;
194        case 'waiting_elements':
195          $fields = array('id');
196          break;
197      }
198
199    $query = 'SELECT distinct '.implode(', ', $fields).'
200'.$query;
201    $result = pwg_query($query);
202
203    $infos = array();
204
205    while ($row = mysql_fetch_array($result))
206    {
207      array_push($infos, $row);
208    }
209
210    return $infos;
211
212    break;
213  }
214
215  //return is done on previous switch($action)
216}
217
218/**
219 * new comments between two dates, according to authorized categories
220 *
221 * @param string start (mysql datetime format)
222 * @param string end (mysql datetime format)
223 * @param string forbidden categories (comma separated)
224 * @return count comment ids
225 */
226function nb_new_comments($start, $end)
227{
228  return custom_notification_query('count', 'new_comments', $start, $end);
229}
230
231/**
232 * new comments between two dates, according to authorized categories
233 *
234 * @param string start (mysql datetime format)
235 * @param string end (mysql datetime format)
236 * @param string forbidden categories (comma separated)
237 * @return array comment ids
238 */
239function new_comments($start, $end)
240{
241  return custom_notification_query('info', 'new_comments', $start, $end);
242}
243
244/**
245 * unvalidated at a precise date
246 *
247 * Comments that are registered and not validated yet on a precise date
248 *
249 * @param string start (mysql datetime format)
250 * @param string end (mysql datetime format)
251 * @return count comment ids
252 */
253function nb_unvalidated_comments($start, $end)
254{
255  return custom_notification_query('count', 'unvalidated_comments', $start, $end);
256}
257
258
259/**
260 * new elements between two dates, according to authorized categories
261 *
262 * @param string start (mysql datetime format)
263 * @param string end (mysql datetime format)
264 * @param string forbidden categories (comma separated)
265 * @return count element ids
266 */
267function nb_new_elements($start, $end)
268{
269  return custom_notification_query('count', 'new_elements', $start, $end);
270}
271
272/**
273 * new elements between two dates, according to authorized categories
274 *
275 * @param string start (mysql datetime format)
276 * @param string end (mysql datetime format)
277 * @param string forbidden categories (comma separated)
278 * @return array element ids
279 */
280function new_elements($start, $end)
281{
282  return custom_notification_query('info', 'new_elements', $start, $end);
283}
284
285/**
286 * updated categories between two dates, according to authorized categories
287 *
288 * @param string start (mysql datetime format)
289 * @param string end (mysql datetime format)
290 * @param string forbidden categories (comma separated)
291 * @return count element ids
292 */
293function nb_updated_categories($start, $end)
294{
295  return custom_notification_query('count', 'updated_categories', $start, $end);
296}
297
298/**
299 * updated categories between two dates, according to authorized categories
300 *
301 * @param string start (mysql datetime format)
302 * @param string end (mysql datetime format)
303 * @param string forbidden categories (comma separated)
304 * @return array element ids
305 */
306function updated_categories($start, $end)
307{
308  return custom_notification_query('info', 'updated_categories', $start, $end);
309}
310
311/**
312 * new registered users between two dates
313 *
314 * @param string start (mysql datetime format)
315 * @param string end (mysql datetime format)
316 * @return count user ids
317 */
318function nb_new_users($start, $end)
319{
320  return custom_notification_query('count', 'new_users', $start, $end);
321}
322
323/**
324 * new registered users between two dates
325 *
326 * @param string start (mysql datetime format)
327 * @param string end (mysql datetime format)
328 * @return array user ids
329 */
330function new_users($start, $end)
331{
332  return custom_notification_query('info', 'new_users', $start, $end);
333}
334
335/**
336 * currently waiting pictures
337 *
338 * @return count waiting ids
339 */
340function nb_waiting_elements()
341{
342  return custom_notification_query('count', 'waiting_elements', '', '');
343}
344
345/**
346 * currently waiting pictures
347 *
348 * @return array waiting ids
349 */
350function waiting_elements()
351{
352  return custom_notification_query('info', 'waiting_elements', $start, $end);
353}
354
355/**
356 * There are new between two dates ?
357 *
358 * Informations : number of new comments, number of new elements, number of
359 * updated categories. Administrators are also informed about : number of
360 * unvalidated comments, number of new users (TODO : number of unvalidated
361 * elements)
362 *
363 * @param string start date (mysql datetime format)
364 * @param string end date (mysql datetime format)
365 *
366 * @return boolean : true if exist news else false
367 */
368function news_exists($start, $end)
369{
370  return (
371          (nb_new_comments($start, $end) > 0) or
372          (nb_new_elements($start, $end) > 0) or
373          (nb_updated_categories($start, $end) > 0) or
374          ((is_admin()) and (nb_unvalidated_comments($start, $end) > 0)) or
375          ((is_admin()) and (nb_new_users($start, $end) > 0)) or
376          ((is_admin()) and (nb_waiting_elements() > 0))
377        );
378}
379
380/**
381 * Formats a news line and adds it to the array (e.g. '5 new elements')
382 */
383function add_news_line(&$news, $count, $singular_fmt_key, $plural_fmt_key, $url='', $add_url=false)
384{
385  if ($count > 0)
386  {
387    $line = l10n_dec($singular_fmt_key, $plural_fmt_key, $count);
388    if ($add_url and !empty($url) )
389    {
390      $line = '<a href="'.$url.'">'.$line.'</a>';
391    }
392    array_push($news, $line);
393  }
394}
395
396/**
397 * What's new between two dates ?
398 *
399 * Informations : number of new comments, number of new elements, number of
400 * updated categories. Administrators are also informed about : number of
401 * unvalidated comments, number of new users (TODO : number of unvalidated
402 * elements)
403 *
404 * @param string start date (mysql datetime format)
405 * @param string end date (mysql datetime format)
406 * @param bool exclude_img_cats if true, no info about new images/categories
407 * @param bool add_url add html A link around news
408 *
409 * @return array of news
410 */
411function news($start, $end, $exclude_img_cats=false, $add_url=false)
412{
413  $news = array();
414
415  if (!$exclude_img_cats)
416  {
417    add_news_line( $news,
418      nb_new_elements($start, $end), '%d new element', '%d new elements',
419      make_index_url(array('section'=>'recent_pics')), $add_url );
420  }
421
422  if (!$exclude_img_cats)
423  {
424    add_news_line( $news,
425      nb_updated_categories($start, $end), '%d category updated', '%d categories updated',
426      make_index_url(array('section'=>'recent_cats')), $add_url );
427  }
428
429  add_news_line( $news,
430      nb_new_comments($start, $end), '%d new comment', '%d new comments',
431      get_root_url().'comments.php', $add_url );
432
433  if (is_admin())
434  {
435    add_news_line( $news,
436        nb_unvalidated_comments($start, $end), '%d comment to validate', '%d comments to validate',
437        get_root_url().'admin.php?page=comments', $add_url );
438
439    add_news_line( $news,
440        nb_new_users($start, $end), '%d new user', '%d new users',
441        get_root_url().'admin.php?page=user_list', $add_url );
442
443    add_news_line( $news,
444        nb_waiting_elements(), '%d waiting element', '%d waiting elements',
445        get_root_url().'admin.php?page=waiting', $add_url );
446  }
447
448  return $news;
449}
450
451/**
452 * returns information about recently published elements grouped by post date
453 * @param int max_dates maximum returned number of recent dates
454 * @param int max_elements maximum returned number of elements per date
455 * @param int max_cats maximum returned number of categories per date
456 */
457function get_recent_post_dates($max_dates, $max_elements, $max_cats)
458{
459  global $conf, $user;
460
461  $where_sql = get_std_sql_where_restrict_filter('WHERE', true);
462
463  $query = '
464SELECT date_available,
465      COUNT(DISTINCT id) nb_elements,
466      COUNT(DISTINCT category_id) nb_cats
467  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
468  '.$where_sql.'
469  GROUP BY date_available
470  ORDER BY date_available DESC
471  LIMIT 0,'.$max_dates.'
472;';
473  $result = pwg_query($query);
474  $dates = array();
475  while ($row = mysql_fetch_assoc($result))
476  {
477    array_push($dates, $row);
478  }
479
480  for ($i=0; $i<count($dates); $i++)
481  {
482    if ($max_elements>0)
483    { // get some thumbnails ...
484      $query = '
485SELECT DISTINCT id, path, name, tn_ext, file
486  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
487  '.$where_sql.'
488    AND date_available="'.$dates[$i]['date_available'].'"
489    AND tn_ext IS NOT NULL
490  ORDER BY RAND(NOW())
491  LIMIT 0,'.$max_elements.'
492;';
493      $dates[$i]['elements'] = array();
494      $result = pwg_query($query);
495      while ($row = mysql_fetch_assoc($result))
496      {
497        array_push($dates[$i]['elements'], $row);
498      }
499    }
500
501    if ($max_cats>0)
502    {// get some categories ...
503      $query = '
504SELECT DISTINCT c.uppercats, COUNT(DISTINCT i.id) img_count
505  FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id=image_id
506    INNER JOIN '.CATEGORIES_TABLE.' c ON c.id=category_id
507  '.$where_sql.'
508    AND date_available="'.$dates[$i]['date_available'].'"
509  GROUP BY category_id
510  ORDER BY img_count DESC
511  LIMIT 0,'.$max_cats.'
512;';
513      $dates[$i]['categories'] = array();
514      $result = pwg_query($query);
515      while ($row = mysql_fetch_assoc($result))
516      {
517        array_push($dates[$i]['categories'], $row);
518      }
519    }
520  }
521  return $dates;
522}
523
524/*
525  Call function get_recent_post_dates but
526  the parameters to be passed to the function, as an indexed array.
527
528*/
529function get_recent_post_dates_array($args)
530{
531  return
532    get_recent_post_dates
533    (
534      (empty($args['max_dates']) ? 3 : $args['max_dates']),
535      (empty($args['max_elements']) ? 3 : $args['max_elements']),
536      (empty($args['max_cats']) ? 3 : $args['max_cats'])
537    );
538}
539
540
541/**
542 * returns html description about recently published elements grouped by post date
543 * @param $date_detail: selected date computed by get_recent_post_dates function
544 */
545function get_html_description_recent_post_date($date_detail)
546{
547  global $conf;
548
549  $description = '';
550
551  $description .=
552        '<li>'
553        .l10n_dec('%d new element', '%d new elements', $date_detail['nb_elements'])
554        .' ('
555        .'<a href="'.make_index_url(array('section'=>'recent_pics')).'">'
556          .l10n('recent_pics_cat').'</a>'
557        .')'
558        .'</li><br/>';
559
560  foreach($date_detail['elements'] as $element)
561  {
562    $tn_src = get_thumbnail_url($element);
563    $description .= '<a href="'.
564                    make_picture_url(array(
565                        'image_id' => $element['id'],
566                        'image_file' => $element['file'],
567                      ))
568                    .'"><img src="'.$tn_src.'"/></a>';
569  }
570  $description .= '...<br/>';
571
572  $description .=
573        '<li>'
574        .l10n_dec('%d category updated', '%d categories updated',
575                  $date_detail['nb_cats'])
576        .'</li>';
577
578  $description .= '<ul>';
579  foreach($date_detail['categories'] as $cat)
580  {
581    $description .=
582          '<li>'
583          .get_cat_display_name_cache($cat['uppercats'])
584          .' ('.
585          l10n_dec('%d new element',
586                   '%d new elements', $cat['img_count']).')'
587          .'</li>';
588  }
589  $description .= '</ul>';
590
591  return $description;
592}
593
594/**
595 * explodes a MySQL datetime format (2005-07-14 23:01:37) in fields "year",
596 * "month", "day", "hour", "minute", "second".
597 *
598 * @param string mysql datetime format
599 * @return array
600 */
601function explode_mysqldt($mysqldt)
602{
603  $date = array();
604  list($date['year'],
605       $date['month'],
606       $date['day'],
607       $date['hour'],
608       $date['minute'],
609       $date['second'])
610    = preg_split('/[-: ]/', $mysqldt);
611
612  return $date;
613}
614
615/**
616 * returns title about recently published elements grouped by post date
617 * @param $date_detail: selected date computed by get_recent_post_dates function
618 */
619function get_title_recent_post_date($date_detail)
620{
621  global $lang;
622
623  $date = $date_detail['date_available'];
624  $exploded_date = explode_mysqldt($date);
625
626  $title = l10n_dec('%d new element', '%d new elements', $date_detail['nb_elements']);
627  $title .= ' ('.$lang['month'][(int)$exploded_date['month']].' '.$exploded_date['day'].')';
628
629  return $title;
630}
631
632?>
Note: See TracBrowser for help on using the repository browser.