示例#1
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;
 }
示例#2
0
 /**
  * Save settings
  *
  * @param      object  $database JDatabase
  * @param      string  $option   Name of the component
  * @param      object  $member   Current member
  * @return     void
  */
 public function savesettings($database, $option, $member)
 {
     // Check for request forgeries
     Request::checkToken();
     // Incoming
     //$override = Request::getInt('override',0);
     $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 = new \Hubzero\Message\Notify($database);
                     $notify->uid = $member->get('uidNumber');
                     $notify->method = $v;
                     $notify->type = $key;
                     $notify->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->id = $ids[$key][$v];
                         $ids[$key][$v] = -1;
                         //echo 'updated: '.$key.':'.$v.'<br />';
                         //} else {
                         //echo 'created: '.$key.':'.$v.'<br />';
                     }
                     // Save
                     if (!$notify->store()) {
                         $this->setError(Lang::txt('PLG_MEMBERS_MESSAGES_ERROR_NOTIFY_FAILED', $notify->method));
                     }
                 }
             }
         }
         $notify = new \Hubzero\Message\Notify($database);
         foreach ($ids as $key => $value) {
             foreach ($value as $k => $v) {
                 if ($v > 0) {
                     $notify->delete($v);
                     //echo 'deleted: '.$v.'<br />';
                 }
             }
         }
         // If they previously had everything turned off, we need to remove that entry saying so
         $records = $notify->getRecords($member->get('uidNumber'), 'all');
         if ($records) {
             foreach ($records as $record) {
                 $notify->delete($record->id);
             }
         }
     } 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 = new \Hubzero\Message\Notify($database);
         $notify->uid = $member->get('uidNumber');
         $records = $notify->getRecords($member->get('uidNumber'), 'all');
         if (!$records) {
             $notify->clearAll();
             $notify->method = 'none';
             $notify->type = 'all';
             $notify->priority = 1;
             if (!$notify->store()) {
                 $this->setError(Lang::txt('PLG_MEMBERS_MESSAGES_ERROR_NOTIFY_FAILED', $notify->method));
             }
         }
     }
     // Push through to the settings view
     $this->addPluginMessage(Lang::txt('You have successfully saved your message settings.'), 'passed');
     return App::redirect(Route::url($member->getLink() . '&active=messages&action=settings'));
 }