/**
  * Delete a message
  *
  * @param      object  $database JDatabase
  * @param      string  $option   Name of the component
  * @param      object  $member   Current member
  * @return     void
  */
 public function delete($database, $option, $member)
 {
     $limit = Request::getInt('limit', Config::get('list_limit'));
     $start = Request::getInt('limitstart', 0);
     $mids = Request::getVar('mid', array());
     if (count($mids) > 0) {
         // Check for request forgeries
         Request::checkToken(['get', 'post']);
         foreach ($mids as $mid) {
             $recipient = new \Hubzero\Message\Recipient($database);
             $recipient->mid = $mid;
             $recipient->uid = $member->get('uidNumber');
             $recipient->loadRecord();
             if (!$recipient->delete()) {
                 $this->setError($recipient->getError());
             }
         }
         $this->addPluginMessage('You have successfully deleted <b><u>' . count($mids) . '</u></b> message(s).', 'passed');
     } else {
         $this->addPluginMessage("No messages selected.", "warning");
     }
     return App::redirect(Route::url($member->getLink() . '&active=messages&task=' . Request::getWord('activetab', 'inbox') . '&start=' . $start . '&limit=' . $limit));
 }
Exemple #2
0
 /**
  * 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;
 }
Exemple #3
0
 /**
  * Mark messages as unread
  *
  * @param   object  $database  Database
  * @param   string  $option    Name of the component
  * @param   object  $member    Current member
  * @return  void
  */
 public function markasunread($database, $option, $member)
 {
     $limit = Request::getInt('limit', Config::get('list_limit'));
     $start = Request::getInt('limitstart', 0);
     $ids = Request::getVar('mid', array());
     if (count($ids) > 0) {
         // Check for request forgeries
         Request::checkToken(['get', 'post']);
         foreach ($ids as $mid) {
             $recipient = Hubzero\Message\Recipient::oneByMessageAndUser($mid, $member->get('id'));
             if (!$recipient->markAsUnread()) {
                 $this->setError($recipient->getError());
                 continue;
             }
         }
         $this->addPluginMessage('You have successfully marked <b><u>' . count($ids) . '</u></b> message(s) as unread.', 'passed');
     } else {
         $this->addPluginMessage("No messages selected.", "warning");
     }
     return App::redirect(Route::url($member->link() . '&active=messages&task=' . Request::getWord('activetab', 'inbox') . '&start=' . $start . '&limit=' . $limit));
 }