/** * Return the demanded frontend module or content element parsed as html string * * Required GET data: * * action: "reload-element" * * element: "ce::id" or "mod::id" (replace 'id' with the element's id) * * page: "id" (optionally, replace 'id' with the current page's id) * * auto_item: (an optional auto_item which will be set before fetching the element) */ public function getModuleOrContentElement() { if (!\Environment::get('isAjaxRequest') || Input::get('action') != 'reload-element') { return; } global $objPage; // Set page object as it may be needed for the language e.g. if (!$objPage && (int) Input::get('page')) { $objPage = \PageModel::findWithDetails((int) Input::get('page')); } $GLOBALS['TL_LANGUAGE'] = null !== $objPage ? $objPage->language : $GLOBALS['TL_LANGUAGE']; list($strElementType, $intElementId) = trimsplit('::', Input::get('element')); $strError = ''; $return = ''; // Authenticate front end user, e.g. for insert tags if (FE_USER_LOGGED_IN) { /** @noinspection PhpUndefinedMethodInspection */ $this->import('FrontendUser', 'User'); /** @var \FrontendUser $this ->User */ $this->User->authenticate(); } // Load default language file \System::loadLanguageFile('default'); // Set a given auto_item to fetch the correct version of a module or content element if ($strAutoItem = Input::get('auto_item')) { Input::setGet('auto_item', $strAutoItem); } switch ($strElementType) { case 'mod': /** @type \Model $objModule */ $objModule = \ModuleModel::findByPk($intElementId); if (null === $objModule) { $strError = sprintf('Could not find module ID %s', $intElementId); continue; } if (!$objModule->allowAjaxReload) { $strError = sprintf('Module ID %u is not allowed to fetch', $intElementId); continue; } $return = \Controller::getFrontendModule($objModule); break; case 'ce': /** @type \Model $objContent */ $objContent = ContentModel::findByPk($intElementId); if (null === $objContent) { $strError = sprintf('Could not find content element ID %s', $intElementId); continue; } if (!$objContent->allowAjaxReload) { $strError = sprintf('Content element ID %u is not allowed to fetch', $intElementId); continue; } $return = \Controller::getContentElement($objContent); break; default: $strError = 'Could not determine whether the element is a module or content element'; break; } $arrResponse = array(); if ($strError) { $arrResponse['status'] = 'error'; $arrResponse['error'] = $strError; } else { $arrResponse['status'] = 'ok'; $arrResponse['html'] = $return; } $objResponse = new JsonResponse($arrResponse); $objResponse->send(); }