Example #1
0
/**
 * ...
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 *
 * @package application.helper.smarty
 * @author Integry Systems
 */
function smarty_function_includeJs($params, Smarty_Internal_Template $smarty)
{
    static $jsPath;
    if (!$jsPath) {
        $jsPath = ClassLoader::getRealPath('public.javascript.');
    }
    $fileName = $params['file'];
    $filePath = substr($fileName, 0, 1) != '/' ? $jsPath . $fileName : ClassLoader::getRealPath('public') . $fileName;
    $fileName = substr($fileName, 0, 1) != '/' ? 'javascript/' . $fileName : substr($fileName, 1);
    //  fix slashes
    $filePath = str_replace('\\', DIRECTORY_SEPARATOR, $filePath);
    $filePath = str_replace('/', DIRECTORY_SEPARATOR, $filePath);
    if (isset($params['path'])) {
        $filePath = $params['path'];
    }
    if (!is_file($filePath) || substr($filePath, -3) != '.js') {
        return;
    }
    if (isset($params['inline']) && $params['inline'] == 'true') {
        return '<script src="' . str_replace(DIRECTORY_SEPARATOR, '/', $fileName) . '?' . filemtime($filePath) . '" type="text/javascript"></script>' . "\n";
    } else {
        $includedJavascriptTimestamp = $smarty->getGlobal("INCLUDED_JAVASCRIPT_TIMESTAMP");
        if (!($includedJavascriptFiles = $smarty->getGlobal('INCLUDED_JAVASCRIPT_FILES'))) {
            $includedJavascriptFiles = array();
        }
        if (isset($includedJavascriptFiles[$filePath])) {
            if (!isset($params['front'])) {
                return false;
            } else {
                unset($includedJavascriptFiles[$filePath]);
            }
        }
        $fileMTime = filemtime($filePath);
        if ($fileMTime > (int) $includedJavascriptTimestamp) {
            $smarty->setGlobal('INCLUDED_JAVASCRIPT_TIMESTAMP', $fileMTime);
        }
        if (isset($params['front'])) {
            $includedJavascriptFiles = array_merge(array($filePath => $fileName), $includedJavascriptFiles);
        } else {
            $includedJavascriptFiles[$filePath] = $fileName;
        }
        $smarty->setGlobal('INCLUDED_JAVASCRIPT_FILES', $includedJavascriptFiles);
    }
    foreach ($smarty->getApplication()->getConfigContainer()->getFilesByRelativePath('public/' . $fileName, true) as $file) {
        if (realpath($file) == realpath($filePath)) {
            continue;
        }
        $file = substr($file, strlen(ClassLoader::getRealPath('public')));
        $params['file'] = $file;
        smarty_function_includeJs($params, $smarty);
    }
}
Example #2
0
/**
 * ...
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 *
 * @package application.helper.smarty
 * @author Integry Systems
 */
function smarty_function_compiledJs($params, Smarty_Internal_Template $smarty)
{
    $includedJavascriptTimestamp = $smarty->getGlobal("INCLUDED_JAVASCRIPT_TIMESTAMP");
    $includedJavascriptFiles = $smarty->getGlobal("INCLUDED_JAVASCRIPT_FILES");
    $app = $smarty->getApplication();
    if ($includedJavascriptFiles && isset($params['glue']) && $params['glue'] == 'true' && !$smarty->getApplication()->isDevMode() && !$smarty->getApplication()->isTemplateCustomizationMode()) {
        $request = $app->getRequest();
        if (isset($params['nameMethod']) && 'hash' == $params['nameMethod']) {
            $names = array_keys($includedJavascriptFiles);
            sort($names);
            $compiledFileName = md5(implode("\n", $names)) . '.js';
        } else {
            $compiledFileName = $request->getControllerName() . '-' . $request->getActionName() . '.js';
        }
        $compiledFilePath = ClassLoader::getRealPath('public.cache.javascript.') . $compiledFileName;
        $baseDir = ClassLoader::getRealPath('public.javascript.');
        $compiledFileTimestamp = 0;
        if (!is_file($compiledFilePath) || filemtime($compiledFilePath) < $includedJavascriptTimestamp) {
            if (!is_dir(ClassLoader::getRealPath('public.cache.javascript'))) {
                mkdir(ClassLoader::getRealPath('public.cache.javascript'), 0777, true);
            }
            // compile
            $compiledFileContent = "";
            $compiledFilesList = array();
            foreach ($includedJavascriptFiles as $jsFile => $fileName) {
                $compiledFileContent .= "\n\n\n/***************************************************\n";
                $compiledFileContent .= " * " . str_replace($baseDir, '', $jsFile) . "\n";
                $compiledFileContent .= " ***************************************************/\n\n";
                $compiledFileContent .= file_get_contents($jsFile);
                $compiledFilesList[] = basename($jsFile);
            }
            file_put_contents($compiledFilePath, $compiledFileContent);
            if (function_exists('gzencode')) {
                file_put_contents($compiledFilePath . '.gz', gzencode($compiledFileContent, 9));
            }
        }
        $compiledFileTimestamp = filemtime($compiledFilePath);
        return '<script src="' . $app->getPublicUrl('gzip.php') . '?file=' . $compiledFileName . '&amp;time=' . $compiledFileTimestamp . '" type="text/javascript"></script>';
    } else {
        if ($includedJavascriptFiles) {
            $includeString = "";
            $publicPath = ClassLoader::getRealPath('public.');
            foreach ($includedJavascriptFiles as $path => $jsFile) {
                $urlPath = str_replace('\\', '/', str_replace($publicPath, '', $jsFile));
                $includeString .= '<script src="' . $app->getPublicUrl($urlPath) . '?' . filemtime($path) . '" type="text/javascript"></script>' . "\n";
            }
            return $includeString;
        }
    }
}
Example #3
0
/**
 * ...
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 *
 * @package application.helper.smarty
 * @author Integry Systems
 */
