source: branches/1.7/include/functions_notification.inc.php @ 24883

Last change on this file since 24883 was 2336, checked in by rub, 17 years ago

Admin waiting page was changed to upload, there are a lot time...

Merge BSF 2334:2335 into branch-1_7

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