source: branches/2.1/include/functions_notification.inc.php @ 6669

Last change on this file since 6669 was 6669, checked in by rvelices, 14 years ago

merge r6668 from trunk

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