Ignore:
Timestamp:
Mar 27, 2006, 12:06:20 AM (18 years ago)
Author:
rub
Message:

[NBM] Step 4: Screen NBM is available

o Add news parameters
o Add file functions_notification_by_mail.inc.php in order to use in future, these functions on php file of subscribe/unsubscribe
o Write a little html help file

+ improve mass_update in order to used binary fields (used collate and SHOW COLUMNS FROM)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/notification_by_mail.php

    r1094 r1105  
    3737
    3838include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    39 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     39include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
    4040include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
    4141include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
     
    5050// | functions                                                             |
    5151// +-----------------------------------------------------------------------+
     52
    5253/*
    53  * Search an available check_key
    54  *
    55  * It's a copy of function find_available_feed_id
    56  *
    57  * @return string feed identifier
     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
    5878 */
    59 function find_available_check_key()
    60 {
    61   while (true)
    62   {
    63     $key = generate_key(128);
    64     $query = '
    65 select
    66   count(*)
    67 from
    68   '.USER_MAIL_NOTIFICATION_TABLE.'
    69 where
    70   check_key = \''.$key.'\';';
    71 
    72     list($count) = mysql_fetch_row(pwg_query($query));
    73     if ($count == 0)
    74     {
    75       return $key;
    76     }
    77   }
    78 }
    79 
    80 /*
    81  * Updating News users
    82  */
    83 function update_data_user_mail_notification()
     79function insert_new_data_user_mail_notification()
    8480{
    8581  global $conf, $page;
     
    9591  pwg_query($query);
    9692
     93  // null mail_address are not selected in the list
    9794  $query = '
    9895select
     
    111108  {
    112109    $inserts = array();
     110    $check_key_list = array();
    113111
    114112    while ($row = mysql_fetch_array($result))
    115113    {
    116       array_push($inserts, array('user_id' => $row['user_id'],
    117                                  'check_key' => find_available_check_key(),
    118                                  'enabled' => ($conf['default_value_user_mail_notification_enabled'] == true ? 'true' : 'false')));
     114      // Calculate key
     115      $row['check_key'] = find_available_check_key();
     116
     117      // Save key
     118      array_push($check_key_list, $row['check_key']);
     119
     120      // Insert new rows
     121      array_push
     122      (
     123        $inserts,
     124        array
     125        (
     126          'user_id' => $row['user_id'],
     127          'check_key' => $row['check_key'],
     128          'enabled' => 'false' // By default if false, set to true with specific functions
     129        )
     130      );
     131
    119132      array_push($page['infos'], sprintf(l10n('nbm_User %s [%s] added.'), $row['username'], $row['mail_address']));
    120133    }
    121134
     135    // Insert new rows
    122136    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
     137    // Update field enabled with specific function
     138    do_subscribe_unsubcribe_notification_by_mail
     139    (
     140      ($conf['default_value_user_mail_notification_enabled'] == true ? true : false),
     141      $check_key_list
     142    );
    123143  }
    124144}
    125145
    126146/*
    127  * Updating News users
     147 * Send mail for notification to all users
     148 * Return list of "treated/selected" users
    128149 */
    129 function send_all_user_mail_notification()
    130 {
    131   global $conf, $conf_mail, $page, $user, $lang_info, $lang;
     150function do_action_send_mail_notification($action = 'prepare', $check_key_list = array(), $customize_mail_content = '')
     151{
     152  global $conf, $page, $user, $lang_info, $lang;
    132153  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
    133 
     154  $return_list = array();
     155  $is_action_send = ($action == 'send');
     156
     157  $quoted_check_key_list = quote_check_key_list($check_key_list);
     158  if (count($quoted_check_key_list) != 0 )
     159  {
     160    $query_and_check_key = ' and
     161  check_key in ('.implode(",", $quoted_check_key_list).') ';
     162  }
     163  else
     164  {
     165    $query_and_check_key = '';
     166  }
     167
     168  if (isset($customize_mail_content))
     169  {
     170    $customize_mail_content = $conf['nbm_complementary_mail_content'];
     171  }
     172
     173  // disabled and null mail_address are not selected in the list
    134174  $query = '
    135175select
    136   N.user_id, U.username, U.mail_address, N.last_send
     176  N.user_id, N.check_key, U.username, U.mail_address, N.last_send
    137177from
    138178  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
     
    141181  N.user_id =  U.id and
    142182  N.enabled = \'true\' and
    143   U.mail_address is not null
     183  U.mail_address is not null'.$query_and_check_key.'
    144184order by
    145   user_id;';
     185  username;';
    146186
    147187  $result = pwg_query($query);
     
    150190  {
    151191    $error_on_mail_count = 0;
    152     $sended_mail_count = 0;
     192    $sent_mail_count = 0;
    153193    $datas = array();
    154194    // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
     
    157197    $sav_mailtousers_lang = $lang;
    158198    // Save message info and error in the original language
    159     $msg_info = l10n('nbm_Mail sended to %s [%s].');
     199    $msg_info = l10n('nbm_Mail sent to %s [%s].');
    160200    $msg_error = l10n('nbm_Error when sending email to %s [%s].');
    161201    // Last Language
    162202    $last_mailtousers_language = $user['language'];
     203
     204    if ($is_action_send)
     205    {
     206      // Init mail configuration
     207      $send_as_name = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']);
     208      $send_as_mail_address = get_webmaster_mail_address();
     209      $send_as_mail_formated = format_email($send_as_name, $send_as_mail_address);
     210    }
    163211
    164212    while ($row = mysql_fetch_array($result))
     
    187235      if (count($news) > 0)
    188236      {
    189         $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_New elements added');
    190         $message .= sprintf(l10n('nbm_Hello %s'), $row['username']).",\n\n";
    191 
    192         if (!is_null($row['last_send']))
    193           $message .= sprintf(l10n('nbm_New elements were added between %s and %s:'), $row['last_send'], $dbnow);
    194         else
    195           $message .= sprintf(l10n('nbm_New elements were added on %s:'), $dbnow);
    196         $message .= "\n";
    197 
    198         foreach ($news as $line)
     237        array_push($return_list, $row);
     238
     239        if ($is_action_send)
    199240        {
    200           $message .= '  o '.$line."\n";
    201         }
    202 
    203         $message .= "\n".sprintf(l10n('nbm_Go to %s %s.'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
    204         $message .= "\n".sprintf(l10n('nbm_To unsubscribe send a message to %s.'), $conf_mail['email_webmaster'])."\n\n";
    205 
    206         if (pwg_mail(format_email($row['username'], $row['mail_address']), '', $subject, $message))
    207         {
    208           $sended_mail_count += 1;
    209           array_push($page['infos'], sprintf($msg_info, $row['username'], $row['mail_address']));
    210           $data = array('user_id' => $row['user_id'],
    211                         'last_send' => $dbnow);
    212           array_push($datas, $data);
    213         }
    214         else
    215         {
    216           $error_on_mail_count += 1;
    217           array_push($page['errors'], sprintf($msg_error, $row['username'], $row['mail_address']));
     241          $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_ContentObject');
     242          $message .= sprintf(l10n('nbm_ContentHello'), $row['username']).",\n\n";
     243
     244          if (!is_null($row['last_send']))
     245            $message .= sprintf(l10n('nbm_ContentNewElementsBetween'), $row['last_send'], $dbnow);
     246          else
     247            $message .= sprintf(l10n('nbm_ContentNewElements'), $dbnow);
     248
     249          if ($conf['nbm_send_detailed_content'])
     250          {
     251            $message .= ":\n";
     252
     253            foreach ($news as $line)
     254            {
     255              $message .= '  o '.$line."\n";
     256            }
     257            $message .= "\n";
     258          }
     259          else
     260          {
     261            $message .= ".\n";
     262          }
     263
     264          $message .= sprintf(l10n('nbm_ContentGoTo'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
     265          $message .= $customize_mail_content."\n\n";
     266          $message .= l10n('nbm_ContentByeBye')."\n   ".$send_as_name."\n\n";
     267          $message .= "\n".sprintf(l10n('nbm_ContentUnsubscribe'), $send_as_mail_address)."\n\n";
     268
     269          if (pwg_mail(format_email($row['username'], $row['mail_address']), $send_as_mail_formated, $subject, $message))
     270          {
     271            $sent_mail_count += 1;
     272            array_push($page['infos'], sprintf($msg_info, $row['username'], $row['mail_address']));
     273            $data = array('user_id' => $row['user_id'],
     274                          'last_send' => $dbnow);
     275            array_push($datas, $data);
     276          }
     277          else
     278          {
     279            $error_on_mail_count += 1;
     280            array_push($page['errors'], sprintf($msg_error, $row['username'], $row['mail_address']));
     281          }
    218282        }
    219283      }
     
    225289    $lang = $sav_mailtousers_lang;
    226290
    227     mass_updates(
    228       USER_MAIL_NOTIFICATION_TABLE,
    229       array(
    230         'primary' => array('user_id'),
    231         'update' => array('last_send')
    232        ),
    233        $datas
    234        );
    235 
    236     if ($error_on_mail_count != 0)
    237     {
    238       array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sended.'), $error_on_mail_count));
     291    if ($is_action_send)
     292    {
     293      mass_updates(
     294        USER_MAIL_NOTIFICATION_TABLE,
     295        array(
     296          'primary' => array('user_id'),
     297          'update' => array('last_send')
     298         ),
     299         $datas
     300         );
     301
     302      if ($error_on_mail_count != 0)
     303      {
     304        array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sent.'), $error_on_mail_count));
     305      }
     306      else
     307      {
     308        if ($sent_mail_count == 0)
     309          array_push($page['infos'], l10n('nbm_No mail to send.'));
     310        else
     311          array_push($page['infos'], sprintf(l10n('nbm_%d mails were sent.'), $sent_mail_count));
     312      }
     313    }
     314  }
     315  else
     316  {
     317    if ($is_action_send)
     318    {
     319      array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
     320    }
     321  }
     322  return $return_list;
     323}
     324
     325// +-----------------------------------------------------------------------+
     326// | Main                                                                  |
     327// +-----------------------------------------------------------------------+
     328if (!isset($_GET['mode']))
     329{
     330  $page['mode'] = 'send';
     331}
     332else
     333{
     334  $page['mode'] = $_GET['mode'];
     335}
     336
     337// +-----------------------------------------------------------------------+
     338// | Check Access and exit when user status is not ok                      |
     339// +-----------------------------------------------------------------------+
     340check_status(get_tab_status($page['mode']));
     341
     342// +-----------------------------------------------------------------------+
     343// | Insert new users with mails                                           |
     344// +-----------------------------------------------------------------------+
     345if (!isset($_POST) or (count($_POST) ==0))
     346{
     347  // No insert data in post mode
     348  insert_new_data_user_mail_notification();
     349}
     350
     351// +-----------------------------------------------------------------------+
     352// | Treatment of tab post                                                 |
     353// +-----------------------------------------------------------------------+
     354switch ($page['mode'])
     355{
     356  case 'param' :
     357  {
     358    $updated_param_count = 0;
     359    // Update param
     360    $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
     361    while ($row = mysql_fetch_array($result))
     362    {
     363      if (isset($_POST['param_submit']))
     364      {
     365        if (isset($_POST[$row['param']]))
     366        {
     367          $value = $_POST[$row['param']];
     368
     369          $query = '
     370update
     371  '.CONFIG_TABLE.'
     372set
     373  value = \''. str_replace("\'", "''", $value).'\'
     374where
     375  param = \''.$row['param'].'\';';
     376          pwg_query($query);
     377          $updated_param_count += 1;
     378        }
     379      }
     380
     381      $conf[$row['param']] = $row['value'];
     382
     383      // if the parameter is present in $_POST array (if a form is submited), we
     384      // override it with the submited value
     385      if (isset($_POST[$row['param']]))
     386      {
     387        $conf[$row['param']] = stripslashes($_POST[$row['param']]);
     388      }
     389
     390      // If the field is true or false, the variable is transformed into a
     391      // boolean value.
     392      if ($conf[$row['param']] == 'true' or $conf[$row['param']] == 'false')
     393      {
     394        $conf[$row['param']] = get_boolean($conf[$row['param']]);
     395      }
     396    }
     397   
     398    if ($updated_param_count != 0)
     399    {
     400      array_push($page['infos'], sprintf(l10n('nbm_updated_param_count'), $updated_param_count));
     401    }
     402  }
     403  case 'subscribe' :
     404  {
     405    if (isset($_POST['falsify']) and isset($_POST['cat_true']))
     406    {
     407      unsubcribe_notification_by_mail($_POST['cat_true']);
    239408    }
    240409    else
    241     {
    242       if ($sended_mail_count == 0)
    243         array_push($page['infos'], l10n('nbm_No mail to send.'));
    244       else
    245         array_push($page['infos'], sprintf(l10n('nbm_%d mails were sended.'), $sended_mail_count));
    246     }
    247   }
    248   else
    249   {
    250     array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
    251   }
    252 }
    253 
    254 // +-----------------------------------------------------------------------+
    255 // | Main                                                                  |
    256 // +-----------------------------------------------------------------------+
    257 update_data_user_mail_notification();
    258 //send_all_user_mail_notification();
    259 
    260 if (!isset($_GET['mode']))
    261 {
    262   $page['mode'] = 'send';
    263 }
    264 else
    265 {
    266   $page['mode'] = $_GET['mode'];
     410    if (isset($_POST['trueify']) and isset($_POST['cat_false']))
     411    {
     412      subcribe_notification_by_mail($_POST['cat_false']);
     413    }
     414    break;
     415  }
     416
     417  case 'send' :
     418  {
     419    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']))
     420    {
     421      do_action_send_mail_notification('send', $_POST['send_selection'], $_POST['send_customize_mail_content']);
     422    }
     423  }
    267424}
    268425
     
    270427// | template initialization                                               |
    271428// +-----------------------------------------------------------------------+
    272 $template->set_filenames(
    273   array(
     429$template->set_filenames
     430(
     431  array
     432  (
    274433    'double_select' => 'admin/double_select.tpl',
    275434    'notification_by_mail'=>'admin/notification_by_mail.tpl'
     435  )
     436);
     437
     438$base_url = get_root_url().'admin.php';
     439
     440$template->assign_vars
     441(
     442  array
     443  (
     444    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
     445    'U_HELP' => add_url_params(get_root_url().'/popuphelp.php', array('page' => 'notification_by_mail')),
     446    'F_ACTION'=> $base_url.get_query_string_diff(array())
     447  )
     448);
     449
     450if (is_autorize_status(ACCESS_WEBMASTER))
     451{
     452  $template->assign_block_vars
     453  (
     454    'header_link',
     455    array
     456    (
     457      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
     458      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
     459      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
    276460    )
    277461  );
    278 
    279 $base_url = get_root_url().'admin.php';
    280 
    281 $template->assign_vars(
    282   array(
    283     'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
    284     'U_HELP' => add_url_params(get_root_url().'/popuphelp.php', array('page'=>'notification_by_mail') ),
    285     'U_PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'param') ),
    286     'U_SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'subscribe') ),
    287     'U_SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'send') ),
    288     'F_ACTION'=> $base_url.get_query_string_diff(array())
    289     ));
     462}
    290463
    291464switch ($page['mode'])
     
    296469      $page['mode'],
    297470      array(
    298         //'HISTORY_YES'=>$history_yes
     471        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
     472        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
     473        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
     474        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content']
    299475        ));
    300476    break;
    301477  }
     478
    302479  case 'subscribe' :
    303480  {
     
    305482      $page['mode'],
    306483      array(
    307         //'HISTORY_YES'=>$history_yes
    308484        ));
    309485
     
    315491      );
    316492
    317 
    318 /*    $template->assign_block_vars(
    319       $blockname,
    320       array('SELECTED'=>$selected,
    321             'VALUE'=>$category['id'],
    322             'OPTION'=>$option
    323         ));*/
    324     $template->assign_block_vars(
    325       'category_option_true',
    326       array('SELECTED'=>'',
    327             'VALUE'=>'rub',
    328             'OPTION'=>'rub [rub@phpwebgallery.net]'
    329         ));
     493    $query = '
     494select
     495  N.check_key, N.enabled, U.username, U.mail_address
     496from
     497  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
     498  '.USERS_TABLE.' as U
     499where
     500  N.user_id =  U.id
     501order by
     502  username;';
     503
     504    $result = pwg_query($query);
     505    if (!empty($result))
     506    {
     507      while ($row = mysql_fetch_array($result))
     508      {
     509        $template->assign_block_vars(
     510          (get_boolean($row['enabled']) ? 'category_option_true' : 'category_option_false'),
     511          array('SELECTED' => '',
     512                'VALUE' => $row['check_key'],
     513                'OPTION' => $row['username'].'['.$row['mail_address'].']'
     514            ));
     515      }
     516    }
    330517
    331518    break;
    332519  }
     520
    333521  case 'send' :
    334522  {
    335     $template->assign_block_vars(
    336       $page['mode'],
    337       array(
    338         //'HISTORY_YES'=>$history_yes
    339         ));
    340 
    341     $template->assign_vars(
    342       array(
    343         'L_CAT_OPTIONS_TRUE' => l10n('nbm_send_col'),
    344         'L_CAT_OPTIONS_FALSE' => l10n('nbm_nosend_col')
    345         )
    346       );
    347 
    348 
    349 /*    $template->assign_block_vars(
    350       $blockname,
    351       array('SELECTED'=>$selected,
    352             'VALUE'=>$category['id'],
    353             'OPTION'=>$option
    354         ));*/
    355     $template->assign_block_vars(
    356       'category_option_true',
    357       array('SELECTED'=>' selected="selected"',
    358             'VALUE'=>'rub',
    359             'OPTION'=>'rub [2006-03-20 23:35:23]'
    360         ));
     523    $template->assign_block_vars($page['mode'], array());
     524
     525    $data_rows = do_action_send_mail_notification('prepare');
     526
     527    if  (count($data_rows) == 0)
     528    {
     529      $template->assign_block_vars($page['mode'].'.send_empty', array());
     530    }
     531    else
     532    {
     533      $template->assign_block_vars(
     534        $page['mode'].'.send_data',
     535        array(
     536  //        'URL_CHECK_ALL' => add_url_params($base_url.get_query_string_diff(array('select')), array('select' => 'all')),
     537  //        'URL_UNCHECK_ALL' => add_url_params($base_url.get_query_string_diff(array('select')), array('select' => 'none')),
     538          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? $_POST['send_customize_mail_content'] : $conf['nbm_complementary_mail_content']
     539          ));
     540
     541      foreach ($data_rows as $num => $local_user)
     542          $template->assign_block_vars(
     543            $page['mode'].'.send_data.user_send_mail',
     544            array(
     545              'CLASS' => ($num % 2 == 1) ? 'row2' : 'row1',
     546              'ID' => $local_user['check_key'],
     547              'CHECKED' => isset($_POST['send_uncheck_all']) ? '' : 'checked="checked"',
     548              'USERNAME'=> $local_user['username'],
     549              'EMAIL' => $local_user['mail_address'],
     550              'LAST_SEND'=> $local_user['last_send']
     551              ));
     552    }
    361553
    362554    break;
    363555  }
    364556}
    365 
    366 // +-----------------------------------------------------------------------+
    367 // | infos & errors display                                                |
    368 // +-----------------------------------------------------------------------+
    369 
    370 /*echo '<pre>';
    371 
    372 if (count($page['errors']) != 0)
    373 {
    374   echo "\n\nErrors:\n";
    375   foreach ($page['errors'] as $error)
    376   {
    377     echo $error."\n";
    378   }
    379 }
    380 
    381 if (count($page['infos']) != 0)
    382 {
    383   echo "\n\nInformations:\n";
    384   foreach ($page['infos'] as $info)
    385   {
    386     echo $info."\n";
    387   }
    388 }
    389 
    390 echo '</pre>';
    391 */
    392557
    393558// +-----------------------------------------------------------------------+
Note: See TracChangeset for help on using the changeset viewer.