1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\XoopsTpl;
13:
14: /**
15: * Usage : just place {xoblock id=1} inside any template or theme, replace '1' with the id of the block you want to show
16: *
17: * Other options:
18: * display = 'title' -> shows just title
19: * display = 'none' -> renders the block but does not display it
20: * options = 'enter|block|options' -> overwrites block default options
21: * groups = 'enter|allowed|groups' -> overwrites block default group view permissions
22: * cache = 3600 -> overwrite cache time(in seconds)
23: *
24: * Examples:
25: * {xoblock id=1 display="title"} displays just the block title
26: * {xoblock id=1} displays just the block content
27: * {xoblock id=7 display="none"} does not display nothing but executes the block, this can go for online block or to trigger some cron block
28: * {xoblock id=600 groups="0|1" cache=20} display block just for this 2 groups and sets a cache of 20 seconds
29: * {block id=600 options="100|100|s_poweredby.gif|0"} displays block with diferent options
30: *
31: * @copyright XOOPS Project (http://xoops.org)
32: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
33: * @author trabis <lusopoemas@gmail.com>
34: * @version $Id$
35: */
36:
37: /**
38: * @param array $params
39: * @param Smarty $smarty
40: * @return bool|mixed|string
41: */
42: function smarty_function_xoblock($params, &$smarty)
43: {
44: if (!isset($params['id'])) {
45: return false;
46: }
47:
48: $xoops = Xoops::getInstance();
49:
50: $display_title = (isset($params['display']) && $params['display'] === 'title') ? true : false;
51: $display_none = (isset($params['display']) && $params['display'] === 'none') ? true : false;
52: $options = (isset($params['options'])) ? $params['options'] : false;
53: $groups = (isset($params['groups'])) ? explode('|', $params['groups']) : false;
54: $cache = (isset($params['cache'])) ? (int)($params['cache']) : false;
55:
56: $block_id = (int)($params['id']);
57:
58: $block_handler = $xoops->getHandlerBlock();
59: static $block_objs;
60: if (!isset($block_objs[$block_id])) {
61: $blockObj = $block_handler->get($block_id);
62: if (!is_object($blockObj)) {
63: return false;
64: }
65: $block_objs[$block_id] = $blockObj;
66: } else {
67: $blockObj = $block_objs[$block_id];
68: }
69: $user_groups = $xoops->getUserGroups();
70:
71: static $allowed_blocks;
72: if (count($allowed_blocks) == 0) {
73: $allowed_blocks = $block_handler->getAllBlocksByGroup($user_groups, false);
74: }
75:
76: if ($groups) {
77: if (!array_intersect($user_groups, $groups)) {
78: return false;
79: }
80: } else {
81: if (!in_array($block_id, $allowed_blocks)) {
82: return false;
83: }
84: }
85:
86: if ($options) {
87: $blockObj->setVar('options', $options);
88: }
89:
90: if ($cache) {
91: $blockObj->setVar('bcachetime', $cache);
92: }
93:
94: if ($display_title) {
95: return $blockObj->getVar('title');
96: }
97:
98: $tpl = new XoopsTpl();
99: $block_renderer = new \Xoops\Core\Theme\Plugins\Blocks();
100: $block_renderer->theme = $xoops->theme();
101: $block = $block_renderer->buildBlock($blockObj, $tpl);
102: if (!$display_none) {
103: return $block['content'];
104: }
105: return '';
106: }
107: