XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
block.php
Go to the documentation of this file.
1 <?php
17 defined('XOOPS_ROOT_PATH') or die('Restricted access');
18 
19 require_once XOOPS_ROOT_PATH . '/kernel/block.php';
20 
27 class SystemBlock extends XoopsBlock
28 {
29  function __construct()
30  {
31  parent::__construct();
32  }
33 
34  function getForm($mode='edit')
35  {
36  if ($this->isNew()) {
38  $modules = array(-1);
39  $groups = array( XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS, XOOPS_GROUP_ADMIN );
40  $this->setVar('block_type', 'C');
41  $this->setVar('visible',1);
42  $op = 'save';
43  } else {
44  // Search modules
45  $blocklinkmodule_handler =& xoops_getmodulehandler('blocklinkmodule');
46  $criteria = new CriteriaCompo(new Criteria('block_id', $this->getVar('bid') ));
47  $blocklinkmodule = $blocklinkmodule_handler->getObjects($criteria);
48  foreach ($blocklinkmodule as $link) {
49  $modules[] = $link->getVar('module_id');
50  }
51  // Saerch perms
52  $groupperm_handler =& xoops_gethandler('groupperm');
53  $groups =& $groupperm_handler->getGroupIds('block_read', $this->getVar('bid'));
54  switch ($mode) {
55  case 'edit':
57  break;
58  case 'clone':
60  $this->setVar('bid', 0);
61  if ( $this->isCustom() ) {
62  $this->setVar('block_type', 'C');
63  } else {
64  $this->setVar('block_type', 'D');
65  }
66  break;
67  }
68  $op = 'save';
69  }
70  $form = new XoopsThemeForm($title, 'blockform', 'admin.php', 'post', true);
71  if (!$this->isNew()) {
72  $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_NAME, $this->getVar('name')));
73  }
74  // Side position
75  $side_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_TYPE, 'side', $this->getVar('side'));
76  $side_select->addOptionArray(array(
85  $form->addElement($side_select);
86  // Order
87  $form->addElement(new XoopsFormText(_AM_SYSTEM_BLOCKS_WEIGHT, 'weight', 2, 5, $this->getVar('weight')));
88  // Display
89  $form->addElement(new XoopsFormRadioYN(_AM_SYSTEM_BLOCKS_VISIBLE, 'visible', $this->getVar('visible')));
90  // Visible In
91  $mod_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_VISIBLEIN, 'modules', $modules, 5, true);
92  $module_handler =& xoops_gethandler('module');
93  $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
94  $criteria->add(new Criteria('isactive', 1));
95  $module_list = $module_handler->getList($criteria);
96  $module_list[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
97  $module_list[0] = _AM_SYSTEM_BLOCKS_ALLPAGES;
98  ksort($module_list);
99  $mod_select->addOptionArray($module_list);
100  $form->addElement($mod_select);
101  // Title
102  $form->addElement(new XoopsFormText(_AM_SYSTEM_BLOCKS_TITLE, 'title', 50, 255, $this->getVar('title')), false );
103  if ($this->isNew() || $this->isCustom()) {
104  $editor_configs=array();
105  $editor_configs["name"] ="content_block";
106  $editor_configs["value"] = $this->getVar('content', 'e');
107  $editor_configs["rows"] = 20;
108  $editor_configs["cols"] = 100;
109  $editor_configs["width"] = "100%";
110  $editor_configs["height"] = "400px";
111  $editor_configs["editor"] = xoops_getModuleOption('blocks_editor', 'system');
112  $form->addElement(new XoopsFormEditor(_AM_SYSTEM_BLOCKS_CONTENT, "content_block", $editor_configs), true);
113  if ( in_array( $editor_configs["editor"], array('dhtmltextarea','textarea' ) ) ) {
114  $ctype_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_CTYPE, 'c_type', $this->getVar('c_type'));
115  $ctype_select->addOptionArray(array(
116  'H' => _AM_SYSTEM_BLOCKS_HTML,
117  'P' => _AM_SYSTEM_BLOCKS_PHP,
120  $form->addElement($ctype_select);
121  } else {
122  $form->addElement(new XoopsFormHidden('c_type', 'H'));
123  }
124  } else {
125  if ($this->getVar('template') != '') {
126  $tplfile_handler =& xoops_gethandler('tplfile');
127  $btemplate = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $this->getVar('bid') );
128  if (count($btemplate) > 0) {
129  $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate[0]->getVar('tpl_id') . '">' . _AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
130  } else {
131  $btemplate2 = $tplfile_handler->find('default', 'block', $this->getVar('bid'));
132  if (count($btemplate2) > 0) {
133  $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate2[0]->getVar('tpl_id') . '" rel="external">' . _AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
134  }
135  }
136  }
137  if ( $this->getOptions() != false ) {
138  $form->addElement(new XoopsFormLabel(_AM_SYSTEM_BLOCKS_OPTIONS, $this->getOptions()));
139  } else {
140  $form->addElement(new XoopsFormHidden('options', $this->getVar('options')));
141  }
142  $form->addElement(new XoopsFormHidden('c_type', 'H'));
143  }
144  $cache_select = new XoopsFormSelect(_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $this->getVar('bcachetime'));
145  $cache_select->addOptionArray(array(
146  '0' => _NOCACHE,
147  '30' => sprintf(_SECONDS, 30),
148  '60' => _MINUTE,
149  '300' => sprintf(_MINUTES, 5),
150  '1800' => sprintf(_MINUTES, 30),
151  '3600' => _HOUR,
152  '18000' => sprintf(_HOURS, 5),
153  '86400' => _DAY,
154  '259200' => sprintf(_DAYS, 3),
155  '604800' => _WEEK,
156  '2592000' => _MONTH));
157  $form->addElement($cache_select);
158  // Groups
159  $form->addElement(new XoopsFormSelectGroup( _AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true) );
160 
161  $form->addElement(new XoopsFormHidden('block_type', $this->getVar('block_type')));
162  $form->addElement(new XoopsFormHidden('mid', $this->getVar('mid')));
163  $form->addElement(new XoopsFormHidden('func_num', $this->getVar('func_num')));
164  $form->addElement(new XoopsFormHidden('func_file', $this->getVar('func_file')));
165  $form->addElement(new XoopsFormHidden('show_func', $this->getVar('show_func')));
166  $form->addElement(new XoopsFormHidden('edit_func', $this->getVar('edit_func')));
167  $form->addElement(new XoopsFormHidden('template', $this->getVar('template')));
168  $form->addElement(new XoopsFormHidden('dirname', $this->getVar('dirname')));
169  $form->addElement(new XoopsFormHidden('name', $this->getVar('name')));
170  $form->addElement(new XoopsFormHidden('bid', $this->getVar('bid')));
171  $form->addElement(new XoopsFormHidden('op', $op ));
172  $form->addElement(new XoopsFormHidden('fct', 'blocksadmin'));
173  $button_tray = new XoopsFormElementTray('', '&nbsp;');
174  if ($this->isNew() || $this->isCustom()) {
175  $preview = new XoopsFormButton('', 'previewblock', _PREVIEW, 'preview');
176  $preview->setExtra("onclick=\"blocks_preview();\"");
177  $button_tray->addElement( $preview );
178  }
179  $button_tray->addElement(new XoopsFormButton('', 'submitblock', _SUBMIT, 'submit'));
180  $form->addElement($button_tray);
181 
182  return $form;
183  }
184 
190  function getOptions()
191  {
192  global $xoopsConfig;
193  if (!$this->isCustom()) {
194  $edit_func = $this->getVar('edit_func');
195  if (!$edit_func) {
196  return false;
197  }
198  if (file_exists($GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
199  if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/blocks.php'))) {
200  include_once $file;
201  } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/english/blocks.php'))) {
202  include_once $file;
203  }
204  include_once $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'));
205  $options = explode("|", $this->getVar("options"));
206  $edit_form = $edit_func($options);
207  if (!$edit_form) {
208  return false;
209  }
210  return $edit_form;
211  } else {
212  return false;
213  }
214  } else {
215  return false;
216  }
217  }
218 
219  function isCustom()
220  {
221  if ($this->getVar('block_type') == 'C') return true;
222  return false;
223  }
224 
232  function getContent($format = 's', $c_type = 'T')
233  {
234  $format = strtolower($format);
235  $c_type = strtoupper($c_type);
236  switch ($format) {
237  case 's':
238  // check the type of content
239  // H : custom HTML block
240  // P : custom PHP block
241  // S : use text sanitizater (smilies enabled)
242  // T : use text sanitizater (smilies disabled)
243  if ($c_type == 'H') {
244  return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
245  } else if ($c_type == 'P') {
246  ob_start();
247  echo eval($this->getVar('content', 'n'));
248  $content = ob_get_contents();
249  ob_end_clean();
250  return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content);
251  } else if ($c_type == 'S') {
253  $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
254  return $myts->displayTarea($content, 1, 1);
255  } else {
257  $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
258  return $myts->displayTarea($content, 1, 0);
259  }
260  break;
261  case 'e':
262  return $this->getVar('content', 'e');
263  break;
264  default:
265  return $this->getVar('content', 'n');
266  break;
267  }
268  }
269 }
270 
282 {
283  function __construct($db)
284  {
285  parent::__construct($db, 'newblocks', 'SystemBlock', 'bid', 'title');
286  }
287 
288  function insert($obj)
289  {
290  $obj->setVar('last_modified', time());
291  return parent::insert($obj);
292  }
293 
301  function getObjects($criteria = null, $id_as_key = false)
302  {
303  $ret = array();
304  $limit = $start = 0;
305  $sql = 'SELECT DISTINCT(b.bid), b.* FROM ' . $this->db->prefix('newblocks') . ' b LEFT JOIN ' . $this->db->prefix('block_module_link') . ' l ON b.bid=l.block_id';
306  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
307  $sql .= ' ' . $criteria->renderWhere();
308  $limit = $criteria->getLimit();
309  $start = $criteria->getStart();
310  }
311  $result = $this->db->query($sql, $limit, $start);
312  if (!$result) {
313  return $ret;
314  }
315 
316  while ($myrow = $this->db->fetchArray($result)) {
317  $block = new SystemBlock();
318  $block->assignVars($myrow);
319  if (!$id_as_key) {
320  $ret[] =& $block;
321  } else {
322  $ret[$myrow['bid']] = & $block;
323  }
324  unset($block);
325  }
326  return $ret;
327  }
328 
345  function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = "b.weight,b.bid", $isactive = 1)
346  {
348  $ret = array();
349  if (!$asobject) {
350  $sql = 'SELECT b.bid ';
351  } else {
352  $sql = 'SELECT b.* ';
353  }
354  $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";
355  if (is_array($groupid)) {
356  $sql .= " AND (l.gperm_groupid=" . $groupid[0] . "";
357  $size = count($groupid);
358  if ($size > 1) {
359  for($i = 1; $i < $size; $i ++) {
360  $sql .= " OR l.gperm_groupid=" . $groupid[$i] . "";
361  }
362  }
363  $sql .= ")";
364  } else {
365  $sql .= " AND l.gperm_groupid=" . $groupid . "";
366  }
367  $sql .= " AND b.isactive=" . $isactive;
368  if (isset($side)) {
369  // get both sides in sidebox? (some themes need this)
370  if ($side == XOOPS_SIDEBLOCK_BOTH) {
371  $side = "(b.side=0 OR b.side=1)";
372  } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
373  $side = "(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )";
374  } else {
375  $side = "b.side=" . $side;
376  }
377  $sql .= " AND " . $side;
378  }
379  if (isset($visible)) {
380  $sql .= " AND b.visible=$visible";
381  }
382  $sql .= " ORDER BY $orderby";
383  $result = $db->query($sql);
384  $added = array();
385  while ($myrow = $db->fetchArray($result)) {
386  if (!in_array($myrow['bid'], $added)) {
387  if (!$asobject) {
388  $ret[] = $myrow['bid'];
389  } else {
390  $ret[] = new XoopsBlock($myrow);
391  }
392  array_push($added, $myrow['bid']);
393  }
394  }
395  return $ret;
396  }
397 
398  function getBlockByPerm( $groupid )
399  {
400  $ret = array();
401  if (isset($groupid)) {
402  $sql = "SELECT DISTINCT gperm_itemid FROM " . $this->db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1";
403  if ( is_array($groupid) ) {
404  $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')';
405  } else {
406  if (intval($groupid) > 0) {
407  $sql .= ' AND gperm_groupid=' . intval($groupid);
408  }
409  }
410  $result = $this->db->query($sql);
411  $blockids = array();
412  while ( $myrow = $this->db->fetchArray( $result ) ) {
413  $blockids[] = $myrow['gperm_itemid'];
414  }
415  if (empty($blockids)) {
416  return $blockids;
417  }
418  return $blockids;
419  }
420  }
421 
422  function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
423  {
424  $isactive = intval($isactive);
425  $db = $GLOBALS['xoopsDB'];
426  $ret = array();
427  if (isset($groupid)) {
428  $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1";
429  if ( is_array($groupid) ) {
430  $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).')';
431  } else {
432  if (intval($groupid) > 0) {
433  $sql .= ' AND gperm_groupid='.intval($groupid);
434  }
435  }
436  $result = $db->query($sql);
437  $blockids = array();
438  while ( $myrow = $db->fetchArray($result) ) {
439  $blockids[] = $myrow['gperm_itemid'];
440  }
441  if (empty($blockids)) {
442  return $blockids;
443  }
444  }
445  $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
446  $sql .= ' AND b.isactive='.$isactive;
447  if (isset($visible)) {
448  $sql .= ' AND b.visible='.intval($visible);
449  }
450  if (!isset($module_id)) {
451  } elseif (!empty($module_id)) {
452  $sql .= ' AND m.module_id IN (0,'. intval($module_id);
453  if ($toponlyblock) {
454  $sql .= ',-1';
455  }
456  $sql .= ')';
457  } else {
458  if ($toponlyblock) {
459  $sql .= ' AND m.module_id IN (0,-1)';
460  } else {
461  $sql .= ' AND m.module_id=0';
462  }
463  }
464  if (!empty($blockids)) {
465  $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
466  }
467  $sql .= ' ORDER BY '.$orderby;
468  $result = $db->query($sql);
469  while ( $myrow = $db->fetchArray($result) ) {
470  $block = new XoopsBlock($myrow);
471  $ret[$myrow['bid']] =& $block;
472  unset($block);
473  }
474  return $ret;
475  }
476 
477  function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
478  {
479  $db = $GLOBALS['xoopsDB'];
480  $ret = array();
481  $bids = array();
482  $sql = "SELECT DISTINCT(bid) from ".$db->prefix('newblocks');
483  if ($result = $db->query($sql)) {
484  while ( $myrow = $db->fetchArray($result) ) {
485  $bids[] = $myrow['bid'];
486  }
487  }
488  $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'";
489  $grouped = array();
490  if ($result = $db->query($sql)) {
491  while ( $myrow = $db->fetchArray($result) ) {
492  $grouped[] = $myrow['gperm_itemid'];
493  }
494  }
495  $non_grouped = array_diff($bids, $grouped);
496  if (!empty($non_grouped)) {
497  $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
498  $sql .= ' AND b.isactive='.intval($isactive);
499  if (isset($visible)) {
500  $sql .= ' AND b.visible='.intval($visible);
501  }
502  if (!isset($module_id)) {
503  } elseif (!empty($module_id)) {
504  $sql .= ' AND m.module_id IN (0,'. intval($module_id);
505  if ($toponlyblock) {
506  $sql .= ',-1';
507  }
508  $sql .= ')';
509  } else {
510  if ($toponlyblock) {
511  $sql .= ' AND m.module_id IN (0,-1)';
512  } else {
513  $sql .= ' AND m.module_id=0';
514  }
515  }
516  $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')';
517  $sql .= ' ORDER BY '.$orderby;
518  $result = $db->query($sql);
519  while ( $myrow = $db->fetchArray($result) ) {
520  $block = new XoopsBlock($myrow);
521  $ret[$myrow['bid']] =& $block;
522  unset($block);
523  }
524  }
525  return $ret;
526  }
527 
536  function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
537  {
538  $funcNum = intval($funcNum);
539  $moduleId = intval($moduleId);
540  if ($funcNum < 1 || $moduleId < 1) {
541  // invalid query
542  return 0;
543  }
545  if (isset($showFunc)) {
546  // showFunc is set for more strict comparison
547  $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)));
548  } else {
549  $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum);
550  }
551  if (!$result = $db->query($sql)) {
552  return 0;
553  }
554  list ($count) = $db->fetchRow($result);
555  return $count;
556  }
557 }
558 
559 ?>