/** * Show sent messages * * @param object $database Database * @param string $option Name of the component * @param object $member Current member * @return string */ public function sent($database, $option, $member) { // Filters for returning results $filters = array('limit' => Request::getState($option . '.plugin.messages.limit', 'limit', Config::get('list_limit'), 'int'), 'start' => Request::getState($option . '.plugin.messages.limitstart', 'limitstart', 0, 'int'), 'created_by' => $member->get('id')); $recipient = Hubzero\Message\Message::blank(); $total = $recipient->getSentMessagesCount($filters); $rows = $recipient->getSentMessages($filters); // Output view $view = $this->view('sent', 'default')->set('option', $option)->set('member', $member)->set('filters', $filters)->set('total', $total)->set('rows', $rows); return $view->loadTemplate(); }
/** * 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 onSendMessage($type, $subject, $message, $from = array(), $to = array(), $component = '', $element = null, $description = '', $group_id = 0, $bypassGroupsCheck = false) { // Do we have a message? if (!$message) { return false; } $database = App::get('db'); // Create the message object $xmessage = Hubzero\Message\Message::blank(); if ($type == 'member_message') { $time_limit = intval($this->params->get('time_limit', 30)); $daily_limit = intval($this->params->get('daily_limit', 100)); // First, let's see if they've surpassed their daily limit for sending messages $filters = array('created_by' => User::get('id'), 'daily_limit' => $daily_limit); $number_sent = $xmessage->getSentMessagesCount($filters); if ($number_sent >= $daily_limit) { return false; } // Next, we see if they've passed the time limit for sending consecutive messages $filters['limit'] = 1; $filters['start'] = 0; $sent = $xmessage->getSentMessages($filters); if ($sent->count() > 0) { $last_sent = $sent->first(); $last_time = 0; if ($last_sent->created) { $last_time = Date::of($last_sent->created)->toUnix(); } $time_difference = Date::toUnix() + $time_limit - $last_time; if ($time_difference < $time_limit) { return false; } } } // Store the message in the database $xmessage->set('message', is_array($message) && isset($message['plaintext']) ? $message['plaintext'] : $message); // Do we have a subject line? If not, create it from the message if (!$subject && $xmessage->get('message')) { $subject = substr($xmessage->get('message'), 0, 70); if (strlen($subject) >= 70) { $subject .= '...'; } } $xmessage->set('subject', $subject); $xmessage->set('created', Date::toSql()); $xmessage->set('created_by', User::get('id')); $xmessage->set('component', $component); $xmessage->set('type', $type); $xmessage->set('group_id', $group_id); if (!$xmessage->save()) { return $xmessage->getError(); } if (is_array($message)) { $xmessage->set('message', $message); } // Do we have any recipients? if (count($to) > 0) { $mconfig = Component::params('com_members'); // Get all the sender's groups if ($mconfig->get('user_messaging', 1) == 1 && !$bypassGroupsCheck) { $xgroups = User::groups('all'); $usersgroups = array(); if (!empty($xgroups)) { foreach ($xgroups as $group) { if ($group->regconfirmed) { $usersgroups[] = $group->cn; } } } } // Loop through each recipient foreach ($to as $uid) { // Create a recipient object that ties a user to a message $recipient = Hubzero\Message\Recipient::blank(); $recipient->set('uid', $uid); $recipient->set('mid', $xmessage->get('id')); $recipient->set('created', Date::toSql()); $recipient->set('expires', Date::of(time() + 168 * 24 * 60 * 60)->toSql()); $recipient->set('actionid', 0); //(is_object($action)) ? $action->id : 0; [zooley] Phasing out action items // Get the user's methods for being notified $notify = Hubzero\Message\Notify::blank(); $methods = $notify->getRecords($uid, $type); $user = User::getInstance($uid); if (!is_object($user) || !$user->get('username')) { continue; } if ($mconfig->get('user_messaging', 1) == 1 && ($type == 'member_message' || $type == 'group_message')) { $pgroups = \Hubzero\User\Helper::getGroups($user->get('id'), 'all', 1); $profilesgroups = array(); if (!empty($pgroups)) { foreach ($pgroups as $group) { if ($group->regconfirmed) { $profilesgroups[] = $group->cn; } } } // Find the common groups if (!$bypassGroupsCheck) { $common = array_intersect($usersgroups, $profilesgroups); if (count($common) <= 0) { continue; } } } // Do we have any methods? if ($methods->count()) { // Loop through each method foreach ($methods as $method) { $action = strtolower($method->method); if ($action == 'internal') { if (!$recipient->save()) { $this->setError($recipient->getError()); } } else { if (!Event::trigger('onMessage', array($from, $xmessage, $user, $action))) { $this->setError(Lang::txt('PLG_XMESSAGE_HANDLER_ERROR_UNABLE_TO_MESSAGE', $uid, $action)); } } } } else { // First check if they have ANY methods saved (meaning they've changed their default settings) // If They do have some methods, then they simply turned off everything for this $type $methods = $notify->getRecords($uid); if (!$methods || $methods->count() <= 0) { // Load the default method $p = Plugin::byType('members', 'messages'); $pp = new \Hubzero\Config\Registry(is_object($p) ? $p->params : ''); $d = $pp->get('default_method', 'email'); if (!$recipient->save()) { $this->setError($recipient->getError()); } // Use the Default in the case the user has no methods if (!Event::trigger('onMessage', array($from, $xmessage, $user, $d))) { $this->setError(Lang::txt('PLG_XMESSAGE_HANDLER_ERROR_UNABLE_TO_MESSAGE', $uid, $d)); } } } } } return true; }
/** * Show sent messages * * @return string */ protected function _sent() { // Set the page title Document::setTitle(Lang::txt(strtoupper($this->_name)) . ': ' . $this->group->get('description') . ': ' . Lang::txt('PLG_GROUPS_MESSAGES_SENT')); // Filters for returning results $filters = array('limit' => Request::getInt('limit', 10), 'start' => Request::getInt('limitstart', 0), 'group_id' => $this->group->get('gidNumber')); // Instantiate our message object $database = App::get('db'); $recipient = Hubzero\Message\Message::blank(); // Retrieve data $total = $recipient->getSentMessagesCount($filters); $rows = $recipient->getSentMessages($filters); // Instantiate a view $view = $this->view('default', 'sent'); // Pass some info to the view $view->option = $this->_option; $view->group = $this->group; $view->authorized = $this->authorized; $view->rows = $rows; $view->filters = $filters; $view->total = $total; foreach ($this->getErrors() as $error) { $view->setError($error); } // Return the output return $view->loadTemplate(); }