Changeset 1116 for trunk/admin/include


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

Location:
trunk/admin/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r1111 r1116  
    763763          $column.= " default '".$row['Default']."'";
    764764        }
    765         if (isset($row['Collation']))
     765        if (isset($row['Collation']) and $row['Collation'] != 'NULL')
    766766        {
    767767          $column.= " collate '".$row['Collation']."'";
  • trunk/admin/include/functions_notification_by_mail.inc.php

    r1105 r1116  
    2727// +-----------------------------------------------------------------------+
    2828
     29/* nbm_global_var */
     30$env_nbm = array();
     31
    2932/*
    3033 * Search an available check_key
     
    3841  while (true)
    3942  {
    40     $key = generate_key(128);
     43    $key = generate_key(16);
    4144    $query = '
    4245select
     
    6770
    6871/*
    69  * Subscribe or unsubscribe notification by mail
    70  *
    71  * is_subscribe define if action=subscribe or unsubscribe
    72  * check_key list where action will be done
    73  *
    74  * @return updated data count
    75  */
    76 function do_subscribe_unsubcribe_notification_by_mail($is_subscribe = false, $check_key_list = array())
    77 {
    78   global $page;
    79 
    80   $updated_data_count = 0;
    81   if ($is_subscribe)
    82   {
    83     $msg_info = l10n('nbm_user_change_enabled_true');
    84   }
    85   else
    86   {
    87     $msg_info = l10n('nbm_user_change_enabled_false');
    88   }
    89 
    90   if (count($check_key_list) != 0)
     72 * Execute all main queries to get list of user
     73 *
     74 * Type are the type of list 'subscribe', 'send'
     75 *
     76 * return array of users
     77 */
     78function get_user_notifications($action, $check_key_list = array(), $enabled_filter_value = '')
     79{
     80  global $conf;
     81
     82  $data_users = array();
     83
     84  if (in_array($action, array('subscribe', 'send')))
    9185  {
    9286    $quoted_check_key_list = quote_check_key_list($check_key_list);
     87    if (count($quoted_check_key_list) != 0 )
     88    {
     89      $query_and_check_key = ' and
     90    check_key in ('.implode(",", $quoted_check_key_list).') ';
     91    }
     92    else
     93    {
     94      $query_and_check_key = '';
     95    }
    9396
    9497    $query = '
    9598select
    96   N.check_key, U.username, U.mail_address
     99  N.user_id,
     100  N.check_key,
     101  U.'.$conf['user_fields']['username'].' as username,
     102  U.'.$conf['user_fields']['email'].' as mail_address,
     103  N.enabled,
     104  N.last_send
    97105from
    98106  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
    99107  '.USERS_TABLE.' as U
    100108where
    101   N.user_id =  U.id and
    102   N.enabled = \''.boolean_to_string(!$is_subscribe).'\' and
    103   check_key in ('.implode(",", $quoted_check_key_list).')
    104 order by
     109  N.user_id =  U.'.$conf['user_fields']['id'];
     110 
     111    if ($action == 'send')
     112    {
     113      // No mail empty and all users enabled
     114      $query .= ' and
     115  N.enabled = \'true\' and
     116  U.'.$conf['user_fields']['email'].' is not null';
     117    }
     118
     119    $query .= $query_and_check_key;
     120
     121    if (isset($enabled_filter_value) and ($enabled_filter_value != ''))
     122    {
     123      $query .= ' and
     124        N.enabled = \''.boolean_to_string($enabled_filter_value).'\'';
     125    }
     126
     127    $query .= '
     128order by';
     129
     130    if ($action == 'send')
     131    {
     132      $query .= '
     133  last_send, username;';
     134    }
     135    else
     136    {
     137      $query .= '
    105138  username;';
     139    }
     140
     141    $query .= ';';
    106142
    107143    $result = pwg_query($query);
    108144    if (!empty($result))
    109145    {
    110       $updates = array();
    111       $enabled_value = boolean_to_string($is_subscribe);
    112 
    113       while ($row = mysql_fetch_array($result))
     146      while ($nbm_user = mysql_fetch_array($result))
     147      {
     148        array_push($data_users, $nbm_user);
     149      }
     150    }
     151  }
     152  return $data_users;
     153}
     154
     155/*
     156 * Begin of use nbm environment
     157 * Prepare and save current environment and initialize data in order to send mail
     158 *
     159 * Return none
     160 */
     161function begin_users_env_nbm($is_to_send_mail = false)
     162{
     163  global $user, $lang, $lang_info, $conf, $env_nbm;
     164
     165  // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
     166  $env_nbm['save_user'] = $user;
     167  $env_nbm['save_lang_info'] = $lang_info;
     168  $env_nbm['save_lang'] = $lang;
     169  // Last Language
     170  $env_nbm['last_language'] = $user['language'];
     171
     172  $env_nbm['is_to_send_mail'] = $is_to_send_mail;
     173
     174  if ($is_to_send_mail)
     175  {
     176    // Init mail configuration
     177    $env_nbm['send_as_name'] = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']);
     178    $env_nbm['send_as_mail_address'] = get_webmaster_mail_address();
     179    $env_nbm['send_as_mail_formated'] = format_email($env_nbm['send_as_name'], $env_nbm['send_as_mail_address']);
     180    // Init mail counter
     181    $env_nbm['error_on_mail_count'] = 0;
     182    $env_nbm['sent_mail_count'] = 0;
     183    // Save sendmail message info and error in the original language
     184    $env_nbm['msg_info'] = l10n('nbm_msg_mail_sent_to');
     185    $env_nbm['msg_error'] = l10n('nbm_msg_error_sending_email_to');
     186  }
     187}
     188
     189/*
     190 * End of use nbm environment
     191 * Restore environment
     192 *
     193 * Return none
     194 */
     195function end_users_env_nbm()
     196{
     197  global $user, $lang, $lang_info, $env_nbm;
     198
     199  // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
     200  $user = $env_nbm['save_user'];
     201  $lang_info = $env_nbm['save_lang_info'];
     202  $lang = $env_nbm['save_lang'];
     203}
     204
     205/*
     206 * Set user_id on nbm enviromnent
     207 *
     208 * Return none
     209 */
     210function set_user_id_on_env_nbm($user_id)
     211{
     212  global $user, $lang, $lang_info, $env_nbm;
     213
     214  $user = array();
     215  $user['id'] = $user_id;
     216  $user = array_merge($user, getuserdata($user['id'], true));
     217
     218  if ($env_nbm['last_language'] != $user['language'])
     219  {
     220    $env_nbm['last_language'] = $user['language'];
     221
     222    // Re-Init language arrays
     223    $lang_info = array();
     224    $lang  = array();
     225
     226    // language files
     227    include(get_language_filepath('common.lang.php'));
     228    // No test admin because script is checked admin (user selected no)
     229    // Translations are in admin file too
     230    include(get_language_filepath('admin.lang.php'));
     231  }
     232}
     233
     234/*
     235 * Inc Counter success
     236 *
     237 * Return none
     238 */
     239function inc_mail_sent_success($nbm_user)
     240{
     241  global $page, $env_nbm;
     242
     243  $env_nbm['sent_mail_count'] += 1;
     244  array_push($page['infos'], sprintf($env_nbm['msg_info'], $nbm_user['username'], $nbm_user['mail_address']));
     245}
     246
     247/*
     248 * Inc Counter failed
     249 *
     250 * Return none
     251 */
     252function inc_mail_sent_failed($nbm_user)
     253{
     254  global $page, $env_nbm;
     255
     256  $env_nbm['error_on_mail_count'] += 1;
     257  array_push($page['errors'], sprintf($env_nbm['msg_error'], $nbm_user['username'], $nbm_user['mail_address']));
     258}
     259
     260/*
     261 * Display Counter Info
     262 *
     263 * Return none
     264 */
     265function display_counter_info()
     266{
     267  global $page, $env_nbm;
     268
     269  if ($env_nbm['error_on_mail_count'] != 0)
     270  {
     271    array_push($page['errors'], sprintf(l10n('nbm_msg_no_mail_to_send'), $env_nbm['error_on_mail_count']));
     272    if ($env_nbm['sent_mail_count'] != 0)
     273      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
     274  }
     275  else
     276  {
     277    if ($env_nbm['sent_mail_count'] == 0)
     278      array_push($page['infos'], l10n('nbm_no_mail_to_send'));
     279    else
     280      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
     281  }
     282}
     283
     284function get_mail_content_subscribe_unsubcribe($nbm_user)
     285{
     286  global $page, $env_nbm;
     287 
     288  $content = "\n\n\n";
     289 
     290  if ( isset($page['root_path']) )
     291  {
     292    $save_root_path = $page['root_path'];
     293  }
     294
     295  $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path();
     296 
     297  $content .= "___________________________________________________\n\n";
     298  $content .= sprintf(l10n('nbm_content_unsubscribe_link'), add_url_params(get_root_url().'/nbm.php', array('unsubscribe' => $nbm_user['check_key'])))."\n";
     299  $content .= sprintf(l10n('nbm_content_subscribe_link'), add_url_params(get_root_url().'/nbm.php', array('subscribe' => $nbm_user['check_key'])))."\n";
     300  $content .= sprintf(l10n('nbm_content_subscribe_unsubscribe_contact'), $env_nbm['send_as_mail_address'])."\n";
     301  $content .= "___________________________________________________\n\n\n\n";
     302
     303  if (isset($save_root_path))
     304  {
     305    $page['root_path'] = $save_root_path;
     306  }
     307  else
     308  {
     309    unset($page['root_path']);
     310  }
     311
     312  return $content;
     313}
     314
     315/*
     316 * Subscribe or unsubscribe notification by mail
     317 *
     318 * is_subscribe define if action=subscribe or unsubscribe
     319 * check_key list where action will be done
     320 *
     321 * @return updated data count
     322 */
     323function do_subscribe_unsubcribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array())
     324{
     325  global $conf, $page, $env_nbm, $conf;
     326
     327  $updated_data_count = 0;
     328  $error_on_updated_data_count = 0;
     329
     330  if ($is_subscribe)
     331  {
     332    $msg_info = l10n('nbm_user_change_enabled_true');
     333    $msg_error = l10n('nbm_user_not_change_enabled_true');
     334  }
     335  else
     336  {
     337    $msg_info = l10n('nbm_user_change_enabled_false');
     338    $msg_error = l10n('nbm_user_not_change_enabled_false');
     339  }
     340
     341  if (count($check_key_list) != 0)
     342  {
     343    $updates = array();
     344    $enabled_value = boolean_to_string($is_subscribe);
     345    $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe);
     346
     347    // Begin nbm users environment
     348    begin_users_env_nbm(true);
     349
     350    foreach ($data_users as $nbm_user)
     351    {
     352      if (($env_nbm['error_on_mail_count'] + $env_nbm['sent_mail_count']) >= $conf['nbm_max_mails_send'])
     353      {
     354        // Stop fill list on 'send', if the quota is override
     355        array_push($page['errors'], sprintf(l10n('nbm_nbm_break_send_mail'), $conf['nbm_max_mails_send']));
     356        break;
     357      }
     358
     359      $do_update = true;
     360      if ($nbm_user['mail_address'] != '')
     361      {
     362        // set env nbm user
     363        set_user_id_on_env_nbm($nbm_user['user_id']);
     364
     365        $message = '';
     366
     367        $subject = '['.$conf['gallery_title'].']: '.($is_subscribe ? l10n('nbm_object_subcribe'): l10n('nbm_object_unsubcribe'));
     368        $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n";
     369
     370        if ($is_subscribe)
     371        {
     372          $message .= l10n($is_admin_request ? 'nbm_content_subscribe_by_admin' : 'nbm_content_subscribe_by_himself');
     373        }
     374        else
     375        {
     376          $message .= l10n($is_admin_request ? 'nbm_content_unsubscribe_by_admin' : 'nbm_content_unsubscribe_by_himself');
     377        }
     378
     379        $message .= "\n\n";
     380        $message .= l10n('nbm_content_byebye')."\n   ".$env_nbm['send_as_name']."\n\n";
     381
     382        $message .= get_mail_content_subscribe_unsubcribe($nbm_user);
     383
     384        if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message))
     385        {
     386          inc_mail_sent_success($nbm_user);
     387        }
     388        else
     389        {
     390          inc_mail_sent_failed($nbm_user);
     391          $do_update = false;
     392        }
     393      }
     394
     395      if ($do_update)
    114396      {
    115397        array_push
     
    118400          array
    119401          (
    120             'check_key' => $row['check_key'],
     402            'check_key' => $nbm_user['check_key'],
    121403            'enabled' => $enabled_value
    122404          )
    123405        );
    124406        $updated_data_count += 1;
    125         array_push($page['infos'], sprintf($msg_info, $row['username'], $row['mail_address']));
     407        array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
    126408      }
    127 
    128       mass_updates(
    129         USER_MAIL_NOTIFICATION_TABLE,
    130         array(
    131           'primary' => array('check_key'),
    132           'update' => array('enabled')
    133         ),
    134         $updates
    135       );
    136     }
     409      else
     410      {
     411        $error_on_updated_data_count += 1;
     412        array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
     413      }
     414
     415    }
     416
     417    // Restore nbm environment
     418    end_users_env_nbm();
     419
     420    display_counter_info();
     421
     422    mass_updates(
     423      USER_MAIL_NOTIFICATION_TABLE,
     424      array(
     425        'primary' => array('check_key'),
     426        'update' => array('enabled')
     427      ),
     428      $updates
     429    );
     430
    137431  }
    138432
    139433  array_push($page['infos'], sprintf(l10n('nbm_user_change_enabled_updated_data_count'), $updated_data_count));
     434  if ($error_on_updated_data_count != 0)
     435  {
     436    array_push($page['errors'], sprintf(l10n('nbm_user_change_enabled_error_on_updated_data_count'), $error_on_updated_data_count));
     437  }
    140438
    141439  return $updated_data_count;
     
    149447 * @return updated data count
    150448 */
    151 function unsubcribe_notification_by_mail($check_key_list = array())
    152 {
    153   return do_subscribe_unsubcribe_notification_by_mail(false, $check_key_list);
     449function unsubcribe_notification_by_mail($is_admin_request, $check_key_list = array())
     450{
     451  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, false, $check_key_list);
    154452}
    155453
     
    161459 * @return updated data count
    162460 */
    163 function subcribe_notification_by_mail($check_key_list = array())
    164 {
    165   return do_subscribe_unsubcribe_notification_by_mail(true, $check_key_list);
     461function subcribe_notification_by_mail($is_admin_request, $check_key_list = array())
     462{
     463  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, true, $check_key_list);
    166464}
    167465
Note: See TracChangeset for help on using the changeset viewer.