Exemple #1
0
/**
 * this function will output the URL to a given compressed and cached js file.
 * NOTE: if you use the util package, it will automatically insert the javascript/ subdir
 * 
 * @param string $pParams[ipackage] package the javascript file is in. this will default to: 'util'. e.g.: wiki
 * @param string $pParams[ifile] subdir and filename of the file you wish to pack and cache. e.g.: libs/jsfile.js
 * @param string $pParams[defer] includes defer='defer'
 * @param object $gBitSmarty 
 * @access public
 * @return URL to cached javascript file
 */
function smarty_function_jspack($pParams, &$gBitSmarty)
{
    // make sure we have a file to pack
    if (empty($pParams['ifile'])) {
        die;
    }
    if (empty($pParams['ipackage'])) {
        $pParams['ipackage'] = 'util';
    }
    // get the full path to the file we want to pack - insert javasscript/ into path when we're getting stuff in util
    $jsfile = constant(strtoupper($pParams['ipackage']) . '_PKG_PATH') . ($pParams['ipackage'] == 'util' ? 'javascript/' : '') . $pParams['ifile'];
    if (is_file($jsfile)) {
        // get a name for the cache file we're going to store
        $cachefile = $pParams['ipackage'] . '_' . str_replace('/', '_', $pParams['ifile']);
        require_once KERNEL_PKG_PATH . 'BitCache.php';
        $bitCache = new BitCache('javascript', TRUE);
        // if the file hasn't been packed and cached yet, we do that now.
        if (!$bitCache->isCached($cachefile, filemtime($jsfile))) {
            /*
             * params of the constructor :
             * $script:       the JavaScript to pack, string.
             * $encoding:     level of encoding, int or string :
             *                0,10,62,95 or 'None', 'Numeric', 'Normal', 'High ASCII'.
             *                default: 62.
             * $fastDecode:   include the fast decoder in the packed result, boolean.
             *                default : true.
             * $specialChars: if you have flagged your private and local variables
             *                in the script, boolean.
             *                default: false.
             */
            require_once UTIL_PKG_PATH . 'javascript/class.JavaScriptPacker.php';
            $packer = new JavaScriptPacker(file_get_contents($jsfile), 'Normal', TRUE, FALSE);
            $bitCache->writeCacheFile($cachefile, $packer->pack());
        }
        $defer = !empty($pParams['defer']) ? " defer='" . $pParams['defer'] . "'" : "";
        return '<script' . $defer . ' type="text/javascript" src="' . $bitCache->getCacheUrl($cachefile) . '"></script>';
    } else {
        return "<!-- " . tra('not a valid file: ') . $pParams['ifile'] . " -->";
    }
}
/**
 * Smarty {include_js} function plugin
 *
 * Type:     function
 * Name:     include_js
 * Input:
 *        - file      (required) - fully qualified file to a javascript file
 *		  - nopack	  (optional) - do not pack
 */
function smarty_function_include_js($params, &$gBitSmarty)
{
    global $gBitSystem;
    $pack = TRUE;
    foreach ($params as $key => $val) {
        switch ($key) {
            case 'file':
                $file = $val;
                break;
            case 'nopack':
                $pack = FALSE;
            default:
                break;
        }
    }
    if (is_file($file)) {
        // pack it
        if ($pack && $gBitSystem->isFeatureActive('themes_packed_js_css') && shell_exec('which java')) {
            // start up caching engine - pretend we are themes package
            $BitCache = new BitCache('themes', TRUE);
            // get a name for the cache file we're going to store
            $cachefile = md5($file) . '.js';
            // if the file hasn't been packed and cached yet, we do that now.
            if (!$BitCache->isCached($cachefile, filemtime($file))) {
                // pack and cache it
                $cachedata = shell_exec('java -jar ' . UTIL_PKG_PATH . 'yui/yuicompressor-2.4.2.jar --type js ' . $file);
                $BitCache->writeCacheFile($cachefile, $cachedata);
            }
            // update the file path with new path
            $file = $BitCache->getCacheFile($cachefile);
        }
        // get file text
        $text = fread(fopen($file, 'r'), filesize($file));
    } else {
        // dump a comment to the page
        $text = "if( typeof( console ) != undefined ){ console.log( 'There was an error trying to include js file: " . $file . ", it could not be found. Please check the file path.' ); }";
    }
    return $text;
}
| colspan="2" | error
|- class="warning"
| colspan="2" | warning
|- class="info"
| colspan="2" | info
|}'), 'Example 4' => array('data' => '{| class="table" style="background:yellow;color:green"
|+ Table with many colours
|-
| abc
| colspan="2" style="text-align:center;background:lightblue;" | defghi
|- style="background:red;color:white"
| jkl
| mno
| pqr
|-
| style="font-weight:bold" | stu
| style="background:silver" | vwx
| yz
|}'));
    // parse tables
    foreach ($mediawiki as $title => $example) {
        if (empty($example['result'])) {
            $example['format_guid'] = 'tikiwiki';
            $mediawiki[$title]['result'] = LibertyContent::parseData($example);
        }
    }
    $examples['tikiwiki'] = $tikiwiki;
    $examples['mediawiki'] = $mediawiki;
    $cache->writeCacheFile($cacheFile, serialize($examples));
}
$gBitSmarty->assign('examples', $examples);