function smarty_function_includeCss($params, Smarty_Internal_Template $smarty)
{
    $fileName = $params['file'];
    $filePath = substr($fileName, 0, 1) != '/' ? ClassLoader::getRealPath('public.stylesheet.') . $fileName : ClassLoader::getRealPath('public') . $fileName;
    // fix slashes
    $filePath = str_replace('\\', DIRECTORY_SEPARATOR, $filePath);
    $filePath = str_replace('/', DIRECTORY_SEPARATOR, $filePath);
    if (!is_file($filePath) && !isset($params['external']) || substr($filePath, -4) != '.css') {
        return;
    }
    $css = CssFile::getInstanceFromPath($filePath, $smarty->getApplication()->getTheme());
    $origFileName = $fileName;
    if ($css->isPatched()) {
        $filePath = $css->getPatchedFilePath();
        $fileName = $css->getPatchedFileRelativePath();
    }
    if (isset($params['inline']) && $params['inline'] == 'true') {
        $path = 'stylesheet/' . str_replace(DIRECTORY_SEPARATOR, '/', $fileName) . '?' . filemtime($filePath);
        return '<link href="' . $path . '" media="screen" rel="Stylesheet" type="text/css" />' . "\n";
    } else {
        if (isset($params['external'])) {
            $external = (array) $smarty->getGlobal('INCLUDED_STYLESHEET_FILES_EXTERNAL');
            $external[] = $fileName;
            $smarty->setGlobal('INCLUDED_STYLESHEET_FILES_EXTERNAL', $external);
        } else {
            $includedStylesheetTimestamp = $smarty->getGlobal('INCLUDED_STYLESHEET_TIMESTAMP');
            if (!($includedStylesheetFiles = $smarty->getGlobal('INCLUDED_STYLESHEET_FILES'))) {
                $includedStylesheetFiles = array();
            }
            if (in_array($filePath, $includedStylesheetFiles)) {
                if (isset($params['front'])) {
                    unset($includedStylesheetFiles[array_search($filePath, $includedStylesheetFiles)]);
                } else {
                    return;
                }
            }
            $fileMTime = filemtime($filePath);
            if ($fileMTime > (int) $includedStylesheetTimestamp) {
                $smarty->setGlobal('INCLUDED_STYLESHEET_TIMESTAMP', $fileMTime);
            }
            if (isset($params['front'])) {
                array_unshift($includedStylesheetFiles, $filePath);
            } else {
                if (isset($params['last'])) {
                    $includedStylesheetFiles['x' . (count($includedStylesheetFiles) + 200) * (int) $params['last']] = $filePath;
                } else {
                    array_push($includedStylesheetFiles, $filePath);
                }
            }
            $smarty->setGlobal('INCLUDED_STYLESHEET_FILES', $includedStylesheetFiles);
        }
    }
    foreach ($smarty->getApplication()->getConfigContainer()->getFilesByRelativePath('public/stylesheet/' . $origFileName, true) as $file) {
        if (realpath($file) == realpath($filePath)) {
            continue;
        }
        $file = substr($file, strlen(ClassLoader::getRealPath('public')));
        $params['file'] = $file;
        smarty_function_includeCss($params, $smarty);
    }
}