public function timeline() { $result = null; $app = JFactory::getApplication(); try { $userid = self::validateRequest(); //get necessary arguments $id = $app->input->getInt('id', null); switch ($app->input->getMethod()) { //fetch issue's timeline case 'GET': if ($id == null) { throw new Exception('Id is not set'); } //get logs model $issueModel = JModelLegacy::getInstance('Issue', 'ImcModel', array('ignore_request' => true)); $logsModel = JModelLegacy::getInstance('Logs', 'ImcModel', array('ignore_request' => true)); //handle unexpected warnings from model set_error_handler(array($this, 'exception_error_handler')); $data = $issueModel->getData($id); if (!is_object($data)) { throw new Exception('Issue does not exist'); } $result = ImcFrontendHelper::sanitizeIssue($data, $userid); if ($result->state != 1) { throw new Exception('Issue is not published'); } if (!$result->myIssue && $result->moderation) { $app->enqueueMessage('Issue is under moderation', 'info'); } $data = $logsModel->getItemsByIssue($id); $result = ImcFrontendHelper::sanitizeLogs($data); restore_error_handler(); break; case 'POST': if ($id != null) { throw new Exception('You cannot use POST to fetch issue. Use GET instead'); } //TODO: Future implementation break; //update existing issue //update existing issue case 'PUT': case 'PATCH': if ($id == null) { throw new Exception('Id is not set'); } break; default: throw new Exception('HTTP method is not supported'); } echo new JResponseJson($result, 'Timeline action completed successfully'); } catch (Exception $e) { header("HTTP/1.0 202 Accepted"); echo new JResponseJson($e); } }
public function issue() { $result = null; $app = JFactory::getApplication(); try { $userid = self::validateRequest(); //get necessary arguments $id = $app->input->getInt('id', null); switch ($app->input->getMethod()) { //fetch existing issue case 'GET': if ($id == null) { throw new Exception('Id is not set'); } //get issue model $issueModel = JModelLegacy::getInstance('Issue', 'ImcModel', array('ignore_request' => true)); //handle unexpected warnings from model set_error_handler(array($this, 'exception_error_handler')); $data = $issueModel->getData($id); restore_error_handler(); if (!is_object($data)) { throw new Exception('Issue does not exist'); } $result = ImcFrontendHelper::sanitizeIssue($data, $userid); //check for any restrictions if (!$result->myIssue && $result->moderation) { throw new Exception('Issue is under moderation'); } if ($result->state != 1) { throw new Exception('Issue is not published'); } //be consistent return as array (of size 1) $result = array($result); break; //create new issue //create new issue case 'POST': if ($id != null) { throw new Exception('You cannot use POST to fetch issue. Use GET instead'); } //get necessary arguments $args = array('catid' => $app->input->getInt('catid'), 'title' => $app->input->getString('title'), 'description' => $app->input->getString('description'), 'address' => $app->input->getString('address'), 'latitude' => $app->input->getString('lat'), 'longitude' => $app->input->getString('lng')); ImcFrontendHelper::checkNullArguments($args); $args['userid'] = $userid; $args['created_by'] = $userid; $args['stepid'] = ImcFrontendHelper::getPrimaryStepId(); $args['id'] = 0; $args['created'] = date('Y-m-d H:i:s'); $args['updated'] = $args['created']; $args['language'] = '*'; $args['note'] = 'modality=' . $app->input->getInt('m_id'); $args['subgroup'] = 0; $args['photo'] = json_encode(array('isnew' => 1, 'id' => time(), 'imagedir' => 'images/imc', 'files' => array()), JSON_UNESCAPED_SLASHES); // '{"isnew":1,"id":1440286789,"imagedir":"images/imc","files":[]}'; //get issueForm model //JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/models'); $issueFormModel = JModelLegacy::getInstance('IssueForm', 'ImcModel', array('ignore_request' => true)); //handle unexpected warnings from model set_error_handler(array($this, 'exception_error_handler')); $issueFormModel->save($args); $insertid = JFactory::getApplication()->getUserState('com_imc.edit.issue.insertid'); //call post save hook require_once JPATH_COMPONENT . '/controllers/issueform.php'; $issueFormController = new ImcControllerIssueForm(); $issueFormController->postSaveHook($issueFormModel, $args); restore_error_handler(); $result = 'Newly submitted issue ID is ' . $insertid; break; //update existing issue //update existing issue case 'PUT': case 'PATCH': if ($id == null) { throw new Exception('Id is not set'); } break; default: throw new Exception('HTTP method is not supported'); } echo new JResponseJson($result, 'Issue action completed successfully'); } catch (Exception $e) { echo new JResponseJson($e); } }