Example #1
0
 /**
  * Saves posted data for a new/edited forum thread post
  *
  * @return     void
  */
 public function savethread()
 {
     if (User::isGuest()) {
         App::redirect(Route::url('index.php?option=com_users&view=login&return=' . base64_encode(Route::url($this->base))));
         return;
     }
     // Incoming
     $section = Request::getVar('section', '');
     $fields = Request::getVar('fields', array(), 'post', 'none', 2);
     $fields = array_map('trim', $fields);
     $this->_authorize('thread', intval($fields['id']));
     $asset = 'thread';
     if ($fields['parent']) {
         //$asset = 'post';
     }
     if ($fields['id']) {
         $old = new \Components\Forum\Tables\Post($this->database);
         $old->load(intval($fields['id']));
         if ($old->created_by == User::get('id')) {
             $this->params->set('access-edit-thread', true);
         }
     }
     if ($fields['id'] && !$this->params->get('access-edit-thread') || !$fields['id'] && !$this->params->get('access-create-thread')) {
         App::redirect(Route::url('index.php?option=' . $this->option . '&cn=' . $this->group->get('cn') . '&active=forum'), Lang::txt('PLG_GROUPS_FORUM_NOT_AUTHORIZED'), 'warning');
         return;
     }
     $fields['sticky'] = isset($fields['sticky']) ? $fields['sticky'] : 0;
     $fields['closed'] = isset($fields['closed']) ? $fields['closed'] : 0;
     // Bind data
     $model = new \Components\Forum\Tables\Post($this->database);
     if (!$model->bind($fields)) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     if (!$model->anonymous) {
         $model->anonymous = 0;
     }
     // Check content
     if (!$model->check()) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     // Store new content
     if (!$model->store()) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     $parent = $model->parent ? $model->parent : $model->id;
     //update
     $this->upload($parent, $model->id);
     if ($fields['id']) {
         if ($old->category_id != $fields['category_id']) {
             $model->updateReplies(array('category_id' => $fields['category_id']), $model->id);
         }
     }
     $category = new \Components\Forum\Tables\Category($this->database);
     $category->load(intval($model->category_id));
     $sectionTbl = new \Components\Forum\Tables\Section($this->database);
     $sectionTbl->load(intval($category->section_id));
     $tags = Request::getVar('tags', '', 'post');
     $tagger = new \Components\Forum\Models\Tags($model->id);
     $tagger->setTags($tags, User::get('id'));
     // Determine post save message
     // Also, get subject of post for outgoing email, either the title of parent post (for replies), or title of current post (for new threads)
     if (!$fields['parent']) {
         $message = Lang::txt('PLG_GROUPS_FORUM_THREAD_STARTED');
         $posttitle = $model->title;
     } else {
         $message = Lang::txt('PLG_GROUPS_FORUM_POST_ADDED');
         $parentForumTablePost = new \Components\Forum\Tables\Post($this->database);
         $parentForumTablePost->load(intval($fields['parent']));
         $posttitle = $parentForumTablePost->title;
     }
     if ($fields['id']) {
         $message = $model->modified_by ? Lang::txt('PLG_GROUPS_FORUM_POST_EDITED') : Lang::txt('PLG_GROUPS_FORUM_POST_ADDED');
     }
     // Determine route
     if ($model->parent) {
         $thread = $model->thread;
     } else {
         $thread = $model->id;
     }
     $params = Component::params('com_groups');
     // Email the group and insert email tokens to allow them to respond to group posts via email
     if ($params->get('email_comment_processing')) {
         $esection = new \Components\Forum\Models\Section($sectionTbl);
         $ecategory = new \Components\Forum\Models\Category($category);
         $ecategory->set('section_alias', $esection->get('alias'));
         $ethread = new \Components\Forum\Models\Thread(intval($thread));
         $ethread->set('section', $esection->get('alias'));
         $ethread->set('category', $ecategory->get('alias'));
         $epost = new \Components\Forum\Models\Thread($model);
         $epost->set('section', $esection->get('alias'));
         $epost->set('category', $ecategory->get('alias'));
         // Figure out who should be notified about this comment (all group members for now)
         $userIDsToEmail = array();
         foreach ($this->members as $mbr) {
             //Look up user info
             $user = User::getInstance($mbr);
             if ($user->get('id')) {
                 include_once PATH_CORE . DS . 'plugins' . DS . 'groups' . DS . 'memberoptions' . DS . 'memberoption.class.php';
                 // Find the user's group settings, do they want to get email (0 or 1)?
                 $groupMemberOption = new GroupsTableMemberoption($this->database);
                 $groupMemberOption->loadRecord($this->group->get('gidNumber'), $user->get('id'), GROUPS_MEMBEROPTION_TYPE_DISCUSSION_NOTIFICIATION);
                 $sendEmail = 0;
                 if ($groupMemberOption->id) {
                     $sendEmail = $groupMemberOption->optionvalue;
                 }
                 if ($sendEmail == 1) {
                     $userIDsToEmail[] = $user->get('id');
                 }
             }
         }
         $encryptor = new \Hubzero\Mail\Token();
         $from = array('name' => Config::get('sitename'), 'email' => Config::get('mailfrom'));
         // Email each group member separately, each needs a user specific token
         foreach ($userIDsToEmail as $userID) {
             // Construct User specific Email ThreadToken
             // Version, type, userid, xforumid
             $token = $encryptor->buildEmailToken(1, 2, $userID, $parent);
             // add unsubscribe link
             $unsubscribeToken = $encryptor->buildEmailToken(1, 3, $userID, $this->group->get('gidNumber'));
             $unsubscribeLink = rtrim(Request::base(), '/') . '/' . ltrim(Route::url('index.php?option=com_groups&cn=' . $this->group->get('cn') . '&active=forum&action=unsubscribe&t=' . $unsubscribeToken), DS);
             $msg = array();
             // create view object
             $eview = new \Hubzero\Mail\View(array('base_path' => __DIR__, 'name' => 'email', 'layout' => 'comment_plain'));
             // plain text
             $eview->set('delimiter', '~!~!~!~!~!~!~!~!~!~!')->set('unsubscribe', $unsubscribeLink)->set('group', $this->group)->set('section', $esection)->set('category', $ecategory)->set('thread', $ethread)->set('post', $epost);
             $plain = $eview->loadTemplate(false);
             $msg['plaintext'] = str_replace("\n", "\r\n", $plain);
             // HTML
             $eview->setLayout('comment_html');
             $html = $eview->loadTemplate();
             $msg['multipart'] = str_replace("\n", "\r\n", $html);
             $subject = ' - ' . $this->group->get('cn') . ' - ' . $posttitle;
             $from['replytoemail'] = 'hgm-' . $token . '@' . $_SERVER['HTTP_HOST'];
             if (!Event::trigger('xmessage.onSendMessage', array('group_message', $subject, $msg, $from, array($userID), $this->option, null, '', $this->group->get('gidNumber')))) {
                 $this->setError(Lang::txt('GROUPS_ERROR_EMAIL_MEMBERS_FAILED'));
             }
         }
     }
     // Set the redirect
     App::redirect(Route::url($this->base . '&scope=' . $section . '/' . $category->alias . '/' . $thread), $message, 'passed');
 }
