<?php
/* 
 * block.cache.php
 * version 0.1
 *
 */
function smarty_cms_block_cache($params, $content, &$smarty, &$repeat)
{
    global $gCms;

    // set defaults
    if (!isset($params['key1'])) { $params['key1'] = ''; };
    if (!isset($params['key2'])) { $params['key2'] = ''; };
    if (!isset($params['key3'])) { $params['key3'] = ''; };
    if (!isset($params['ttl'])) { $params['ttl'] = '+1 day'; };

    // generate cache id and filename
    $cacheId = md5($params['key1'].$params['key2'].$params['key3']);
    $cacheFile = $gCms->config['root_path'] . '/tmp/cache/' . $cacheId . '.tsc';

    // opening tag
    if ($repeat) {
        // see if cache file exists and is still valid
        if (file_exists($cacheFile)) {
            $expires = strtotime($params['ttl'], filemtime($cacheFile));
            if ($expires > mktime()) {
                // if we have it in cache, show it, and exit block funtion (repeat = false)
                echo '<!-- Cache begin ' .$cacheId. ' -->';
                $repeat = false;
                readfile($cacheFile);
                echo '<!-- Cache end -->';
                return ;
            }     
        }
    } else {
        // closing tag
        // store content in cachefile for next time
        file_put_contents($cacheFile,$content);
        return $content;
    }
}

// function names are a bit of a hack, since it should be imho smarty_cms_help_block_cache
// bur lisstags php does not support block type plugin functions
function smarty_cms_help_function_cache() {
?>
<h3>What does this do? </h3>
<p>This is meant for caching parts of your website. It is intended for upscaling and speeding up your website.</p>
<p>You can put {cache} and {/cache} tags around the parts in your template or content or basically almost anywhere. Always provide a key1 parameter which should be unique to the cached element</p?

<h3>How do I use it?</h3>
<p>
    <code>
        {cache key1="menu" ttl="+1 day"}<br />
        {menu}<br />
        {/cache}<br />
    </code>
</p>
<p>or in combination with FEU... the keys make element caching per page per user possible</p>
<p>
<code>
        {cache key1="content" key2=$page_id key3=$user_id ttl="+1 week"}<br />
        {content}<br />
        {/cache}<br />
    </code>
</p>
<p>Cache can be deleted throught the cms admin
</p>
<h3>Warning</h3>
This is manual caching. It bypasses cmsms caching, ie  it doesn't look at last modified dates of content etc.
So using this function improperly might give you strange result if not used correctly.
It's best used for time consuming and query intensive elements, ie a menu. But remember that a menu is different on each page (the active element, the active parent etc). So a menu should in
general be cached on a per page basis.

<h3>What parameters does it take?</h3>
<ul>
        <li>key1 - Main cache key for this element</li>
        <li>key2 - (optional) sub key</li>
        <li>key2 - (optional) sub sub key</li>

        <li>ttl - Time to live for cached element. Default is "+1 day". see <a href="http://www.php.net/manual/en/function.strtotime.php">strtotime</a> documentation for possible values</li>
</ul>
<?php
}

function smarty_cms_about_function_cache() {
    ?>
	<p>Author: Teackele Soepboer</p>
	<p>Version: 0.1</p>
	<p>
	Change History:<br/>
	2010-01-09  Initial realease
	</p>

    <?php

}

?>