Changeset 1114


Ignore:
Timestamp:
Mar 30, 2006, 4:08:53 AM (18 years ago)
Author:
rvelices
Message:

NBM: check/uncheck all completely done by Javascript

NBM: add function make_index_absolute_url

NBM: add function get_user_notifications and simplified code

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/notification_by_mail.php

    r1112 r1114  
    7474}
    7575
     76function get_user_notifications($enabled_only=false, $valid_email_only=false)
     77{
     78  global $conf;
     79  $query = '
     80SELECT
     81  N.user_id, N.check_key, N.last_send, N.enabled,
     82  U.'.$conf['user_fields']['username'].' AS username, U.'.$conf['user_fields']['email'].' AS mail_address
     83FROM
     84  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
     85  '.USERS_TABLE.' as U
     86WHERE
     87  N.user_id = U.'.$conf['user_fields']['id'];
     88  if ($enabled_only)
     89  {
     90    $query .= '
     91AND N.enabled = \'true\'';
     92  }
     93
     94  if ($valid_email_only)
     95  {
     96    $query .= '
     97AND U.'.$conf['user_fields']['email'].' IS NOT NULL';
     98  }
     99  $query .= '
     100ORDER BY username
     101;';
     102  $users = array();
     103  $result = pwg_query($query);
     104  while ($row = mysql_fetch_array($result))
     105  {
     106    $row['enabled'] = ($row['enabled']=='true') ? true:false;
     107    array_push($users, $row);
     108  }
     109  return $users;
     110}
     111
     112function make_index_absolute_url($cat_id)
     113{
     114  global $page, $conf;
     115  if ( isset($page['root_path']) )
     116  {
     117    $save_root_path = $page['root_path'];
     118  }
     119  $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path().'/';
     120  $url = make_index_url( array('category'=>$cat_id) );
     121  if (isset($save_root_path))
     122  {
     123    $page['root_path'] = $save_root_path;
     124  }
     125  else
     126  {
     127    unset($page['root_path']);
     128  }
     129  return $url;
     130}
     131
    76132/*
    77133 * Inserting News users
     
    121177      array_push
    122178      (
    123         $inserts, 
     179        $inserts,
    124180        array
    125181        (
     
    148204 * Return list of "treated/selected" users
    149205 */
    150 function do_action_send_mail_notification($action = 'prepare', $check_key_list = array(), $customize_mail_content = '')
     206function do_action_send_mail_notification($check_key_list = array(), $customize_mail_content = '')
    151207{
    152208  global $conf, $page, $user, $lang_info, $lang;
     209
    153210  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
    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).') ';
     211
     212  if (isset($customize_mail_content))
     213  {
     214    $customize_mail_content = $conf['nbm_complementary_mail_content'];
     215  }
     216
     217  $user_notifications = get_user_notifications(true,true);
     218
     219  $error_on_mail_count = 0;
     220  $sent_mail_count = 0;
     221  $datas = array();
     222  // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
     223  $sav_mailtousers_user = $user;
     224  $sav_mailtousers_lang_info = $lang_info;
     225  $sav_mailtousers_lang = $lang;
     226  // Save message info and error in the original language
     227  $msg_info = l10n('nbm_Mail sent to %s [%s].');
     228  $msg_error = l10n('nbm_Error when sending email to %s [%s].');
     229  // Last Language
     230  $last_mailtousers_language = $user['language'];
     231
     232  // Init mail configuration
     233  $send_as_name = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']);
     234  $send_as_mail_address = get_webmaster_mail_address();
     235  $send_as_mail_formated = format_email($send_as_name, $send_as_mail_address);
     236
     237  foreach ($user_notifications as $user_notification)
     238  {
     239    if (!in_array($user_notification['check_key'], $check_key_list))
     240      continue;
     241    if (!$user_notification['enabled'])
     242      continue;
     243    $user = array();
     244    $user['id'] = $user_notification['user_id'];
     245    $user = array_merge($user, getuserdata($user['id'], true));
     246
     247    if ($last_mailtousers_language != $user['language'])
     248    {
     249      $last_mailtousers_language = $user['language'];
     250
     251      // Re-Init language arrays
     252      $lang_info = array();
     253      $lang  = array();
     254
     255      // language files
     256      include(get_language_filepath('common.lang.php'));
     257      // No test admin because script is checked admin (user selected no)
     258      // Translations are in admin file too
     259      include(get_language_filepath('admin.lang.php'));
     260    }
     261
     262    $message = '';
     263    $news = news($user_notification['last_send'], $dbnow);
     264    if (count($news) > 0)
     265    {
     266      $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_ContentObject');
     267      $message .= sprintf(l10n('nbm_ContentHello'), $user_notification['username']).",\n\n";
     268
     269      if (!is_null($user_notification['last_send']))
     270        $message .= sprintf(l10n('nbm_ContentNewElementsBetween'), $user_notification['last_send'], $dbnow);
     271      else
     272        $message .= sprintf(l10n('nbm_ContentNewElements'), $dbnow);
     273
     274      if ($conf['nbm_send_detailed_content'])
     275      {
     276        $message .= ":\n";
     277        foreach ($news as $line)
     278        {
     279          $message .= '  o '.$line."\n";
     280        }
     281      }
     282      $message .= ".\n";
     283
     284      $message .= sprintf(l10n('nbm_ContentGoTo'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
     285      $message .= $customize_mail_content."\n\n";
     286      $message .= l10n('nbm_ContentByeBye')."\n   ".$send_as_name."\n\n";
     287      $message .= "\n".sprintf(l10n('nbm_ContentUnsubscribe'), $send_as_mail_address)."\n\n";
     288      if (pwg_mail(format_email($user_notification['username'], $user_notification['mail_address']), $send_as_mail_formated, $subject, $message))
     289      {
     290        $sent_mail_count++;
     291        array_push($page['infos'], sprintf($msg_info, $user_notification['username'], $user_notification['mail_address']));
     292        $data = array('user_id' => $user_notification['user_id'],
     293                      'last_send' => $dbnow);
     294        array_push($datas, $data);
     295      }
     296      else
     297      {
     298        $error_on_mail_count++;
     299        array_push($page['errors'], sprintf($msg_error, $user_notification['username'], $user_notification['mail_address']));
     300      }
     301    }
     302  }
     303
     304  // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
     305  $user = $sav_mailtousers_user;
     306  $lang_info = $sav_mailtousers_lang_info;
     307  $lang = $sav_mailtousers_lang;
     308
     309  mass_updates(
     310    USER_MAIL_NOTIFICATION_TABLE,
     311    array(
     312      'primary' => array('user_id'),
     313      'update' => array('last_send')
     314     ),
     315     $datas
     316     );
     317
     318  if ($error_on_mail_count != 0)
     319  {
     320    array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sent.'), $error_on_mail_count));
    162321  }
    163322  else
    164323  {
    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
    174   $query = '
    175 select
    176   N.user_id, N.check_key, U.username, U.mail_address, N.last_send
    177 from
    178   '.USER_MAIL_NOTIFICATION_TABLE.' as N,
    179   '.USERS_TABLE.' as U
    180 where
    181   N.user_id =  U.id and
    182   N.enabled = \'true\' and
    183   U.mail_address is not null'.$query_and_check_key.'
    184 order by
    185   username;';
    186 
    187   $result = pwg_query($query);
    188 
    189   if (mysql_num_rows($result) > 0)
    190   {
    191     $error_on_mail_count = 0;
    192     $sent_mail_count = 0;
    193     $datas = array();
    194     // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
    195     $sav_mailtousers_user = $user;
    196     $sav_mailtousers_lang_info = $lang_info;
    197     $sav_mailtousers_lang = $lang;
    198     // Save message info and error in the original language
    199     $msg_info = l10n('nbm_Mail sent to %s [%s].');
    200     $msg_error = l10n('nbm_Error when sending email to %s [%s].');
    201     // Last Language
    202     $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     }
    211 
    212     while ($row = mysql_fetch_array($result))
    213     {
    214       $user = array();
    215       $user['id'] = $row['user_id'];
    216       $user = array_merge($user, getuserdata($user['id'], true));
    217 
    218       if ($last_mailtousers_language != $user['language'])
    219       {
    220         $last_mailtousers_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       if ($is_action_send)
    234       {
    235         $message = '';
    236         $news = news($row['last_send'], $dbnow);
    237         if (count($news) > 0)
    238         {
    239           array_push($return_list, $row);
    240 
    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           }
    282         }
    283       }
    284       else
    285       {
    286         if (news_exists($row['last_send'], $dbnow))
    287         {
    288           array_push($return_list, $row);
    289         }
    290       }
    291     }
    292 
    293     // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
    294     $user = $sav_mailtousers_user;
    295     $lang_info = $sav_mailtousers_lang_info;
    296     $lang = $sav_mailtousers_lang;
    297 
    298     if ($is_action_send)
    299     {
    300       mass_updates(
    301         USER_MAIL_NOTIFICATION_TABLE,
    302         array(
    303           'primary' => array('user_id'),
    304           'update' => array('last_send')
    305          ),
    306          $datas
    307          );
    308 
    309       if ($error_on_mail_count != 0)
    310       {
    311         array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sent.'), $error_on_mail_count));
    312       }
    313       else
    314       {
    315         if ($sent_mail_count == 0)
    316           array_push($page['infos'], l10n('nbm_No mail to send.'));
    317         else
    318           array_push($page['infos'], sprintf(l10n('nbm_%d mails were sent.'), $sent_mail_count));
    319       }
    320     }
    321   }
    322   else
    323   {
    324     if ($is_action_send)
    325     {
    326       array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
    327     }
    328   }
    329   return $return_list;
     324    if ($sent_mail_count == 0)
     325      array_push($page['infos'], l10n('nbm_No mail to send.'));
     326    else
     327      array_push($page['infos'], sprintf(l10n('nbm_%d mails were sent.'), $sent_mail_count));
     328  }
    330329}
    331330
     
    377376update
    378377  '.CONFIG_TABLE.'
    379 set 
     378set
    380379  value = \''. str_replace("\'", "''", $value).'\'
    381380where
     
    402401      }
    403402    }
    404    
     403
    405404    if ($updated_param_count != 0)
    406405    {
     
    426425    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']))
    427426    {
    428       do_action_send_mail_notification('send', $_POST['send_selection'], $_POST['send_customize_mail_content']);
     427      do_action_send_mail_notification($_POST['send_selection'], $_POST['send_customize_mail_content']);
    429428    }
    430429  }
     
    498497      );
    499498
    500     $query = '
    501 select
    502   N.check_key, N.enabled, U.username, U.mail_address
    503 from
    504   '.USER_MAIL_NOTIFICATION_TABLE.' as N,
    505   '.USERS_TABLE.' as U
    506 where
    507   N.user_id =  U.id
    508 order by
    509   username;';
    510 
    511     $result = pwg_query($query);
    512     if (!empty($result))
    513     {
    514       while ($row = mysql_fetch_array($result))
    515       {
    516         $template->assign_block_vars(
    517           (get_boolean($row['enabled']) ? 'category_option_true' : 'category_option_false'),
    518           array('SELECTED' => '',
    519                 'VALUE' => $row['check_key'],
    520                 'OPTION' => $row['username'].'['.$row['mail_address'].']'
    521             ));
    522       }
    523     }
    524 
     499    $user_notifications = get_user_notifications();
     500
     501    foreach( $user_notifications as $user_notification)
     502    {
     503      $template->assign_block_vars(
     504        $user_notification['enabled'] ? 'category_option_true' : 'category_option_false',
     505        array('SELECTED' => '',
     506              'VALUE' => $user_notification['check_key'],
     507              'OPTION' => $user_notification['username'].'['.$user_notification['mail_address'].']'
     508          ));
     509    }
    525510    break;
    526511  }
     
    530515    $template->assign_block_vars($page['mode'], array());
    531516
    532     $data_rows = do_action_send_mail_notification('prepare');
     517    $data_rows = get_user_notifications(true,true);
    533518
    534519    if  (count($data_rows) == 0)
     
    541526        $page['mode'].'.send_data',
    542527        array(
    543   //        'URL_CHECK_ALL' => add_url_params($base_url.get_query_string_diff(array('select')), array('select' => 'all')),
    544   //        'URL_UNCHECK_ALL' => add_url_params($base_url.get_query_string_diff(array('select')), array('select' => 'none')),
    545528          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? $_POST['send_customize_mail_content'] : $conf['nbm_complementary_mail_content']
    546529          ));
    547 
    548530      foreach ($data_rows as $num => $local_user)
    549           $template->assign_block_vars(
     531      {
     532        $checked = 'checked="checked"';
     533        if ( isset($_POST['send_submit']) and
     534             ( !isset($_POST['send_selection']) or
     535               !in_array($local_user['check_key'], $_POST['send_selection'])
     536             )
     537           )
     538        {
     539          $checked='';
     540        }
     541        $template->assign_block_vars(
    550542            $page['mode'].'.send_data.user_send_mail',
    551543            array(
    552544              'CLASS' => ($num % 2 == 1) ? 'row2' : 'row1',
    553545              'ID' => $local_user['check_key'],
    554               'CHECKED' => isset($_POST['send_uncheck_all']) ? '' : 'checked="checked"',
     546              'CHECKED' => $checked,
    555547              'USERNAME'=> $local_user['username'],
    556548              'EMAIL' => $local_user['mail_address'],
    557549              'LAST_SEND'=> $local_user['last_send']
    558550              ));
     551      }
    559552    }
    560553
  • trunk/include/scripts.js

    r858 r1114  
    99  {
    1010        formulaire.elements[i].checked = true;
     11  }
     12}
     13}
     14
     15function DeselectAll( formulaire )
     16{
     17len = formulaire.elements.length;
     18var i=0;
     19for( i = 0; i < len; i++)
     20{
     21  if ( formulaire.elements[i].type=='checkbox'
     22           && formulaire.elements[i].name != 'copie')
     23  {
     24        formulaire.elements[i].checked = false;
    1125  }
    1226}
     
    2741}
    2842
    29 function phpWGOpenWindow(theURL,winName,features) 
     43function phpWGOpenWindow(theURL,winName,features)
    3044{
    3145  window.open(theURL,winName,features);
  • trunk/template/yoga/admin/notification_by_mail.tpl

    • Property svn:eol-style set to native
    • Property svn:keywords set to Author Date Id Revision
    r1105 r1114  
    88  <h3>
    99    <p style="text-align:center;">
    10       <a href="{header_link.PARAM_MODE}">{lang:nbm_param_mode}</a> | 
    11       <a href="{header_link.SUBSCRIBE_MODE}">{lang:nbm_subscribe_mode}</a> | 
     10      <a href="{header_link.PARAM_MODE}">{lang:nbm_param_mode}</a> |
     11      <a href="{header_link.SUBSCRIBE_MODE}">{lang:nbm_subscribe_mode}</a> |
    1212      <a href="{header_link.SEND_MODE}">{lang:nbm_send_mode}</a>
    1313    </p>
     
    1616</div>
    1717
    18 <form method="post" name="notification_by_mail" action="{F_ACTION}">
     18<form method="post" name="notification_by_mail" id="notification_by_mail" action="{F_ACTION}">
    1919
    2020  <!-- BEGIN param -->
     
    8484        <!-- END user_send_mail -->
    8585      </table>
    86   <!--
    8786      <p>
    88           <a href="{send.send_data.URL_CHECK_ALL}">{lang:nbm_send_check_all}</a>
    89         / <a href="{send.send_data.URL_UNCHECK_ALL}">{lang:nbm_send_uncheck_all}</a>
    90       </p>
    91   -->
    92       <p>
    93         <input type="submit" value="{lang:nbm_send_check_all}" name="send_check_all"/>
     87        <input type="button" value="{lang:nbm_send_check_all}" onclick="SelectAll(document.getElementById('notification_by_mail'))"/>
    9488        /
    95         <input type="submit" value="{lang:nbm_send_uncheck_all}" name="send_uncheck_all"/>
     89        <input type="button" value="{lang:nbm_send_uncheck_all}" onclick="DeselectAll(document.getElementById('notification_by_mail'))"/>
    9690      </p>
    9791    </fieldset>
Note: See TracChangeset for help on using the changeset viewer.