Example #2
0
 /**
  * Saves posted data for a new/edited forum thread post
  *
  * @return  void
  */
 public function savethread()
 {
     // Check for request forgeries
     Request::checkToken();
     // Must be logged in
     if (User::isGuest()) {
         App::redirect(Route::url('index.php?option=com_users&view=login&return=' . base64_encode(Route::url($this->base, false, true))));
         return;
     }
     // Incoming
     $section = Request::getVar('section', '');
     $no_html = Request::getInt('no_html', 0);
     $fields = Request::getVar('fields', array(), 'post', 'none', 2);
     $fields = array_map('trim', $fields);
     // Check permissions
     $this->_authorize('thread', intval($fields['id']));
     $asset = 'thread';
     if ($fields['id'] && !$this->params->get('access-edit-thread') || !$fields['id'] && !$this->params->get('access-create-thread')) {
         App::redirect(Route::url($this->base), Lang::txt('You are not authorized to perform this action.'), 'warning');
         return;
     }
     if ($fields['id']) {
         $old = new \Components\Forum\Tables\Post($this->database);
         $old->load(intval($fields['id']));
     }
     // Bind data
     $model = new \Components\Forum\Tables\Post($this->database);
     if (!$model->bind($fields)) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     // Double comment?
     $query = "SELECT * FROM `#__forum_posts` WHERE object_id=" . $this->database->Quote($model->object_id);
     $query .= " AND scope_id=" . $this->database->Quote($model->scope_id) . " AND scope=" . $this->database->Quote($model->scope);
     $query .= " AND comment=" . $this->database->Quote($model->comment) . " AND created_by=" . $this->database->Quote($model->created_by);
     $query .= " LIMIT 1";
     $this->database->setQuery($query);
     if ($result = $this->database->loadAssoc()) {
         $model->bind($result);
     }
     // Load the category
     $category = new \Components\Forum\Tables\Category($this->database);
     $category->load(intval($model->category_id));
     if (!$model->object_id && $category->object_id) {
         $model->object_id = $category->object_id;
     }
     // Check content
     if (!$model->check()) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     // Store new content
     if (!$model->store()) {
         $this->addPluginMessage($model->getError(), 'error');
         return $this->editthread($model);
     }
     // Determine parent ID
     $parent = $model->parent ? $model->parent : $model->id;
     // Get the thread ID
     if (!$model->thread && !$model->parent) {
         $model->thread = $model->id;
     }
     // Upload file
     $this->upload($model->thread, $model->id);
     // Update category ID if it was changed
     if ($fields['id']) {
         if ($old->category_id != $fields['category_id']) {
             $model->updateReplies(array('category_id' => $fields['category_id']), $model->id);
         }
     }
     // Save tags
     $tags = Request::getVar('tags', '', 'post');
     $tagger = new \Components\Forum\Models\Tags($model->id);
     $tagger->setTags($tags, User::get('id'), 1);
     // Being called through AJAX?
     if ($no_html) {
         // Set the thread
         Request::setVar('thread', $model->thread);
         // Is this a new post in a thread or new thread entirely?
         if (!$model->parent) {
             // New thread
             // Update the thread list and get the contents of the thread
             Request::setVar('action', 'both');
         } else {
             // Get a list of new posts in the thread
             Request::setVar('action', 'posts');
         }
         // If we have a lecture set, push through to the lecture view
         if (Request::getVar('group', '')) {
             $unit = $this->course->offering()->unit($category->alias);
             $lecture = new \Components\Courses\Models\Assetgroup($model->object_id);
             return $this->onCourseAfterLecture($this->course, $unit, $lecture);
         } else {
             // Display main panel
             return $this->panel();
         }
     }
     $rtrn = base64_decode(Request::getVar('return', '', 'post'));
     if (!$rtrn) {
         $rtrn = Route::url($this->base . '&thread=' . $thread);
     }
     // Set the redirect
     App::redirect($rtrn, $message, 'passed');
 }