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

Last change on this file since 1401 was 1112, checked in by rub, 19 years ago

[NBM] Step 5: Change notification queries in order to try to optimize treatment duration.

File size: 11.7 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 * What's new between two dates ?
349 *
350 * Informations : number of new comments, number of new elements, number of
351 * updated categories. Administrators are also informed about : number of
352 * unvalidated comments, number of new users (TODO : number of unvalidated
353 * elements)
354 *
355 * @param string start date (mysql datetime format)
356 * @param string end date (mysql datetime format)
357 *
358 * @return array of news
359 */
360function news($start, $end)
361{
362  $news = array();
363
364  $nb_new_comments = nb_new_comments($start, $end);
365  if ($nb_new_comments > 0)
366  {
367    array_push($news, sprintf(l10n('%d new comments'), $nb_new_comments));
368  }
369
370  $nb_new_elements = nb_new_elements($start, $end);
371  if ($nb_new_elements > 0)
372  {
373    array_push($news, sprintf(l10n('%d new elements'), $nb_new_elements));
374  }
375
376  $nb_updated_categories = nb_updated_categories($start, $end);
377  if ($nb_updated_categories > 0)
378  {
379    array_push($news, sprintf(l10n('%d categories updated'),
380                              $nb_updated_categories));
381  }
382 
383  if (is_admin())
384  {
385    $nb_unvalidated_comments = nb_unvalidated_comments($end);
386    if ($nb_unvalidated_comments > 0)
387    {
388      array_push($news, sprintf(l10n('%d comments to validate'),
389                                $nb_unvalidated_comments));
390    }
391
392    $nb_new_users = nb_new_users($start, $end);
393    if ($nb_new_users > 0)
394    {
395      array_push($news, sprintf(l10n('%d new users'), $nb_new_users));
396    }
397
398    $nb_waiting_elements = nb_waiting_elements();
399    if ($nb_waiting_elements > 0)
400    {
401      array_push(
402        $news,
403        sprintf(
404          l10n('%d waiting elements'),
405          $nb_waiting_elements
406          )
407        );
408    }
409  }
410
411  return $news;
412}
413
414?>
Note: See TracBrowser for help on using the repository browser.