XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
xoopsblock.php
Go to the documentation of this file.
1 <?php
22 defined('XOOPS_ROOT_PATH') or die('Restricted access');
23 
24 include_once $GLOBALS['xoops']->path( 'kernel/object.php' );
25 
26 class XoopsBlock extends XoopsObject
27 {
28  var $db;
29 
30  function XoopsBlock($id = null)
31  {
33  $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
34  $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
35  $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
36  $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
37  $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
38  //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
39  $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
40  $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
41  $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
42  $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
43  $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
44  // The block_type is in a mess, let's say:
45  // S - generated by system module
46  // M - generated by a non-system module
47  // C - Custom block
48  // D - cloned system/module block
49  // E - cloned custom block, DON'T use it
50  $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
51  $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
52  $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
53 
54  $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
55  $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
56  $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
57  $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
58 
59  $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
60  $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
61  $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
62 
63  if (!empty($id)) {
64  if (is_array($id)) {
65  $this->assignVars($id);
66  } else {
67  $this->load(intval($id));
68  }
69  }
70  }
71 
77  function load($id)
78  {
79  $id = intval($id);
80  $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks') . ' WHERE bid = ' . $id;
81  $arr = $this->db->fetchArray($this->db->query($sql));
82  $this->assignVars($arr);
83  }
84 
90  function store()
91  {
92  if (!$this->cleanVars()) {
93  return false;
94  }
95  foreach ($this->cleanVars as $k => $v) {
96  ${$k} = $v;
97  }
98  if (empty($bid)) {
99  $bid = $this->db->genId($this->db->prefix("newblocks") . "_bid_seq");
100  $sql = sprintf("INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, %s, %s, %s, %s, %u, %u, %u, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u)", $this->db->prefix('newblocks'), $bid, $mid, $func_num, $this->db->quoteString($options), $this->db->quoteString($name), $this->db->quoteString($title), $this->db->quoteString($content), $side, $weight, $visible, $this->db->quoteString($block_type), $this->db->quoteString($c_type), 1, $this->db->quoteString($dirname), $this->db->quoteString($func_file), $this->db->quoteString($show_func), $this->db->quoteString($edit_func), $this->db->quoteString($template), $bcachetime, time());
101  } else {
102  $sql = "UPDATE " . $this->db->prefix("newblocks") . " SET options=" . $this->db->quoteString($options);
103  // a custom block needs its own name
104  if ($this->isCustom() /* in_array( $block_type , array( 'C' , 'E' ) ) */) {
105  $sql .= ", name=" . $this->db->quoteString($name);
106  }
107  $sql .= ", isactive=" . $isactive . ", title=" . $this->db->quoteString($title) . ", content=" . $this->db->quoteString($content) . ", side=" . $side . ", weight=" . $weight . ", visible=" . $visible . ", c_type=" . $this->db->quoteString($c_type) . ", template=" . $this->db->quoteString($template) . ", bcachetime=" . $bcachetime . ", last_modified=" . time() . " WHERE bid=" . $bid;
108  }
109  if (!$this->db->query($sql)) {
110  $this->setErrors("Could not save block data into database");
111  return false;
112  }
113  if (empty($bid)) {
114  $bid = $this->db->getInsertId();
115  }
116  return $bid;
117  }
118 
124  function delete()
125  {
126  $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $this->getVar('bid'));
127  if (! $this->db->query($sql)) {
128  return false;
129  }
130  $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'block_read' AND gperm_itemid = %u AND gperm_modid = 1", $this->db->prefix('group_permission'), $this->getVar('bid'));
131  $this->db->query($sql);
132  $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $this->getVar('bid'));
133  $this->db->query($sql);
134  return true;
135  }
136 
144  function getContent($format = 's', $c_type = 't')
145  {
146  switch ($format) {
147  case 's':
148  // check the type of content
149  // H : custom HTML block
150  // P : custom PHP block
151  // S : use text sanitizater (smilies enabled)
152  // T : use text sanitizater (smilies disabled)
153  if ($c_type == 'H') {
154  return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
155  } else if ($c_type == 'P') {
156  ob_start();
157  echo eval($this->getVar('content', 'n'));
158  $content = ob_get_contents();
159  ob_end_clean();
160  return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content);
161  } else if ($c_type == 'S') {
163  $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
164  return $myts->displayTarea($content, 1, 1);
165  } else {
167  $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
168  return $myts->displayTarea($content, 1, 0);
169  }
170  break;
171  case 'e':
172  return $this->getVar('content', 'e');
173  break;
174  default:
175  return $this->getVar('content', 'n');
176  break;
177  }
178  }
179 
185  function buildBlock()
186  {
188  $block = array();
189  if (!$this->isCustom()) {
190  // get block display function
191  $show_func = $this->getVar('show_func');
192  if (!$show_func) {
193  return false;
194  }
195  if (!file_exists($func_file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
196  return false;
197  }
198  // must get lang files b4 including the file
199  // some modules require it for code that is outside the function
200  xoops_loadLanguage('blocks', $this->getVar('dirname'));
201  include_once $func_file;
202 
203  if (function_exists($show_func)) {
204  // execute the function
205  $options = explode('|', $this->getVar('options'));
206  $block = $show_func($options);
207  if (!$block) {
208  return false;
209  }
210  } else {
211  return false;
212  }
213  } else {
214  // it is a custom block, so just return the contents
215  $block['content'] = $this->getContent('s', $this->getVar('c_type'));
216  if (empty($block['content'])) {
217  return false;
218  }
219  }
220  return $block;
221  }
222 
223  /*
224  * Aligns the content of a block
225  * If position is 0, content in DB is positioned
226  * before the original content
227  * If position is 1, content in DB is positioned
228  * after the original content
229  */
230  function buildContent($position, $content = "", $contentdb = "")
231  {
232  if ($position == 0) {
233  $ret = $contentdb . $content;
234  } else if ($position == 1) {
235  $ret = $content . $contentdb;
236  }
237  return $ret;
238  }
239 
247  function buildTitle($originaltitle, $newtitle = '')
248  {
249  if ($newtitle != '') {
250  $ret = $newtitle;
251  } else {
252  $ret = $originaltitle;
253  }
254  return $ret;
255  }
256 
262  function isCustom()
263  {
264  return in_array($this->getVar('block_type'), array(
265  'C' ,
266  'E'));
267  }
268 
274  function getOptions()
275  {
276  global $xoopsConfig;
277  if (!$this->isCustom()) {
278  $edit_func = $this->getVar('edit_func');
279  if (!$edit_func) {
280  return false;
281  }
282  if (file_exists($GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
283  if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/blocks.php'))) {
284  include_once $file;
285  } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/english/blocks.php'))) {
286  include_once $file;
287  }
288  include_once $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'));
289  $options = explode("|", $this->getVar("options"));
290  $edit_form = $edit_func($options);
291  if (!$edit_form) {
292  return false;
293  }
294  return $edit_form;
295  } else {
296  return false;
297  }
298  } else {
299  return false;
300  }
301  }
302 
317  function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = "b.weight,b.bid", $isactive = 1)
318  {
320  $ret = array();
321  if (!$asobject) {
322  $sql = 'SELECT b.bid ';
323  } else {
324  $sql = 'SELECT b.* ';
325  }
326  $sql .= "FROM " . $db->prefix("newblocks") . " b LEFT JOIN " . $db->prefix("group_permission") . " l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1";
327  if (is_array($groupid)) {
328  $sql .= " AND (l.gperm_groupid=" . $groupid[0] . "";
329  $size = count($groupid);
330  if ($size > 1) {
331  for($i = 1; $i < $size; $i ++) {
332  $sql .= " OR l.gperm_groupid=" . $groupid[$i] . "";
333  }
334  }
335  $sql .= ")";
336  } else {
337  $sql .= " AND l.gperm_groupid=" . $groupid . "";
338  }
339  $sql .= " AND b.isactive=" . $isactive;
340  if (isset($side)) {
341  // get both sides in sidebox? (some themes need this)
342  if ($side == XOOPS_SIDEBLOCK_BOTH) {
343  $side = "(b.side=0 OR b.side=1)";
344  } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
345  $side = "(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )";
346  } else {
347  $side = "b.side=" . $side;
348  }
349  $sql .= " AND " . $side;
350  }
351  if (isset($visible)) {
352  $sql .= " AND b.visible=$visible";
353  }
354  $sql .= " ORDER BY $orderby";
355  $result = $db->query($sql);
356  $added = array();
357  while ($myrow = $db->fetchArray($result)) {
358  if (!in_array($myrow['bid'], $added)) {
359  if (!$asobject) {
360  $ret[] = $myrow['bid'];
361  } else {
362  $ret[] = new XoopsBlock($myrow);
363  }
364  array_push($added, $myrow['bid']);
365  }
366  }
367  return $ret;
368  }
369 
380  function getAllBlocks($rettype = "object", $side = null, $visible = null, $orderby = "side,weight,bid", $isactive = 1)
381  {
383  $ret = array();
384  $where_query = " WHERE isactive=" . $isactive;
385  if (isset($side)) {
386  // get both sides in sidebox? (some themes need this)
387  if ($side == 2) {
388  $side = "(side=0 OR side=1)";
389  } elseif ($side == 6) {
390  $side = "(side=3 OR side=4 OR side=5 OR side=7 OR side=8 OR side=9)";
391  } else {
392  $side = "side=" . $side;
393  }
394  $where_query .= " AND " . $side;
395  }
396  if (isset($visible)) {
397  $where_query .= " AND visible=.". $visible;
398  }
399  $where_query .= " ORDER BY ".$orderby;
400  switch ($rettype) {
401  case "object":
402  $sql = "SELECT * FROM " . $db->prefix("newblocks") . "" . $where_query;
403  $result = $db->query($sql);
404  while ($myrow = $db->fetchArray($result)) {
405  $ret[] = new XoopsBlock($myrow);
406  }
407  break;
408  case "list":
409  $sql = "SELECT * FROM " . $db->prefix("newblocks") . "" . $where_query;
410  $result = $db->query($sql);
411  while ($myrow = $db->fetchArray($result)) {
412  $block = new XoopsBlock($myrow);
413  $title = $block->getVar("title");
414  $title = empty($title) ? $block->getVar("name") : $title;
415  $ret[$block->getVar("bid")] = $title;
416  }
417  break;
418  case "id":
419  $sql = "SELECT bid FROM " . $db->prefix("newblocks") . "" . $where_query;
420  $result = $db->query($sql);
421  while ($myrow = $db->fetchArray($result)) {
422  $ret[] = $myrow['bid'];
423  }
424  break;
425  }
426  //echo $sql;
427  return $ret;
428  }
429 
437  function getByModule($moduleid, $asobject = true)
438  {
439  $moduleid = intval($moduleid);
441  if ($asobject == true) {
442  $sql = $sql = "SELECT * FROM " . $db->prefix("newblocks") . " WHERE mid=" . $moduleid;
443  } else {
444  $sql = "SELECT bid FROM " . $db->prefix("newblocks") . " WHERE mid=" . $moduleid;
445  }
446  $result = $db->query($sql);
447  $ret = array();
448  while ($myrow = $db->fetchArray($result)) {
449  if ($asobject) {
450  $ret[] = new XoopsBlock($myrow);
451  } else {
452  $ret[] = $myrow['bid'];
453  }
454  }
455  return $ret;
456  }
457 
469  function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
470  {
471  $isactive = intval($isactive);
473  $ret = array();
474  if (isset($groupid)) {
475  $sql = "SELECT DISTINCT gperm_itemid FROM " . $db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1";
476  if (is_array($groupid)) {
477  $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')';
478  } else {
479  if (intval($groupid) > 0) {
480  $sql .= ' AND gperm_groupid=' . intval($groupid);
481  }
482  }
483  $result = $db->query($sql);
484  $blockids = array();
485  while ($myrow = $db->fetchArray($result)) {
486  $blockids[] = $myrow['gperm_itemid'];
487  }
488  if (empty($blockids)) {
489  return $blockids;
490  }
491  }
492  $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
493  $sql .= ' AND b.isactive=' . $isactive;
494  if (isset($visible)) {
495  $sql .= ' AND b.visible=' . intval($visible);
496  }
497  if (!isset($module_id)) {
498  } else if (!empty($module_id)) {
499  $sql .= ' AND m.module_id IN (0,' . intval($module_id);
500  if ($toponlyblock) {
501  $sql .= ',-1';
502  }
503  $sql .= ')';
504  } else {
505  if ($toponlyblock) {
506  $sql .= ' AND m.module_id IN (0,-1)';
507  } else {
508  $sql .= ' AND m.module_id=0';
509  }
510  }
511  if (!empty($blockids)) {
512  $sql .= ' AND b.bid IN (' . implode(',', $blockids) . ')';
513  }
514  $sql .= ' ORDER BY ' . $orderby;
515  $result = $db->query($sql);
516  while ($myrow = $db->fetchArray($result)) {
517  $block = new XoopsBlock($myrow);
518  $ret[$myrow['bid']] = & $block;
519  unset($block);
520  }
521  return $ret;
522  }
523 
534  function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
535  {
537  $ret = array();
538  $bids = array();
539  $sql = "SELECT DISTINCT(bid) from " . $db->prefix('newblocks');
540  if ($result = $db->query($sql)) {
541  while ($myrow = $db->fetchArray($result)) {
542  $bids[] = $myrow['bid'];
543  }
544  }
545  $sql = "SELECT DISTINCT(p.gperm_itemid) from " . $db->prefix('group_permission') . " p, " . $db->prefix('groups') . " g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'";
546  $grouped = array();
547  if ($result = $db->query($sql)) {
548  while ($myrow = $db->fetchArray($result)) {
549  $grouped[] = $myrow['gperm_itemid'];
550  }
551  }
552  $non_grouped = array_diff($bids, $grouped);
553  if (!empty($non_grouped)) {
554  $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
555  $sql .= ' AND b.isactive=' . intval($isactive);
556  if (isset($visible)) {
557  $sql .= ' AND b.visible=' . intval($visible);
558  }
559  if (!isset($module_id)) {
560  } else if (!empty($module_id)) {
561  $sql .= ' AND m.module_id IN (0,' . intval($module_id);
562  if ($toponlyblock) {
563  $sql .= ',-1';
564  }
565  $sql .= ')';
566  } else {
567  if ($toponlyblock) {
568  $sql .= ' AND m.module_id IN (0,-1)';
569  } else {
570  $sql .= ' AND m.module_id=0';
571  }
572  }
573  $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')';
574  $sql .= ' ORDER BY ' . $orderby;
575  $result = $db->query($sql);
576  while ($myrow = $db->fetchArray($result)) {
577  $block = new XoopsBlock($myrow);
578  $ret[$myrow['bid']] =& $block;
579  unset($block);
580  }
581  }
582  return $ret;
583  }
584 
593  function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
594  {
595  $funcNum = intval($funcNum);
596  $moduleId = intval($moduleId);
597  if ($funcNum < 1 || $moduleId < 1) {
598  // invalid query
599  return 0;
600  }
602  if (isset($showFunc)) {
603  // showFunc is set for more strict comparison
604  $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s", $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc)));
605  } else {
606  $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum);
607  }
608  if (!$result = $db->query($sql)) {
609  return 0;
610  }
611  list ($count) = $db->fetchRow($result);
612  return $count;
613  }
614 }
615 ?>