XOOPS  2.6.0
theme_blocks.php
Go to the documentation of this file.
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 
32 {
36  public $theme = false;
37 
41  public $blocks = array();
42 
48  public function xoInit()
49  {
50  $this->retrieveBlocks();
51  if ($this->theme) {
52  $this->theme->template->assignByRef('xoBlocks', $this->blocks);
53  }
54  return true;
55  }
56 
63  public function preRender($zone = '')
64  {
65  }
66 
73  public function postRender($zone = '')
74  {
75  }
76 
82  public function retrieveBlocks()
83  {
84  $xoops = Xoops::getinstance();
85 
86  if ($xoops->isModule()) {
87  $mid = $xoops->module->getVar('mid');
88  $isStart = (substr($_SERVER['PHP_SELF'], -9) == 'index.php' && $xoops->getConfig('startpage') == $xoops->module->getVar('dirname') && empty($_SERVER['QUERY_STRING']));
89  } else {
90  $mid = 0;
91  $isStart = $xoops->getOption('show_cblock');
92  }
93 
94  $groups = $xoops->getUserGroups();
95 
96  $oldzones = array(
97  XOOPS_SIDEBLOCK_LEFT => 'canvas_left', XOOPS_SIDEBLOCK_RIGHT => 'canvas_right',
98  XOOPS_CENTERBLOCK_LEFT => 'page_topleft', XOOPS_CENTERBLOCK_CENTER => 'page_topcenter',
99  XOOPS_CENTERBLOCK_RIGHT => 'page_topright', XOOPS_CENTERBLOCK_BOTTOMLEFT => 'page_bottomleft',
100  XOOPS_CENTERBLOCK_BOTTOM => 'page_bottomcenter', XOOPS_CENTERBLOCK_BOTTOMRIGHT => 'page_bottomright'
101  );
102  foreach ($oldzones as $zone) {
103  $this->blocks[$zone] = array();
104  }
105  $backup = array();
106  if ($this->theme) {
107  $template = $this->theme->template;
108  $backup = array(
109  $template->caching, $template->cache_lifetime
110  );
111  } else {
112  $template = null;
113  $template = new XoopsTpl();
114  }
115  $block_handler = $xoops->getHandlerBlock();
116  $block_arr = $block_handler->getAllByGroupModule($groups, $mid, $isStart, XOOPS_BLOCK_VISIBLE);
117  $xoops->preload()->triggerEvent('core.class.theme_blocks.retrieveBlocks', array(&$this, &$template, &$block_arr));
118  foreach ($block_arr as $block) {
119  /* @var $block XoopsBlock */
120  $side = $oldzones[$block->getVar('side')];
121  if ($var = $this->buildBlock($block, $template)) {
122  $this->blocks[$side][$var["id"]] = $var;
123  }
124  }
125  if ($this->theme) {
126  list ($template->caching, $template->cache_lifetime) = $backup;
127  }
128  }
129 
136  public function generateCacheId($cache_id)
137  {
138  if ($this->theme) {
139  $cache_id = $this->theme->generateCacheId($cache_id);
140  }
141  return $cache_id;
142  }
143 
151  public function buildBlock($xobject, &$template)
152  {
154  // The lame type workaround will change
155  // bid is added temporarily as workaround for specific block manipulation
156  $dirname = $xobject->getVar('dirname');
157  $block = array(
158  'id' => $xobject->getVar('bid'), 'module' => $dirname, 'title' => $xobject->getVar('title'),
159  'weight' => $xobject->getVar('weight'), 'lastmod' => $xobject->getVar('last_modified')
160  );
161 
162  $bcachetime = intval($xobject->getVar('bcachetime'));
163  if (empty($bcachetime)) {
164  $template->caching = 0;
165  } else {
166  $template->caching = 2;
167  $template->cache_lifetime = $bcachetime;
168  }
169  $template->setCompileId($dirname);
170  $tplName = ($tplName = $xobject->getVar('template'))
171  ? "block:{$dirname}/{$tplName}"
172  : "module:system/system_block_dummy.tpl";
173  //$tplName = str_replace('.html', '.tpl', $tplName);
174 
175  $cacheid = $this->generateCacheId('blk_' . $xobject->getVar('bid'));
176 
177  $xoops->preload()->triggerEvent('core.themeblocks.buildblock.start', array($xobject, $template->isCached($tplName, $cacheid)));
178 
179  if (!$bcachetime || !$template->isCached($tplName, $cacheid)) {
180 
181  //Get theme metas
182  $old = array();
183  if ($this->theme && $bcachetime) {
184  foreach ($this->theme->metas as $type => $value) {
185  $old[$type] = $this->theme->metas[$type];
186  }
187  }
188 
189  //build block
190  if ($bresult = $xobject->buildBlock()) {
191  $template->assign('block', $bresult);
192  $block['content'] = $template->fetch($tplName, $cacheid);
193  } else {
194  $block = false;
195  }
196 
197  //check if theme added new metas
198  if ($this->theme && $bcachetime) {
199  $metas = array();
200  foreach ($this->theme->metas as $type => $value) {
201  $dif = Xoops_Utils::arrayRecursiveDiff($this->theme->metas[$type], $old[$type]);
202  if (count($dif)) {
203  $metas[$type] = $dif;
204  }
205  }
206  if (count($metas)) {
207  Xoops_Cache::write($cacheid, $metas);
208  }
209  }
210  } else {
211  $block['content'] = $template->fetch($tplName, $cacheid);
212  }
213 
214  //add block cached metas
215  if ($this->theme && $bcachetime) {
216  if ($metas = Xoops_Cache::read($cacheid)) {
217  foreach ($metas as $type => $value) {
218  $this->theme->metas[$type] = array_merge($this->theme->metas[$type], $metas[$type]);
219  }
220  }
221  }
222  $template->setCompileId();
223  return $block;
224  }
225 }
$mid
Definition: index.php:39
const XOOPS_SIDEBLOCK_RIGHT
Definition: defines.php:23
static getInstance()
Definition: Xoops.php:160
$_SERVER['REQUEST_URI']
const XOOPS_CENTERBLOCK_BOTTOMLEFT
Definition: defines.php:29
const XOOPS_CENTERBLOCK_LEFT
Definition: defines.php:25
static read($key)
Definition: Legacy.php:94
$xoops
Definition: admin.php:25
const XOOPS_BLOCK_VISIBLE
Definition: defines.php:33
const XOOPS_CENTERBLOCK_RIGHT
Definition: defines.php:26
const XOOPS_SIDEBLOCK_LEFT
Definition: defines.php:22
$type
Definition: misc.php:33
$groups
static write($key, $value, $duration=0)
Definition: Legacy.php:76
$dirname
Definition: backend.php:38
$var
Definition: userinfo.php:125
const XOOPS_CENTERBLOCK_BOTTOM
Definition: defines.php:31
const XOOPS_CENTERBLOCK_CENTER
Definition: defines.php:27
buildBlock($xobject, &$template)
static arrayRecursiveDiff(array $aArray1, array $aArray2)
Definition: Utils.php:77
const XOOPS_CENTERBLOCK_BOTTOMRIGHT
Definition: defines.php:30