示例#1
0
 /**
  * Add membership request for user
  *
  * @return  array
  */
 public function dorequestTask()
 {
     // Check if they're logged in
     if (User::isGuest()) {
         $this->loginTask(Lang::txt('COM_GROUPS_INVITE_MUST_BE_LOGGED_IN_TO_REQUEST'));
         return;
     }
     Request::checkToken();
     //check to make sure we have  cname
     if (!$this->cn) {
         $this->_errorHandler(400, Lang::txt('COM_GROUPS_ERROR_NO_ID'));
     }
     // Load the group page
     $this->view->group = Group::getInstance($this->cn);
     // Ensure we found the group info
     if (!$this->view->group || !$this->view->group->get('gidNumber')) {
         $this->_errorHandler(404, Lang::txt('COM_GROUPS_ERROR_NOT_FOUND'));
     }
     // Get the group params
     $gparams = new Registry($this->view->group->get('params'));
     // If membership is managed in seperate place disallow action
     if ($gparams->get('membership_control', 1) == 0) {
         $this->setNotification(Lang::txt('COM_GROUPS_MEMBERSHIP_MANAGED_ELSEWHERE'), 'error');
         App::redirect(Route::url('index.php?option=com_groups&cn=' . $this->view->group->get('cn')));
         return;
     }
     //make sure group has restricted policy
     if ($this->view->group->get('join_policy') != 1) {
         return;
     }
     //add user to applicants
     $this->view->group->add('applicants', array(User::get('id')));
     $this->view->group->update();
     // Instantiate the reason object and bind the incoming data
     $row = new Reason($this->database);
     $row->uidNumber = User::get('id');
     $row->gidNumber = $this->view->group->get('gidNumber');
     $row->reason = Request::getVar('reason', Lang::txt('GROUPS_NO_REASON_GIVEN'), 'post');
     $row->reason = \Hubzero\Utility\Sanitize::stripAll($row->reason);
     $row->date = Date::toSql();
     // Check and store the reason
     if (!$row->check()) {
         return App::abort(500, $row->getError());
     }
     if (!$row->store()) {
         return App::abort(500, $row->getError());
     }
     // Log the membership request
     Log::log(array('gidNumber' => $this->view->group->get('gidNumber'), 'action' => 'membership_requested', 'comments' => array(User::get('id'))));
     // Log activity
     $url = Route::url('index.php?option=' . $this->_option . '&cn=' . $this->view->group->get('cn'));
     $recipients = array(['group', $this->view->group->get('gidNumber')], ['user', User::get('id')]);
     foreach ($this->view->group->get('managers') as $recipient) {
         $recipients[] = ['user', $recipient];
     }
     Event::trigger('system.logActivity', ['activity' => ['action' => 'requested', 'scope' => 'group', 'scope_id' => $this->view->group->get('gidNumber'), 'description' => Lang::txt('COM_GROUPS_ACTIVITY_GROUP_USER_REQUESTED', '<a href="' . $url . '">' . $this->view->group->get('description') . '</a>'), 'details' => array('title' => $this->view->group->get('description'), 'url' => $url, 'cn' => $this->view->group->get('cn'), 'gidNumber' => $this->view->group->get('gidNumber'))], 'recipients' => $recipients]);
     // E-mail subject
     $subject = Lang::txt('COM_GROUPS_JOIN_REQUEST_EMAIL_SUBJECT', $this->view->group->get('cn'));
     // Build the e-mail message
     $eview = new \Hubzero\Component\View(array('name' => 'emails', 'layout' => 'request'));
     $eview->option = $this->_option;
     $eview->sitename = Config::get('sitename');
     $eview->user = User::getInstance();
     $eview->group = $this->view->group;
     $eview->row = $row;
     $html = $eview->loadTemplate();
     $html = str_replace("\n", "\r\n", $html);
     // Get the system administrator e-mail
     $emailadmin = Config::get('mailfrom');
     // Build the "from" portion of the e-mail
     $from = array();
     $from['name'] = Config::get('sitename') . ' ' . Lang::txt(strtoupper($this->_name));
     $from['email'] = Config::get('mailfrom');
     // build array of managers
     $managers = array();
     foreach ($this->view->group->get('managers') as $m) {
         $profile = User::getInstance($m);
         if ($profile) {
             $managers[$profile->get('email')] = $profile->get('name');
         }
     }
     // create new message
     $message = new \Hubzero\Mail\Message();
     // build message object and send
     $message->setSubject($subject)->addFrom($from['email'], $from['name'])->setTo($managers)->addHeader('X-Mailer', 'PHP/' . phpversion())->addHeader('X-Component', 'com_groups')->addHeader('X-Component-Object', 'group_membership_requested')->addPart($html, 'text/plain')->send();
     //tell the user they just did good
     $this->setNotification(Lang::txt('COM_GROUPS_INVITE_REQUEST_FORWARDED'), 'passed');
     // Push through to the groups listing
     App::redirect($url);
 }