/** * Send a message to one or more users * * @param string $type Message type (maps to #__xmessage_component table) * @param string $subject Message subject * @param string $message Message to send * @param array $from Message 'from' data (e.g., name, address) * @param array $to List of user IDs * @param string $component Component name * @param integer $element ID of object that needs an action item * @param string $description Action item description * @param integer $group_id Parameter description (if any) ... * @return mixed True if no errors else error message */ public function sendMessage($type, $subject, $message, $from = array(), $to = array(), $component = '', $element = null, $description = '', $group_id = 0) { // Do we have a message? if (!$message) { return false; } // Do we have a subject line? If not, create it from the message if (!$subject && $message) { $subject = substr($message, 0, 70); if (strlen($subject) >= 70) { $subject .= '...'; } } $database = \App::get('db'); // Create the message object and store it in the database $xmessage = new Message($database); $xmessage->subject = $subject; $xmessage->message = $message; $xmessage->created = Date::toSql(); $xmessage->created_by = User::get('id'); $xmessage->component = $component; $xmessage->type = $type; $xmessage->group_id = $group_id; if (!$xmessage->store()) { return $xmessage->getError(); } // Does this message require an action? // **DEPRECATED** $action = new Action($database); /*if ($element || $description) { $action->class = $component; $action->element = $element; $action->description = $description; if (!$action->store()) { return $action->getError(); } }*/ // Do we have any recipients? if (count($to) > 0) { // Loop through each recipient foreach ($to as $uid) { // Create a recipient object that ties a user to a message $recipient = new Recipient($database); $recipient->uid = $uid; $recipient->mid = $xmessage->id; $recipient->created = Date::toSql(); $recipient->expires = Date::of(time() + 168 * 24 * 60 * 60)->toSql(); $recipient->actionid = $action->id; if (!$recipient->store()) { return $recipient->getError(); } // Get the user's methods for being notified $notify = new Notify($database); $methods = $notify->getRecords($uid, $type); $user = User::getInstance($uid); // Do we have any methods? if ($methods) { // Loop through each method foreach ($methods as $method) { $action = strtolower($method->method); if (!Event::trigger('xmessage.onMessage', array($from, $xmessage, $user, $action))) { $this->setError(Lang::txt('Unable to message user %s with method %s', $uid, $action)); } } } } } return true; }
/** * Delete a record * * @return void */ public function removeTask() { // Check for request forgeries Request::checkToken(); // Incoming $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; // Do we have any IDs? if (!empty($ids)) { $notify = new Message\Notify($this->database); // Loop through each ID and delete the necessary items foreach ($ids as $id) { $id = intval($id); $row = new Message\Component($this->database); $row->load($id); // Remove any associations $notify->deleteType($row->action); // Remove the record $row->delete($id); } } // Output messsage and redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('Message Action removed')); }
/** * Save settings * * @return void */ public function savesettingsTask() { // Check for request forgeries Request::checkToken(); $id = Request::getInt('id', 0); $member = User::getInstance($id); if (!$member || !$member->get('id')) { // Output messsage and redirect App::abort(404, Lang::txt('Unknown or invalid member ID')); } $database = App::get('db'); // Incoming $settings = Request::getVar('settings', array()); $ids = Request::getVar('ids', array()); // Ensure we have data to work with if ($settings && count($settings) > 0) { // Loop through each setting foreach ($settings as $key => $value) { foreach ($value as $v) { if ($v) { // Instantiate a Notify object and set its values $notify = Message\Notify::blank(); $notify->set('uid', $member->get('id')); $notify->set('method', $v); $notify->set('type', $key); $notify->set('priority', 1); // Do we have an ID for this setting? // Determines if the store() method is going to INSERT or UPDATE if ($ids[$key][$v] > 0) { $notify->set('id', $ids[$key][$v]); $ids[$key][$v] = -1; } // Save if (!$notify->save()) { Notify::error(Lang::txt('PLG_MEMBERS_MESSAGES_ERROR_NOTIFY_FAILED', $notify->get('method'))); } } } } foreach ($ids as $key => $value) { foreach ($value as $k => $v) { if ($v > 0) { $notify = Message\Notify::oneOrFail($v); $notify->destroy(); } } } // If they previously had everything turned off, we need to remove that entry saying so $notify = Message\Notify::blank(); $records = $notify->getRecords($member->get('uidNumber'), 'all'); if ($records) { foreach ($records as $record) { $record->destroy(); } } } else { // This creates a single entry to let the system know that the user has explicitly chosen "none" for all options // It ensures we can know the difference between someone who has never changed their settings (thus, no database entries) // and someone who purposely wants everything turned off. $notify = Message\Notify::blank(); $notify->set('uid', $member->get('uidNumber')); $records = $notify->getRecords($member->get('uidNumber'), 'all'); if (!$records->count()) { $notify->deleteByUser($member->get('uidNumber')); $notify->set('method', 'none'); $notify->set('type', 'all'); $notify->set('priority', 1); if (!$notify->save()) { $this->setError(Lang::txt('PLG_MEMBERS_MESSAGES_ERROR_NOTIFY_FAILED', $notify->get('method'))); } } } $tmpl = Request::getWord('tmpl'); App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=settings&id=' . $id . ($tmpl ? '&tmpl=' . $tmpl : ''), false)); }