Source for file message.php

Documentation is available at message.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_messages
  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.  * Private Message model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_messages
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * message
  22.      */
  23.     protected $item;
  24.  
  25.     /**
  26.      * Method to auto-populate the model state.
  27.      *
  28.      * Note. Calling getState in this method will result in recursion.
  29.      *
  30.      * @since   1.6
  31.      */
  32.     protected function populateState()
  33.     {
  34.         parent::populateState();
  35.  
  36.         $input JFactory::getApplication()->input;
  37.  
  38.         $user JFactory::getUser();
  39.         $this->setState('user.id'$user->get('id'));
  40.  
  41.         $messageId = (int) $input->getInt('message_id');
  42.         $this->setState('message.id'$messageId);
  43.  
  44.         $replyId = (int) $input->getInt('reply_id');
  45.         $this->setState('reply.id'$replyId);
  46.     }
  47.  
  48.     /**
  49.      * Check that recipient user is the one trying to delete and then call parent delete method
  50.      *
  51.      * @param   array  &$pks  An array of record primary keys.
  52.      *
  53.      * @return  boolean  True if successful, false if an error occurs.
  54.      *
  55.      * @since  3.1
  56.      */
  57.     public function delete(&$pks)
  58.     {
  59.         $pks = (array) $pks;
  60.         $table $this->getTable();
  61.         $user JFactory::getUser();
  62.  
  63.         // Iterate the items to delete each one.
  64.         foreach ($pks as $i => $pk)
  65.         {
  66.             if ($table->load($pk))
  67.             {
  68.                 if ($table->user_id_to !== $user->id)
  69.                 {
  70.                     // Prune items that you can't change.
  71.                     unset($pks[$i]);
  72.                     JLog::add(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED')JLog::WARNING'jerror');
  73.                     return false;
  74.                 }
  75.             }
  76.             else
  77.             {
  78.                 $this->setError($table->getError());
  79.                 return false;
  80.             }
  81.         }
  82.         return parent::delete($pks);
  83.     }
  84.  
  85.     /**
  86.      * Returns a Table object, always creating it.
  87.      *
  88.      * @param   type    The table type to instantiate
  89.      * @param   string    A prefix for the table class name. Optional.
  90.      * @param   array  Configuration array for model. Optional.
  91.      * @return  JTable    A database object
  92.      * @since   1.6
  93.     */
  94.     public function getTable($type 'Message'$prefix 'MessagesTable'$config array())
  95.     {
  96.         return JTable::getInstance($type$prefix$config);
  97.     }
  98.  
  99.     /**
  100.      * Method to get a single record.
  101.      *
  102.      * @param   integer    The id of the primary key.
  103.      * @return  mixed  Object on success, false on failure.
  104.      * @since   1.6
  105.      */
  106.     public function getItem($pk null)
  107.     {
  108.         if (!isset($this->item))
  109.         {
  110.             if ($this->item = parent::getItem($pk))
  111.             {
  112.                 // Prime required properties.
  113.                 if (empty($this->item->message_id))
  114.                 {
  115.                     // Prepare data for a new record.
  116.                     if ($replyId $this->getState('reply.id'))
  117.                     {
  118.                         // If replying to a message, preload some data.
  119.                         $db        $this->getDbo();
  120.                         $query    $db->getQuery(true)
  121.                             ->select('subject, user_id_from')
  122.                             ->from('#__messages')
  123.                             ->where('message_id = '.(int) $replyId);
  124.  
  125.                         try
  126.                         {
  127.                             $message $db->setQuery($query)->loadObject();
  128.                         }
  129.                         catch (RuntimeException $e)
  130.                         {
  131.                             $this->setError($e->getMessage());
  132.                             return false;
  133.                         }
  134.  
  135.                         $this->item->set('user_id_to'$message->user_id_from);
  136.                         $re JText::_('COM_MESSAGES_RE');
  137.                         if (stripos($message->subject$re!== 0)
  138.                         {
  139.                             $this->item->set('subject'$re.$message->subject);
  140.                         }
  141.                     }
  142.                 }
  143.                 elseif ($this->item->user_id_to != JFactory::getUser()->id)
  144.                 {
  145.                     $this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
  146.                     return false;
  147.                 }
  148.                 else {
  149.                     // Mark message read
  150.                     $db        $this->getDbo();
  151.                     $query    $db->getQuery(true)
  152.                         ->update('#__messages')
  153.                         ->set('state = 1')
  154.                         ->where('message_id = '.$this->item->message_id);
  155.                     $db->setQuery($query)->execute();
  156.                 }
  157.             }
  158.  
  159.             // Get the user name for an existing messasge.
  160.             if ($this->item->user_id_from && $fromUser new JUser($this->item->user_id_from))
  161.             {
  162.                 $this->item->set('from_user_name'$fromUser->name);
  163.             }
  164.         }
  165.         return $this->item;
  166.     }
  167.  
  168.     /**
  169.      * Method to get the record form.
  170.      *
  171.      * @param   array  $data        Data for the form.
  172.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  173.      * @return  JForm    A JForm object on success, false on failure
  174.      * @since   1.6
  175.      */
  176.     public function getForm($data array()$loadData true)
  177.     {
  178.         // Get the form.
  179.         $form $this->loadForm('com_messages.message''message'array('control' => 'jform''load_data' => $loadData));
  180.         if (empty($form))
  181.         {
  182.             return false;
  183.         }
  184.  
  185.         return $form;
  186.     }
  187.  
  188.     /**
  189.      * Method to get the data that should be injected in the form.
  190.      *
  191.      * @return  mixed  The data for the form.
  192.      * @since   1.6
  193.      */
  194.     protected function loadFormData()
  195.     {
  196.         // Check the session for previously entered form data.
  197.         $data JFactory::getApplication()->getUserState('com_messages.edit.message.data'array());
  198.  
  199.         if (empty($data))
  200.         {
  201.             $data $this->getItem();
  202.         }
  203.  
  204.         $this->preprocessData('com_messages.message'$data);
  205.  
  206.         return $data;
  207.     }
  208.  
  209.     /**
  210.      * Checks that the current user matches the message recipient and calls the parent publish method
  211.      *
  212.      * @param   array    &$pks   A list of the primary keys to change.
  213.      * @param   integer  $value  The value of the published state.
  214.      *
  215.      * @return  boolean  True on success.
  216.      *
  217.      * @since   3.1
  218.      */
  219.     public function publish(&$pks$value 1)
  220.     {
  221.         $user JFactory::getUser();
  222.         $table $this->getTable();
  223.         $pks = (array) $pks;
  224.  
  225.         // Check that the recipient matches the current user
  226.         foreach ($pks as $i => $pk)
  227.         {
  228.             $table->reset();
  229.  
  230.             if ($table->load($pk))
  231.             {
  232.                 if ($table->user_id_to !== $user->id)
  233.                 {
  234.                     // Prune items that you can't change.
  235.                     unset($pks[$i]);
  236.                     JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED')JLog::WARNING'jerror');
  237.                     return false;
  238.                 }
  239.             }
  240.  
  241.         }
  242.  
  243.         return parent::publish($pks$value);
  244.     }
  245.  
  246.     /**
  247.      * Method to save the form data.
  248.      *
  249.      * @param   array  The form data.
  250.      *
  251.      * @return  boolean  True on success.
  252.      */
  253.     public function save($data)
  254.     {
  255.         $table $this->getTable();
  256.  
  257.         // Bind the data.
  258.         if (!$table->bind($data))
  259.         {
  260.             $this->setError($table->getError());
  261.             return false;
  262.         }
  263.  
  264.         // Assign empty values.
  265.         if (empty($table->user_id_from))
  266.         {
  267.             $table->user_id_from JFactory::getUser()->get('id');
  268.         }
  269.         if ((int) $table->date_time == 0)
  270.         {
  271.             $table->date_time JFactory::getDate()->toSql();
  272.         }
  273.  
  274.         // Check the data.
  275.         if (!$table->check())
  276.         {
  277.             $this->setError($table->getError());
  278.             return false;
  279.         }
  280.  
  281.         // Load the recipient user configuration.
  282.         $model JModelLegacy::getInstance('Config''MessagesModel'array('ignore_request' => true));
  283.         $model->setState('user.id'$table->user_id_to);
  284.         $config $model->getItem();
  285.         if (empty($config))
  286.         {
  287.             $this->setError($model->getError());
  288.             return false;
  289.         }
  290.  
  291.         if ($config->get('locked'false))
  292.         {
  293.             $this->setError(JText::_('COM_MESSAGES_ERR_SEND_FAILED'));
  294.             return false;
  295.         }
  296.  
  297.         // Store the data.
  298.         if (!$table->store())
  299.         {
  300.             $this->setError($table->getError());
  301.             return false;
  302.         }
  303.  
  304.         if ($config->get('mail_on_new'true))
  305.         {
  306.             // Load the user details (already valid from table check).
  307.             $fromUser JUser::getInstance($table->user_id_from);
  308.             $toUser JUser::getInstance($table->user_id_to);
  309.             $debug JFactory::getConfig()->get('debug_lang');
  310.             $default_language JComponentHelper::getParams('com_languages')->get('administrator');
  311.             $lang JLanguage::getInstance($toUser->getParam('admin_language'$default_language)$debug);
  312.             $lang->load('com_messages'JPATH_ADMINISTRATOR);
  313.  
  314.             $siteURL  JUri::root('administrator/index.php?option=com_messages&view=message&message_id='.$table->message_id;
  315.             $sitename JFactory::getApplication()->getCfg('sitename');
  316.  
  317.             $subject sprintf($lang->_('COM_MESSAGES_NEW_MESSAGE_ARRIVED')$sitename);
  318.             $msg     sprintf($lang->_('COM_MESSAGES_PLEASE_LOGIN')$siteURL);
  319.             JFactory::getMailer()->sendMail($fromUser->email$fromUser->name$toUser->email$subject$msg);
  320.         }
  321.  
  322.         return true;
  323.     }
  324. }

Documentation generated on Tue, 19 Nov 2013 15:08:04 +0100 by phpDocumentor 1.4.3