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

Last change on this file since 1639 was 1639, checked in by rvelices, 17 years ago

put some functionnality from feed.php into a function (to be used
later in the notification by email)

File size: 14.0 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-11-26 21:15:50 +0100 (sam., 26 nov. 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 958 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28// +-----------------------------------------------------------------------+
29// |                               functions                               |
30// +-----------------------------------------------------------------------+
31
32/*
33 * Execute custom notification query
34 *
35 * @param string action ('count' or 'info')
36 * @param string type of query ('new_comments', 'unvalidated_comments', 'new_elements', 'updated_categories', 'new_users', 'waiting_elements')
37 * @param string start (mysql datetime format)
38 * @param string end (mysql datetime format)
39 *
40 * @return integer for action count
41 *         array for info
42 */
43function custom_notification_query($action, $type, $start, $end)
44{
45  global $user;
46
47  switch($type)
48  {
49    case 'new_comments':
50      $query = '
51  FROM '.COMMENTS_TABLE.' AS c
52     , '.IMAGE_CATEGORY_TABLE.' AS ic
53  WHERE c.image_id = ic.image_id
54    AND c.validation_date > \''.$start.'\'
55    AND c.validation_date <= \''.$end.'\'
56    AND category_id NOT IN ('.$user['forbidden_categories'].')
57;';
58      break;
59    case 'unvalidated_comments':
60      $query = '
61  FROM '.COMMENTS_TABLE.'
62  WHERE date <= \''.$end.'\'
63    AND (validated = \'false\'
64         OR validation_date > \''.$end.'\')
65;';
66      break;
67    case 'new_elements':
68      $query = '
69  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
70  WHERE date_available > \''.$start.'\'
71    AND date_available <= \''.$end.'\'
72    AND category_id NOT IN ('.$user['forbidden_categories'].')
73;';
74      break;
75    case 'updated_categories':
76      $query = '
77  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
78  WHERE date_available > \''.$start.'\'
79    AND date_available <= \''.$end.'\'
80    AND category_id NOT IN ('.$user['forbidden_categories'].')
81;';
82      break;
83    case 'new_users':
84      $query = '
85  FROM '.USER_INFOS_TABLE.'
86  WHERE registration_date > \''.$start.'\'
87    AND registration_date <= \''.$end.'\'
88;';
89      break;
90    case 'waiting_elements':
91      $query = '
92  FROM '.WAITING_TABLE.'
93  WHERE validated = \'false\'
94;';
95      break;
96    default:
97      // stop this function and return nothing
98      return;
99      break;
100  }
101
102  switch($action)
103  {
104    case 'count':
105      switch($type)
106      {
107        case 'new_comments':
108          $field_id = 'c.id';
109          break;
110        case 'unvalidated_comments':
111          $field_id = 'id';
112          break;
113        case 'new_elements':
114          $field_id = 'image_id';
115          break;
116        case 'updated_categories':
117          $field_id = 'category_id';
118          break;
119        case 'new_users':
120          $field_id = 'user_id';
121          break;
122        case 'waiting_elements':
123          $field_id = 'id';
124          break;
125    }
126    $query = 'SELECT count(distinct '.$field_id.') as CountId
127'.$query;
128    list($count) = mysql_fetch_array(pwg_query($query));
129    return $count;
130
131    break;
132    case 'info':
133      switch($type)
134      {
135        case 'new_comments':
136          $fields = array('c.id');
137          break;
138        case 'unvalidated_comments':
139          $fields = array('id');
140          break;
141        case 'new_elements':
142          $fields = array('image_id');
143          break;
144        case 'updated_categories':
145          $fields = array('category_id');
146          break;
147        case 'new_users':
148          $fields = array('user_id');
149          break;
150        case 'waiting_elements':
151          $fields = array('id');
152          break;
153      }
154
155    $query = 'SELECT distinct '.implode(', ', $fields).'
156'.$query;
157    $result = pwg_query($query);
158
159    $infos = array();
160
161    while ($row = mysql_fetch_array($result))
162    {
163      array_push($infos, $row);
164    }
165
166    return $infos;
167
168    break;
169  }
170
171  //return is done on previous switch($action)
172}
173
174/**
175 * new comments between two dates, according to authorized categories
176 *
177 * @param string start (mysql datetime format)
178 * @param string end (mysql datetime format)
179 * @param string forbidden categories (comma separated)
180 * @return count comment ids
181 */
182function nb_new_comments($start, $end)
183{
184  return custom_notification_query('count', 'new_comments', $start, $end);
185}
186
187/**
188 * new comments between two dates, according to authorized categories
189 *
190 * @param string start (mysql datetime format)
191 * @param string end (mysql datetime format)
192 * @param string forbidden categories (comma separated)
193 * @return array comment ids
194 */
195function new_comments($start, $end)
196{
197  return custom_notification_query('info', 'new_comments', $start, $end);
198}
199
200/**
201 * unvalidated at a precise date
202 *
203 * Comments that are registered and not validated yet on a precise date
204 *
205 * @param string date (mysql datetime format)
206 * @return count comment ids
207 */
208function nb_unvalidated_comments($date)
209{
210  return custom_notification_query('count', 'unvalidated_comments', $date, $date);
211}
212
213/**
214 * unvalidated at a precise date
215 *
216 * Comments that are registered and not validated yet on a precise date
217 *
218 * @param string date (mysql datetime format)
219 * @return array comment ids
220 */
221function unvalidated_comments($date)
222{
223  return custom_notification_query('info', 'unvalidated_comments', $start, $end);
224}
225
226/**
227 * new elements between two dates, according to authorized categories
228 *
229 * @param string start (mysql datetime format)
230 * @param string end (mysql datetime format)
231 * @param string forbidden categories (comma separated)
232 * @return count element ids
233 */
234function nb_new_elements($start, $end)
235{
236  return custom_notification_query('count', 'new_elements', $start, $end);
237}
238
239/**
240 * new elements between two dates, according to authorized categories
241 *
242 * @param string start (mysql datetime format)
243 * @param string end (mysql datetime format)
244 * @param string forbidden categories (comma separated)
245 * @return array element ids
246 */
247function new_elements($start, $end)
248{
249  return custom_notification_query('info', 'new_elements', $start, $end);
250}
251
252/**
253 * updated categories between two dates, according to authorized categories
254 *
255 * @param string start (mysql datetime format)
256 * @param string end (mysql datetime format)
257 * @param string forbidden categories (comma separated)
258 * @return count element ids
259 */
260function nb_updated_categories($start, $end)
261{
262  return custom_notification_query('count', 'updated_categories', $start, $end);
263}
264
265/**
266 * updated categories between two dates, according to authorized categories
267 *
268 * @param string start (mysql datetime format)
269 * @param string end (mysql datetime format)
270 * @param string forbidden categories (comma separated)
271 * @return array element ids
272 */
273function updated_categories($start, $end)
274{
275  return custom_notification_query('info', 'updated_categories', $start, $end);
276}
277
278/**
279 * new registered users between two dates
280 *
281 * @param string start (mysql datetime format)
282 * @param string end (mysql datetime format)
283 * @return count user ids
284 */
285function nb_new_users($start, $end)
286{
287  return custom_notification_query('count', 'new_users', $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 array user ids
296 */
297function new_users($start, $end)
298{
299  return custom_notification_query('info', 'new_users', $start, $end);
300}
301
302/**
303 * currently waiting pictures
304 *
305 * @return count waiting ids
306 */
307function nb_waiting_elements()
308{
309  return custom_notification_query('count', 'waiting_elements', '', '');
310}
311
312/**
313 * currently waiting pictures
314 *
315 * @return array waiting ids
316 */
317function waiting_elements()
318{
319  return custom_notification_query('info', 'waiting_elements', $start, $end);
320}
321
322/**
323 * There are new between two dates ?
324 *
325 * Informations : number of new comments, number of new elements, number of
326 * updated categories. Administrators are also informed about : number of
327 * unvalidated comments, number of new users (TODO : number of unvalidated
328 * elements)
329 *
330 * @param string start date (mysql datetime format)
331 * @param string end date (mysql datetime format)
332 *
333 * @return boolean : true if exist news else false
334 */
335function news_exists($start, $end)
336{
337  return (
338          (nb_new_comments($start, $end) > 0) or
339          (nb_new_elements($start, $end) > 0) or
340          (nb_updated_categories($start, $end) > 0) or
341          ((is_admin()) and (nb_unvalidated_comments($end) > 0)) or
342          ((is_admin()) and (nb_new_users($start, $end) > 0)) or
343          ((is_admin()) and (nb_waiting_elements() > 0))
344        );
345}
346
347/**
348 * Formats a news line and adds it to the array (e.g. '5 new elements')
349 */
350function add_news_line(&$news, $count, $singular_fmt_key, $plural_fmt_key, $url='', $add_url=false)
351{
352  if ($count > 0)
353  {
354    $line = l10n_dec($singular_fmt_key, $plural_fmt_key, $count);
355    if ($add_url and !empty($url) )
356    {
357      $line = '<a href="'.$url.'">'.$line.'</a>';
358    }
359    array_push($news, $line);
360  }
361}
362
363/**
364 * What's new between two dates ?
365 *
366 * Informations : number of new comments, number of new elements, number of
367 * updated categories. Administrators are also informed about : number of
368 * unvalidated comments, number of new users (TODO : number of unvalidated
369 * elements)
370 *
371 * @param string start date (mysql datetime format)
372 * @param string end date (mysql datetime format)
373 * @param bool exclude_img_cats if true, no info about new images/categories
374 * @param bool add_url add html A link around news
375 *
376 * @return array of news
377 */
378function news($start, $end, $exclude_img_cats=false, $add_url=false)
379{
380  $news = array();
381
382  if (!$exclude_img_cats)
383  {
384    add_news_line( $news,
385      nb_new_elements($start, $end), '%d new element', '%d new elements');
386  }
387
388  if (!$exclude_img_cats)
389  {
390    add_news_line( $news,
391      nb_updated_categories($start, $end), '%d category updated', '%d categories updated');
392  }
393
394  add_news_line( $news,
395      nb_new_comments($start, $end), '%d new comment', '%d new comments',
396      get_root_url().'comments.php', $add_url );
397
398  if (is_admin())
399  {
400    add_news_line( $news,
401        nb_unvalidated_comments($end), '%d comment to validate', '%d comments to validate',
402        get_root_url().'admin.php?page=comments', $add_url );
403
404    add_news_line( $news,
405        nb_new_users($start, $end), '%d new user', '%d new users',
406        get_root_url().'admin.php?page=user_list', $add_url );
407
408    add_news_line( $news,
409        nb_waiting_elements(), '%d waiting element', '%d waiting elements',
410        get_root_url().'admin.php?page=waiting', $add_url );
411  }
412
413  return $news;
414}
415
416/**
417 * returns information about recently published elements grouped by post date
418 * @param int max_dates maximum returned number of recent dates
419 * @param int max_elements maximum returned number of elements per date
420 * @param int max_cats maximum returned number of categories per date
421 */
422function get_recent_post_dates($max_dates, $max_elements, $max_cats)
423{
424  global $conf, $user;
425
426  $where_sql = 'WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
427
428  $query = '
429SELECT date_available,
430      COUNT(DISTINCT id) nb_elements,
431      COUNT(DISTINCT category_id) nb_cats
432  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
433  '.$where_sql.'
434  GROUP BY date_available
435  ORDER BY date_available DESC
436  LIMIT 0,'.$max_dates.'
437;';
438  $result = pwg_query($query);
439  $dates = array();
440  while ($row = mysql_fetch_assoc($result))
441  {
442    array_push($dates, $row);
443  }
444
445  for ($i=0; $i<count($dates); $i++)
446  {
447    if ($max_elements>0)
448    { // get some thumbnails ...
449      $query = '
450SELECT DISTINCT id, path, name, tn_ext
451  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
452  '.$where_sql.'
453    AND date_available="'.$dates[$i]['date_available'].'"
454    AND tn_ext IS NOT NULL
455  LIMIT 0,'.$max_elements.'
456;';
457      $dates[$i]['elements'] = array();
458      $result = pwg_query($query);
459      while ($row = mysql_fetch_assoc($result))
460      {
461        array_push($dates[$i]['elements'], $row);
462      }
463    }
464
465    if ($max_cats>0)
466    {// get some categories ...
467      $query = '
468SELECT DISTINCT c.uppercats, COUNT(DISTINCT i.id) img_count
469  FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id
470    INNER JOIN '.CATEGORIES_TABLE.' c ON c.id=category_id
471  '.$where_sql.'
472    AND date_available="'.$dates[$i]['date_available'].'"
473  GROUP BY category_id
474  ORDER BY img_count DESC
475  LIMIT 0,'.$max_cats.'
476;';
477      $dates[$i]['categories'] = array();
478      $result = pwg_query($query);
479      while ($row = mysql_fetch_assoc($result))
480      {
481        array_push($dates[$i]['categories'], $row);
482      }
483    }
484  }
485  return $dates;
486}
487?>
Note: See TracBrowser for help on using the repository browser.