Beispiel #1
0
 /**
  * Vote for an item
  *
  * @return  void
  */
 public function voteTask()
 {
     $no_html = Request::getInt('no_html', 0);
     // Is the user logged in?
     if (User::isGuest()) {
         if (!$no_html) {
             $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE'));
             $this->loginTask();
         }
         return;
     }
     // Incoming
     $id = Request::getInt('id', 0);
     $type = Request::getVar('category', '');
     $vote = Request::getVar('vote', '');
     $ip = Request::ip();
     // Check for reference ID
     if (!$id) {
         // cannot proceed
         if (!$no_html) {
             App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('COM_ANSWERS_ERROR_ID_NOT_FOUND'), 'error');
         }
         return;
     }
     if ($type == 'question') {
         $row = Question::oneOrFail($id);
         $scope = 'question';
     } elseif ($type == 'response') {
         $row = Response::oneOrFail($id);
         $scope = 'question.answer';
     } elseif ($type == 'comment') {
         $row = Comment::oneOrFail($id);
         $scope = 'question.answer.comment';
     }
     // Can't vote for your own comment
     if ($row->get('created_by') == User::get('id')) {
         if (!$no_html) {
             App::redirect(Route::url($row->link()), Lang::txt('COM_ANSWERS_ERROR_CANNOT_VOTE_FOR_OWN'), 'warning');
         }
         return;
     }
     if (!$vote) {
         if (!$no_html) {
             App::redirect(Route::url($row->link()), Lang::txt('COM_ANSWERS_ERROR_VOTE_NOT_FOUND'), 'warning');
         }
         return;
     }
     if (!$row->vote($vote, User::get('id'), $ip)) {
         $this->setError($row->getError());
     }
     if (!$this->getError()) {
         // Log activity
         $recipients = array(User::get('id'));
         if ($row instanceof Question) {
             $question = $row;
             $txt = $row->get('subject');
         }
         if ($row instanceof Response) {
             $question = Question::oneOrFail($row->get('question_id'));
             $txt = Lang::txt('COM_ANSWERS_ACTIVITY_ANSWER_ON', $row->get('id'), $question->get('subject'));
         }
         if ($row instanceof Comment) {
             $question = Question::oneOrFail($row->get('question_id'));
             $txt = Lang::txt('COM_ANSWERS_ACTIVITY_COMMENT_ON', $row->get('id'), $question->get('subject'));
         }
         Event::trigger('system.logActivity', ['activity' => ['action' => 'voted', 'scope' => $scope, 'scope_id' => $id, 'description' => Lang::txt('COM_ANSWERS_ACTIVITY_VOTED_' . ($vote == 'yes' ? 'UP' : 'DOWN'), '<a href="' . Route::url($row->link()) . '">' . $txt . '</a>'), 'details' => array('title' => $question->get('title'), 'question_id' => $question->get('id'), 'url' => $question->link())], 'recipients' => $recipients]);
     }
     // update display
     if ($no_html) {
         $row->set('vote', $vote);
         $this->view->setError($this->getErrors())->set('item', $row)->set('vote', $row->ballot())->setLayout('_vote')->display();
     } else {
         App::redirect(Route::url($row->link()));
     }
 }
Beispiel #2
0
 /**
  * Release a reported item
  *
  * @param      string $refid    ID of the database table row
  * @param      string $parent   If the element has a parent element
  * @param      string $category Element type (determines table to look in)
  * @return     array
  */
 public function releaseReportedItem($refid, $parent, $category)
 {
     if (!$this->_canHandle($category)) {
         return null;
     }
     require_once PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'models' . DS . 'question.php';
     $database = App::get('db');
     $state = 1;
     switch ($category) {
         case 'answer':
             $comment = \Components\Answers\Models\Response::oneOrFail($refid);
             $state = 0;
             break;
         case 'question':
             $comment = \Components\Answers\Models\Question::oneOrFail($refid);
             break;
         case 'answercomment':
             $comment = \Components\Answers\Models\Comment::oneOrFail($refid);
             break;
     }
     $comment->set('state', $state);
     $comment->save();
     return '';
 }
