/** * Create a new comment * * @apiMethod POST * @apiUri /support/{ticket}/comments * @apiParameter { * "name": "scope", * "description": "Scope type (group, member, etc.)", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "scope_id", * "description": "Scope object ID", * "type": "integer", * "required": true, * "default": null * } * @apiParameter { * "name": "title", * "description": "Entry title", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "alias", * "description": "Entry alias", * "type": "string", * "required": false, * "default": null * } * @return void */ public function createTask() { $this->requiresAuthentication(); if (!$this->acl->check('create', 'comments')) { throw new Exception(Lang::txt('Not authorized'), 403); } $ticket_id = Request::getInt('ticket', 0, 'post'); // Load the old ticket so we can compare for the changelog $old = new \Components\Support\Models\Ticket($ticket_id); $old->set('tags', $old->tags('string')); if (!$old->exists()) { $this->errorMessage(500, Lang::txt('Ticket "%s" does not exist.', $ticket_id)); return; } // Initiate class and bind posted items to database fields $ticket = new \Components\Support\Models\Ticket($ticket_id); $ticket->set('status', Request::getInt('status', $ticket->get('status'), 'post')); $ticket->set('open', Request::getInt('open', $ticket->get('open'), 'post')); $ticket->set('category', Request::getInt('category', $ticket->get('category'), 'post')); $ticket->set('severity', Request::getVar('severity', $ticket->get('severity'), 'post')); $ticket->set('owner', Request::getVar('owner', $ticket->get('owner'), 'post')); $ticket->set('group', Request::getVar('group', $ticket->get('group'), 'post')); // If an existing ticket AND closed AND previously open if ($ticket_id && !$ticket->get('open') && $ticket->get('open') != $old->get('open')) { // Record the closing time $ticket->set('closed', Date::toSql()); } // Any tags? if ($tags = trim(Request::getVar('tags', '', 'post'))) { $ticket->tag($tags, $user->get('uidNumber')); $ticket->set('tags', $ticket->tags('string')); } // Store new content if (!$ticket->store()) { $this->errorMessage(500, $ticket->getError()); return; } // Create a new comment $comment = new \Components\Support\Models\Comment(); $comment->set('ticket', $ticket->get('id')); $comment->set('comment', nl2br(Request::getVar('comment', '', 'post', 'none', 2))); if ($comment->get('comment')) { // If a comment was posted by the ticket submitter to a "waiting user response" ticket, change status. if ($ticket->isWaiting() && $user->get('username') == $ticket->get('login')) { $ticket->open(); } } $comment->set('created', Date::toSql()); $comment->set('created_by', $user->get('uidNumber')); $comment->set('access', Request::getInt('access', 0, 'post')); // Compare fields to find out what has changed for this ticket and build a changelog $comment->changelog()->diff($old, $ticket); $comment->changelog()->cced(Request::getVar('cc', '', 'post')); // Store new content if (!$comment->store()) { $this->errorMessage(500, $comment->getError()); return; } if ($ticket->get('owner')) { $comment->addTo(array('role' => Lang::txt('COM_SUPPORT_COMMENT_SEND_EMAIL_OWNER'), 'name' => $ticket->owner('name'), 'email' => $ticket->owner('email'), 'id' => $ticket->owner('id'))); } // Add any CCs to the e-mail list foreach ($comment->changelog()->get('cc') as $cc) { $comment->addTo($cc, Lang::txt('COM_SUPPORT_COMMENT_SEND_EMAIL_CC')); } // Check if the notify list has eny entries if (count($comment->to())) { include_once PATH_CORE . DS . 'components' . DS . 'com_support' . DS . 'helpers' . DS . 'utilities.php'; $allowEmailResponses = $ticket->config('email_processing'); if ($allowEmailResponses) { try { $encryptor = new \Hubzero\Mail\Token(); } catch (Exception $e) { $allowEmailResponses = false; } } $subject = Lang::txt('COM_SUPPORT_EMAIL_SUBJECT_TICKET_COMMENT', $ticket->get('id')); $from = array('name' => Lang::txt('COM_SUPPORT_EMAIL_FROM', Config::get('sitename')), 'email' => Config::get('mailfrom'), 'multipart' => md5(date('U'))); $message = array(); // Plain text email $eview = new \Hubzero\Mail\View(array('base_path' => PATH_CORE . '/components/com_support/site', 'name' => 'emails', 'layout' => 'comment_plain')); $eview->option = 'com_support'; $eview->controller = 'tickets'; $eview->comment = $comment; $eview->ticket = $ticket; $eview->delimiter = $allowEmailResponses ? '~!~!~!~!~!~!~!~!~!~!' : ''; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML email $eview->setLayout('comment_html'); $message['multipart'] = $eview->loadTemplate(); // Send e-mail to admin? foreach ($comment->to('ids') as $to) { if ($allowEmailResponses) { // The reply-to address contains the token $token = $encryptor->buildEmailToken(1, 1, $to['id'], $ticket->get('id')); $from['replytoemail'] = 'htc-' . $token . strstr(Config::get('mailfrom'), '@'); } // Get the user's email address if (!Event::trigger('xmessage.onSendMessage', array('support_reply_submitted', $subject, $message, $from, array($to['id']), 'com_support'))) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_FAILED_TO_MESSAGE', $to['name'] . '(' . $to['role'] . ')')); } $comment->changelog()->notified($to['role'], $to['name'], $to['email']); } foreach ($comment->to('emails') as $to) { if ($allowEmailResponses) { $token = $encryptor->buildEmailToken(1, 1, -9999, $ticket->get('id')); $email = array($to['email'], 'htc-' . $token . strstr(Config::get('mailfrom'), '@')); // In this case each item in email in an array, 1- To, 2:reply to address \Components\Support\Helpers\Utilities::sendEmail($email[0], $subject, $message, $from, $email[1]); } else { // email is just a plain 'ol string \Components\Support\Helpers\Utilities::sendEmail($to['email'], $subject, $message, $from); } $comment->changelog()->notified($to['role'], $to['name'], $to['email']); } } // Were there any changes? if (count($comment->changelog()->get('notifications')) > 0 || count($comment->changelog()->get('cc')) > 0 || count($comment->changelog()->get('changes')) > 0) { // Save the data if (!$comment->store()) { $this->errorMessage(500, $comment->getError()); return; } } $msg = new stdClass(); $msg->ticket = $ticket->get('id'); $msg->comment = $comment->get('id'); $msg->notified = $comment->changelog()->get('notifications'); $this->setMessageType(Request::getVar('format', 'json')); $this->send($msg, 200, 'OK'); }