source: branches/branch-1_7/include/functions_notification.inc.php @ 2139

Last change on this file since 2139 was 1959, checked in by rub, 18 years ago

Merge BSF 1956:1958 into branch-1_7

Small improvement:

Random selection of pictures on notifications.

Caddie:

Fix issue when the are not pictures attached to disassociated categories

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