/** * Initializes the controller before invoking an action method. * * Override this method to solve tasks which all actions have in * common. * * @return void * @api */ protected function initializeAction() { if ($this->settings['listenOnEvents']) { $eventNames = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['listenOnEvents']); foreach ($eventNames as $eventName) { $this->hijaxEventDispatcher->connect($eventName); } } }
/** * @param string $action * @param array $arguments * @param string $controller * @param string $extensionName * @param string $pluginName * @param string $format * @param int $pageUid * @param boolean $cachedAjaxIfPossible TRUE if the URI should be cached (with respect to non-cacheable actions) * @param boolean $forceContext TRUE if the controller/action/... should be passed * @param boolean $noAjax * * @return string */ public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $format = '', $pageUid = NULL, $cachedAjaxIfPossible = TRUE, $forceContext = TRUE, $noAjax = FALSE) { $request = $this->mvcDispatcher->getCurrentRequest(); if ($forceContext) { $requestArguments = $this->controllerContext->getRequest()->getArguments(); $requestArguments = array_merge($requestArguments, $this->hijaxEventDispatcher->getContextArguments()); $requestArguments = array_merge($requestArguments, $arguments); $arguments = $requestArguments; } if ($noAjax) { return parent::render($action, $arguments, $controller, $extensionName, $pluginName, $pageUid); } else { /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */ $listener = $this->mvcDispatcher->getCurrentListener(); if ($request) { if ($action === NULL) { $action = $request->getControllerActionName(); } if ($controller === NULL) { $controller = $request->getControllerName(); } if ($extensionName === NULL) { $extensionName = $request->getControllerExtensionName(); } if ($pluginName === NULL && TYPO3_MODE === 'FE') { $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controller, $action); } if ($pluginName === NULL) { $pluginName = $request->getPluginName(); } $cachedAjaxIfPossible = $cachedAjaxIfPossible ? $this->configurationManager->getContentObject()->getUserObjectType() != ContentObjectRenderer::OBJECTTYPE_USER_INT : false; if ($cachedAjaxIfPossible) { /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ $cacheHash = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\CacheHashCalculator'); $chash = $cacheHash->calculateCacheHash(array('encryptionKey' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 'action' => $action, 'controller' => $controller, 'extension' => $extensionName, 'plugin' => $pluginName, 'arguments' => $this->array_map_recursive('strval', $arguments), 'settingsHash' => $listener->getId())); } } $additionalArguments = array(); $this->hA('r[0][arguments]', $arguments, $additionalArguments); $language = intval($GLOBALS['TSFE'] ? $GLOBALS['TSFE']->sys_language_content : 0); $additionalParams = "&r[0][extension]={$extensionName}&r[0][plugin]={$pluginName}&r[0][controller]={$controller}&r[0][action]={$action}&r[0][format]={$format}&r[0][settingsHash]={$listener->getId()}&eID=extbase_hijax_dispatcher&L={$language}"; if ($additionalArguments) { $additionalParams .= '&' . implode('&', $additionalArguments); } if ($chash) { $additionalParams .= '&r[0][chash]=' . $chash; } /* @var $cObj \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer */ $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer'); $uri = $cObj->typoLink('', array('returnLast' => 'url', 'additionalParams' => $additionalParams, 'parameter' => $pageUid ? $pageUid : ($GLOBALS['TSFE'] ? $GLOBALS['TSFE']->id : 0))); $this->tag->addAttribute('href', $uri); $this->tag->setContent($this->renderChildren()); $this->tag->forceClosingTag(TRUE); return $this->tag->render(); } }
/** * Render the form. * * @param string $action Target action * @param array $arguments Arguments * @param string $controller Target controller * @param string $extensionName Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used * @param string $pluginName Target plugin. If empty, the current plugin name is used * @param integer $pageUid Target page uid * @param mixed $object Object to use for the form. Use in conjunction with the "property" attribute on the sub tags * @param integer $pageType Target page type * @param boolean $noCache set this to disable caching for the target page. You should not need this. * @param boolean $noCacheHash set this to supress the cHash query parameter created by TypoLink. You should not need this. * @param string $section The anchor to be added to the action URI (only active if $actionUri is not set) * @param string $format The requested format (e.g. ".html") of the target page (only active if $actionUri is not set) * @param array $additionalParams additional action URI query parameters that won't be prefixed like $arguments (overrule $arguments) (only active if $actionUri is not set) * @param boolean $absolute If set, an absolute action URI is rendered (only active if $actionUri is not set) * @param boolean $addQueryString If set, the current query parameters will be kept in the action URI (only active if $actionUri is not set) * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the action URI. Only active if $addQueryString = TRUE and $actionUri is not set * @param string $fieldNamePrefix Prefix that will be added to all field names within this form. If not set the prefix will be tx_yourExtension_plugin * @param string $actionUri can be used to overwrite the "action" attribute of the form tag * @param string $objectName name of the object that is bound to this form. If this argument is not specified, the name attribute of this form is used to determine the FormObjectName * @param string $resultTarget target where the results will be loaded * @param string $loaders target where the loader will be shown * @return string rendered form */ public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, $object = NULL, $pageType = 0, $noCache = FALSE, $noCacheHash = FALSE, $section = '', $format = '', array $additionalParams = array(), $absolute = FALSE, $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array(), $fieldNamePrefix = NULL, $actionUri = NULL, $objectName = NULL, $resultTarget = NULL, $loaders = NULL) { $this->renderHijaxDataAttributes($action, $arguments, $controller, $extensionName, $pluginName); $this->hijaxEventDispatcher->setIsHijaxElement(true); if ($resultTarget) { $this->tag->addAttribute('data-hijax-result-target', $resultTarget); } else { /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */ $listener = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')->get('EssentialDots\\ExtbaseHijax\\MVC\\Dispatcher')->getCurrentListener(); $this->tag->addAttribute('data-hijax-result-target', "jQuery(this).parents('.hijax-element[data-hijax-listener-id=\"" . $listener->getId() . "\"]')"); $this->tag->addAttribute('data-hijax-result-wrap', 'false'); } if ($loaders) { $this->tag->addAttribute('data-hijax-loaders', $loaders); } parent::render($action, $arguments, $controller, $extensionName, $pluginName, $pageUid, $object, $pageType, $noCache, $noCacheHash, $section, $format, $additionalParams, $absolute, $addQueryString, $argumentsToBeExcludedFromQueryString, $fieldNamePrefix, $actionUri, $objectName); $this->tag->setContent('<div class="hijax-content">' . $this->tag->getContent() . '</div><div class="hijax-loading"></div>'); return $this->tag->render(); }
/** * @param $responses * @param $eventsToListen * @param bool $processOriginal */ protected function parseAndRunEventListeners(&$responses, $eventsToListen, $processOriginal = TRUE) { if ($processOriginal) { foreach ($responses['original'] as $response) { $this->hijaxEventDispatcher->parseAndRunEventListeners($response['response']); } } if ($eventsToListen && is_array($eventsToListen)) { foreach ($eventsToListen as $listenerId => $eventNames) { $shouldProcess = FALSE; foreach ($eventNames as $eventName) { if ($this->hijaxEventDispatcher->hasPendingEventWithName($eventName, $listenerId)) { $shouldProcess = TRUE; break; } } if ($shouldProcess) { /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */ $listener = $this->listenerFactory->findById($listenerId); if ($listener) { $configuration = $listener->getConfiguration(); $bootstrap = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Core\\Bootstrap'); $bootstrap->cObj = $listener->getCObj(); $bootstrap->initialize($configuration); /* @var $request \TYPO3\CMS\Extbase\Mvc\Web\Request */ $request = $listener->getRequest(); $request->setDispatched(false); $this->setPreventMarkupUpdateOnAjaxLoad(false); /* @var $response \TYPO3\CMS\Extbase\Mvc\Web\Response */ $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response'); /* @var $dispatcher \EssentialDots\ExtbaseHijax\MVC\Dispatcher */ $dispatcher = $this->objectManager->get('EssentialDots\\ExtbaseHijax\\MVC\\Dispatcher'); try { $dispatcher->dispatch($request, $response, $listener); $this->parseHeaders($response); $content = $response->getContent(); $this->serviceContent->processIntScripts($content); $this->serviceContent->processAbsRefPrefix($content, $configuration['settings']['absRefPrefix']); $responses['affected'][] = array('id' => $listenerId, 'format' => $request->getFormat(), 'response' => $content, 'preventMarkupUpdate' => $this->getPreventMarkupUpdateOnAjaxLoad()); } catch (\EssentialDots\ExtbaseHijax\MVC\Exception\StopProcessingAction $exception) { $this->parseHeaders($response); } } else { // TODO: log error message } } } } }
/** * @param array $params * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $pObj */ public function initFEuser($params, $pObj) { /* @var $fe_user \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication */ $fe_user = $pObj->fe_user; if ($fe_user->user && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP($fe_user->formfield_status) == 'login') { $event = new \EssentialDots\ExtbaseHijax\Event\Event('user-loggedIn', array('user' => $fe_user->user)); $this->hijaxEventDispatcher->notify($event); } elseif (!$fe_user->user && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP($fe_user->formfield_status) == 'logout') { $event = new \EssentialDots\ExtbaseHijax\Event\Event('user-loggedOut'); $this->hijaxEventDispatcher->notify($event); } elseif (!$fe_user->user && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP($fe_user->formfield_status) == 'login') { $event = new \EssentialDots\ExtbaseHijax\Event\Event('user-loginFailure'); $this->hijaxEventDispatcher->notify($event); } }
/** * Returns JS callback for the given action * * @param null $action * @param array $arguments * @param null $controller * @param null $extensionName * @param null $pluginName * @param string $format * @param string $section * @return string */ public function getAjaxFunction($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $format = '', $section = 'footer') { // current element needs to have additional logic... $this->hijaxEventDispatcher->setIsHijaxElement(true); $request = $this->mvcDispatcher->getCurrentRequest(); if ($request) { if ($action === NULL) { $action = $request->getControllerActionName(); } if ($controller === NULL) { $controller = $request->getControllerName(); } if ($extensionName === NULL) { $extensionName = $request->getControllerExtensionName(); } if ($pluginName === NULL && TYPO3_MODE === 'FE') { $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controller, $action); } if ($pluginName === NULL) { $pluginName = $request->getPluginName(); } } $settings = array('extension' => $extensionName, 'plugin' => $pluginName, 'controller' => $controller, 'format' => $format ? $format : 'html', 'action' => $action, 'arguments' => $arguments, 'settingsHash' => $this->mvcDispatcher->getCurrentListener() ? $this->mvcDispatcher->getCurrentListener()->getId() : '', 'namespace' => $extensionName && $pluginName ? $this->extensionService->getPluginNamespace($extensionName, $pluginName) : ''); $functionName = 'extbaseHijax_' . md5(serialize($settings)); $content = "; {$functionName}=function(settings, pendingElement, loaders) {"; foreach ($settings as $k => $v) { $content .= "if (typeof settings.{$k} == 'undefined') settings.{$k}=" . json_encode($v) . ";"; } $content .= "return jQuery.hijax(settings, pendingElement, loaders);};"; if ($this->isCached()) { if ($section == 'footer') { $this->pageRenderer->addJsFooterInlineCode(md5($content), $content, FALSE, TRUE); } else { $this->pageRenderer->addJsInlineCode(md5($content), $content, FALSE, TRUE); } } else { // additionalFooterData not possible in USER_INT $GLOBALS['TSFE']->additionalHeaderData[md5($content)] = \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($content); } return $functionName; }
/** * Dispatches a request to a controller and initializes the security framework. * * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, to be modified by the controller * @param \EssentialDots\ExtbaseHijax\Event\Listener $listener Listener * @return void */ public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response, \EssentialDots\ExtbaseHijax\Event\Listener $listener = NULL) { /* @var $request \TYPO3\CMS\Extbase\Mvc\Request */ $this->currentRequest = $request; array_push($this->requestsStack, $this->currentRequest); if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) { parent::dispatch($request, $response); } else { array_push($this->listenersStack, $this->currentListener); if ($listener) { $this->currentListener = $listener; } else { $this->currentListener = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('EssentialDots\\ExtbaseHijax\\Event\\Listener', $request); } if (!$this->serviceContent->getExecuteExtbasePlugins()) { $this->listenerFactory->persist($this->currentListener); $this->serviceContent->setCurrentListener($this->currentListener); } else { $this->hijaxEventDispatcher->startContentElement(); try { parent::dispatch($request, $response); } catch (\TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException $requiredArgumentMissingException) { try { // this happens with simple reload on pages where some argument is required $configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); $defaultControllerName = current(array_keys($configuration['controllerConfiguration'])); $allowedControllerActions = array(); foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) { $allowedControllerActions[$controllerName] = $controllerActions['actions']; } $defaultActionName = is_array($allowedControllerActions[$request->getControllerName()]) ? current($allowedControllerActions[$request->getControllerName()]) : ''; // try to run the current controller with the default action $request->setDispatched(false); $request->setControllerActionName($defaultActionName); parent::dispatch($request, $response); } catch (\TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException $requiredArgumentMissingException) { if ($defaultControllerName != $request->getControllerName()) { $request->setControllerName($defaultControllerName); $defaultActionName = is_array($allowedControllerActions[$defaultControllerName]) ? current($allowedControllerActions[$defaultControllerName]) : ''; // try to run the default plugin controller with the default action $request->setDispatched(false); $request->setControllerActionName($defaultActionName); parent::dispatch($request, $response); } } } if ($this->hijaxEventDispatcher->getIsHijaxElement()) { $this->listenerFactory->persist($this->currentListener); } if (($this->ajaxDispatcher->getIsActive() || $this->hijaxEventDispatcher->getIsHijaxElement()) && !$this->ajaxDispatcher->getPreventMarkupUpdateOnAjaxLoad()) { $currentListeners = $this->hijaxEventDispatcher->getListeners('', TRUE); $signature = $this->getCurrentListener()->getId() . '(' . $this->convertArrayToCSV(array_keys($currentListeners)) . '); '; $content = $response->getContent(); $content = '<!-- ###EVENT_LISTENER_' . self::$id . '### START ' . $signature . ' -->' . $content . '<!-- ###EVENT_LISTENER_' . self::$id . '### END -->'; if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('eID') && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('eID') != 'extbase_hijax_dispatcher') { $this->hijaxEventDispatcher->replaceXMLCommentsWithDivs($content, 'html'); } $response->setContent($content); $this->extensionConfiguration->setNextElementId(++self::$id); } $this->hijaxEventDispatcher->endContentElement(); } $this->currentListener = array_pop($this->listenersStack); } $this->currentRequest = array_pop($this->requestsStack); }
/** * Renders hijax-related data attributes * * @param null $action * @param array $arguments * @param array $contextArguments * @param bool $ajax * @param bool $cachedAjaxIfPossible * @param bool $forceContext * @return string */ protected function getWidgetUri($action = NULL, array $arguments = array(), array $contextArguments = array(), $ajax = TRUE, $cachedAjaxIfPossible = TRUE, $forceContext = FALSE) { $this->hijaxEventDispatcher->setIsHijaxElement(true); $request = $this->controllerContext->getRequest(); /* @var $widgetContext \EssentialDots\ExtbaseHijax\Core\Widget\WidgetContext */ $widgetContext = $request->getWidgetContext(); $tagAttributes = array(); if ($ajax) { $tagAttributes['data-hijax-element-type'] = 'link'; $this->tag->addAttribute('class', trim($this->arguments['class'] . ' hijax-element')); } if ($action === NULL) { $action = $widgetContext->getParentControllerContext()->getRequest()->getControllerActionName(); } if ($ajax) { $tagAttributes['data-hijax-action'] = $action; } $controller = $widgetContext->getParentControllerContext()->getRequest()->getControllerName(); if ($ajax) { $tagAttributes['data-hijax-controller'] = $controller; } $extensionName = $widgetContext->getParentControllerContext()->getRequest()->getControllerExtensionName(); if ($ajax) { $tagAttributes['data-hijax-extension'] = $extensionName; } if (TYPO3_MODE === 'FE') { $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controller, $action); } if (!$pluginName) { $pluginName = $request->getPluginName(); } if (!$pluginName) { $pluginName = $widgetContext->getParentPluginName(); } if ($ajax) { $tagAttributes['data-hijax-plugin'] = $pluginName; } $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName); if ($ajax) { $tagAttributes['data-hijax-namespace'] = $pluginNamespace; } $requestArguments = $widgetContext->getParentControllerContext()->getRequest()->getArguments(); $requestArguments = array_merge($requestArguments, $this->hijaxEventDispatcher->getContextArguments()); $requestArguments = array_merge($requestArguments, $contextArguments); $requestArguments[$widgetContext->getWidgetIdentifier()] = $arguments && is_array($arguments) ? $arguments : array(); if (version_compare(TYPO3_version, '6.1.0', '>=')) { $variableContainer = $widgetContext->getViewHelperChildNodeRenderingContext()->getViewHelperVariableContainer(); if ($variableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames')) { $formFieldNames = $variableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames'); $mvcPropertyMappingConfigurationService = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\MvcPropertyMappingConfigurationService'); /* @var $mvcPropertyMappingConfigurationService \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService */ $requestHash = $mvcPropertyMappingConfigurationService->generateTrustedPropertiesToken($formFieldNames, $pluginNamespace); $requestArguments['__trustedProperties'] = $requestHash; } } if ($ajax) { $tagAttributes['data-hijax-arguments'] = serialize($requestArguments); } /* @var $listener \EssentialDots\ExtbaseHijax\Event\Listener */ $listener = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')->get('EssentialDots\\ExtbaseHijax\\MVC\\Dispatcher')->getCurrentListener(); if ($ajax) { $tagAttributes['data-hijax-settings'] = $listener->getId(); } $cachedAjaxIfPossible = $cachedAjaxIfPossible ? $this->configurationManager->getContentObject()->getUserObjectType() != ContentObjectRenderer::OBJECTTYPE_USER_INT : false; if ($cachedAjaxIfPossible) { /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ $cacheHash = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\CacheHashCalculator'); $tagAttributes['data-hijax-chash'] = $cacheHash->calculateCacheHash(array('encryptionKey' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 'action' => $tagAttributes['data-hijax-action'], 'controller' => $tagAttributes['data-hijax-controller'], 'extension' => $tagAttributes['data-hijax-extension'], 'plugin' => $tagAttributes['data-hijax-plugin'], 'arguments' => $tagAttributes['data-hijax-arguments'], 'settingsHash' => $tagAttributes['data-hijax-settings'])); } foreach ($tagAttributes as $tagAttribute => $attributeValue) { $this->tag->addAttribute($tagAttribute, $attributeValue); } $uriBuilder = $this->controllerContext->getUriBuilder(); $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix(); if ($this->hasArgument('format') && $this->arguments['format'] !== '') { $requestArguments['format'] = $this->arguments['format']; } $uriBuilder->reset()->setArguments(array($pluginNamespace => $requestArguments))->setSection($this->arguments['section'])->setAddQueryString(TRUE)->setArgumentsToBeExcludedFromQueryString(array($argumentPrefix, 'cHash'))->setFormat($this->arguments['format']); if ($forceContext) { return $uriBuilder->uriFor($action, $arguments, $controller, $extensionName, $pluginName); } else { return $uriBuilder->build(); } }