<?php

/**
 * Recently added pages.
 *
 * This tag loads last added pages and assigns their data to a template variable.
 *
 * Usage example:
 * <code>
 * {recently_added limit=5 varname=page}
 *     <h1>{$page.title}</h1>
 *     <p>{$page.content}</p>
 * {/recently_added}
 * </code>
 *
 * Parameters (both optional):
 * - limit - Number of pages to select.
 * - varname - Name of the template variable to create.
 *
 * Items of the created variable:
 * - title
 * - content - Precompiled.
 * - url
 * - created_date - Timestamp.
 * - modified_date - Dtto.
 * 
 * @param array $params 
 * @param string|null $content 
 * @param Smarty $smarty 
 * @param bool $repeat 
 * @access public
 * @return string
 */
function smarty_cms_block_recently_added($params, $content, &$smarty, &$repeat) {
    global $gCms;
    static $result;

    // Set default parameters.
    $params += array(
        "limit" => 5,
        "varname" => "page"
    );

    // The first iteration.
    if (is_null($content)) {

        // Perform the query.
        $db = $gCms->db;
        $query = "SELECT content_id
            FROM " . cms_db_prefix() . "content
            WHERE (type='content' OR type='link')
            AND active = 1 AND show_in_menu = 1
            
            ORDER BY create_date DESC
            LIMIT $params[limit]";
        $result = $db->Execute($query);
        if (!$result) {
            throw new Exception($db->ErrorMsg());
        }
    }


    if ($pageRow = $result->FetchRow()) {
        $repeat = true;
        $cmsContent = $gCms
            ->GetHierarchyManager()
            ->GetNodeById($pageRow["content_id"])
            ->GetContent();
        
        $page = array(
            "title" => $cmsContent->Name(),
            "content" => $cmsContent->GetPropertyValue("content_en"),
            "url" => $cmsContent->GetUrl(),
            "created_date" => strtotime($cmsContent->GetCreationDate()),
            "modified_date" => strtotime($cmsContent->GetModifiedDate())
        );

        // Precompile the content.
        foreach ($gCms->modules as $module) {
            if ($module["installed"] && $module["active"]) {
                $module["object"]->ContentPreCompile($page["content"]);
            }
        }

        // Assign the template variable.
        $smarty->assign($params["varname"], $page);
    } else {
        $result->Close();
    }

    return $content;
}



function smarty_cms_help_function_recently_added() {
    ?><h3>What does this do?</h3>
<p>This tag loads last added pages and assigns their data to a template variable.</p>
<h3>How do I use it?</h3>
<p>Usage example:</p>
<pre><code>
{recently_added}
    &lt;h1&gt;{$page.title}&lt;/h1&gt;
    &lt;p&gt;{$page.content}&lt;/p&gt;
{/recently_added}
</code></pre>
 
<h3>What parameters does this take?</h3>
<ul>
    <li><b>limit</b> - Number of pages to select.</li>
    <li><b>varname</b> - Name of the template variable to create.</li>
</ul>

<h3>What data will it assign?</h3>
<p>Items of the created variable:</p>
<ul>
    <li>title</li>
    <li>content - Precompiled.</li>
    <li>url</li>
    <li>created_date - Timestamp.</li>
    <li>modified_date - Dtto.</li>
</ul>
    <?php
}
function smarty_cms_about_function_recently_added() {
    ?><p>Author: V&aacute;clav &Scaron;&iacute;r &lt;vaclav.sir@gmail.com&gt;</p><?php
}
