Ignore:
Timestamp:
Apr 1, 2006, 3:14:57 AM (18 years ago)
Author:
rub
Message:

[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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/notification_by_mail.php

    r1115 r1116  
    7575
    7676/*
    77  * Execute all main queries to get list of user
    78  *
    79  * Type are the type of list 'subscribe', 'send'
    80  *
    81  * return array of users
    82  */
    83 function get_user_notifications($action, $check_key_list = array())
    84 {
    85   global $conf;
    86 
    87   $data_users = array();
    88 
    89   if (in_array($action, array('subscribe', 'send')))
    90   {
    91     $quoted_check_key_list = quote_check_key_list($check_key_list);
    92     if (count($quoted_check_key_list) != 0 )
    93     {
    94       $query_and_check_key = ' and
    95     check_key in ('.implode(",", $quoted_check_key_list).') ';
    96     }
    97     else
    98     {
    99       $query_and_check_key = '';
    100     }
    101 
    102     $query = '
    103 select
    104   N.user_id,
    105   N.check_key,
    106   U.'.$conf['user_fields']['username'].' as username,
    107   U.'.$conf['user_fields']['email'].' as mail_address,
    108   N.enabled,
    109   N.last_send
    110 from
    111   '.USER_MAIL_NOTIFICATION_TABLE.' as N,
    112   '.USERS_TABLE.' as U
    113 where
    114   N.user_id =  U.'.$conf['user_fields']['id'];
    115  
    116     if ($action == 'send')
    117     {
    118       $query .= ' and
    119   N.enabled = \'true\' and
    120   U.'.$conf['user_fields']['email'].' is not null'.$query_and_check_key;
    121     }
    122 
    123     $query .= '
    124 order by
    125   username;';
    126 
    127     $result = pwg_query($query);
    128     if (!empty($result))
    129     {
    130       while ($nbm_user = mysql_fetch_array($result))
    131       {
    132         array_push($data_users, $nbm_user);
    133       }
    134     }
    135   }
    136   return $data_users;
    137 }
    138 
    139 /*
    14077 * Inserting News users
    14178 */
     
    195132      );
    196133
    197       array_push($page['infos'], sprintf(l10n('nbm_User %s [%s] added.'), $nbm_user['username'], $nbm_user['mail_address']));
     134      array_push($page['infos'], sprintf(l10n('nbm_user_x_added'), $nbm_user['username'], $nbm_user['mail_address']));
    198135    }
    199136
     
    203140    do_subscribe_unsubcribe_notification_by_mail
    204141    (
    205       ($conf['default_value_user_mail_notification_enabled'] == true ? true : false),
     142      true,
     143      $conf['nbm_default_value_user_enabled'],
    206144      $check_key_list
    207145    );
     
    213151 * Return list of "treated/selected" users
    214152 */
    215 function do_action_send_mail_notification($action = 'list', $check_key_list = array(), $customize_mail_content = '')
    216 {
    217   global $conf, $page, $user, $lang_info, $lang;
     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;
    218156  $return_list = array();
    219157 
    220   if (in_array($action, array('list', 'send')))
     158  if (in_array($action, array('list_to_send', 'send')))
    221159  {
    222160    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
     
    232170    $data_users = get_user_notifications('send', $check_key_list);
    233171
    234     if (count($data_users) > 0)
    235     {
    236       $error_on_mail_count = 0;
    237       $sent_mail_count = 0;
    238       // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
    239       $sav_mailtousers_user = $user;
    240       $sav_mailtousers_lang_info = $lang_info;
    241       $sav_mailtousers_lang = $lang;
    242       // Save message info and error in the original language
    243       $msg_info = l10n('nbm_Mail sent to %s [%s].');
    244       $msg_error = l10n('nbm_Error when sending email to %s [%s].');
    245       // Last Language
    246       $last_mailtousers_language = $user['language'];
    247 
    248       if ($is_action_send)
     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)
    249176      {
    250         // Init mail configuration
    251         $send_as_name = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']);
    252         $send_as_mail_address = get_webmaster_mail_address();
    253         $send_as_mail_formated = format_email($send_as_name, $send_as_mail_address);
    254       }
    255 
    256       foreach ($data_users as $nbm_user)
    257       {
    258         $user = array();
    259         $user['id'] = $nbm_user['user_id'];
    260         $user = array_merge($user, getuserdata($user['id'], true));
    261 
    262         if ($last_mailtousers_language != $user['language'])
     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)
    263183        {
    264           $last_mailtousers_language = $user['language'];
    265 
    266           // Re-Init language arrays
    267           $lang_info = array();
    268           $lang  = array();
    269 
    270           // language files
    271           include(get_language_filepath('common.lang.php'));
    272           // No test admin because script is checked admin (user selected no)
    273           // Translations are in admin file too
    274           include(get_language_filepath('admin.lang.php'));
    275         }
    276 
    277         if ($is_action_send)
    278         {
    279           $message = '';
    280 
    281           if ($conf['nbm_send_detailed_content'])
     184          if ((!$is_action_send) and (count($return_list) >= $conf['nbm_max_list_users_to_send']))
    282185          {
    283              $news = news($nbm_user['last_send'], $dbnow);
    284              $exist_data = count($news) > 0;
     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            }
    285260          }
    286261          else
    287262          {
    288             $exist_data = news_exists($nbm_user['last_send'], $dbnow);
    289           }
    290 
    291           if ($exist_data)
    292           {
    293             array_push($return_list, $nbm_user);
    294 
    295             $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_ContentObject');
    296             $message .= sprintf(l10n('nbm_ContentHello'), $nbm_user['username']).",\n\n";
    297 
    298             if (!is_null($nbm_user['last_send']))
    299               $message .= sprintf(l10n('nbm_ContentNewElementsBetween'), $nbm_user['last_send'], $dbnow);
    300             else
    301               $message .= sprintf(l10n('nbm_ContentNewElements'), $dbnow);
    302 
    303             if ($conf['nbm_send_detailed_content'])
     263            if (news_exists($nbm_user['last_send'], $dbnow))
    304264            {
    305               $message .= ":\n";
    306 
    307               foreach ($news as $line)
    308               {
    309                 $message .= '  o '.$line."\n";
    310               }
    311               $message .= "\n";
    312             }
    313             else
    314             {
    315               $message .= ".\n";
    316             }
    317 
    318             $message .= sprintf(l10n('nbm_ContentGoTo'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
    319             $message .= $customize_mail_content."\n\n";
    320             $message .= l10n('nbm_ContentByeBye')."\n   ".$send_as_name."\n\n";
    321             $message .= "\n".sprintf(l10n('nbm_ContentUnsubscribe'), $send_as_mail_address)."\n\n";
    322 
    323             if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $send_as_mail_formated, $subject, $message))
    324             {
    325               $sent_mail_count += 1;
    326               array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
    327 
    328               $data = array('user_id' => $user_notification['user_id'],
    329                             'last_send' => $dbnow);
    330               array_push($datas, $data);
    331             }
    332             else
    333             {
    334               $error_on_mail_count += 1;
    335               array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
     265              array_push($return_list, $nbm_user);
    336266            }
    337267          }
    338268        }
    339         else
     269
     270        // Restore nbm environment
     271        end_users_env_nbm();
     272
     273        if ($is_action_send)
    340274        {
    341           if (news_exists($nbm_user['last_send'], $dbnow))
    342           {
    343             array_push($return_list, $nbm_user);
    344           }
     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();
    345285        }
    346286      }
    347 
    348       // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
    349       $user = $sav_mailtousers_user;
    350       $lang_info = $sav_mailtousers_lang_info;
    351       $lang = $sav_mailtousers_lang;
    352 
    353       if ($is_action_send)
     287      else
    354288      {
    355         mass_updates(
    356           USER_MAIL_NOTIFICATION_TABLE,
    357           array(
    358             'primary' => array('user_id'),
    359             'update' => array('last_send')
    360            ),
    361            $datas
    362            );
    363 
    364 
    365         if ($error_on_mail_count != 0)
     289        if ($is_action_send)
    366290        {
    367           array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sent.'), $error_on_mail_count));
    368         }
    369         else
    370         {
    371           if ($sent_mail_count == 0)
    372             array_push($page['infos'], l10n('nbm_No mail to send.'));
    373           else
    374             array_push($page['infos'], sprintf(l10n('nbm_%d mails were sent.'), $sent_mail_count));
     291          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
    375292        }
    376293      }
     
    378295    else
    379296    {
    380       if ($is_action_send)
    381       {
    382         array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
    383       }
     297      // Quick List, don't check news
     298      $return_list = $data_users;
    384299    }
    385300  }
     
    469384    if (isset($_POST['falsify']) and isset($_POST['cat_true']))
    470385    {
    471       unsubcribe_notification_by_mail($_POST['cat_true']);
     386      unsubcribe_notification_by_mail(true, $_POST['cat_true']);
    472387    }
    473388    else
    474389    if (isset($_POST['trueify']) and isset($_POST['cat_false']))
    475390    {
    476       subcribe_notification_by_mail($_POST['cat_false']);
     391      subcribe_notification_by_mail(true, $_POST['cat_false']);
    477392    }
    478393    break;
     
    560475      $template->assign_block_vars(
    561476        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
    562         array('SELECTED' => '',
     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"' : '',
    563481              'VALUE' => $nbm_user['check_key'],
    564482              'OPTION' => $nbm_user['username'].'['.$nbm_user['mail_address'].']'
     
    573491    $template->assign_block_vars($page['mode'], array());
    574492
    575     $data_users = do_action_send_mail_notification('list');
     493    $data_users = do_action_send_mail_notification('list_to_send');
    576494
    577495    if  (count($data_users) == 0)
Note: See TracChangeset for help on using the changeset viewer.