XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
theme_blocks.php
Go to the documentation of this file.
1 <?php
23 defined('XOOPS_ROOT_PATH') or die('Restricted access');
24 
25 include_once $GLOBALS['xoops']->path('class/xoopsblock.php');
26 include_once $GLOBALS['xoops']->path('class/template.php');
27 
37 {
38  var $theme = false;
39  var $blocks = array();
40 
47  function xoInit($options = array())
48  {
49  $this->retrieveBlocks();
50  if ($this->theme) {
51  $this->theme->template->assign_by_ref('xoBlocks', $this->blocks);
52  }
53  return true;
54  }
55 
59  function preRender($zone = '')
60  {
61  }
65  function postRender($zone = '')
66  {
67  }
68 
74  function retrieveBlocks()
75  {
76  global $xoopsConfig;
78 
79  $startMod = ($xoopsConfig['startpage'] == '--') ? 'system' : $xoopsConfig['startpage'];
80  if (isset($GLOBALS['xoopsModule']) && is_object($GLOBALS['xoopsModule'])) {
81  list ($mid, $dirname) = array(
82  $GLOBALS['xoopsModule']->getVar('mid') ,
83  $GLOBALS['xoopsModule']->getVar('dirname'));
84  $isStart = (substr($_SERVER['PHP_SELF'], - 9) == 'index.php' && $xoopsConfig['startpage'] == $dirname && empty($_SERVER['QUERY_STRING']));
85  } else {
86  list ($mid, $dirname) = array(
87  0 ,
88  'system');
89  $isStart = !empty($GLOBALS['xoopsOption']['show_cblock']);
90  }
91 
92  $groups = (isset($GLOBALS['xoopsUser']) && is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getGroups() : array(
93  XOOPS_GROUP_ANONYMOUS);
94 
95  $oldzones = array(
96  XOOPS_SIDEBLOCK_LEFT => 'canvas_left' ,
97  XOOPS_SIDEBLOCK_RIGHT => 'canvas_right' ,
98  XOOPS_CENTERBLOCK_LEFT => 'page_topleft' ,
99  XOOPS_CENTERBLOCK_CENTER => 'page_topcenter' ,
100  XOOPS_CENTERBLOCK_RIGHT => 'page_topright' ,
101  XOOPS_CENTERBLOCK_BOTTOMLEFT => 'page_bottomleft' ,
102  XOOPS_CENTERBLOCK_BOTTOM => 'page_bottomcenter' ,
103  XOOPS_CENTERBLOCK_BOTTOMRIGHT => 'page_bottomright');
104  foreach ($oldzones as $zone) {
105  $this->blocks[$zone] = array();
106  }
107  if ($this->theme) {
108  $template =& $this->theme->template;
109  $backup = array(
110  $template->caching ,
111  $template->cache_lifetime);
112  } else {
113  $template = null;
114  $template = new XoopsTpl();
115  }
116  $xoopsblock = new XoopsBlock();
117  $block_arr = array();
118  $block_arr = $xoopsblock->getAllByGroupModule($groups, $mid, $isStart, XOOPS_BLOCK_VISIBLE);
119  $xoopsPreload->triggerEvent('core.class.theme_blocks.retrieveBlocks', array(&$this, &$template, &$block_arr));
120  foreach ($block_arr as $block) {
121  $side = $oldzones[$block->getVar('side')];
122  if ($var = $this->buildBlock($block, $template)) {
123  $this->blocks[$side][$var["id"]] = $var;
124  }
125  }
126  if ($this->theme) {
127  list ($template->caching, $template->cache_lifetime) = $backup;
128  }
129  }
130 
137  function generateCacheId($cache_id)
138  {
139  if ($this->theme) {
140  $cache_id = $this->theme->generateCacheId($cache_id);
141  }
142  return $cache_id;
143  }
144 
152  function buildBlock($xobject, &$template)
153  {
154  // The lame type workaround will change
155  // bid is added temporarily as workaround for specific block manipulation
156  $block = array(
157  'id' => $xobject->getVar('bid') ,
158  'module' => $xobject->getVar('dirname') ,
159  'title' => $xobject->getVar('title') ,
160  // 'name' => strtolower( preg_replace( '/[^0-9a-zA-Z_]/', '', str_replace( ' ', '_', $xobject->getVar( 'name' ) ) ) ),
161  'weight' => $xobject->getVar('weight') ,
162  'lastmod' => $xobject->getVar('last_modified'));
163 
164  $bcachetime = intval($xobject->getVar('bcachetime'));
165  if (empty($bcachetime)) {
166  $template->caching = 0;
167  } else {
168  $template->caching = 2;
169  $template->cache_lifetime = $bcachetime;
170  }
171  $template->setCompileId($xobject->getVar('dirname', 'n'));
172  $tplName = ($tplName = $xobject->getVar('template')) ? "db:$tplName" : 'db:system_block_dummy.html';
173  $cacheid = $this->generateCacheId('blk_' . $xobject->getVar('bid'));
174 
176  if (!$bcachetime || !$template->is_cached($tplName, $cacheid)) {
177 
178  //Get theme metas
179  if ($this->theme && $bcachetime) {
180  foreach ($this->theme->metas as $type => $value) {
181  $old[$type] = $this->theme->metas[$type];
182  }
183  }
184 
185  //build block
186  $xoopsLogger->addBlock($xobject->getVar('name'));
187  if ($bresult = $xobject->buildBlock()) {
188  $template->assign('block', $bresult);
189  $block['content'] = $template->fetch($tplName, $cacheid);
190  } else {
191  $block = false;
192  }
193 
194  //check if theme added new metas
195  if ($this->theme && $bcachetime) {
196  $metas = array();
197  foreach ($this->theme->metas as $type => $value) {
198  $dif = array_diff_assoc($this->theme->metas[$type], $old[$type]);
199  if (count($dif)) {
200  $metas[$type] = $dif;
201  }
202  }
203  if (count($metas)) {
204  xoops_load('xoopscache');
205  $cache =& XoopsCache::getInstance();
206  $cache->write($cacheid, $metas);
207  }
208  }
209  } else {
210  $xoopsLogger->addBlock($xobject->getVar('name'), true, $bcachetime);
211  $block['content'] = $template->fetch($tplName, $cacheid);
212  }
213 
214  //add block cached metas
215  if ($this->theme && $bcachetime) {
216  xoops_load('xoopscache');
217  $cache =& XoopsCache::getInstance();
218  if ($metas = $cache->read($cacheid)) {
219  foreach ($metas as $type => $value) {
220  $this->theme->metas[$type] = array_merge($this->theme->metas[$type], $metas[$type]);
221  }
222  }
223  }
224 
225  $template->setCompileId();
226 
227  return $block;
228  }
229 }
230 
231 ?>