/** * Filter the Response to add page assets and vars and return. * @param Response $response * @return Response */ private function filter(Response $response) { // @todo START legacy block - remove at Core-2.0 $baseUri = \System::getBaseUri(); $jsAssets = []; $javascripts = \JCSSUtil::prepareJavascripts(\PageUtil::getVar('javascript')); $i = 60; $legacyAjaxScripts = 0; foreach ($javascripts as $javascript) { $javascript = !empty($baseUri) && false === strpos($javascript, $baseUri) ? "{$baseUri}/{$javascript}" : "{$javascript}"; $javascript = $javascript[0] == '/' ? $javascript : "/{$javascript}"; // add slash to start if not present. // Add legacy ajax scripts (like prototype/scriptaculous) at the lightest weight (0) and in order from there. // Add others after core default assets (like jQuery) but before pageAddAsset default weight (100) and in order from there. $jsAssets[$javascript] = false !== strpos($javascript, 'javascript/ajax/') ? $legacyAjaxScripts++ : $i++; } $cssAssets = []; $stylesheets = \PageUtil::getVar('stylesheet'); $i = 60; foreach ($stylesheets as $stylesheet) { $stylesheet = $baseUri . '/' . $stylesheet; $cssAssets[$stylesheet] = $i++; // add before pageAddAsset default weight (100) } // @todo END legacy block - remove at Core-2.0 $filteredContent = $this->filterService->filter($response->getContent(), $jsAssets, $cssAssets); $response->setContent($filteredContent); return $response; }
/** * Inserts the common ajax javascript files in page header. * * Insert the common ajax javascript files (prototype, scriptaculous) in the * page header using page vars. <i>All other javascript files have to be added * manually on-demand using the {@link smarty_function_pageaddvar() pageaddvar} plugin.</i> * * Available attributes: * - modname (string) the module name in which to look for the base javascript file for the module; defaults to top level module when used in a block template. * - filename (string) (optional) filename to load (default ajax.js) * - noscriptaculous (mixed) (optional) does not include scriptaculous.js if set * - validation (mixed) (optional) includes validation.js if set * - lightbox (mixed) (optional) includes lightbox.js if set (loads scriptaculous effects if noscriptaculous is set) * - imageviewer (mixed) (optional) includes Zikula.ImageViewer.js if set (loads scriptaculous effects and dragdrop if noscriptaculous is set) * - assign (string) (optional) the name of the template variable to which the script tag string is assigned, <i>instead of</i> * adding them to the page variables through PageUtil::addVar * * * Examples: * * <samp>{ajaxheader modname='Example' filename='example.js'}</samp> * * <samp>{ajaxheader modname='Example' noscriptaculous=1}</samp> * * @param array $params All attributes passed to this function from the template. * @param Zikula_View $view Reference to the {@link Zikula_View} object. * * @return void */ function smarty_function_ajaxheader($params, Zikula_View $view) { // use supplied modname or top level module $modname = (isset($params['modname'])) ? $params['modname'] : ModUtil::getName(); // define the default filename $filename = (isset($params['filename'])) ? $params['filename'] : 'Zikula.js'; $validation = (isset($params['validation'])) ? true : false; $lightbox = (isset($params['lightbox'])) ? true : false; $ui = (isset($params['ui'])) ? true : false; $imageviewer = (isset($params['imageviewer'])) ? true : false; // create an empty return $return = ''; // we always need those $scripts = array('prototype', 'zikula'); if ($validation) { $scripts[] = 'validation'; } if ($ui) { $scripts[] = 'livepipe'; $scripts[] = 'zikula.ui'; } if ($lightbox) { // check if lightbox is present - if not, load ImageViewer instead if (is_readable('javascript/ajax/lightbox.js')) { $scripts[] = 'javascript/ajax/lightbox.js'; if (isset($params['assign'])) { $return = '<link rel="stylesheet" href="javascript/ajax/lightbox/lightbox.css" type="text/css" media="screen" />'; } else { PageUtil::addVar('stylesheet', 'javascript/ajax/lightbox/lightbox.css'); } } else { $imageviewer = true; } } if ($imageviewer) { $scripts[] = 'zikula.imageviewer'; if (isset($params['assign'])) { $return = '<link rel="stylesheet" href="javascript/helpers/ImageViewer/ImageViewer.css" type="text/css" media="screen" />'; } } $modinfo = ModUtil::getInfoFromName($modname); if ($modinfo !== false) { $osdirectory = DataUtil::formatForOS($modinfo['directory']); $osfilename = DataUtil::formatForOS($filename); $base = $modinfo['type'] == ModUtil::TYPE_SYSTEM ? 'system' : 'modules'; if (file_exists($file = "$base/$osdirectory/javascript/$osfilename") || file_exists($file = "$base/$osdirectory/pnjavascript/$osfilename")) { $scripts[] = DataUtil::formatForDisplay($file); } } if (isset($params['assign'])) { // create script tags now $scripts = JCSSUtil::prepareJavascripts($scripts); foreach ($scripts as $script) { $return .= '<script type="text/javascript" src="' . $script . '"></script>' . "\n"; } $view->assign($params['assign'], $return); } else { PageUtil::addVar('javascript', $scripts); } return; }