XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
function.block.php
Go to the documentation of this file.
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 function smarty_function_block($params, &$smarty)
26 {
27  if (!isset($params['id'])) return;
28 
29  $display_title = (isset($params['display']) && $params['display'] == 'title') ? true : false;
30  $display_none = (isset($params['display']) && $params['display'] == 'none') ? true : false;
31  $options = (isset($params['options'])) ? $params['options'] : false;
32  $groups = (isset($params['groups'])) ? explode('|', $params['groups']) : false;
33  $cache = (isset($params['cache'])) ? intval($params['cache']) : false;
34 
35  $block_id = intval($params['id']);
36 
37  static $block_objs;
38  if (!isset($block_objs[$block_id])) {
39  include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
40 
41  $blockObj = new XoopsBlock($block_id);
42 
43  if (!is_object($blockObj)) return;
44 
45  $block_objs[$block_id] = $blockObj;
46 
47  } else {
48  $blockObj = $block_objs[$block_id];
49  }
50 
51  $user_groups = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
52 
53  static $allowed_blocks;
54  if (count($allowed_blocks) == 0) {
55  $allowed_blocks = XoopsBlock::getAllBlocksByGroup($user_groups, false);
56  }
57 
58  if ($groups) {
59  if (!array_intersect($user_groups, $groups)) return;
60  } else {
61  if (!in_array($block_id, $allowed_blocks)) return;
62  }
63 
64  if ($options) {
65  $blockObj->setVar('options', $options);
66  }
67 
68  if ($cache) {
69  $blockObj->setVar('bcachetime', $cache);
70  }
71 
72  if ($display_title) return $blockObj->getVar('title');
73 
75  $template =& $GLOBALS['xoopsTpl'];
76 
77  $bcachetime = intval($blockObj->getVar('bcachetime'));
78  if (empty($bcachetime)) {
79  $template->caching = 0;
80  } else {
81  $template->caching = 2;
82  $template->cache_lifetime = $bcachetime;
83  }
84 
85  $template->setCompileId($blockObj->getVar('dirname', 'n'));
86  $tplName = ($tplName = $blockObj->getVar('template')) ? "db:{$tplName}" : "db:system_block_dummy.html";
87  $cacheid = 'blk_' . $block_id;
88 
89  if (!$bcachetime || !$template->is_cached($tplName, $cacheid)) {
90  $xoopsLogger->addBlock($blockObj->getVar('name'));
91  if (!($bresult = $blockObj->buildBlock())) {
92  return;
93  }
94  if (!$display_none) {
95  $template->assign('block', $bresult);
96  $template->display( $tplName, $cacheid );
97  }
98  } else {
99  $xoopsLogger->addBlock($blockObj->getVar('name'), true, $bcachetime);
100  if (!$display_none) {
101  $template->display( $tplName, $cacheid );
102  }
103  }
104  $template->setCompileId($blockObj->getVar('dirname', 'n'));
105 }
106 
107 ?>