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

Last change on this file since 25578 was 25578, checked in by mistic100, 10 years ago

feature 2999: Documentation of include/functions_notification + clean code

  • Property svn:eol-style set to LF
File size: 16.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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 * @package functions\notification
26 */
27 
28// TODO : use a cache for all data returned by custom_notification_query()
29
30
31/**
32 * Get standard sql where in order to restrict and filter categories and images.
33 * IMAGE_CATEGORY_TABLE must be named "ic" in the query
34 *
35 * @param string $prefix_condition
36 * @param string $img_field
37 * @param bool $force_one_condition
38 * @return string
39 */
40function get_std_sql_where_restrict_filter($prefix_condition,
41                                           $img_field = 'ic.image_id',
42                                           $force_one_condition = false)
43{
44  return get_sql_condition_FandF(
45    array(
46      'forbidden_categories' => 'ic.category_id',
47      'visible_categories' => 'ic.category_id',
48      'visible_images' => $img_field
49      ),
50    $prefix_condition,
51    $force_one_condition
52    );
53}
54
55/**
56 * Execute custom notification query.
57 *
58 * @param string $action 'count', 'info'
59 * @param string $type 'new_comments', 'unvalidated_comments', 'new_elements', 'updated_categories', 'new_users'
60 * @param string $start (mysql datetime format)
61 * @param string $end (mysql datetime format)
62 * @return int|array int for action count array for info
63 */
64function custom_notification_query($action, $type, $start=null, $end=null)
65{
66  global $user;
67
68  switch($type)
69  {
70    case 'new_comments':
71    {
72      $query = '
73  FROM '.COMMENTS_TABLE.' AS c
74    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON c.image_id = ic.image_id
75  WHERE 1=1';
76      if (!empty($start))
77      {
78        $query.= '
79    AND c.validation_date > \''.$start.'\'';
80      }
81      if (!empty($end))
82      {
83        $query.= '
84    AND c.validation_date <= \''.$end.'\'';
85      }
86      $query.= get_std_sql_where_restrict_filter('AND');
87      break;
88    }
89
90    case 'unvalidated_comments':
91    {
92      $query = '
93  FROM '.COMMENTS_TABLE.'
94  WHERE 1=1';
95      if (!empty($start))
96      {
97        $query.= '
98    AND date > \''.$start.'\'';
99      }
100      if (!empty($end))
101      {
102        $query.= '
103    AND date <= \''.$end.'\'';
104      }
105      $query.= '
106    AND validated = \'false\'';
107      break;
108    }
109
110    case 'new_elements':
111    {
112      $query = '
113  FROM '.IMAGES_TABLE.'
114    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id
115  WHERE 1=1';
116      if (!empty($start))
117      {
118        $query.= '
119    AND date_available > \''.$start.'\'';
120      }
121      if (!empty($end))
122      {
123        $query.= '
124    AND date_available <= \''.$end.'\'';
125      }
126      $query.= get_std_sql_where_restrict_filter('AND', 'id');
127      break;
128    }
129
130    case 'updated_categories':
131    {
132      $query = '
133  FROM '.IMAGES_TABLE.'
134    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = id
135  WHERE 1=1';
136      if (!empty($start))
137      {
138        $query.= '
139    AND date_available > \''.$start.'\'';
140      }
141      if (!empty($end))
142      {
143        $query.= '
144    AND date_available <= \''.$end.'\'';
145      }
146      $query.= get_std_sql_where_restrict_filter('AND', 'id');
147      break;
148    }
149
150    case 'new_users':
151    {
152      $query = '
153  FROM '.USER_INFOS_TABLE.'
154  WHERE 1=1';
155      if (!empty($start))
156      {
157        $query.= '
158    AND registration_date > \''.$start.'\'';
159      }
160      if (!empty($end))
161      {
162        $query.= '
163    AND registration_date <= \''.$end.'\'';
164      }
165      break;
166    }
167
168    default:
169      return null; // stop and return nothing
170  }
171
172  switch($action)
173  {
174    case 'count':
175    {
176      switch($type)
177      {
178        case 'new_comments':
179          $field_id = 'c.id';
180          break;
181        case 'unvalidated_comments':
182          $field_id = 'id';
183          break;
184        case 'new_elements':
185          $field_id = 'image_id';
186          break;
187        case 'updated_categories':
188          $field_id = 'category_id';
189          break;
190        case 'new_users':
191          $field_id = 'user_id';
192          break;
193      }
194      $query = 'SELECT COUNT(DISTINCT '.$field_id.') '.$query.';';
195      list($count) = pwg_db_fetch_row(pwg_query($query));
196      return $count;
197      break;
198    }
199
200    case 'info':
201    {
202      switch($type)
203      {
204        case 'new_comments':
205          $field_id = 'c.id';
206          break;
207        case 'unvalidated_comments':
208          $field_id = 'id';
209          break;
210        case 'new_elements':
211          $field_id = 'image_id';
212          break;
213        case 'updated_categories':
214          $field_id = 'category_id';
215          break;
216        case 'new_users':
217          $field_id = 'user_id';
218          break;
219      }
220      $query = 'SELECT DISTINCT '.$field_id.' '.$query.';';
221      $infos = array_from_query($query);
222      return $infos;
223      break;
224    }
225
226    default:
227      return null; // stop and return nothing
228  }
229}
230
231/**
232 * Returns number of new comments between two dates.
233 *
234 * @param string $start (mysql datetime format)
235 * @param string $end (mysql datetime format)
236 * @return int
237 */
238function nb_new_comments($start=null, $end=null)
239{
240  return custom_notification_query('count', 'new_comments', $start, $end);
241}
242
243/**
244 * Returns new comments between two dates.
245 *
246 * @param string $start (mysql datetime format)
247 * @param string $end (mysql datetime format)
248 * @return int[] comment ids
249 */
250function new_comments($start=null, $end=null)
251{
252  return custom_notification_query('info', 'new_comments', $start, $end);
253}
254
255/**
256 * Returns number of unvalidated comments between two dates.
257 *
258 * @param string $start (mysql datetime format)
259 * @param string $end (mysql datetime format)
260 * @return int
261 */
262function nb_unvalidated_comments($start=null, $end=null)
263{
264  return custom_notification_query('count', 'unvalidated_comments', $start, $end);
265}
266
267
268/**
269 * Returns number of new photos between two dates.
270 *
271 * @param string $start (mysql datetime format)
272 * @param string $end (mysql datetime format)
273 * @return int
274 */
275function nb_new_elements($start=null, $end=null)
276{
277  return custom_notification_query('count', 'new_elements', $start, $end);
278}
279
280/**
281 * Returns new photos between two dates.es
282 *
283 * @param string $start (mysql datetime format)
284 * @param string $end (mysql datetime format)
285 * @return int[] photos ids
286 */
287function new_elements($start=null, $end=null)
288{
289  return custom_notification_query('info', 'new_elements', $start, $end);
290}
291
292/**
293 * Returns number of updated categories between two dates.
294 *
295 * @param string $start (mysql datetime format)
296 * @param string $end (mysql datetime format)
297 * @return int
298 */
299function nb_updated_categories($start=null, $end=null)
300{
301  return custom_notification_query('count', 'updated_categories', $start, $end);
302}
303
304/**
305 * Returns updated categories between two dates.
306 *
307 * @param string $start (mysql datetime format)
308 * @param string $end (mysql datetime format)
309 * @return int[] categories ids
310 */
311function updated_categories($start=null, $end=null)
312{
313  return custom_notification_query('info', 'updated_categories', $start, $end);
314}
315
316/**
317 * Returns number of new users between two dates.
318 *
319 * @param string $start (mysql datetime format)
320 * @param string $end (mysql datetime format)
321 * @return int
322 */
323function nb_new_users($start=null, $end=null)
324{
325  return custom_notification_query('count', 'new_users', $start, $end);
326}
327
328/**
329 * Returns new users between two dates.
330 *
331 * @param string $start (mysql datetime format)
332 * @param string $end (mysql datetime format)
333 * @return int[] user ids
334 */
335function new_users($start=null, $end=null)
336{
337  return custom_notification_query('info', 'new_users', $start, $end);
338}
339
340/**
341 * Returns if there was new activity between two dates.
342 *
343 * Takes in account: number of new comments, number of new elements, number of
344 * updated categories. Administrators are also informed about: number of
345 * unvalidated comments, number of new users.
346 * TODO : number of unvalidated elements
347 *
348 * @param string $start (mysql datetime format)
349 * @param string $end (mysql datetime format)
350 * @return boolean
351 */
352function news_exists($start=null, $end=null)
353{
354  return (
355          (nb_new_comments($start, $end) > 0) or
356          (nb_new_elements($start, $end) > 0) or
357          (nb_updated_categories($start, $end) > 0) or
358          ((is_admin()) and (nb_unvalidated_comments($start, $end) > 0)) or
359          ((is_admin()) and (nb_new_users($start, $end) > 0)));
360}
361
362/**
363 * Formats a news line and adds it to the array (e.g. '5 new elements')
364 *
365 * @param array $news
366 * @param int $count
367 * @param string $singular_key
368 * @param string $plural_key
369 * @param string $url
370 * @param bool $add_url
371 */
372function add_news_line(&$news, $count, $singular_key, $plural_key, $url='', $add_url=false)
373{
374  if ($count > 0)
375  {
376    $line = l10n_dec($singular_key, $plural_key, $count);
377    if ($add_url and !empty($url) )
378    {
379      $line = '<a href="'.$url.'">'.$line.'</a>';
380    }
381    $news[] = $line;
382  }
383}
384
385/**
386 * Returns new activity between two dates.
387 *
388 * Takes in account: number of new comments, number of new elements, number of
389 * updated categories. Administrators are also informed about: number of
390 * unvalidated comments, number of new users.
391 * TODO : number of unvalidated elements
392 *
393 * @param string $start (mysql datetime format)
394 * @param string $end (mysql datetime format)
395 * @param bool $exclude_img_cats if true, no info about new images/categories
396 * @param bool $add_url add html link around news
397 * @return array
398 */
399function news($start=null, $end=null, $exclude_img_cats=false, $add_url=false)
400{
401  $news = array();
402
403  if (!$exclude_img_cats)
404  {
405    add_news_line( $news,
406      nb_new_elements($start, $end), '%d new photo', '%d new photos',
407      make_index_url(array('section'=>'recent_pics')), $add_url );
408  }
409
410  if (!$exclude_img_cats)
411  {
412    add_news_line( $news,
413      nb_updated_categories($start, $end), '%d album updated', '%d albums updated',
414      make_index_url(array('section'=>'recent_cats')), $add_url );
415  }
416
417  add_news_line( $news,
418      nb_new_comments($start, $end), '%d new comment', '%d new comments',
419      get_root_url().'comments.php', $add_url );
420
421  if (is_admin())
422  {
423    add_news_line( $news,
424        nb_unvalidated_comments($start, $end), '%d comment to validate', '%d comments to validate',
425        get_root_url().'admin.php?page=comments', $add_url );
426
427    add_news_line( $news,
428        nb_new_users($start, $end), '%d new user', '%d new users',
429        get_root_url().'admin.php?page=user_list', $add_url );
430  }
431
432  return $news;
433}
434
435/**
436 * Returns information about recently published elements grouped by post date.
437 *
438 * @param int $max_dates maximum number of recent dates
439 * @param int $max_elements maximum number of elements per date
440 * @param int $max_cats maximum number of categories per date
441 * @return array
442 */
443function get_recent_post_dates($max_dates, $max_elements, $max_cats)
444{
445  global $conf, $user;
446
447  $where_sql = get_std_sql_where_restrict_filter('WHERE', 'i.id', true);
448
449  $query = '
450SELECT
451    date_available,
452    COUNT(DISTINCT id) AS nb_elements,
453    COUNT(DISTINCT category_id) AS nb_cats
454  FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
455  '.$where_sql.'
456  GROUP BY date_available
457  ORDER BY date_available DESC
458  LIMIT '.$max_dates.'
459;';
460  $dates = array_from_query($query);
461
462  for ($i=0; $i<count($dates); $i++)
463  {
464    if ($max_elements>0)
465    { // get some thumbnails ...
466      $query = '
467SELECT DISTINCT i.*
468  FROM '.IMAGES_TABLE.' i
469    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id=image_id
470  '.$where_sql.'
471    AND date_available=\''.$dates[$i]['date_available'].'\'
472  ORDER BY '.DB_RANDOM_FUNCTION.'()
473  LIMIT '.$max_elements.'
474;';
475      $dates[$i]['elements'] = array_from_query($query);
476    }
477
478    if ($max_cats>0)
479    {// get some categories ...
480      $query = '
481SELECT
482    DISTINCT c.uppercats,
483    COUNT(DISTINCT i.id) AS img_count
484  FROM '.IMAGES_TABLE.' i
485    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id=image_id
486    INNER JOIN '.CATEGORIES_TABLE.' c ON c.id=category_id
487  '.$where_sql.'
488    AND date_available=\''.$dates[$i]['date_available'].'\'
489  GROUP BY category_id, c.uppercats
490  ORDER BY img_count DESC
491  LIMIT '.$max_cats.'
492;';
493      $dates[$i]['categories'] = array_from_query($query);
494    }
495  }
496
497  return $dates;
498}
499
500/**
501 * Returns information about recently published elements grouped by post date.
502 * Same as get_recent_post_dates() but parameters as an indexed array.
503 * @see get_recent_post_dates()
504 *
505 * @param array $args
506 * @return array
507 */
508function get_recent_post_dates_array($args)
509{
510  return get_recent_post_dates(
511    (empty($args['max_dates']) ? 3 : $args['max_dates']),
512    (empty($args['max_elements']) ? 3 : $args['max_elements']),
513    (empty($args['max_cats']) ? 3 : $args['max_cats'])
514    );
515}
516
517
518/**
519 * Returns html description about recently published elements grouped by post date.
520 * TODO : clean up HTML output, currently messy and invalid !
521 *
522 * @param array $date_detail returned value of get_recent_post_dates()
523 * @return string
524 */
525function get_html_description_recent_post_date($date_detail)
526{
527  global $conf;
528
529  $description = '<ul>';
530
531  $description .=
532        '<li>'
533        .l10n_dec('%d new photo', '%d new photos', $date_detail['nb_elements'])
534        .' ('
535        .'<a href="'.make_index_url(array('section'=>'recent_pics')).'">'
536          .l10n('Recent photos').'</a>'
537        .')'
538        .'</li><br>';
539
540  foreach($date_detail['elements'] as $element)
541  {
542    $tn_src = DerivativeImage::thumb_url($element);
543    $description .= '<a href="'.
544                    make_picture_url(array(
545                        'image_id' => $element['id'],
546                        'image_file' => $element['file'],
547                      ))
548                    .'"><img src="'.$tn_src.'"></a>';
549  }
550  $description .= '...<br>';
551
552  $description .=
553        '<li>'
554        .l10n_dec('%d album updated', '%d albums updated', $date_detail['nb_cats'])
555        .'</li>';
556
557  $description .= '<ul>';
558  foreach($date_detail['categories'] as $cat)
559  {
560    $description .=
561          '<li>'
562          .get_cat_display_name_cache($cat['uppercats'])
563          .' ('.
564          l10n_dec('%d new photo', '%d new photos', $cat['img_count']).')'
565          .'</li>';
566  }
567  $description .= '</ul>';
568
569  $description .= '</ul>';
570
571  return $description;
572}
573
574/**
575 * Returns title about recently published elements grouped by post date.
576 *
577 * @param array $date_detail returned value of get_recent_post_dates()
578 * @return string
579 */
580function get_title_recent_post_date($date_detail)
581{
582  global $lang;
583
584  $date = $date_detail['date_available'];
585  $exploded_date = strptime($date, '%Y-%m-%d %H:%M:%S');
586
587  $title = l10n_dec('%d new photo', '%d new photos', $date_detail['nb_elements']);
588  $title .= ' ('.$lang['month'][1+$exploded_date['tm_mon']].' '.$exploded_date['tm_mday'].')';
589
590  return $title;
591}
592
593if (!function_exists('strptime'))
594{
595  function strptime($date, $fmt)
596  {
597    if ($fmt != '%Y-%m-%d %H:%M:%S')
598      die('Invalid strptime format '.$fmt);
599    list($y,$m,$d,$H,$M,$S) = preg_split('/[-: ]/', $date);
600    $res = localtime( mktime($H,$M,$S,$m,$d,$y), true );
601    return $res;
602  }
603}
604
605?>
Note: See TracBrowser for help on using the repository browser.