Beispiel #3
0
 /**
  * Save a reply
  *
  * @return  void
  */
 public function savereplyTask()
 {
     // Check for request forgeries
     Request::checkToken();
     // Is the user logged in?
     if (User::isGuest()) {
         $this->setError(Lang::txt('COM_ANSWERS_LOGIN_TO_COMMENT'));
         $this->loginTask();
         return;
     }
     // Incoming
     $questionID = Request::getVar('rid');
     $comment = Request::getVar('comment', array(), 'post', 'none', 2);
     // clean input
     array_walk($comment, function (&$field, $key) {
         $field = \Hubzero\Utility\Sanitize::clean($field);
     });
     if (!$comment['item_id']) {
         throw new Exception(Lang::txt('COM_ANSWERS_ERROR_QUESTION_ID_NOT_FOUND'), 500);
     }
     if ($comment['item_type']) {
         $row = new Comment(0);
         if (!$row->bind($comment)) {
             throw new Exception($row->getError(), 500);
         }
         // Perform some text cleaning, etc.
         $row->set('anonymous', $row->get('anonymous') ? 1 : 0);
         $row->set('created', Date::toSql());
         $row->set('state', 0);
         $row->set('created_by', User::get('id'));
         // Save the data
         if (!$row->store(true)) {
             throw new Exception($row->getError(), 500);
         }
     }
     //For email
     // Load question
     $question = new Question($questionID);
     // Get users who need to be notified on updates
     $apu = $this->config->get('notify_users', '');
     $apu = explode(',', $apu);
     $apu = array_map('trim', $apu);
     $receivers = array();
     // Build the "from" info
     $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U')));
     // Build the message subject
     $subject = Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('COM_ANSWERS_QUESTION') . ' #' . $question->get('id') . ' ' . Lang::txt('COM_ANSWERS_RESPONSE');
     $message = array();
     // Plain text message
     $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'response_plaintext'));
     $eview->option = $this->_option;
     $eview->sitename = Config::get('sitename');
     $eview->question = $question;
     $eview->row = $row;
     $eview->boundary = $from['multipart'];
     $message['plaintext'] = $eview->loadTemplate();
     $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']);
     // HTML message
     $eview->setLayout('response_html');
     $message['multipart'] = $eview->loadTemplate();
     $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']);
     // ---
     $authorid = $question->creator('id');
     $apu = $this->config->get('notify_users', '');
     $apu = explode(',', $apu);
     $apu = array_map('trim', $apu);
     $receivers = array();
     if (!empty($apu)) {
         foreach ($apu as $u) {
             $user = User::getInstance($u);
             if ($user) {
                 $receivers[] = $user->get('id');
             }
         }
         $receivers = array_unique($receivers);
     }
     // send the response, unless the author is also in the admin list.
     if (!in_array($authorid, $receivers) && $question->get('email')) {
         if (!Event::trigger('xmessage.onSendMessage', array('answers_reply_comment', $subject, $message, $from, array($authorid), $this->_option))) {
             $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED'));
         }
     }
     // admin emails
     if (!empty($receivers)) {
         if (!Event::trigger('xmessage.onSendMessage', array('new_answer_admin', $subject, $message, $from, $receivers, $this->_option))) {
             $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED'));
         }
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . Request::getInt('rid', 0)));
 }
Beispiel #4
0
 /**
  * Set and get a specific comment
  *
  * @return     void
  */
 public function comment($id = null)
 {
     if (!isset($this->_comment) || $id !== null && (int) $this->_comment->get('id') != $id) {
         $this->_comment = null;
         // See if we already have a list of comments that we can look through
         if ($this->_comments instanceof ItemList) {
             foreach ($this->_comments as $key => $comment) {
                 if ((int) $comment->get('id') == $id) {
                     $this->_comment = $comment;
                     break;
                 }
             }
         }
         // Nothing found so far?
         if (!$this->_comment) {
             // Load the record
             $this->_comment = Comment::getInstance($id);
         }
     }
     return $this->_comment;
 }
Beispiel #5
0
 /**
  * Vote for an item
  *
  * @return  void
  */
 public function voteTask()
 {
     $no_html = Request::getInt('no_html', 0);
     // Is the user logged in?
     if (User::isGuest()) {
         if (!$no_html) {
             $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE'));
             $this->loginTask();
         }
         return;
     }
     // Incoming
     $id = Request::getInt('id', 0);
     $type = Request::getVar('category', '');
     $vote = Request::getVar('vote', '');
     $ip = Request::ip();
     // Check for reference ID
     if (!$id) {
         // cannot proceed
         if (!$no_html) {
             App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('No ID provided.'), 'error');
         }
         return;
     }
     if ($type == 'question') {
         $row = Question::oneOrFail($id);
     } elseif ($type == 'response') {
         $row = Response::oneOrFail($id);
     } elseif ($type == 'comment') {
         $row = Comment::oneOrFail($id);
     }
     // Can't vote for your own comment
     if ($row->get('created_by') == User::get('id')) {
         if (!$no_html) {
             App::redirect(Route::url($row->link()), Lang::txt('Cannot vote for your own entries.'), 'warning');
         }
         return;
     }
     if (!$vote) {
         if (!$no_html) {
             App::redirect(Route::url($row->link()), Lang::txt('No vote provided.'), 'warning');
         }
         return;
     }
     if (!$row->vote($vote, User::get('id'), $ip)) {
         $this->setError($row->getError());
     }
     // update display
     if ($no_html) {
         $row->set('vote', $vote);
         $this->view->setError($this->getErrors())->set('item', $row)->set('vote', $row->ballot())->setLayout('_vote')->display();
     } else {
         App::redirect(Route::url($row->link()));
     }
 }