示例#1
0
/**
 *
 * Builds an archive listing looping through years. Echoes out the resulting mark-up and content
 * @param string $template
 * @param bool $return if set to true returns the output rather than echoing it
 */
function perch_blog_date_archive_years($opts = 'year_link.html', $return = false)
{
    $default_opts = array('template' => 'year_link.html', 'skip-template' => false, 'split-items' => false, 'cache' => true, 'section' => false, 'blog' => false);
    if (!is_array($opts)) {
        $opts = array('template' => $opts);
    }
    if (is_array($opts)) {
        $opts = array_merge($default_opts, $opts);
    } else {
        $opts = $default_opts;
    }
    if ($opts['skip-template'] || $opts['split-items']) {
        $return = true;
    }
    $cache = false;
    $template = $opts['template'];
    if ($opts['cache']) {
        $cache_key = 'perch_blog_date_archive_years' . md5(serialize($opts));
        $cache = PerchBlog_Cache::get_static($cache_key, 10);
        if ($opts['skip-template'] || $opts['split-items']) {
            $cache = unserialize($cache);
        }
    }
    if ($cache) {
        if ($return) {
            return $cache;
        }
        echo $cache;
        return '';
    }
    $API = new PerchAPI(1.0, 'perch_blog');
    $BlogPosts = new PerchBlog_Posts($API);
    $blogID = null;
    if ($opts['blog']) {
        $Blogs = new PerchBlog_Blogs($API);
        $Blog = $Blogs->get_one_by('blogSlug', $opts['blog']);
        if ($Blog) {
            $blogID = (int) $Blog->id();
        }
    }
    $sectionID = null;
    if ($opts['section']) {
        $Sections = new PerchBlog_Sections($API);
        $Section = $Sections->find_by_given($opts['section']);
        if (is_object($Section)) {
            $sectionID = (int) $Section->id();
        }
    }
    $years = $BlogPosts->get_years($sectionID, $blogID);
    if ($opts['skip-template'] || $opts['split-items']) {
        if ($opts['cache']) {
            PerchBlog_Cache::save_static($cache_key, serialize($years));
        }
        return $years;
    }
    $Template = $API->get('Template');
    $Template->set('blog/' . $template, 'blog');
    $r = $Template->render_group($years, true);
    if ($r != '') {
        PerchBlog_Cache::save_static($cache_key, $r);
    }
    if ($return) {
        return $r;
    }
    echo $r;
    return false;
}