/** * 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')); return $this->loginTask(); } // 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']) { App::abort(404, Lang::txt('COM_ANSWERS_ERROR_QUESTION_ID_NOT_FOUND')); } $row = Comment::oneOrNew($comment['id'])->set($comment); // 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->save()) { App::abort(500, $row->getError()); } // For email // Load question $question = Question::oneOrFail($questionID); // 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->get('created_by'); $receivers = $this->recipients(); // 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')); } } // Log activity $recipients = array($question->get('created_by')); $recipients[] = $row->get('created_by'); if ($row->get('parent')) { $recipients[] = $row->parent()->get('created_by'); } else { $response = Response::oneOrFail($row->get('item_id')); $recipients[] = $response->get('created_by'); } $recipients = $this->recipients($recipients); Event::trigger('system.logActivity', ['activity' => ['action' => $comment['id'] ? 'updated' : 'created', 'scope' => 'question.answer.comment', 'scope_id' => $row->get('id'), 'anonymous' => $row->get('anonymous', 0), 'description' => Lang::txt('COM_ANSWERS_ACTIVITY_COMMENT_' . ($comment['id'] ? 'UPDATED' : 'CREATED'), '<a href="' . Route::url($question->link()) . '">' . $question->get('subject') . '</a>'), 'details' => array('title' => $question->get('title'), 'url' => $question->link())], 'recipients' => $recipients]); App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $questionID)); }
/** * 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')); return $this->loginTask(); } // 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']) { App::abort(404, Lang::txt('COM_ANSWERS_ERROR_QUESTION_ID_NOT_FOUND')); } $row = Comment::oneOrNew($comment['id'])->set($comment); // 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->save()) { throw new Exception($row->getError(), 500); } // For email // Load question $question = Question::oneOrFail($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->get('created_by'); $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=' . $questionID)); }