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