1<?php
2
3//  Author: Trabis
4//  URL: http://www.xuups.com
5//  E-Mail: lusopoemas@gmail.com
6//  Plugin version: 1.1
7//  Release date: 06-04-2009
8//  Usage : just place <{block id=1}> inside any template or theme, replace '1' with the id of the block you want to show
9//
10//  Other options:
11//  display = 'title' -> shows just title
12//  display = 'none' -> renders the block but does not display it
13//  options = 'enter|block|options' -> overwrites block default options
14//  groups = 'enter|allowed|groups' -> overwrites block default group view permissions
15//  cache = 3600 -> overwrite cache time(in seconds)
16//
17//  Examples:
18//  <{block id=1 display="title"}>   displays just the block title
19//  <{block id=1}>                   displays just the block content
20//  <{block id=7 display="none"}>    does not display nothing but executes the block, this can go for online block or to trigger some cron block
21//  <{block id=600 groups="0|1" cache=20}>  display block just for this 2 groups and sets a cache of 20 seconds
22//  <{block id=600 options="100|100|s_poweredby.gif|0"}> displays block with diferent options
23
24/**
25 * @param $params
26 * @param $smarty
27 *
28 * @return mixed
29 */
30function smarty_function_block($params, &$smarty)
31{
32    if (!isset($params['id'])) {
33        return null;
34    }
35
36    $display_title = (isset($params['display']) && $params['display'] === 'title');
37    $display_none  = (isset($params['display']) && $params['display'] === 'none');
38    $options       = isset($params['options']) ? $params['options'] : false;
39    $groups        = isset($params['groups']) ? explode('|', $params['groups']) : false;
40    $cache         = isset($params['cache']) ? (int)$params['cache'] : false;
41
42    $block_id = (int)$params['id'];
43
44    static $block_objs;
45    if (!isset($block_objs[$block_id])) {
46        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
47
48        $blockObj = new XoopsBlock($block_id);
49
50        if (!is_object($blockObj)) {
51            return null;
52        }
53
54        $block_objs[$block_id] = $blockObj;
55    } else {
56        $blockObj = $block_objs[$block_id];
57    }
58
59    $user_groups = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
60
61    static $allowed_blocks;
62    if (!is_array(@$allowed_blocks) || count($allowed_blocks) == 0) {
63        $allowed_blocks = XoopsBlock::getAllBlocksByGroup($user_groups, false);
64    }
65
66    if ($groups) {
67        if (!array_intersect($user_groups, $groups)) {
68            return null;
69        }
70    } else {
71        if (!in_array($block_id, $allowed_blocks)) {
72            return null;
73        }
74    }
75
76    if ($options) {
77        $blockObj->setVar('options', $options);
78    }
79
80    if ($cache) {
81        $blockObj->setVar('bcachetime', $cache);
82    }
83
84    if ($display_title) {
85        return $blockObj->getVar('title');
86    }
87
88    $xoopsLogger = XoopsLogger::getInstance();
89    $template    =& $GLOBALS['xoopsTpl'];
90
91    $bcachetime = (int)$blockObj->getVar('bcachetime');
92    if (empty($bcachetime)) {
93        $template->caching = 0;
94    } else {
95        $template->caching        = 2;
96        $template->cache_lifetime = $bcachetime;
97    }
98
99    $template->setCompileId($blockObj->getVar('dirname', 'n'));
100    $tplName = ($tplName = $blockObj->getVar('template')) ? "db:{$tplName}" : 'db:system_block_dummy.tpl';
101    $cacheid = 'blk_' . $block_id;
102
103    if (!$bcachetime || !$template->is_cached($tplName, $cacheid)) {
104        $xoopsLogger->addBlock($blockObj->getVar('name'));
105        if (!($bresult = $blockObj->buildBlock())) {
106            return null;
107        }
108        if (!$display_none) {
109            $template->assign('block', $bresult);
110            $template->display($tplName, $cacheid);
111        }
112    } else {
113        $xoopsLogger->addBlock($blockObj->getVar('name'), true, $bcachetime);
114        if (!$display_none) {
115            $template->display($tplName, $cacheid);
116        }
117    }
118    $template->setCompileId($blockObj->getVar('dirname', 'n'));
119
120    return null;
121}
122