Source for file mail.php

Documentation is available at mail.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_users
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  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.         // Get the form.
  32.         $form $this->loadForm('com_users.mail''mail'array('control' => 'jform''load_data' => $loadData));
  33.         if (empty($form))
  34.         {
  35.             return false;
  36.         }
  37.  
  38.         return $form;
  39.     }
  40.  
  41.     /**
  42.      * Method to get the data that should be injected in the form.
  43.      *
  44.      * @return  mixed  The data for the form.
  45.      * @since   1.6
  46.      */
  47.     protected function loadFormData()
  48.     {
  49.         // Check the session for previously entered form data.
  50.         $data JFactory::getApplication()->getUserState('com_users.display.mail.data'array());
  51.  
  52.         $this->preprocessData('com_users.mail'$data);
  53.  
  54.         return $data;
  55.     }
  56.  
  57.     /**
  58.      * Override preprocessForm to load the user plugin group instead of content.
  59.      *
  60.      * @param   object    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.         $app    JFactory::getApplication();
  73.         $data   $app->input->post->get('jform'array()'array');
  74.         $user   JFactory::getUser();
  75.         $access new JAccess;
  76.         $db     $this->getDbo();
  77.  
  78.         $mode        array_key_exists('mode'$data? (int) $data['mode'0;
  79.         $subject    array_key_exists('subject'$data$data['subject''';
  80.         $grp        array_key_exists('group'$data? (int) $data['group'0;
  81.         $recurse    array_key_exists('recurse'$data? (int) $data['recurse'0;
  82.         $bcc        array_key_exists('bcc'$data? (int) $data['bcc'0;
  83.         $disabled    array_key_exists('disabled'$data? (int) $data['disabled'0;
  84.         $message_body array_key_exists('message'$data$data['message''';
  85.  
  86.         // automatically removes html formatting
  87.         if (!$mode)
  88.         {
  89.             $message_body JFilterInput::getInstance()->clean($message_body'string');
  90.         }
  91.  
  92.         // Check for a message body and subject
  93.         if (!$message_body || !$subject)
  94.         {
  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 $access->getUsersByGroup($grp$recurse);
  102.  
  103.         // Get all users email and group except for senders
  104.         $query    $db->getQuery(true)
  105.             ->select('email')
  106.             ->from('#__users')
  107.             ->where('id != '.(int) $user->get('id'));
  108.         if ($grp !== 0)
  109.         {
  110.             if (empty($to))
  111.             {
  112.                 $query->where('0');
  113.             else {
  114.                 $query->where('id IN (' implode(','$to')');
  115.             }
  116.         }
  117.  
  118.         if ($disabled == 0){
  119.             $query->where("block = 0");
  120.         }
  121.  
  122.         $db->setQuery($query);
  123.         $rows $db->loadColumn();
  124.  
  125.         // Check to see if there are any users in this group before we continue
  126.         if (!count($rows))
  127.         {
  128.             $app->setUserState('com_users.display.mail.data'$data);
  129.             if (in_array($user->id$to))
  130.             {
  131.                 $this->setError(JText::_('COM_USERS_MAIL_ONLY_YOU_COULD_BE_FOUND_IN_THIS_GROUP'));
  132.             }
  133.             else
  134.             {
  135.                 $this->setError(JText::_('COM_USERS_MAIL_NO_USERS_COULD_BE_FOUND_IN_THIS_GROUP'));
  136.             }
  137.             return false;
  138.         }
  139.  
  140.         // Get the Mailer
  141.         $mailer JFactory::getMailer();
  142.         $params JComponentHelper::getParams('com_users');
  143.  
  144.         // Build email message format.
  145.         $mailer->setSender(array($app->getCfg('mailfrom')$app->getCfg('fromname')));
  146.         $mailer->setSubject($params->get('mailSubjectPrefix'stripslashes($subject));
  147.         $mailer->setBody($message_body $params->get('mailBodySuffix'));
  148.         $mailer->IsHTML($mode);
  149.  
  150.         // Add recipients
  151.         if ($bcc)
  152.         {
  153.             $mailer->addBCC($rows);
  154.             $mailer->addRecipient($app->getCfg('mailfrom'));
  155.         }
  156.         else
  157.         {
  158.             $mailer->addRecipient($rows);
  159.         }
  160.  
  161.         // Send the Mail
  162.         $rs    $mailer->Send();
  163.  
  164.         // Check for an error
  165.         if ($rs instanceof Exception)
  166.         {
  167.             $app->setUserState('com_users.display.mail.data'$data);
  168.             $this->setError($rs->getError());
  169.             return false;
  170.         elseif (empty($rs))
  171.         {
  172.             $app->setUserState('com_users.display.mail.data'$data);
  173.             $this->setError(JText::_('COM_USERS_MAIL_THE_MAIL_COULD_NOT_BE_SENT'));
  174.             return false;
  175.         }
  176.         else
  177.         {
  178.             // Fill the data (specially for the 'mode', 'group' and 'bcc': they could not exist in the array
  179.             // when the box is not checked and in this case, the default value would be used instead of the '0'
  180.             // one)
  181.             $data['mode'$mode;
  182.             $data['subject'$subject;
  183.             $data['group'$grp;
  184.             $data['recurse'$recurse;
  185.             $data['bcc'$bcc;
  186.             $data['message'$message_body;
  187.             $app->setUserState('com_users.display.mail.data'array());
  188.             $app->enqueueMessage(JText::plural('COM_USERS_MAIL_EMAIL_SENT_TO_N_USERS'count($rows))'message');
  189.             return true;
  190.         }
  191.     }
  192. }

Documentation generated on Tue, 19 Nov 2013 15:07:29 +0100 by phpDocumentor 1.4.3