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)); $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); //merge logs as timeline if (is_object($data)) { $data->timeline = $logsModel->getItemsByIssue($id); $votesModel = JModelLegacy::getInstance('Votes', 'ImcModel', array('ignore_request' => true)); $data->hasVoted = $votesModel->hasVoted($data->id, $userid); } restore_error_handler(); if (!is_object($data)) { throw new Exception(JText::_('COM_IMC_API_ISSUE_NOT_EXIST')); } $result = ImcFrontendHelper::sanitizeIssue($data, $userid); //check for any restrictions if (!$result->myIssue && $result->moderation) { throw new Exception(JText::_('COM_IMC_API_ISSUE_UNDER_MODERATION')); } if ($result->state != 1) { throw new Exception(JText::_('COM_IMC_API_ISSUE_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'); } //guests are not allowed to post issues //TODO: get this from settings if ($userid == 0) { throw new Exception(JText::_('COM_IMC_API_NO_GUESTS_NO_POST')); } //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); //check if category exists if (is_null(ImcFrontendHelper::getCategoryNameByCategoryId($args['catid'], true))) { throw new Exception(JText::_('COM_IMC_API_CATEGORY_NOT_EXIST')); } $args['userid'] = $userid; $args['created_by'] = $userid; $args['stepid'] = ImcFrontendHelper::getPrimaryStepId(); $args['id'] = 0; $args['created'] = ImcFrontendHelper::convert2UTC(date('Y-m-d H:i:s')); $args['updated'] = $args['created']; $args['note'] = 'modality=' . $app->input->getInt('m_id'); $args['language'] = '*'; $args['subgroup'] = 0; $m_id = $app->input->getInt('m_id', 0); $args['modality'] = $m_id; $tmpTime = time(); //used for temporary id $imagedir = 'images/imc'; //check if post contains files $file = $app->input->files->get('files'); if (!empty($file)) { require_once JPATH_ROOT . '/components/com_imc/models/fields/multiphoto/server/UploadHandler.php'; $options = array('script_url' => JRoute::_(JURI::root(true) . '/administrator/index.php?option=com_imc&task=upload.handler&format=json&id=' . $tmpTime . '&imagedir=' . $imagedir . '&' . JSession::getFormToken() . '=1'), 'upload_dir' => JPATH_ROOT . '/' . $imagedir . '/' . $tmpTime . '/', 'upload_url' => $imagedir . '/' . $tmpTime . '/', 'param_name' => 'files', 'imc_api' => true); $upload_handler = new UploadHandler($options); if (isset($upload_handler->imc_api)) { $files_json = json_decode($upload_handler->imc_api); $args['photo'] = json_encode(array('isnew' => 1, 'id' => $tmpTime, 'imagedir' => $imagedir, 'files' => $files_json->files)); $app->enqueueMessage('File(s) uploaded successfully', 'info'); } else { throw new Exception(JText::_('COM_IMC_API_UPLOAD_FAILED')); } } else { $args['photo'] = json_encode(array('isnew' => 1, 'id' => $tmpTime, 'imagedir' => $imagedir, 'files' => array())); } //get issueForm model and save $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 = array('issueid' => $insertid); //be consistent return as array (of size 1) $result = array($result); 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) { header("HTTP/1.0 202 Accepted"); echo new JResponseJson($e); } }
protected function postSaveHook(JModelLegacy $model, $validData = array()) { //A: inform log table about the new issue if ($validData['id'] == 0) { $log = JTable::getInstance('Log', 'ImcTable', array()); $catTitle = ImcFrontendHelper::getCategoryNameByCategoryId($validData['catid']); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = JText::_('COM_IMC_LOGS_ACTION_INITIAL_COMMIT'); $data2['issueid'] = $model->getItem()->get('id'); $data2['stepid'] = $validData['stepid']; $data2['description'] = JText::_('COM_IMC_LOGS_ACTION_INITIAL_COMMIT') . ' ' . JText::_('COM_IMC_LOGS_AT_CATEGORY') . ' ' . $catTitle; $data2['created'] = $validData['created']; $data2['created_by'] = $validData['created_by']; $data2['updated'] = $validData['created']; $data2['language'] = $validData['language']; $data2['rules'] = $validData['rules']; if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } try { $dispatcher = JEventDispatcher::getInstance(); $results = $dispatcher->trigger('onAfterNewIssueAdded', array($model, $validData)); // Check the returned results. This is for plugins that don't throw // exceptions when they encounter serious errors. if (in_array(false, $results)) { throw new Exception($dispatcher->getError(), 500); } } catch (Exception $e) { // Handle a caught exception. throw $e; } } else { //a. check for step modification if (isset($validData['is_step_modified']) && $validData['is_step_modified'] === 'true') { $user = JFactory::getUser(); $log = JTable::getInstance('Log', 'ImcTable', array()); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = JText::_('COM_IMC_LOGS_ACTION_STEP_MODIFIED'); $data2['issueid'] = $validData['id']; $data2['stepid'] = $validData['stepid']; $data2['description'] = $validData['step_modified_description']; $data2['created'] = $validData['updated']; $data2['created_by'] = $user->id; $data2['updated'] = $validData['updated']; $data2['language'] = $validData['language']; $data2['rules'] = $validData['rules']; if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } $dispatcher = JEventDispatcher::getInstance(); $dispatcher->trigger('onAfterStepModified', array($model, $validData)); } //b. check for category modification if (isset($validData['is_category_modified']) && $validData['is_category_modified'] === 'true') { $user = JFactory::getUser(); $log = JTable::getInstance('Log', 'ImcTable', array()); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = JText::_('COM_IMC_LOGS_ACTION_CATEGORY_MODIFIED'); $data2['issueid'] = $validData['id']; $data2['stepid'] = $validData['stepid']; $data2['description'] = $validData['category_modified_description']; $data2['created'] = $validData['updated']; $data2['created_by'] = $user->id; $data2['updated'] = $validData['updated']; $data2['language'] = $validData['language']; $data2['rules'] = $validData['rules']; if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } $dispatcher = JEventDispatcher::getInstance(); $dispatcher->trigger('onAfterCategoryModified', array($model, $validData)); } } //B: move any images only if record is new if ($validData['id'] == 0) { //check if any files uploaded $obj = json_decode($validData['photo']); if (empty($obj->files)) { return; } $srcDir = JPATH_ROOT . '/' . $obj->imagedir . '/' . $obj->id; $dstDir = JPATH_ROOT . '/' . $obj->imagedir . '/' . $model->getItem()->get('id'); $success = rename($srcDir, $dstDir); if ($success) { //update photo json isnew, id unset($obj->isnew); $obj->id = $model->getItem()->get('id'); $photo = json_encode($obj); // Create an object for the record we are going to update. $object = new stdClass(); $object->id = $model->getItem()->get('id'); $object->photo = $photo; // Update photo $result = JFactory::getDbo()->updateObject('#__imc_issues', $object, 'id'); } else { JFactory::getApplication()->enqueueMessage('Cannot move ' . $srcDir . ' to ' . $dstDir . '. Check folder rights', 'error'); } } }
public function onAfterStepModified($model, $validData, $id = null) { $details = $this->getDetails($id, $model); $app = JFactory::getApplication(); $showMsgsFrontend = $this->params->get('messagesfrontend') && !$app->isAdmin(); $showMsgsBackend = $this->params->get('messagesbackend') && $app->isAdmin(); $step = ImcFrontendHelper::getStepByStepId($validData['stepid']); $MENUALIAS = $this->params->get('menualias'); $appSite = JApplication::getInstance('site'); $router = $appSite->getRouter(); $uri = $router->build('index.php?option=com_imc&view=issue&id=' . (int) ($id == null ? $validData['id'] : $id)); $parsed_url = $uri->toString(); $parsed_url = str_replace('administrator/', '', $parsed_url); $parsed_url = str_replace('component/imc', $MENUALIAS, $parsed_url); $issueLink = $_SERVER['HTTP_HOST'] . $parsed_url; //Prepare email for admins if ($this->params->get('mailstatuschangeadmins')) { $subject = sprintf(JText::_('PLG_IMC_MAIL_NOTIFIER_ADMINS_STEP_MODIFIED_SUBJECT'), $id == null ? $validData['id'] : $id); $body = sprintf(JText::_('PLG_IMC_MAIL_NOTIFIER_ADMINS_STEP_MODIFIED_BODY'), $validData['title'], $step['stepid_title'], JFactory::getUser()->name); if (empty($details->emails) || $details->emails[0] == '') { if ($showMsgsBackend) { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_ADMINS_MAIL_NOT_SET') . ImcFrontendHelper::getCategoryNameByCategoryId($validData['catid']), 'warning'); } } else { $recipients = implode(',', $details->emails); if ($this->sendMail($subject, $body, $details->emails)) { if ($showMsgsBackend) { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_ADMINS_MAIL_CONFIRM') . $recipients); } } else { if ($showMsgsBackend) { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_MAIL_FAILED') . $recipients, 'error'); } } } } //Prepare email for user if ($this->params->get('mailstatuschangeuser')) { $subject = sprintf(JText::_('PLG_IMC_MAIL_NOTIFIER_USER_STEP_MODIFIED_SUBJECT'), $id == null ? $validData['id'] : $id); $body = sprintf(JText::_('PLG_IMC_MAIL_NOTIFIER_USER_STEP_MODIFIED_BODY'), $validData['title'], $step['stepid_title'], $issueLink); $body .= '<a href="http://' . $issueLink . '">' . $issueLink . '</a>'; if ($this->sendMail($subject, $body, $details->usermail)) { if ($showMsgsBackend) { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_MAIL_STEP_MODIFIED_CONFIRM') . $details->usermail . ' (' . $details->username . ')'); } if ($showMsgsFrontend) { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_MAIL_STEP_MODIFIED_CONFIRM') . $details->usermail . ' (' . $details->username . ')'); } } else { $app->enqueueMessage(JText::_('PLG_IMC_MAIL_NOTIFIER_MAIL_FAILED') . $recipients, 'error'); } } }
?> <span style="color: <?php echo $step['stepid_color']; ?> "><?php echo $step['stepid_title']; ?> </span> </p> <p><strong><?php echo JText::_('COM_IMC_FORM_LBL_ISSUE_CATID'); ?> </strong>: <?php echo ImcFrontendHelper::getCategoryNameByCategoryId($this->item->catid); ?> </p> <p><strong><?php echo JText::_('COM_IMC_FORM_LBL_ISSUE_ADDRESS'); ?> </strong>: <?php echo $this->item->address; ?> </p> <p><strong><?php echo JText::_('COM_IMC_FORM_LBL_ISSUE_DESCRIPTION'); ?>
public function postSaveHook(JModelLegacy $model, $validData = array()) { $insertid = JFactory::getApplication()->getUserState('com_imc.edit.issue.insertid'); //A: inform log table about the new issue if ($validData['id'] == 0) { JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables'); $log = JTable::getInstance('Log', 'ImcTable', array()); $catTitle = ImcFrontendHelper::getCategoryNameByCategoryId($validData['catid']); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = 'step'; //enum(step|category) $data2['issueid'] = $insertid; //$model->getItem()->get('id'); $data2['stepid'] = $validData['stepid']; $data2['description'] = JText::_('COM_IMC_LOGS_ACTION_INITIAL_COMMIT') . ' ' . JText::_('COM_IMC_LOGS_AT_CATEGORY') . ' ' . $catTitle; $data2['created'] = $validData['created']; $data2['created_by'] = $validData['created_by']; $data2['updated'] = $validData['created']; $data2['language'] = $validData['language']; if (isset($data2['rules'])) { $data2['rules'] = $validData['rules']; } if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } $dispatcher = JEventDispatcher::getInstance(); $results = $dispatcher->trigger('onAfterNewIssueAdded', array($model, $validData, $insertid)); } else { //a. check for step modification if (isset($validData['is_step_modified']) && $validData['is_step_modified'] === 'true') { $user = JFactory::getUser(); $log = JTable::getInstance('Log', 'ImcTable', array()); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = 'step'; //enum(step|category) $data2['issueid'] = $validData['id']; $data2['stepid'] = $validData['stepid']; $data2['description'] = $validData['step_modified_description']; $data2['created'] = $validData['updated']; $data2['created_by'] = $user->id; $data2['updated'] = $validData['updated']; $data2['language'] = $validData['language']; if (isset($data2['rules'])) { $data2['rules'] = $validData['rules']; } if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } $dispatcher = JEventDispatcher::getInstance(); $dispatcher->trigger('onAfterStepModified', array($model, $validData, $insertid)); } //b. check for category modification if (isset($validData['is_category_modified']) && $validData['is_category_modified'] === 'true') { $user = JFactory::getUser(); $log = JTable::getInstance('Log', 'ImcTable', array()); $data2['id'] = 0; $data2['state'] = 1; $data2['action'] = 'category'; //enum(step|category) $data2['issueid'] = $validData['id']; $data2['stepid'] = $validData['stepid']; $data2['description'] = $validData['category_modified_description']; $data2['created'] = $validData['updated']; $data2['created_by'] = $user->id; $data2['updated'] = $validData['updated']; $data2['language'] = $validData['language']; $data2['rules'] = $validData['rules']; if (!$log->bind($data2)) { JFactory::getApplication()->enqueueMessage('Cannot bind data to log table', 'error'); } if (!$log->save($data2)) { JFactory::getApplication()->enqueueMessage('Cannot save data to log table', 'error'); } $dispatcher = JEventDispatcher::getInstance(); $dispatcher->trigger('onAfterCategoryModified', array($model, $validData, $insertid)); } } //B: move any images only if record is new if ($validData['id'] == 0) { //check if any files uploaded $obj = json_decode($validData['photo']); if (empty($obj->files)) { return; } $srcDir = JPATH_ROOT . '/' . $obj->imagedir . '/' . $obj->id; $dstDir = JPATH_ROOT . '/' . $obj->imagedir . '/' . $insertid; $success = rename($srcDir, $dstDir); if ($success) { //update photo json isnew, id unset($obj->isnew); //update files url foreach ($obj->files as &$file) { $file->url = str_replace($obj->id, $insertid, $file->url); $file->mediumUrl = str_replace($obj->id, $insertid, $file->mediumUrl); $file->thumbnailUrl = str_replace($obj->id, $insertid, $file->thumbnailUrl); } //update id $obj->id = $insertid; $photo = json_encode($obj); // Create an object for the record we are going to update. $object = new stdClass(); $object->id = $insertid; $object->photo = $photo; // Update photo $result = JFactory::getDbo()->updateObject('#__imc_issues', $object, 'id'); } else { JFactory::getApplication()->enqueueMessage('Cannot move ' . $srcDir . ' to ' . $dstDir . '. Check folder rights', 'error'); } } }