[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_users/models/ -> mail.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // No direct access
   8  defined('_JEXEC') or die;
   9  
  10  jimport('joomla.application.component.modeladmin');
  11  
  12  /**
  13   * Users mail model.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_users
  17   * @since    1.6
  18   */
  19  class UsersModelMail extends JModelAdmin
  20  {
  21      /**
  22       * Method to get the row form.
  23       *
  24       * @param    array    $data        An optional array of data for the form to interogate.
  25       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
  26       * @return    JForm    A JForm object on success, false on failure
  27       * @since    1.6
  28       */
  29  	public function getForm($data = array(), $loadData = true)
  30      {
  31          // Initialise variables.
  32          $app = JFactory::getApplication();
  33  
  34          // Get the form.
  35          $form = $this->loadForm('com_users.mail', 'mail', array('control' => 'jform', 'load_data' => $loadData));
  36          if (empty($form)) {
  37              return false;
  38          }
  39  
  40          return $form;
  41      }
  42  
  43      /**
  44       * Method to get the data that should be injected in the form.
  45       *
  46       * @return    mixed    The data for the form.
  47       * @since    1.6
  48       */
  49  	protected function loadFormData()
  50      {
  51          // Check the session for previously entered form data.
  52          $data = JFactory::getApplication()->getUserState('com_users.display.mail.data', array());
  53  
  54          return $data;
  55      }
  56  
  57      /**
  58       * Override preprocessForm to load the user plugin group instead of content.
  59       *
  60       * @param    object    A form object.
  61       * @param    mixed    The data expected for the form.
  62       * @throws    Exception if there is an error in the form event.
  63       * @since    1.6
  64       */
  65  	protected function preprocessForm(JForm $form, $data, $group = 'user')
  66      {
  67          parent::preprocessForm($form, $data, $group);
  68      }
  69  
  70  	public function send()
  71      {
  72          // Initialise variables.
  73          $data    = JRequest::getVar('jform', array(), 'post', 'array');
  74          $app    = JFactory::getApplication();
  75          $user    = JFactory::getUser();
  76          $acl    = JFactory::getACL();
  77          $db        = $this->getDbo();
  78  
  79  
  80          $mode        = array_key_exists('mode', $data) ? intval($data['mode']) : 0;
  81          $subject    = array_key_exists('subject', $data) ? $data['subject'] : '';
  82          $grp        = array_key_exists('group', $data) ? intval($data['group']) : 0;
  83          $recurse    = array_key_exists('recurse', $data) ? intval($data['recurse']) : 0;
  84          $bcc        = array_key_exists('bcc', $data) ? intval($data['bcc']) : 0;
  85          $disabled    = array_key_exists('disabled', $data) ? intval($data['disabled']) : 0;
  86          $message_body = array_key_exists('message', $data) ? $data['message'] : '';
  87  
  88          // automatically removes html formatting
  89          if (!$mode) {
  90              $message_body = JFilterInput::getInstance()->clean($message_body, 'string');
  91          }
  92  
  93          // Check for a message body and subject
  94          if (!$message_body || !$subject) {
  95              $app->setUserState('com_users.display.mail.data', $data);
  96              $this->setError(JText::_('COM_USERS_MAIL_PLEASE_FILL_IN_THE_FORM_CORRECTLY'));
  97              return false;
  98          }
  99  
 100          // get users in the group out of the acl
 101          $to = $acl->getUsersByGroup($grp, $recurse);
 102  
 103          // Get all users email and group except for senders
 104          $query    = $db->getQuery(true);
 105          $query->select('email');
 106          $query->from('#__users');
 107          $query->where('id != '.(int) $user->get('id'));
 108          if ($grp !== 0) {
 109              if (empty($to)) {
 110                  $query->where('0');
 111              } else {
 112                  $query->where('id IN (' . implode(',', $to) . ')');
 113              }
 114          }
 115  
 116          if ($disabled == 0){
 117              $query->where("block = 0");
 118          }
 119  
 120          $db->setQuery($query);
 121          $rows = $db->loadColumn();
 122  
 123          // Check to see if there are any users in this group before we continue
 124          if (!count($rows)) {
 125              $app->setUserState('com_users.display.mail.data', $data);
 126              if (in_array($user->id, $to))
 127              {
 128                  $this->setError(JText::_('COM_USERS_MAIL_ONLY_YOU_COULD_BE_FOUND_IN_THIS_GROUP'));
 129              }
 130              else
 131              {
 132                  $this->setError(JText::_('COM_USERS_MAIL_NO_USERS_COULD_BE_FOUND_IN_THIS_GROUP'));
 133              }
 134              return false;
 135          }
 136  
 137          // Get the Mailer
 138          $mailer = JFactory::getMailer();
 139          $params = JComponentHelper::getParams('com_users');
 140  
 141          // Build email message format.
 142          $mailer->setSender(array($app->getCfg('mailfrom'), $app->getCfg('fromname')));
 143          $mailer->setSubject($params->get('mailSubjectPrefix') . stripslashes($subject));
 144          $mailer->setBody($message_body . $params->get('mailBodySuffix'));
 145          $mailer->IsHTML($mode);
 146  
 147          // Add recipients
 148          if ($bcc) {
 149              $mailer->addBCC($rows);
 150              $mailer->addRecipient($app->getCfg('mailfrom'));
 151          } else {
 152              $mailer->addRecipient($rows);
 153          }
 154  
 155          // Send the Mail
 156          $rs    = $mailer->Send();
 157  
 158          // Check for an error
 159          if ($rs instanceof Exception) {
 160              $app->setUserState('com_users.display.mail.data', $data);
 161              $this->setError($rs->getError());
 162              return false;
 163          } elseif (empty($rs)) {
 164              $app->setUserState('com_users.display.mail.data', $data);
 165              $this->setError(JText::_('COM_USERS_MAIL_THE_MAIL_COULD_NOT_BE_SENT'));
 166              return false;
 167          } else {
 168              // Fill the data (specially for the 'mode', 'group' and 'bcc': they could not exist in the array
 169              // when the box is not checked and in this case, the default value would be used instead of the '0'
 170              // one)
 171              $data['mode']=$mode;
 172              $data['subject']=$subject;
 173              $data['group']=$grp;
 174              $data['recurse']=$recurse;
 175              $data['bcc']=$bcc;
 176              $data['message']=$message_body;
 177              $app->setUserState('com_users.display.mail.data', array());
 178              $app->enqueueMessage(JText::plural('COM_USERS_MAIL_EMAIL_SENT_TO_N_USERS', count($rows)), 'message');
 179              return true;
 180          }
 181      }
 182  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1