Joomla! PHP Cross Reference Web Portals

Source: /administrator/components/com_messages/models/message.php - 237 lines - 6336 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @package     Joomla.Administrator
   4   * @subpackage  com_messages
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 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   * Private Message model.
  14   *
  15   * @package     Joomla.Administrator
  16   * @subpackage  com_messages
  17   * @since       1.6
  18   */
  19  class MessagesModelMessage extends JModelAdmin
  20  {
  21      /**
  22       * message
  23       */
  24      protected $item;
  25  
  26      /**
  27       * Method to auto-populate the model state.
  28       *
  29       * Note. Calling getState in this method will result in recursion.
  30       *
  31       * @since    1.6
  32       */
  33  	protected function populateState()
  34      {
  35          parent::populateState();
  36  
  37          $input = JFactory::getApplication()->input;
  38  
  39          $user = JFactory::getUser();
  40          $this->setState('user.id', $user->get('id'));
  41  
  42          $messageId = (int) $input->getInt('message_id');
  43          $this->setState('message.id', $messageId);
  44  
  45          $replyId = (int) $input->getInt('reply_id');
  46          $this->setState('reply.id', $replyId);
  47      }
  48  
  49      /**
  50       * Returns a Table object, always creating it.
  51       *
  52       * @param    type    The table type to instantiate
  53       * @param    string    A prefix for the table class name. Optional.
  54       * @param    array    Configuration array for model. Optional.
  55       * @return    JTable    A database object
  56       * @since    1.6
  57      */
  58  	public function getTable($type = 'Message', $prefix = 'MessagesTable', $config = array())
  59      {
  60          return JTable::getInstance($type, $prefix, $config);
  61      }
  62  
  63      /**
  64       * Method to get a single record.
  65       *
  66       * @param    integer    The id of the primary key.
  67       * @return    mixed    Object on success, false on failure.
  68       * @since    1.6
  69       */
  70  	public function getItem($pk = null)
  71      {
  72          if (!isset($this->item))
  73          {
  74              if ($this->item = parent::getItem($pk)) {
  75                  // Prime required properties.
  76                  if (empty($this->item->message_id))
  77                  {
  78                      // Prepare data for a new record.
  79                      if ($replyId = $this->getState('reply.id'))
  80                      {
  81                          // If replying to a message, preload some data.
  82                          $db        = $this->getDbo();
  83                          $query    = $db->getQuery(true);
  84  
  85                          $query->select('subject, user_id_from');
  86                          $query->from('#__messages');
  87                          $query->where('message_id = '.(int) $replyId);
  88  
  89                          try
  90                          {
  91                              $message = $db->setQuery($query)->loadObject();
  92                          }
  93                          catch (RuntimeException $e)
  94                          {
  95                              $this->setError($e->getMessage());
  96                              return false;
  97                          }
  98  
  99                          $this->item->set('user_id_to', $message->user_id_from);
 100                          $re = JText::_('COM_MESSAGES_RE');
 101                          if (stripos($message->subject, $re) !== 0) {
 102                              $this->item->set('subject', $re.$message->subject);
 103                          }
 104                      }
 105                  }
 106                  elseif ($this->item->user_id_to != JFactory::getUser()->id)
 107                  {
 108                      $this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
 109                      return false;
 110                  }
 111                  else {
 112                      // Mark message read
 113                      $db        = $this->getDbo();
 114                      $query    = $db->getQuery(true);
 115                      $query->update('#__messages');
 116                      $query->set('state = 1');
 117                      $query->where('message_id = '.$this->item->message_id);
 118                      $db->setQuery($query)->execute();
 119                  }
 120              }
 121  
 122              // Get the user name for an existing messasge.
 123              if ($this->item->user_id_from && $fromUser = new JUser($this->item->user_id_from)) {
 124                  $this->item->set('from_user_name', $fromUser->name);
 125              }
 126          }
 127          return $this->item;
 128      }
 129  
 130      /**
 131       * Method to get the record form.
 132       *
 133       * @param    array    $data        Data for the form.
 134       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
 135       * @return    JForm    A JForm object on success, false on failure
 136       * @since    1.6
 137       */
 138  	public function getForm($data = array(), $loadData = true)
 139      {
 140          // Get the form.
 141          $form = $this->loadForm('com_messages.message', 'message', array('control' => 'jform', 'load_data' => $loadData));
 142          if (empty($form)) {
 143              return false;
 144          }
 145  
 146          return $form;
 147      }
 148  
 149      /**
 150       * Method to get the data that should be injected in the form.
 151       *
 152       * @return    mixed    The data for the form.
 153       * @since    1.6
 154       */
 155  	protected function loadFormData()
 156      {
 157          // Check the session for previously entered form data.
 158          $data = JFactory::getApplication()->getUserState('com_messages.edit.message.data', array());
 159  
 160          if (empty($data)) {
 161              $data = $this->getItem();
 162          }
 163  
 164          return $data;
 165      }
 166  
 167      /**
 168       * Method to save the form data.
 169       *
 170       * @param    array    The form data.
 171       *
 172       * @return    boolean    True on success.
 173       */
 174  	public function save($data)
 175      {
 176          $table = $this->getTable();
 177  
 178          // Bind the data.
 179          if (!$table->bind($data)) {
 180              $this->setError($table->getError());
 181              return false;
 182          }
 183  
 184          // Assign empty values.
 185          if (empty($table->user_id_from)) {
 186              $table->user_id_from = JFactory::getUser()->get('id');
 187          }
 188          if ((int) $table->date_time == 0) {
 189              $table->date_time = JFactory::getDate()->toSql();
 190          }
 191  
 192          // Check the data.
 193          if (!$table->check()) {
 194              $this->setError($table->getError());
 195              return false;
 196          }
 197  
 198          // Load the recipient user configuration.
 199          $model = JModelLegacy::getInstance('Config', 'MessagesModel', array('ignore_request' => true));
 200          $model->setState('user.id', $table->user_id_to);
 201          $config = $model->getItem();
 202          if (empty($config)) {
 203              $this->setError($model->getError());
 204              return false;
 205          }
 206  
 207          if ($config->get('locked', false)) {
 208              $this->setError(JText::_('COM_MESSAGES_ERR_SEND_FAILED'));
 209              return false;
 210          }
 211  
 212          // Store the data.
 213          if (!$table->store()) {
 214              $this->setError($table->getError());
 215              return false;
 216          }
 217  
 218          if ($config->get('mail_on_new', true)) {
 219              // Load the user details (already valid from table check).
 220              $fromUser = JUser::getInstance($table->user_id_from);
 221              $toUser = JUser::getInstance($table->user_id_to);
 222              $debug = JFactory::getConfig()->get('debug_lang');
 223              $default_language = JComponentHelper::getParams('com_languages')->get('administrator');
 224              $lang = JLanguage::getInstance($toUser->getParam('admin_language', $default_language), $debug);
 225              $lang->load('com_messages', JPATH_ADMINISTRATOR);
 226  
 227              $siteURL  = JURI::root() . 'administrator/index.php?option=com_messages&view=message&message_id='.$table->message_id;
 228              $sitename = JFactory::getApplication()->getCfg('sitename');
 229  
 230              $subject = sprintf($lang->_('COM_MESSAGES_NEW_MESSAGE_ARRIVED'), $sitename);
 231              $msg     = sprintf($lang->_('COM_MESSAGES_PLEASE_LOGIN'), $siteURL);
 232              JFactory::getMailer()->sendMail($fromUser->email, $fromUser->name, $toUser->email, $subject, $msg);
 233          }
 234  
 235          return true;
 236      }
 237  }

title

Description

title

Description

title

Description

title

title

Body