[ Index ]

PHP Cross Reference of MyBB 1.6.5

title

Body

[close]

/admin/modules/user/ -> mass_mail.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: mass_mail.php 5297 2010-12-28 22:01:14Z Tomm $
  10   */
  11  
  12  // Disallow direct access to this file for security reasons
  13  if(!defined("IN_MYBB"))
  14  {
  15      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  16  }
  17  
  18  require_once  MYBB_ROOT."/inc/functions_massmail.php";
  19  
  20  $page->add_breadcrumb_item($lang->mass_mail, "index.php?module=user-mass_mail");
  21  
  22  if($mybb->input['action'] == "send" || $mybb->input['action'] == "archive" || !$mybb->input['action'])
  23  {
  24      $sub_tabs['mail_queue'] = array(
  25          'title' => $lang->mass_mail_queue,
  26          'link' => 'index.php?module=user-mass_mail',
  27          'description' => $lang->mass_mail_queue_desc
  28      );
  29  
  30      $sub_tabs['send_mass_mail'] = array(
  31          'title' => $lang->create_mass_mail,
  32          'link' => 'index.php?module=user-mass_mail&action=send',
  33          'description' => $lang->create_mass_mail_desc
  34      );
  35  
  36      $sub_tabs['archive'] = array(
  37          'title' => $lang->mass_mail_archive,
  38          'link' => 'index.php?module=user-mass_mail&action=archive',
  39          'description' => $lang->mass_mail_archive_desc
  40      );
  41  }
  42  
  43  if($mybb->input['action'] == "edit")
  44  {    
  45      $page->add_breadcrumb_item($lang->edit_mass_mail);
  46  
  47      $query = $db->simple_select("massemails", "*", "mid='".intval($mybb->input['mid'])."'");
  48      $email = $db->fetch_array($query);
  49      if(!$email['mid'])
  50      {
  51          flash_message($lang->error_invalid_mid, 'error');
  52          admin_redirect("index.php?module=user-mass_mail");
  53      }
  54      
  55      if($email['conditions'] != '')
  56      {
  57          $email['conditions'] = unserialize($email['conditions']);
  58      }
  59      
  60      $sub_tabs['edit_mass_mail'] = array(
  61          'title' => $lang->edit_mass_mail,
  62          'link' => 'index.php?module=user-mass_mail&amp;action=edit&amp;mid='.$email['mid'],
  63          'description' => $lang->edit_mass_mail_desc
  64      );
  65  
  66      $replacement_fields = array(
  67          "{username}" => $lang->username,
  68          "{email}" => $lang->email_addr,
  69          "{bbname}" => $lang->board_name,
  70          "{bburl}" => $lang->board_url
  71      );
  72      
  73      $html_personalisation = $text_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('{$lang->personalize_message} ";
  74      foreach($replacement_fields as $value => $name)
  75      {
  76          $html_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'htmlmessage\')); return false;\">{$name}</a>], ";
  77          $text_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'message\')); return false;\">{$name}</a>], ";
  78      }
  79      $html_personalisation = substr($html_personalisation, 0, -2)."');\n// --></script>\n";
  80      $text_personalisation = substr($text_personalisation, 0, -2)."');\n// --></script>\n";
  81  
  82      // All done here
  83      if($mybb->request_method == "post")
  84      {
  85          // Sending this message now
  86          if($mybb->input['delivery_type'] == "now")
  87          {
  88              $delivery_date = TIME_NOW;
  89          }
  90          // Delivering in the future
  91          else
  92          {
  93              if(strstr($mybb->input['deliverytime_time'], "pm"))
  94              {
  95                  $mybb->input['deliveryhour'] += 12;
  96              }
  97              
  98              $exploded = explode(':', $mybb->input['endtime_time']);
  99              $mybb->input['deliveryhour'] = intval($exploded[0]);
 100              
 101              $exploded = explode(' ', $exploded[1]);
 102              $mybb->input['deliveryminute'] = intval($exploded[0]);
 103              
 104              $delivery_date = gmmktime($mybb->input['deliveryhour'], $mybb->input['deliveryminute'], 0, $mybb->input['endtime_month'], $mybb->input['endtime_day'], $mybb->input['endtime_year']) + $mybb->user['timezone']*3600;
 105              if($delivery_date <= TIME_NOW)
 106              {
 107                  $errors[] = $lang->error_only_in_future;
 108              }
 109          }
 110          
 111          // Need to perform the search to fetch the number of users we're emailing
 112          $member_query = build_mass_mail_query($mybb->input['conditions']);
 113          $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
 114          $num = $db->fetch_field($query, "num");
 115  
 116          if($num == 0)
 117          {
 118              $errors[] = $lang->error_no_users;
 119          }
 120          
 121          if(!trim($mybb->input['subject']))
 122          {
 123              $errors[] = $lang->error_missing_subject;
 124          }
 125          
 126          if($mybb->input['type'] == 1)
 127          {
 128              if(!$mybb->input['message'])
 129              {
 130                  $errors[] = $lang->error_missing_message;
 131              }
 132          }
 133          else
 134          {
 135              if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 0 && !$mybb->input['message'])
 136              {
 137                  $errors[] = $lang->error_missing_plain_text;
 138              }
 139              
 140              if(($mybb->input['format'] == 1 || $mybb->input['format'] == 2) && !$mybb->input['htmlmessage'])
 141              {
 142                  $errors[] = $lang->error_missing_html;
 143              }
 144              else if($mybb->input['format'] == 0 && !$mybb->input['message'])
 145              {
 146                  $errors[] = $lang->error_missing_plain_text;
 147              }
 148          }                    
 149          
 150          if(!$errors)
 151          {
 152              // Sending via a PM
 153              if($mybb->input['type'] == 1)
 154              {
 155                  $mybb->input['format'] = 0;
 156                  $mybb->input['htmlmessage'] = '';
 157              }
 158              // Sending via email
 159              else
 160              {
 161                  // Do we need to generate a text based version?
 162                  if($mybb->input['format'] == 2 && $mybb->input['automatic_text'])
 163                  {
 164                      $mybb->input['message'] = create_text_message($mybb->input['htmlmessage']);
 165                  }
 166                  else if($mybb->input['format'] == 1)
 167                  {
 168                      $mybb->input['message'] = '';
 169                  }
 170                  else if($mybb->input['format'] == 0)
 171                  {
 172                      $mybb->input['htmlmessage'] = '';
 173                  }
 174              }
 175  
 176              // Mark as queued for delivery
 177              $updated_email = array(
 178                  "status" => 1,
 179                  "senddate" => $delivery_date,
 180                  "totalcount" => $num,
 181                  "conditions" => $db->escape_string(serialize($mybb->input['conditions'])),
 182                  "message" => $db->escape_string($mybb->input['message']),
 183                  "subject" => $db->escape_string($mybb->input['subject']),
 184                  "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
 185                  "format" => intval($mybb->input['format']),
 186                  "type" => intval($mybb->input['type']),
 187              );
 188              $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 189  
 190              flash_message($lang->success_mass_mail_saved, 'success');
 191              admin_redirect("index.php?module=user-mass_mail");
 192          }
 193      }
 194      
 195      $page->output_header($lang->edit_mass_mail);
 196  
 197      $page->output_nav_tabs($sub_tabs, 'edit_mass_mail');
 198      
 199      // If we have any error messages, show them
 200      if($errors)
 201      {
 202          $page->output_inline_error($errors);
 203          $input = $mybb->input;
 204      }
 205      else
 206      {
 207          $input = $email;
 208          
 209          if($email['senddate'] != 0)
 210          {
 211              if($email['senddate'] <= TIME_NOW)
 212              {
 213                  $input['delivery_type'] = "now";
 214                  $delivery_type_checked['now'] = " checked=\"checked\"";
 215              }
 216              else
 217              {
 218                  $input['delivery_type'] = "future";
 219                  $time = date("d-n-Y-h-i-a", $email['senddate']);
 220                  $time = explode('-', $time);
 221                  $input['deliveryhour'] = (int)$time[3];
 222                  $input['deliveryminute'] = (int)$time[4];
 223                  $input['deliverymonth'] = (int)$time[1];
 224                  $input['deliveryday'] = (int)$time[0];
 225                  $input['deliveryyear'] = (int)$time[2];
 226                  $input['deliverymeridiem'] = $time[5];
 227                  $delivery_type_checked['future'] = " checked=\"checked\"";
 228              }
 229          }
 230          else
 231          {
 232              $input['delivery_type'] = "now";
 233              $delivery_type_checked['now'] = " checked=\"checked\"";
 234          }
 235      }
 236      
 237      if($input['deliveryhour'])
 238      {
 239          $input['endtime_time'] = intval($input['deliveryhour']).":";
 240      }
 241      else
 242      {
 243          $input['endtime_time'] = "12:";
 244      }
 245      
 246      if($input['deliveryminute'])
 247      {
 248          $input['endtime_time'] .= intval($input['deliveryminute'])." ";
 249      }
 250      else
 251      {
 252          $input['endtime_time'] .= "00 ";
 253      }
 254      
 255      if($input['deliverymeridiem'])
 256      {
 257          $input['endtime_time'] .= $input['deliverymeridiem'];
 258      }
 259      else
 260      {
 261          $input['endtime_time'] .= "am";
 262      }
 263      
 264      if(!$input['deliveryyear'])
 265      {
 266          $enddateyear = gmdate('Y', TIME_NOW);
 267      }
 268      else
 269      {
 270          $enddateyear = intval($input['deliveryyear']);
 271      }
 272      
 273      if(!$input['deliverymonth'])
 274      {
 275          $input['enddatemonth'] = gmdate('n', TIME_NOW);
 276      }
 277      else
 278      {
 279          $input['enddatemonth'] = intval($input['deliverymonth']);
 280      }
 281      
 282      if(!$input['deliveryday'])
 283      {
 284          $input['enddateday'] = gmdate('j', TIME_NOW);
 285      }
 286      else
 287      {
 288          $input['enddateday'] = intval($input['deliveryday']);
 289      }
 290      
 291      $form = new Form("index.php?module=user-mass_mail&amp;action=edit", "post");
 292      echo $form->generate_hidden_field("mid", $email['mid']);
 293  
 294      $mid_add = '';
 295      if($email['mid'])
 296      {
 297          $mid_add = "&amp;mid={$email['mid']}";
 298      }
 299  
 300      $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->message_settings}");
 301  
 302      $form_container->output_row("{$lang->subject}: <em>*</em>", $lang->subject_desc, $form->generate_text_box('subject', $input['subject'], array('id' => 'subject')), 'subject');
 303  
 304      if($input['type'] == 0)
 305      {
 306          $type_email_checked = true;
 307          $type_pm_checked = false;
 308      }
 309      else if($input['type'] == 1)
 310      {
 311          $type_email_checked = false;
 312          $type_pm_checked = true;
 313      }
 314  
 315      $type_options = array(
 316          $form->generate_radio_button("type", 0, $lang->send_via_email, array("id" => "type_email", "checked" => $type_email_checked)),
 317          $form->generate_radio_button("type", 1, $lang->send_via_pm, array("id" => "type_pm", "checked" => $type_pm_checked))
 318      );
 319      $form_container->output_row("{$lang->message_type}: <em>*</em>", "", implode("<br />", $type_options));
 320      
 321      $monthnames = array(
 322          "offset",
 323          $lang->january,
 324          $lang->february,
 325          $lang->march,
 326          $lang->april,
 327          $lang->may,
 328          $lang->june,
 329          $lang->july,
 330          $lang->august,
 331          $lang->september,
 332          $lang->october,
 333          $lang->november,
 334          $lang->december,
 335      );
 336      
 337      $enddatemonth = "";
 338      foreach($monthnames as $key => $month)
 339      {
 340          if($month == "offset")
 341          {
 342              continue;
 343          }
 344          
 345          if($key == $input['enddatemonth'])
 346          {
 347              $enddatemonth .= "<option value=\"{$key}\" selected=\"selected\">{$month}</option>\n";
 348          }
 349          else
 350          {
 351              $enddatemonth .= "<option value=\"{$key}\">{$month}</option>\n";
 352          }
 353      }
 354      
 355      $enddateday = "";
 356      
 357      // Construct option list for days
 358      for($i = 1; $i <= 31; ++$i)
 359      {
 360          if($i == $input['enddateday'])
 361          {
 362              $enddateday .= "<option value=\"{$i}\" selected=\"selected\">{$i}</option>\n";
 363          }
 364          else
 365          {
 366              $enddateday .= "<option value=\"{$i}\">{$i}</option>\n";
 367          }
 368      }
 369      
 370      $actions = "<script type=\"text/javascript\">
 371  		function checkAction(id)
 372          {
 373              var checked = '';
 374  
 375              $$('.'+id+'s_check').each(function(e)
 376              {
 377                  if(e.checked == true)
 378                  {
 379                      checked = e.value;
 380                  }
 381              });
 382              $$('.'+id+'s').each(function(e)
 383              {
 384                  Element.hide(e);
 385              });
 386              if($(id+'_'+checked))
 387              {
 388                  Element.show(id+'_'+checked);
 389              }
 390          }    
 391      </script>
 392          <dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
 393          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"now\" {$delivery_type_checked['now']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_immediately}</strong></label></dt>
 394  
 395          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"future\" {$delivery_type_checked['future']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_specific}</strong></label></dt>
 396              <dd style=\"margin-top: 4px;\" id=\"delivery_type_future\" class=\"delivery_types\">
 397                  <table cellpadding=\"4\">
 398                      <tr>
 399                          <td><select name=\"endtime_day\">\n{$enddateday}</select>\n &nbsp; \n<select name=\"endtime_month\">\n{$enddatemonth}</select>\n &nbsp; \n<input type=\"text\" name=\"endtime_year\" value=\"{$enddateyear}\" class=\"text_input\" size=\"4\" maxlength=\"4\" />\n - {$lang->time} ".$form->generate_text_box('endtime_time', $input['endtime_time'], array('id' => 'endtime_time', 'style' => 'width: 60px;'))."</td>
 400                      </tr>
 401                  </table>
 402              </dd>
 403          </dl>
 404          <script type=\"text/javascript\">
 405          checkAction('delivery_type');
 406          </script>";
 407      $form_container->output_row("{$lang->delivery_date}: <em>*</em>", $lang->delivery_date_desc, $actions);
 408      
 409      $form_container->output_row("{$lang->per_page}: <em>*</em>", $lang->per_page_desc, $form->generate_text_box('perpage', $input['perpage'], array('id' => 'perpage')), 'perpage');
 410      
 411      $format_options = array(
 412          0 => $lang->plain_text_only,
 413          1 => $lang->html_only,
 414          2 => $lang->html_and_plain_text
 415      );
 416      
 417      $form_container->output_row("{$lang->message_format}: <em>*</em>", "", $form->generate_select_box('format', $format_options, $input['format'], array('id' => 'format')), 'format', null, array("id" => "format_container"));
 418      
 419      $form_container->end();
 420  
 421      if($input['format'] == 2)
 422      {
 423          if($input['automatic_text'] && !$email['mid'])
 424          {
 425              $automatic_text_check = true;
 426              $text_display = 'display: none';
 427              $automatic_display = 'display: none;';
 428          }
 429      }
 430      else if($input['format'] == 1 && $input['type'] != 1)
 431      {
 432          $text_display = 'display: none;';
 433      }
 434      else if($input['format'] == 0 || $input['type'] == 1)
 435      {
 436          $html_display = 'display: none';
 437      }
 438  
 439  
 440      echo "<div id=\"message_html\" style=\"{$html_display}\">";
 441      $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_html_message}");
 442      $form_container->output_row("{$lang->define_html_message_desc}:", $html_personalisation, $form->generate_text_area('htmlmessage', $input['htmlmessage'], array('id' => 'htmlmessage', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%'))."<div id=\"automatic_display\" style=\"{$automatic_display}\">".$form->generate_check_box('automatic_text', 1, $lang->auto_gen_plain_text, array('checked' => $automatic_text_check, "id" => "automatic_text"))."</div>");
 443      $form_container->end();
 444      echo "</div>";
 445  
 446      echo "<div id=\"message_text\" style=\"{$text_display}\">";
 447      $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_text_version}");
 448      $form_container->output_row("{$lang->define_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $input['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
 449      $form_container->end();
 450      echo "</div>";
 451  
 452      echo "
 453      <script type=\"text/javascript\">
 454  	function ToggleFormat()
 455      {
 456          var v = $('format').options[$('format').selectedIndex].value;
 457          if(v == 2)
 458          {
 459              $('automatic_display').show();
 460              $('message_html').show();
 461              if($('automatic_text').checked)
 462              {
 463                  $('message_text').hide();
 464              }
 465              else
 466              {
 467                  $('message_text').show();
 468              }
 469          }
 470          else if(v == 1)
 471          {
 472              $('message_text').hide();
 473              $('message_html').show();
 474              $('automatic_display').hide();
 475          }
 476          else
 477          {
 478              $('message_text').show();
 479              $('message_html').hide();
 480          }
 481      }
 482      Event.observe($('format'), 'change', ToggleFormat);
 483  
 484  	function ToggleType()
 485      {
 486          var v = $('type_pm').checked;
 487          if(v == true)
 488          {
 489              $('message_html').hide();
 490              $('message_text').show();
 491              $('format_container').hide();
 492          }
 493          else
 494          {
 495              $('message_html').show();
 496              $('format_container').show();
 497              ToggleFormat();
 498          }
 499      }
 500      Event.observe($('type_pm'), 'click', ToggleType);
 501      Event.observe($('type_email'), 'click', ToggleType);
 502      ToggleType();
 503  
 504  	function ToggleAutomatic()
 505      {
 506          var v = $('automatic_text').checked;
 507          if(v == true)
 508          {
 509              $('message_text').hide();
 510          }
 511          else
 512          {
 513              $('message_text').show();
 514          }
 515      }
 516  
 517      Event.observe($('automatic_text'), 'click', ToggleAutomatic);
 518      
 519  	function insertText(value, textarea)
 520      {
 521          // Internet Explorer
 522          if(document.selection)
 523          {
 524              textarea.focus();
 525              var selection = document.selection.createRange();
 526              selection.text = value;
 527          }
 528          // Firefox
 529          else if(textarea.selectionStart || textarea.selectionStart == '0')
 530          {
 531              var start = textarea.selectionStart;
 532              var end = textarea.selectionEnd;
 533              textarea.value = textarea.value.substring(0, start)    + value    + textarea.value.substring(end, textarea.value.length);
 534          }
 535          else
 536          {
 537              textarea.value += value;
 538          }
 539      }
 540      
 541      </script>";
 542  
 543      $form_container = new FormContainer("{$lang->edit_mass_mail}: {$lang->define_the_recipients}");
 544  
 545      $form_container->output_row($lang->username_contains, "", $form->generate_text_box('conditions[username]', $input['conditions']['username'], array('id' => 'username')), 'username');
 546      $form_container->output_row($lang->email_addr_contains, "", $form->generate_text_box('conditions[email]', $input['conditions']['email'], array('id' => 'email')), 'email');
 547  
 548      $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
 549      while($usergroup = $db->fetch_array($query))
 550      {
 551          $options[$usergroup['gid']] = $usergroup['title'];
 552      }
 553  
 554      $form_container->output_row($lang->members_of, $lang->additional_user_groups_desc, $form->generate_select_box('conditions[usergroup][]', $options, $input['conditions']['usergroup'], array('id' => 'usergroups', 'multiple' => true, 'size' => 5)), 'usergroups');
 555  
 556      $greater_options = array(
 557          "greater_than" => $lang->greater_than,
 558          "is_exactly" => $lang->is_exactly,
 559          "less_than" => $lang->less_than
 560      );
 561      $form_container->output_row($lang->post_count_is, "", $form->generate_select_box('conditions[postnum_dir]', $greater_options, $input['conditions']['postnum_dir'], array('id' => 'numposts_dir'))." ".$form->generate_text_box('conditions[postnum]', $input['conditions']['numposts'], array('id' => 'numposts')), 'numposts');
 562  
 563      // Need to do reg date & last visit periods. FIGURE OUT HOW TO HANDLE/DISPLAY (Do the same as StoreSuite)
 564  
 565      $form_container->end();
 566  
 567      $buttons[] = $form->generate_submit_button($lang->save_mass_mail);
 568      $form->output_submit_wrapper($buttons);
 569  
 570      $form->end();
 571      $page->output_footer();
 572  }
 573  
 574  
 575  if($mybb->input['action'] == "send")
 576  {
 577      $page->add_breadcrumb_item($lang->send_mass_mail);
 578  
 579      if($mybb->input['step'])
 580      {
 581          $query = $db->simple_select("massemails", "*", "status=0 and mid='".intval($mybb->input['mid'])."'");
 582          $email = $db->fetch_array($query);
 583          if(!$email['mid'] && $mybb->input['step'] != 1)
 584          {
 585              flash_message($lang->error_invalid_mid, 'error');
 586              admin_redirect("index.php?module=user-mass_mail");
 587          }
 588      }
 589  
 590      $replacement_fields = array(
 591          "{username}" => $lang->username,
 592          "{email}" => $lang->email_addr,
 593          "{bbname}" => $lang->board_name,
 594          "{bburl}" => $lang->board_url
 595      );
 596      
 597      $html_personalisation = $text_personalisation = "<script type=\"text/javascript\">\n<!--\ndocument.write('{$lang->personalize_message}: ";
 598      foreach($replacement_fields as $value => $name)
 599      {
 600          $html_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'htmlmessage\')); return false;\">{$name}</a>], ";
 601          $text_personalisation .= " [<a href=\"#\" onclick=\"insertText(\'{$value}\', \$(\'message\')); return false;\">{$name}</a>], ";
 602      }
 603      $html_personalisation = substr($html_personalisation, 0, -2)."');\n// --></script>\n";
 604      $text_personalisation = substr($text_personalisation, 0, -2)."');\n// --></script>\n";
 605  
 606      if($mybb->input['step'] == 4)
 607      {
 608          // All done here
 609          if($mybb->request_method == "post")
 610          {
 611              // Sending this message now
 612              if($mybb->input['delivery_type'] == "now")
 613              {
 614                  $delivery_date = TIME_NOW;
 615              }
 616              // Delivering in the future
 617              else
 618              {
 619                  if(strstr($mybb->input['deliverytime_time'], "pm"))
 620                  {
 621                      $mybb->input['deliveryhour'] += 12;
 622                  }
 623                  
 624                  $exploded = explode(':', $mybb->input['endtime_time']);
 625                  $mybb->input['deliveryhour'] = intval($exploded[0]);
 626                  
 627                  $exploded = explode(' ', $exploded[1]);
 628                  $mybb->input['deliveryminute'] = intval($exploded[0]);
 629              
 630                  $delivery_date = gmmktime($mybb->input['deliveryhour'], $mybb->input['deliveryminute'], 0, $mybb->input['endtime_month'], $mybb->input['endtime_day'], $mybb->input['endtime_year']) + $mybb->user['timezone']*3600;
 631                  if($delivery_date <= TIME_NOW)
 632                  {
 633                      $errors[] = $lang->error_only_in_future;
 634                  }
 635              }
 636              
 637              if(!$errors)
 638              {
 639                  // Mark as queued for delivery
 640                  $updated_email = array(
 641                      "status" => 1,
 642                      "senddate" => $delivery_date
 643                  );
 644                  $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 645  
 646                  flash_message($lang->success_mass_mail_saved, 'success');
 647                  admin_redirect("index.php?module=user-mass_mail");
 648              }
 649          }
 650  
 651          // Show summary of the mass email we've just been creating and allow the user to specify the delivery date
 652          $page->output_header("{$lang->send_mass_mail}: {$lang->step_four}");
 653  
 654          $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
 655          
 656          // If we have any error messages, show them
 657          if($errors)
 658          {
 659              $page->output_inline_error($errors);
 660              $input = $mybb->input;
 661          }
 662          else
 663          {
 664              if($email['senddate'] != 0)
 665              {
 666                  $input = array();
 667                  if($email['senddate'] <= TIME_NOW)
 668                  {
 669                      $input['delivery_type'] = "now";
 670                      $delivery_type_checked['now'] = " checked=\"checked\"";
 671                  }
 672                  else
 673                  {
 674                      $input['delivery_type'] = "future";
 675                      $time = date("d-n-Y-h-i-a", $email['senddate']);
 676                      $time = explode('-', $time);
 677                      $input['deliveryhour'] = (int)$time[3];
 678                      $input['deliveryminute'] = (int)$time[4];
 679                      $input['deliverymonth'] = (int)$time[1];
 680                      $input['deliveryday'] = (int)$time[0];
 681                      $input['deliveryyear'] = (int)$time[2];
 682                      $input['deliverymeridiem'] = $time[5];
 683                      $delivery_type_checked['future'] = " checked=\"checked\"";
 684                  }
 685              }
 686              else
 687              {
 688                  $input['delivery_type'] = "now";
 689                  $delivery_type_checked['now'] = " checked=\"checked\"";
 690              }
 691          }
 692          
 693          $table = new Table;
 694          $table->construct_cell("<strong>{$lang->delivery_method}:</strong>", array('width' => '25%'));
 695          if($email['type'] == 1)
 696          {
 697              $delivery_type = $lang->private_message;
 698          }
 699          else if($email['type'] == 0)
 700          {
 701              $delivery_type = $lang->email;
 702          }
 703          $table->construct_cell($delivery_type);
 704          $table->construct_row();
 705          
 706          $table->construct_cell("<strong>{$lang->subject}:</strong>");
 707          $table->construct_cell(htmlspecialchars_uni($email['subject']));
 708          $table->construct_row();
 709          
 710          $table->construct_cell("<strong>{$lang->message}:</strong>");
 711          $format_preview = '';
 712          if($email['format'] == 0 || $email['format'] == 2)
 713          {
 714              $format_preview .= "{$lang->text_based} - <a href=\"#\" onclick=\"javascript:MyBB.popupWindow('index.php?module=user-mass_mail&amp;action=preview&amp;mid={$email['mid']}&amp;format=text', 'preview', 450, 450);\">{$lang->preview}</a>";
 715          }
 716          if($email['format'] == 2)
 717          {
 718              $format_preview .= " {$lang->and} <br />";
 719          }
 720          if($email['format'] == 1 || $email['format'] == 2)
 721          {
 722              $format_preview.= "{$lang->html_based} - <a href=\"#\" onclick=\"javascript:MyBB.popupWindow('index.php?module=user-mass_mail&amp;action=preview&amp;mid={$email['mid']}', 'preview', 450, 450);\">{$lang->preview}</a>";
 723          }
 724          $table->construct_cell($format_preview);
 725          $table->construct_row();
 726          
 727          // Recipient counts & details
 728          $table->construct_cell("<strong>{$lang->total_recipients}:</strong>");
 729          $table->construct_cell(my_number_format($email['totalcount'])." - <a href=\"index.php?module=user-mass_mail&amp;action=send&amp;step=3&amp;mid={$email['mid']}\">{$lang->change_recipient_conds}</a>");
 730          $table->construct_row();
 731          
 732          $table->output("{$lang->send_mass_mail}: {$lang->step_four} - {$lang->review_message}");
 733          
 734          if($input['deliveryhour'])
 735          {
 736              $input['endtime_time'] = intval($input['deliveryhour']).":";
 737          }
 738          else
 739          {
 740              $input['endtime_time'] = "12:";
 741          }
 742          
 743          if($input['deliveryminute'])
 744          {
 745              $input['endtime_time'] .= intval($input['deliveryminute'])." ";
 746          }
 747          else
 748          {
 749              $input['endtime_time'] .= "00 ";
 750          }
 751          
 752          if($input['deliverymeridiem'])
 753          {
 754              $input['endtime_time'] .= $input['deliverymeridiem'];
 755          }
 756          else
 757          {
 758              $input['endtime_time'] .= "am";
 759          }
 760          
 761          if(!$input['deliveryyear'])
 762          {
 763              $enddateyear = gmdate('Y', TIME_NOW);
 764          }
 765          else
 766          {
 767              $enddateyear = intval($input['deliveryyear']);
 768          }
 769          
 770          if(!$input['deliverymonth'])
 771          {
 772              $input['enddatemonth'] = gmdate('n', TIME_NOW);
 773          }
 774          else
 775          {
 776              $input['enddatemonth'] = intval($input['deliverymonth']);
 777          }
 778          
 779          if(!$input['deliveryday'])
 780          {
 781              $input['enddateday'] = gmdate('j', TIME_NOW);
 782          }
 783          else
 784          {
 785              $input['enddateday'] = intval($input['deliveryday']);
 786          }
 787          
 788          $monthnames = array(
 789              "offset",
 790              $lang->january,
 791              $lang->february,
 792              $lang->march,
 793              $lang->april,
 794              $lang->may,
 795              $lang->june,
 796              $lang->july,
 797              $lang->august,
 798              $lang->september,
 799              $lang->october,
 800              $lang->november,
 801              $lang->december,
 802          );
 803          
 804          $enddatemonth = "";
 805          foreach($monthnames as $key => $month)
 806          {
 807              if($month == "offset")
 808              {
 809                  continue;
 810              }
 811              
 812              if($key == $input['enddatemonth'])
 813              {
 814                  $enddatemonth .= "<option value=\"{$key}\" selected=\"selected\">{$month}</option>\n";
 815              }
 816              else
 817              {
 818                  $enddatemonth .= "<option value=\"{$key}\">{$month}</option>\n";
 819              }
 820          }
 821          
 822          $enddateday = "";
 823          
 824          // Construct option list for days
 825          for($i = 1; $i <= 31; ++$i)
 826          {
 827              if($i == $input['enddateday'])
 828              {
 829                  $enddateday .= "<option value=\"{$i}\" selected=\"selected\">{$i}</option>\n";
 830              }
 831              else
 832              {
 833                  $enddateday .= "<option value=\"{$i}\">{$i}</option>\n";
 834              }
 835          }
 836          
 837          $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=4&amp;mid={$email['mid']}", "post");
 838          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_four} - {$lang->define_delivery_date}");
 839  
 840              $actions = "<script type=\"text/javascript\">
 841  		    function checkAction(id)
 842              {
 843                  var checked = '';
 844  
 845                  $$('.'+id+'s_check').each(function(e)
 846                  {
 847                      if(e.checked == true)
 848                      {
 849                          checked = e.value;
 850                      }
 851                  });
 852                  $$('.'+id+'s').each(function(e)
 853                  {
 854                      Element.hide(e);
 855                  });
 856                  if($(id+'_'+checked))
 857                  {
 858                      Element.show(id+'_'+checked);
 859                  }
 860              }    
 861          </script>
 862              <dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
 863              <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"now\" {$delivery_type_checked['now']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_immediately}</strong></label></dt>
 864  
 865              <dt><label style=\"display: block;\"><input type=\"radio\" name=\"delivery_type\" value=\"future\" {$delivery_type_checked['future']} class=\"delivery_types_check\" onclick=\"checkAction('delivery_type');\" style=\"vertical-align: middle;\" /> <strong>{$lang->deliver_specific}</strong></label></dt>
 866                  <dd style=\"margin-top: 4px;\" id=\"delivery_type_future\" class=\"delivery_types\">
 867                      <table cellpadding=\"4\">
 868                          <tr>
 869                              <td><select name=\"endtime_day\">\n{$enddateday}</select>\n &nbsp; \n<select name=\"endtime_month\">\n{$enddatemonth}</select>\n &nbsp; \n<input type=\"text\" name=\"endtime_year\" class=\"text_input\" value=\"{$enddateyear}\" size=\"4\" maxlength=\"4\" />\n - {$lang->time} ".$form->generate_text_box('endtime_time', $input['endtime_time'], array('id' => 'endtime_time', 'style' => 'width: 60px;'))."</td>
 870                          </tr>
 871                      </table>
 872                  </dd>
 873              </dl>
 874              <script type=\"text/javascript\">
 875              checkAction('delivery_type');
 876              </script>";
 877              $form_container->output_row("{$lang->delivery_date}: <em>*</em>", $lang->delivery_date_desc, $actions);
 878              
 879          $form_container->end();
 880  
 881          $buttons[] = $form->generate_submit_button($lang->schedule_for_delivery);
 882          $form->output_submit_wrapper($buttons);
 883  
 884          $form->end();
 885          $page->output_footer();        
 886      }
 887  
 888      if($mybb->input['step'] == 3)
 889      {
 890          // Define the recipients/conditions
 891          if($mybb->request_method == "post")
 892          {
 893              // Need to perform the search to fetch the number of users we're emailing
 894              $member_query = build_mass_mail_query($mybb->input['conditions']);
 895              $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
 896              $num = $db->fetch_field($query, "num");
 897  
 898              if($num == 0)
 899              {
 900                  $errors[] = $lang->error_no_users;
 901              }
 902              // Got one or more results
 903              else
 904              {
 905                  $updated_email = array(
 906                      "totalcount" => $num,
 907                      "conditions" => $db->escape_string(serialize($mybb->input['conditions']))
 908                  );
 909                  $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 910  
 911                  // Take the user to the next step
 912                  admin_redirect("index.php?module=user-mass_mail&action=send&step=4&mid={$email['mid']}");
 913              }
 914          }
 915  
 916          $page->output_header("{$lang->send_mass_mail}: {$lang->step_three}");
 917  
 918          $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=3&amp;mid={$email['mid']}", "post");
 919          $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
 920  
 921          // If we have any error messages, show them
 922          if($errors)
 923          {
 924              $page->output_inline_error($errors);
 925              $input = $mybb->input;
 926          }
 927          else
 928          {
 929              if($email['conditions'] != '')
 930              {
 931                  $input = array(
 932                      "conditions" => unserialize($email['conditions'])
 933                  );
 934              }
 935          }
 936  
 937          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_three} - {$lang->define_the_recipients}");
 938  
 939          $form_container->output_row($lang->username_contains, "", $form->generate_text_box('conditions[username]', $input['conditions']['username'], array('id' => 'username')), 'username');
 940          $form_container->output_row($lang->email_addr_contains, "", $form->generate_text_box('conditions[email]', $input['conditions']['email'], array('id' => 'email')), 'email');
 941  
 942          $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
 943          while($usergroup = $db->fetch_array($query))
 944          {
 945              $options[$usergroup['gid']] = $usergroup['title'];
 946          }
 947  
 948          $form_container->output_row($lang->members_of, $lang->additional_user_groups_desc, $form->generate_select_box('conditions[usergroup][]', $options, $input['conditions']['usergroup'], array('id' => 'usergroups', 'multiple' => true, 'size' => 5)), 'usergroups');
 949  
 950          $greater_options = array(
 951              "greater_than" => $lang->greater_than,
 952              "is_exactly" => $lang->is_exactly,
 953              "less_than" => $lang->less_than
 954          );
 955          $form_container->output_row($lang->post_count_is, "", $form->generate_select_box('conditions[postnum_dir]', $greater_options, $input['conditions']['postnum_dir'], array('id' => 'numposts_dir'))." ".$form->generate_text_box('conditions[postnum]', $input['conditions']['numposts'], array('id' => 'numposts')), 'numposts');
 956  
 957          // Need to do reg date & last visit periods. FIGURE OUT HOW TO HANDLE/DISPLAY (Do the same as StoreSuite)
 958  
 959          $form_container->end();
 960  
 961          $buttons[] = $form->generate_submit_button($lang->next_step);
 962          $form->output_submit_wrapper($buttons);
 963  
 964          $form->end();
 965          $page->output_footer();
 966      }
 967  
 968      // Reviewing the automatic text based version of the message.
 969      if($mybb->input['step'] == 2)
 970      {
 971          // Update text based version
 972          if($mybb->request_method == "post")
 973          {
 974              if(!trim($mybb->input['message']))
 975              {
 976                  $errors[] = $lang->error_missing_plain_text;
 977              }
 978              else
 979              {
 980                  $updated_email = array(
 981                      "message" => $db->escape_string($mybb->input['message'])
 982                  );
 983                  $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
 984  
 985                  // Take the user to the next step
 986                  admin_redirect("index.php?module=user-mass_mail&action=send&step=3&mid={$email['mid']}");
 987              }
 988          }
 989  
 990          $page->output_header("{$lang->send_mass_mail}: {$lang->step_two}");
 991  
 992          $form = new Form("index.php?module=user-mass_mail&amp;action=send&amp;step=2&amp;mid={$email['mid']}", "post");
 993          $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
 994  
 995          // If we have any error messages, show them
 996          if($errors)
 997          {
 998              $page->output_inline_error($errors);
 999          }
1000  
1001          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_two} - {$lang->review_text_version}");
1002          $form_container->output_row("{$lang->review_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $email['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
1003          $form_container->end();
1004  
1005          $buttons[] = $form->generate_submit_button($lang->next_step);
1006          $form->output_submit_wrapper($buttons);
1007  
1008          $form->end();
1009          $page->output_footer();
1010      }
1011  
1012      if(!$mybb->input['step'] || $mybb->input['step'] == 1)
1013      {
1014          if($mybb->request_method == "post")
1015          {
1016              if(!trim($mybb->input['subject']))
1017              {
1018                  $errors[] = $lang->error_missing_subject;
1019              }
1020              
1021              if($mybb->input['type'] == 1)
1022              {
1023                  if(!$mybb->input['message'])
1024                  {
1025                      $errors[] = $lang->error_missing_message;
1026                  }
1027              }
1028              else
1029              {
1030                  if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 0 && !$mybb->input['message'])
1031                  {
1032                      $errors[] = $lang->error_missing_plain_text;
1033                  }
1034                  
1035                  if(($mybb->input['format'] == 1 || $mybb->input['format'] == 2) && !$mybb->input['htmlmessage'])
1036                  {
1037                      $errors[] = $lang->error_missing_html;
1038                  }
1039                  else if($mybb->input['format'] == 0 && !$mybb->input['message'])
1040                  {
1041                      $errors[] = $lang->error_missing_plain_text;
1042                  }
1043              }
1044  
1045              // No errors, insert away
1046              if(!$errors)
1047              {
1048                  if(!$new_email['mid'])
1049                  {
1050                      // Sending via a PM
1051                      if($mybb->input['type'] == 1)
1052                      {
1053                          $mybb->input['format'] = 0;
1054                          $mybb->input['htmlmessage'] = '';
1055                      }
1056                      // Sending via email
1057                      else
1058                      {
1059                          // Do we need to generate a text based version?
1060                          if($mybb->input['format'] == 2 && $mybb->input['automatic_text'])
1061                          {
1062                              $mybb->input['message'] = create_text_message($mybb->input['htmlmessage']);
1063                          }
1064                          else if($mybb->input['format'] == 1)
1065                          {
1066                              $mybb->input['message'] = '';
1067                          }
1068                          else if($mybb->input['format'] == 0)
1069                          {
1070                              $mybb->input['htmlmessage'] = '';
1071                          }
1072                      }
1073  
1074                      $new_email = array(
1075                          "uid" => $mybb->user['uid'],
1076                          "subject" => $db->escape_string($mybb->input['subject']),
1077                          "message" => $db->escape_string($mybb->input['message']),
1078                          "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
1079                          "format" => intval($mybb->input['format']),
1080                          "type" => intval($mybb->input['type']),
1081                          "dateline" => TIME_NOW,
1082                          "senddate" => 0,
1083                          "status" => 0,
1084                          "sentcount" => 0,
1085                          "totalcount" => 0,
1086                          "conditions" => "",
1087                          "perpage" => intval($mybb->input['perpage'])
1088                      );
1089                      $mid = $db->insert_query("massemails", $new_email);
1090                  }
1091                  // Updating an existing one
1092                  else
1093                  {
1094                      $updated_email = array(
1095                          "subject" => $db->escape_string($mybb->input['subject']),
1096                          "message" => $db->escape_string($mybb->input['message']),
1097                          "htmlmessage" => $db->escape_string($mybb->input['htmlmessage']),
1098                          "format" => intval($mybb->input['format']),
1099                          "type" => intval($mybb->input['type']),
1100                          "perpage" => intval($mybb->input['perpage'])
1101                      );
1102                      $db->update_query("massemails", $updated_email, "mid='{$email['mid']}'");
1103                      $mid = $email['mid'];
1104                  }
1105  
1106                  if($mybb->input['format'] == 2 && $mybb->input['automatic_text'] == 1)
1107                  {
1108                      $next = 2;
1109                  }
1110                  else
1111                  {
1112                      $next = 3;
1113                  }
1114                  admin_redirect("index.php?module=user-mass_mail&action=send&step={$next}&mid={$mid}");
1115              }
1116          }
1117  
1118          $page->output_header("{$lang->send_mass_mail}: {$lang->step_one}");
1119  
1120          $mid_add = '';
1121          if($email['mid'])
1122          {
1123              $mid_add = "&amp;mid={$email['mid']}";
1124          }
1125  
1126          $form = new Form("index.php?module=user-mass_mail&amp;action=send{$mid_add}", "post");
1127          $page->output_nav_tabs($sub_tabs, 'send_mass_mail');
1128  
1129          // If we have any error messages, show them
1130          if($errors)
1131          {
1132              $page->output_inline_error($errors);
1133              $input = $mybb->input;
1134          }
1135          else if(!$email)
1136          {
1137              $input = array(
1138                  "type" => 0,
1139                  "format" => 2,
1140                  "automatic_text" => 1,
1141                  "perpage" => 50,
1142              );
1143          }
1144          else
1145          {
1146              $input = $email;
1147          }
1148  
1149          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->message_settings}");
1150  
1151          $form_container->output_row("{$lang->subject}: <em>*</em>", $lang->subject_desc, $form->generate_text_box('subject', $input['subject'], array('id' => 'subject')), 'subject');
1152  
1153          if($mybb->input['type'] == 0)
1154          {
1155              $type_email_checked = true;
1156              $type_pm_checked = false;
1157          }
1158          else if($mybb->input['type'] == 1)
1159          {
1160              $type_email_checked = false;
1161              $type_pm_checked = true;
1162          }
1163  
1164          $type_options = array(
1165              $form->generate_radio_button("type", 0, $lang->send_via_email, array("id" => "type_email", "checked" => $type_email_checked)),
1166              $form->generate_radio_button("type", 1, $lang->send_via_pm, array("id" => "type_pm", "checked" => $type_pm_checked))
1167          );
1168          $form_container->output_row("{$lang->message_type}:", "", implode("<br />", $type_options));
1169  
1170          $format_options = array(
1171              0 => $lang->plain_text_only,
1172              1 => $lang->html_only,
1173              2 => $lang->html_and_plain_text
1174          );
1175  
1176          $form_container->output_row("{$lang->message_format}:", "", $form->generate_select_box('format', $format_options, $input['format'], array('id' => 'format')), 'format', null, array("id" => "format_container"));
1177          
1178          $form_container->output_row("{$lang->per_page}: <em>*</em>", $lang->per_page_desc, $form->generate_text_box('perpage', $input['perpage'], array('id' => 'perpage')), 'perpage');
1179          
1180          $form_container->end();
1181  
1182          if($mybb->input['format'] == 2)
1183          {
1184              if($mybb->input['automatic_text'] && !$email['mid'])
1185              {
1186                  $automatic_text_check = true;
1187                  $text_display = 'display: none';
1188                  $automatic_display = 'display: none;';
1189              }
1190          }
1191          else if($mybb->input['format'] == 1 && $mybb->input['type'] != 1)
1192          {
1193              $text_display = 'display: none;';
1194          }
1195          else if($mybb->input['format'] == 0 || $mybb->input['type'] == 1)
1196          {
1197              $html_display = 'display: none';
1198          }
1199  
1200  
1201          echo "<div id=\"message_html\" style=\"{$html_display}\">";
1202          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->define_html_message}");
1203          $form_container->output_row("{$lang->define_html_message_desc}:", $html_personalisation, $form->generate_text_area('htmlmessage', $input['htmlmessage'], array('id' => 'htmlmessage', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%'))."<div id=\"automatic_display\" style=\"{$automatic_display}\">".$form->generate_check_box('automatic_text', 1, $lang->auto_gen_plain_text, array('checked' => $automatic_text_check, "id" => "automatic_text"))."</div>");
1204          $form_container->end();
1205          echo "</div>";
1206  
1207          echo "<div id=\"message_text\" style=\"{$text_display}\">";
1208          $form_container = new FormContainer("{$lang->send_mass_mail}: {$lang->step_one} - {$lang->define_text_version}");
1209          $form_container->output_row("{$lang->define_text_version_desc}:", $text_personalisation, $form->generate_text_area('message', $input['message'], array('id' => 'message', 'rows' => 15, 'cols '=> 70, 'style' => 'width: 95%')));
1210          $form_container->end();
1211          echo "</div>";
1212  
1213          echo "
1214          <script type=\"text/javascript\">
1215  		function ToggleFormat()
1216          {
1217              var v = $('format').options[$('format').selectedIndex].value;
1218              if(v == 2)
1219              {
1220                  $('automatic_display').show();
1221                  $('message_html').show();
1222                  if($('automatic_text').checked)
1223                  {
1224                      $('message_text').hide();
1225                  }
1226                  else
1227                  {
1228                      $('message_text').show();
1229                  }
1230              }
1231              else if(v == 1)
1232              {
1233                  $('message_text').hide();
1234                  $('message_html').show();
1235                  $('automatic_display').hide();
1236              }
1237              else
1238              {
1239                  $('message_text').show();
1240                  $('message_html').hide();
1241              }
1242          }
1243          Event.observe($('format'), 'change', ToggleFormat);
1244  
1245  		function ToggleType()
1246          {
1247              var v = $('type_pm').checked;
1248              if(v == true)
1249              {
1250                  $('message_html').hide();
1251                  $('message_text').show();
1252                  $('format_container').hide();
1253              }
1254              else
1255              {
1256                  $('message_html').show();
1257                  $('format_container').show();
1258                  ToggleFormat();
1259              }
1260          }
1261          Event.observe($('type_pm'), 'click', ToggleType);
1262          Event.observe($('type_email'), 'click', ToggleType);
1263          ToggleType();
1264  
1265  		function ToggleAutomatic()
1266          {
1267              var v = $('automatic_text').checked;
1268              if(v == true)
1269              {
1270                  $('message_text').hide();
1271              }
1272              else
1273              {
1274                  $('message_text').show();
1275              }
1276          }
1277  
1278          Event.observe($('automatic_text'), 'click', ToggleAutomatic);
1279          
1280  		function insertText(value, textarea)
1281          {
1282              // Internet Explorer
1283              if(document.selection)
1284              {
1285                  textarea.focus();
1286                  var selection = document.selection.createRange();
1287                  selection.text = value;
1288              }
1289              // Firefox
1290              else if(textarea.selectionStart || textarea.selectionStart == '0')
1291              {
1292                  var start = textarea.selectionStart;
1293                  var end = textarea.selectionEnd;
1294                  textarea.value = textarea.value.substring(0, start)    + value    + textarea.value.substring(end, textarea.value.length);
1295              }
1296              else
1297              {
1298                  textarea.value += value;
1299              }
1300          }
1301          
1302          </script>";
1303  
1304          $buttons[] = $form->generate_submit_button($lang->next_step);
1305          $form->output_submit_wrapper($buttons);
1306  
1307          $form->end();
1308          $page->output_footer();
1309      }
1310  }
1311  
1312  if($mybb->input['action'] == "delete")
1313  {
1314      $query = $db->simple_select("massemails", "*", "mid='".intval($mybb->input['mid'])."'");
1315      $mass_email = $db->fetch_array($query);
1316      
1317      if(!$mass_email['mid'])
1318      {
1319          flash_message($lang->error_delete_invalid_mid, 'error');
1320          admin_redirect("index.php?module=user-mass_mail");
1321      }
1322  
1323      // User clicked no
1324      if($mybb->input['no'])
1325      {
1326          admin_redirect("index.php?module=user-mass_mail");
1327      }
1328  
1329      if($mybb->request_method == "post")
1330      {
1331          $db->delete_query("massemails", "mid='{$mass_email['mid']}'");
1332  
1333          $plugins->run_hooks("admin_user_mass_email_delete_commit");
1334  
1335          // Log admin action
1336          log_admin_action($mass_email['mid'], $mass_email['subject']);
1337          
1338          if($mybb->input['archive'] == 1)
1339          {
1340              flash_message($lang->success_mass_mail_deleted, 'success');
1341              admin_redirect("index.php?module=user-mass_mail&action=archive");
1342          }
1343          else
1344          {
1345              flash_message($lang->success_mass_mail_deleted, 'success');
1346              admin_redirect("index.php?module=user-mass_mail");
1347          }
1348      }
1349      else
1350      {
1351          if($mybb->input['archive'] == 1)
1352          {
1353              $page->output_confirm_action("index.php?module=user-mass_mail&amp;action=delete&amp;mid={$mass_email['mid']}&amp;archive=1", $lang->mass_mail_deletion_confirmation);
1354          }
1355          else
1356          {
1357              $page->output_confirm_action("index.php?module=user-mass_mail&amp;action=delete&amp;mid={$mass_email['mid']}", $lang->mass_mail_deletion_confirmation);
1358          }
1359      }
1360  }
1361  
1362  if($mybb->input['action'] == "preview")
1363  {
1364      $query = $db->simple_select("massemails", "*", "mid='".intval($mybb->input['mid'])."'");
1365      $mass_email = $db->fetch_array($query);
1366      
1367      if(!$mass_email['mid'])
1368      {
1369          flash_message($lang->error_invalid_mid, 'error');
1370          admin_redirect("index.php?module=user-mass_mail");
1371      }
1372      
1373      ?>
1374      <html xmlns="http://www.w3.org/1999/xhtml">
1375      <head profile="http://gmpg.org/xfn/1">
1376          <title>Mass Email Preview</title>
1377          <link rel="stylesheet" href="styles/<?php echo $page->style; ?>/main.css" type="text/css" />
1378          <link rel="stylesheet" href="styles/<?php echo $page->style; ?>/popup.css" type="text/css" />
1379      </head>
1380      <body id="popup">
1381          <div id="popup_container">
1382          <div class="popup_title"><a href="#" onClick="window.close();" class="close_link"><?php echo $lang->close_window; ?></a> Mass Email Preview</div>
1383  
1384          <div id="content">    
1385      <?php
1386      
1387      if($mybb->input['format'] == 'text' || !$mass_email['htmlmessage'])
1388      {
1389          // Show preview of the text version
1390          echo nl2br($mass_email['message']);
1391      }
1392      else
1393      {
1394          // Preview the HTML version
1395          echo $mass_email['htmlmessage'];
1396      }
1397      
1398          ?>
1399          </div>
1400      </div>
1401      </body>
1402      </html>
1403      <?php
1404      exit;
1405  }
1406  
1407  if($mybb->input['action'] == "resend")
1408  {
1409      // Copy and resend an email
1410      $query = $db->simple_select("massemails", "*", "mid='".intval($mybb->input['mid'])."'");
1411      $mass_email = $db->fetch_array($query);
1412      
1413      if(!$mass_email['mid'])
1414      {
1415          flash_message($lang->error_invalid_mid, 'error');
1416          admin_redirect("index.php?module=user-mass_mail");        
1417      }
1418  
1419      // Need to perform the search to fetch the number of users we're emailing
1420      $member_query = build_mass_mail_query(unserialize($mass_email['conditions']));
1421      $query = $db->simple_select("users u", "COUNT(uid) AS num", $member_query);
1422      $total_recipients = $db->fetch_field($query, "num");
1423      
1424      // Create the new email based off the old one.
1425      $new_email = array(
1426          "uid" => $mass_email['uid'],
1427          "subject" => $db->escape_string($mass_email['subject']),
1428          "message" => $db->escape_string($mass_email['message']),
1429          "htmlmessage" => $db->escape_string($mass_email['htmlmessage']),
1430          "type" => $db->escape_string($mass_email['type']),
1431          "format" => $db->escape_string($mass_email['format']),
1432          "dateline" => TIME_NOW,
1433          "senddate" => '0',
1434          "status" => 0,
1435          "sentcount" => 0,
1436          "totalcount" => $total_recipients,
1437          "conditions" => $db->escape_string($mass_email['conditions']),
1438          "perpage" => $mass_email['perpage']
1439      );
1440      $mid = $db->insert_query("massemails", $new_email);
1441      
1442      // Redirect the user to the summary page so they can select when to deliver this message
1443      flash_message($lang->success_mass_mail_resent, 'success');
1444      admin_redirect("index.php?module=user-mass_mail&action=send&step=4&mid={$mid}");
1445      exit;
1446  }
1447  
1448  if($mybb->input['action'] == "cancel")
1449  {
1450      // Cancel the delivery of a mass-email.
1451      $query = $db->simple_select("massemails", "*", "mid='".intval($mybb->input['mid'])."'");
1452      $mass_email = $db->fetch_array($query);
1453      
1454      if(!$mass_email['mid'])
1455      {
1456          flash_message($lang->error_invalid_mid, 'error');
1457          admin_redirect("index.php?module=user-mass_mail");        
1458      }
1459      
1460      $updated_email = array(
1461          'status' => 4
1462      );
1463      $db->update_query("massemails", $updated_email, "mid='{$mass_email['mid']}'");
1464      
1465      flash_message($lang->success_mass_mail_canceled, 'success');
1466      admin_redirect("index.php?module=user-mass_mail");
1467      exit;        
1468  }
1469  
1470  if($mybb->input['action'] == "archive")
1471  {
1472      // View a list of archived email messages
1473      $page->output_header($lang->mass_mail_archive);
1474  
1475      $page->output_nav_tabs($sub_tabs, 'archive');
1476  
1477      $table = new Table;
1478      $table->construct_header($lang->subject);
1479      $table->construct_header($lang->status, array('width' => '130', 'class' => 'align_center'));
1480      $table->construct_header($lang->delivery_date, array('width' => '130', 'class' => 'align_center'));
1481      $table->construct_header($lang->recipients, array('width' => '130', 'class' => 'align_center'));
1482      $table->construct_header($lang->controls, array("class" => "align_center", "colspan" => 2, "width" => 200));
1483  
1484      $query = $db->simple_select("massemails", "*", "status NOT IN (0, 1, 2)", array('order_by' => 'senddate'));
1485      while($email = $db->fetch_array($query))
1486      {
1487          $email['subject'] = htmlspecialchars_uni($email['subject']);
1488          if($email['senddate'] < TIME_NOW)
1489          {
1490              $table->construct_cell("<strong>{$email['subject']}</strong>");
1491          }
1492          if($email['status'] == 3)
1493          {
1494              $status = $lang->delivered;
1495          }
1496          else if($email['status'] == 4)
1497          {
1498              $status = $lang->canceled;
1499          }
1500          $table->construct_cell($status, array("class" => "align_center"));
1501  
1502          $delivery_date = my_date($mybb->settings['dateformat'], $email['senddate']);
1503  
1504          $table->construct_cell($delivery_date, array("class" => "align_center"));
1505          $table->construct_cell(my_number_format($email['totalcount']), array("class" => "align_center"));
1506  
1507          $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=resend&amp;mid={$email['mid']}\">{$lang->resend}</a>", array("width" => 100, "class" => "align_center"));
1508          $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=delete&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}&amp;archive=1\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_deletion_confirmation}')\">{$lang->delete}</a>", array("width" => 100, "class" => "align_center"));
1509  
1510          $table->construct_row();
1511      }
1512  
1513      if($table->num_rows() == 0)
1514      {
1515          $table->construct_cell($lang->no_archived_messages, array('colspan' => 6));
1516          $table->construct_row();
1517          $no_results = true;
1518      }
1519  
1520      $table->output($lang->mass_mail_archive);
1521  
1522      $page->output_footer();
1523  }
1524  
1525  if(!$mybb->input['action'])
1526  {
1527      $page->output_header($lang->mass_mail_queue);
1528  
1529      $page->output_nav_tabs($sub_tabs, 'mail_queue');
1530  
1531      $table = new Table;
1532      $table->construct_header($lang->subject);
1533      $table->construct_header($lang->status, array('width' => '130', 'class' => 'align_center'));
1534      $table->construct_header($lang->delivery_date, array('width' => '130', 'class' => 'align_center'));
1535      $table->construct_header($lang->recipients, array('width' => '130', 'class' => 'align_center'));
1536      $table->construct_header($lang->controls, array("class" => "align_center", "colspan" => 2, "width" => 200));
1537  
1538      $query = $db->simple_select("massemails", "*", "status IN (0, 1, 2)", array('order_by' => 'senddate'));
1539      while($email = $db->fetch_array($query))
1540      {
1541          $email['subject'] = htmlspecialchars_uni($email['subject']);
1542          if(TIME_NOW >= $email['senddate'] && $email['status'] > 1)
1543          {
1544              $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=edit&amp;mid={$email['mid']}\"><strong>{$email['subject']}</strong></a>");
1545          }
1546          else
1547          {
1548              $table->construct_cell("<strong>{$email['subject']}</strong>");
1549          }
1550          if($email['status'] == 0)
1551          {
1552              $status = $lang->draft;
1553          }
1554          else if($email['status'] == 1)
1555          {
1556              $status = $lang->queued;
1557          }
1558          else if($email['status'] == 2)
1559          {
1560              $progress = ceil($email['sentcount']/$email['totalcount']*100);
1561              if($progress > 100)
1562              {
1563                  $progress = 100;
1564              }
1565              $status = "{$lang->delivering} ({$progress}%)";
1566          }
1567          $table->construct_cell($status, array("class" => "align_center"));
1568  
1569          if($email['status'] != 0)
1570          {
1571              $delivery_date = my_date($mybb->settings['dateformat'], $email['senddate']);
1572          }
1573          else
1574          {
1575              $delivery_date = $lang->na;
1576          }
1577  
1578          $table->construct_cell($delivery_date, array("class" => "align_center"));
1579          $table->construct_cell(my_number_format($email['totalcount']), array("class" => "align_center"));
1580          if(TIME_NOW >= $email['senddate'] && $email['status'] > 1)
1581          {
1582              $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=cancel&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_cancel_confirmation}')\">{$lang->cancel}</a>", array("width" => 100, "colspan" => 2, "class" => "align_center"));
1583          }
1584          else
1585          {
1586              $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=edit&amp;mid={$email['mid']}\">{$lang->edit}</a>", array("width" => 100, "class" => "align_center"));
1587              $table->construct_cell("<a href=\"index.php?module=user-mass_mail&amp;action=delete&amp;mid={$email['mid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->mass_mail_deletion_confirmation}')\">{$lang->delete}</a>", array("width" => 100, "class" => "align_center"));
1588          }
1589          $table->construct_row();
1590      }
1591      
1592      if($table->num_rows() == 0)
1593      {
1594          $table->construct_cell($lang->no_unsent_messages, array('colspan' => 6));
1595          $table->construct_row();
1596          $no_results = true;
1597      }
1598  
1599      $table->output($lang->mass_mail_queue);
1600  
1601      $page->output_footer();
1602  }
1603  ?>


Generated: Sun Dec 11 14:16:27 2011 Cross-referenced by PHPXref 0.7.1