source: branches/2.1/include/functions_notification.inc.php @ 6630

Last change on this file since 6630 was 6598, checked in by nikrou, 14 years ago

Bug 1736 fixed : Complete RSS Feed returns error start or end dates can be null
merge from trunk

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