1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Theme\Plugins;
13:
14: use Xoops\Core\Kernel\Handlers\XoopsBlock;
15: use Xoops\Core\Theme\PluginAbstract;
16: use Xoops\Core\Theme\XoopsTheme;
17: use Xoops\Core\XoopsTpl;
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
29: class Blocks extends PluginAbstract
30: {
31: 32: 33:
34: public $theme = false;
35:
36: 37: 38:
39: public $blocks = array();
40:
41: 42: 43: 44: 45:
46: public function xoInit()
47: {
48: $this->retrieveBlocks();
49: if ($this->theme) {
50: $this->theme->template->assignByRef('xoBlocks', $this->blocks);
51: $this->theme->template->assign('xoops_showlblock', !empty($this->blocks['canvas_left']));
52: $this->theme->template->assign('xoops_showrblock', !empty($this->blocks['canvas_right']));
53: $this->theme->template->assign(
54: 'xoops_showcblock',
55: !empty($this->blocks['page_topcenter'])
56: || !empty($this->blocks['page_topleft'])
57: || !empty($this->blocks['page_topright'])
58: );
59: }
60: return true;
61: }
62:
63: 64: 65: 66: 67: 68: 69:
70: public function preRender($zone = '')
71: {
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function postRender($zone = '')
82: {
83: }
84:
85: 86: 87: 88: 89:
90: public function retrieveBlocks()
91: {
92: $xoops = \Xoops::getInstance();
93:
94: if ($xoops->isModule()) {
95: $mid = $xoops->module->getVar('mid');
96: $isStart = (substr($_SERVER['PHP_SELF'], -9) === 'index.php'
97: && $xoops->getConfig('startpage') == $xoops->module->getVar('dirname')
98: && empty($_SERVER['QUERY_STRING']));
99: } else {
100: $mid = 0;
101: $isStart = $xoops->getOption('show_cblock');
102: }
103:
104: $groups = $xoops->getUserGroups();
105:
106: $oldzones = array(
107: XOOPS_SIDEBLOCK_LEFT => 'canvas_left', XOOPS_SIDEBLOCK_RIGHT => 'canvas_right',
108: XOOPS_CENTERBLOCK_LEFT => 'page_topleft', XOOPS_CENTERBLOCK_CENTER => 'page_topcenter',
109: XOOPS_CENTERBLOCK_RIGHT => 'page_topright', XOOPS_CENTERBLOCK_BOTTOMLEFT => 'page_bottomleft',
110: XOOPS_CENTERBLOCK_BOTTOM => 'page_bottomcenter', XOOPS_CENTERBLOCK_BOTTOMRIGHT => 'page_bottomright'
111: );
112: foreach ($oldzones as $zone) {
113: $this->blocks[$zone] = array();
114: }
115: $backup = array();
116: if ($this->theme) {
117: $template = $this->theme->template;
118: $backup = array(
119: $template->caching, $template->cache_lifetime
120: );
121: } else {
122: $template = null;
123: $template = new XoopsTpl();
124: }
125: $block_handler = $xoops->getHandlerBlock();
126: $block_arr = $block_handler->getAllByGroupModule($groups, $mid, $isStart, XOOPS_BLOCK_VISIBLE);
127: $xoops->events()->triggerEvent(
128: 'core.class.theme_blocks.retrieveBlocks',
129: array(&$this, &$template, &$block_arr)
130: );
131: foreach ($block_arr as $block) {
132:
133: $side = $oldzones[$block->getVar('side')];
134: if ($var = $this->buildBlock($block, $template)) {
135: $this->blocks[$side][$var["id"]] = $var;
136: }
137: }
138: if ($this->theme) {
139: list ($template->caching, $template->cache_lifetime) = $backup;
140: }
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function generateCacheId($cache_id)
151: {
152: if ($this->theme) {
153: $cache_id = $this->theme->generateCacheId($cache_id);
154: }
155: return $cache_id;
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165:
166: public function buildBlock(XoopsBlock $xobject, XoopsTpl $template)
167: {
168: $xoops = \Xoops::getInstance();
169:
170:
171: $dirname = $xobject->getVar('dirname');
172: $block = array(
173: 'id' => $xobject->getVar('bid'), 'module' => $dirname, 'title' => $xobject->getVar('title'),
174: 'weight' => $xobject->getVar('weight'), 'lastmod' => $xobject->getVar('last_modified')
175: );
176:
177: $bcachetime = (int)($xobject->getVar('bcachetime'));
178: if (empty($bcachetime)) {
179: $template->caching = 0;
180: } else {
181: $template->caching = 2;
182: $template->cache_lifetime = $bcachetime;
183: }
184: $template->setCompileId($dirname);
185: $tplName = ($tplName = $xobject->getVar('template'))
186: ? "block:{$dirname}/{$tplName}"
187: : "module:system/system_block_dummy.tpl";
188:
189:
190: $cacheid = $this->generateCacheId('blk_' . $xobject->getVar('bid'));
191:
192: $xoops->events()->triggerEvent(
193: 'core.themeblocks.buildblock.start',
194: array($xobject, $template->isCached($tplName, $cacheid))
195: );
196:
197: if (!$bcachetime || !$template->isCached($tplName, $cacheid)) {
198:
199:
200: $old = array();
201: if ($this->theme && $bcachetime) {
202: foreach ($this->theme->metas as $type => $value) {
203: $old[$type] = $this->theme->metas[$type];
204: }
205: }
206:
207:
208: if ($bresult = $xobject->buildBlock()) {
209: $template->assign('block', $bresult);
210: $block['content'] = $template->fetch($tplName, $cacheid);
211: } else {
212: $block = false;
213: }
214:
215:
216: if ($this->theme && $bcachetime) {
217: $metas = array();
218: foreach ($this->theme->metas as $type => $value) {
219: $dif = \Xoops\Utils::arrayRecursiveDiff($this->theme->metas[$type], $old[$type]);
220: if (count($dif)) {
221: $metas[$type] = $dif;
222: }
223: }
224: if (count($metas)) {
225: \Xoops\Cache::write($cacheid, $metas);
226: }
227: }
228: } else {
229: $block['content'] = $template->fetch($tplName, $cacheid);
230: }
231:
232:
233: if ($this->theme && $bcachetime) {
234: if ($metas = \Xoops\Cache::read($cacheid)) {
235: foreach ($metas as $type => $value) {
236: $this->theme->metas[$type] = array_merge($this->theme->metas[$type], $metas[$type]);
237: }
238: }
239: }
240: $template->setCompileId();
241: return $block;
242: }
243: }
244: