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

Last change on this file since 4620 was 4542, checked in by rub, 15 years ago

Feature 1255: modification in sql queries
correction

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