source: trunk/admin/notification_by_mail.php @ 1116

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

[NBM] Step 7: Add functionalities subscribe/unsubscribe:

o reduce length of check_key
o fix bugs
o send mail on subscribe/unsubscribe
o add and used $conf parameters
o review keyword of languages
o improve selection/check
o can subscribe/unsubscribe with a link include on mail
o fix bug mass_update collate

  • Property svn:eol-style set to native
  • 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// | Copyright (C) 2006 Ruben ARNAUD - team@phpwebgallery.net              |
7// +-----------------------------------------------------------------------+
8// | branch        : BSF (Best So Far)
9// | file          : $RCSfile$
10// | last update   : $Date: 2006-04-01 01:14:57 +0000 (Sat, 01 Apr 2006) $
11// | last modifier : $Author: rub $
12// | revision      : $Revision: 1116 $
13// +-----------------------------------------------------------------------+
14// | This program is free software; you can redistribute it and/or modify  |
15// | it under the terms of the GNU General Public License as published by  |
16// | the Free Software Foundation                                          |
17// |                                                                       |
18// | This program is distributed in the hope that it will be useful, but   |
19// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
20// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
21// | General Public License for more details.                              |
22// |                                                                       |
23// | You should have received a copy of the GNU General Public License     |
24// | along with this program; if not, write to the Free Software           |
25// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
26// | USA.                                                                  |
27// +-----------------------------------------------------------------------+
28
29// +-----------------------------------------------------------------------+
30// | include                                                               |
31// +-----------------------------------------------------------------------+
32
33if (!defined('PHPWG_ROOT_PATH'))
34{
35  die ("Hacking attempt!");
36}
37
38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
39include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
40include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
41include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
42include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
43
44// +-----------------------------------------------------------------------+
45// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49// +-----------------------------------------------------------------------+
50// | functions                                                             |
51// +-----------------------------------------------------------------------+
52
53/*
54 * Get the authorized_status for each tab
55 * return corresponding status
56 */
57function get_tab_status($mode)
58{
59  $result = ACCESS_WEBMASTER;
60  switch ($mode)
61  {
62    case 'param':
63    case 'subscribe':
64      $result = ACCESS_WEBMASTER;
65      break;
66    case 'send':
67      $result = ACCESS_ADMINISTRATOR;
68      break;
69    default:
70      $result = ACCESS_WEBMASTER;
71      break;
72  }
73  return $result;
74}
75
76/*
77 * Inserting News users
78 */
79function insert_new_data_user_mail_notification()
80{
81  global $conf, $page;
82
83  // Set null mail_address empty
84  $query = '
85update
86  '.USERS_TABLE.'
87set
88  '.$conf['user_fields']['email'].' = null
89where
90  trim('.$conf['user_fields']['email'].') = \'\';';
91  pwg_query($query);
92
93  // null mail_address are not selected in the list
94  $query = '
95select
96  u.'.$conf['user_fields']['id'].' as user_id,
97  u.'.$conf['user_fields']['username'].' as username,
98  u.'.$conf['user_fields']['email'].' as mail_address
99from
100  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.'.$conf['user_fields']['id'].' = m.user_id
101where
102  u.'.$conf['user_fields']['email'].' is not null and
103  m.user_id is null
104order by
105  user_id;';
106
107  $result = pwg_query($query);
108
109  if (mysql_num_rows($result) > 0)
110  {
111    $inserts = array();
112    $check_key_list = array();
113
114    while ($nbm_user = mysql_fetch_array($result))
115    {
116      // Calculate key
117      $nbm_user['check_key'] = find_available_check_key();
118
119      // Save key
120      array_push($check_key_list, $nbm_user['check_key']);
121
122      // Insert new nbm_users
123      array_push
124      (
125        $inserts, 
126        array
127        (
128          'user_id' => $nbm_user['user_id'],
129          'check_key' => $nbm_user['check_key'],
130          'enabled' => 'false' // By default if false, set to true with specific functions
131        )
132      );
133
134      array_push($page['infos'], sprintf(l10n('nbm_user_x_added'), $nbm_user['username'], $nbm_user['mail_address']));
135    }
136
137    // Insert new nbm_users
138    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
139    // Update field enabled with specific function
140    do_subscribe_unsubcribe_notification_by_mail
141    (
142      true,
143      $conf['nbm_default_value_user_enabled'],
144      $check_key_list
145    );
146  }
147}
148
149/*
150 * Send mail for notification to all users
151 * Return list of "treated/selected" users
152 */
153function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
154{
155  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
156  $return_list = array();
157 
158  if (in_array($action, array('list_to_send', 'send')))
159  {
160    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
161
162    $is_action_send = ($action == 'send');
163
164    if (isset($customize_mail_content))
165    {
166      $customize_mail_content = $conf['nbm_complementary_mail_content'];
167    }
168
169    // disabled and null mail_address are not selected in the list
170    $data_users = get_user_notifications('send', $check_key_list);
171
172    // Check if exist news to list user or send mails
173    if (($conf['nbm_list_all_enabled_users_to_send'] == false) or ($is_action_send))
174    {
175      if (count($data_users) > 0)
176      {
177        $datas = array();
178
179        // Begin nbm users environment
180        begin_users_env_nbm($is_action_send);
181
182        foreach ($data_users as $nbm_user)
183        {
184          if ((!$is_action_send) and (count($return_list) >= $conf['nbm_max_list_users_to_send']))
185          {
186            // Stop fill list on 'list_to_send', if the quota is override
187            array_push($page['infos'], sprintf(l10n('nbm_break_list_user'), $conf['nbm_max_list_users_to_send']));
188            break;
189          }
190          if (($is_action_send) and (count($return_list) >= $conf['nbm_max_mails_send']))
191          {
192            // Stop fill list on 'send', if the quota is override
193            array_push($page['errors'], sprintf(l10n('nbm_nbm_break_send_mail'), $conf['nbm_max_mails_send']));
194            break;
195          }
196
197          // set env nbm user
198          set_user_id_on_env_nbm($nbm_user['user_id']);
199
200          if ($is_action_send)
201          {
202            $message = '';
203
204            if ($conf['nbm_send_detailed_content'])
205            {
206               $news = news($nbm_user['last_send'], $dbnow);
207               $exist_data = count($news) > 0;
208            }
209            else
210            {
211              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
212            }
213
214            if ($exist_data)
215            {
216              array_push($return_list, $nbm_user);
217
218              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
219              $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n";
220
221              if (!is_null($nbm_user['last_send']))
222                $message .= sprintf(l10n('nbm_content_new_elements_between'), $nbm_user['last_send'], $dbnow);
223              else
224                $message .= sprintf(l10n('nbm_content_new_elements'), $dbnow);
225
226              if ($conf['nbm_send_detailed_content'])
227              {
228                $message .= ":\n";
229
230                foreach ($news as $line)
231                {
232                  $message .= '  o '.$line."\n";
233                }
234                $message .= "\n";
235              }
236              else
237              {
238                $message .= ".\n";
239              }
240
241              $message .= sprintf(l10n('nbm_content_goto'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
242              $message .= $customize_mail_content."\n\n";
243              $message .= l10n('nbm_content_byebye')."\n   ".$env_nbm['send_as_name']."\n\n";
244
245              $message .= get_mail_content_subscribe_unsubcribe($nbm_user);
246
247              if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message))
248              {
249                inc_mail_sent_success($nbm_user);
250
251                $data = array('user_id' => $nbm_user['user_id'],
252                              'last_send' => $dbnow);
253                array_push($datas, $data);
254              }
255              else
256              {
257                inc_mail_sent_failed($nbm_user);
258              }
259            }
260          }
261          else
262          {
263            if (news_exists($nbm_user['last_send'], $dbnow))
264            {
265              array_push($return_list, $nbm_user);
266            }
267          }
268        }
269
270        // Restore nbm environment
271        end_users_env_nbm();
272
273        if ($is_action_send)
274        {
275          mass_updates(
276            USER_MAIL_NOTIFICATION_TABLE,
277            array(
278              'primary' => array('user_id'),
279              'update' => array('last_send')
280             ),
281             $datas
282             );
283
284          display_counter_info();
285        }
286      }
287      else
288      {
289        if ($is_action_send)
290        {
291          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
292        }
293      }
294    }
295    else
296    {
297      // Quick List, don't check news
298      $return_list = $data_users;
299    }
300  }
301  return $return_list;
302}
303
304// +-----------------------------------------------------------------------+
305// | Main                                                                  |
306// +-----------------------------------------------------------------------+
307if (!isset($_GET['mode']))
308{
309  $page['mode'] = 'send';
310}
311else
312{
313  $page['mode'] = $_GET['mode'];
314}
315
316// +-----------------------------------------------------------------------+
317// | Check Access and exit when user status is not ok                      |
318// +-----------------------------------------------------------------------+
319check_status(get_tab_status($page['mode']));
320
321// +-----------------------------------------------------------------------+
322// | Insert new users with mails                                           |
323// +-----------------------------------------------------------------------+
324if (!isset($_POST) or (count($_POST) ==0))
325{
326  // No insert data in post mode
327  insert_new_data_user_mail_notification();
328}
329
330// +-----------------------------------------------------------------------+
331// | Treatment of tab post                                                 |
332// +-----------------------------------------------------------------------+
333switch ($page['mode'])
334{
335  case 'param' :
336  {
337    $updated_param_count = 0;
338    // Update param
339    $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
340    while ($nbm_user = mysql_fetch_array($result))
341    {
342      if (isset($_POST['param_submit']))
343      {
344        if (isset($_POST[$nbm_user['param']]))
345        {
346          $value = $_POST[$nbm_user['param']];
347
348          $query = '
349update
350  '.CONFIG_TABLE.'
351set
352  value = \''. str_replace("\'", "''", $value).'\'
353where
354  param = \''.$nbm_user['param'].'\';';
355          pwg_query($query);
356          $updated_param_count += 1;
357        }
358      }
359
360      $conf[$nbm_user['param']] = $nbm_user['value'];
361
362      // if the parameter is present in $_POST array (if a form is submited), we
363      // override it with the submited value
364      if (isset($_POST[$nbm_user['param']]))
365      {
366        $conf[$nbm_user['param']] = stripslashes($_POST[$nbm_user['param']]);
367      }
368
369      // If the field is true or false, the variable is transformed into a
370      // boolean value.
371      if ($conf[$nbm_user['param']] == 'true' or $conf[$nbm_user['param']] == 'false')
372      {
373        $conf[$nbm_user['param']] = get_boolean($conf[$nbm_user['param']]);
374      }
375    }
376   
377    if ($updated_param_count != 0)
378    {
379      array_push($page['infos'], sprintf(l10n('nbm_updated_param_count'), $updated_param_count));
380    }
381  }
382  case 'subscribe' :
383  {
384    if (isset($_POST['falsify']) and isset($_POST['cat_true']))
385    {
386      unsubcribe_notification_by_mail(true, $_POST['cat_true']);
387    }
388    else
389    if (isset($_POST['trueify']) and isset($_POST['cat_false']))
390    {
391      subcribe_notification_by_mail(true, $_POST['cat_false']);
392    }
393    break;
394  }
395
396  case 'send' :
397  {
398    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']))
399    {
400      do_action_send_mail_notification('send', $_POST['send_selection'], $_POST['send_customize_mail_content']);
401    }
402  }
403}
404
405// +-----------------------------------------------------------------------+
406// | template initialization                                               |
407// +-----------------------------------------------------------------------+
408$template->set_filenames
409(
410  array
411  (
412    'double_select' => 'admin/double_select.tpl',
413    'notification_by_mail'=>'admin/notification_by_mail.tpl'
414  )
415);
416
417$base_url = get_root_url().'admin.php';
418
419$template->assign_vars
420(
421  array
422  (
423    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
424    'U_HELP' => add_url_params(get_root_url().'/popuphelp.php', array('page' => 'notification_by_mail')),
425    'F_ACTION'=> $base_url.get_query_string_diff(array())
426  )
427);
428
429if (is_autorize_status(ACCESS_WEBMASTER))
430{
431  $template->assign_block_vars
432  (
433    'header_link',
434    array
435    (
436      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
437      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
438      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
439    )
440  );
441}
442
443switch ($page['mode'])
444{
445  case 'param' :
446  {
447    $template->assign_block_vars(
448      $page['mode'],
449      array(
450        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
451        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
452        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
453        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content']
454        ));
455    break;
456  }
457
458  case 'subscribe' :
459  {
460    $template->assign_block_vars(
461      $page['mode'],
462      array(
463        ));
464
465    $template->assign_vars(
466      array(
467        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
468        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
469        )
470      );
471
472    $data_users = get_user_notifications('subscribe');
473    foreach ($data_users as $nbm_user)
474    {
475      $template->assign_block_vars(
476        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
477        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
478                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
479                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
480                            ) ? 'selected="selected"' : '',
481              'VALUE' => $nbm_user['check_key'],
482              'OPTION' => $nbm_user['username'].'['.$nbm_user['mail_address'].']'
483          ));
484    }
485
486    break;
487  }
488
489  case 'send' :
490  {
491    $template->assign_block_vars($page['mode'], array());
492
493    $data_users = do_action_send_mail_notification('list_to_send');
494
495    if  (count($data_users) == 0)
496    {
497      $template->assign_block_vars($page['mode'].'.send_empty', array());
498    }
499    else
500    {
501      $template->assign_block_vars(
502        $page['mode'].'.send_data',
503        array(
504          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? $_POST['send_customize_mail_content'] : $conf['nbm_complementary_mail_content']
505          ));
506
507      foreach ($data_users as $num => $nbm_user)
508          $template->assign_block_vars(
509            $page['mode'].'.send_data.user_send_mail',
510            array(
511              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
512              'ID' => $nbm_user['check_key'],
513              'CHECKED' =>  ( // not check if not selected,  on init select<all
514                              isset($_POST['send_selection']) and // not init
515                              !in_array($nbm_user['check_key'],  $_POST['send_selection']) // not selected
516                            )   ? '' : 'checked="checked"',
517              'USERNAME'=> $nbm_user['username'],
518              'EMAIL' => $nbm_user['mail_address'],
519              'LAST_SEND'=> $nbm_user['last_send']
520              ));
521    }
522
523    break;
524  }
525}
526
527// +-----------------------------------------------------------------------+
528// | Sending html code                                                     |
529// +-----------------------------------------------------------------------+
530$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
531$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
532
533?>
Note: See TracBrowser for help on using the repository browser.