33 public $mBlockFlagMapping = [];
40 static $initVars, $initMap;
41 if (isset($initVars)) {
42 $this->vars = $initVars;
44 $this->
initVar(
'bid', XOBJ_DTYPE_INT,
null,
false);
45 $this->
initVar(
'mid', XOBJ_DTYPE_INT, 0,
false);
46 $this->
initVar(
'func_num', XOBJ_DTYPE_INT, 0,
false);
47 $this->
initVar(
'options', XOBJ_DTYPE_TXTBOX,
null,
false, 255);
48 $this->
initVar(
'name', XOBJ_DTYPE_TXTBOX,
null,
true, 150);
50 $this->
initVar(
'title', XOBJ_DTYPE_TXTBOX,
null,
false, 150);
51 $this->
initVar(
'content', XOBJ_DTYPE_TXTAREA,
null,
false);
52 $this->
initVar(
'side', XOBJ_DTYPE_INT, 0,
false);
53 $this->
initVar(
'weight', XOBJ_DTYPE_INT, 0,
false);
54 $this->
initVar(
'visible', XOBJ_DTYPE_INT, 0,
false);
55 $this->
initVar(
'block_type', XOBJ_DTYPE_OTHER,
null,
false);
56 $this->
initVar(
'c_type', XOBJ_DTYPE_OTHER,
null,
false);
57 $this->
initVar(
'isactive', XOBJ_DTYPE_INT,
null,
false);
58 $this->
initVar(
'dirname', XOBJ_DTYPE_TXTBOX,
null,
false, 50);
59 $this->
initVar(
'func_file', XOBJ_DTYPE_TXTBOX,
null,
false, 50);
60 $this->
initVar(
'show_func', XOBJ_DTYPE_TXTBOX,
null,
false, 50);
61 $this->
initVar(
'edit_func', XOBJ_DTYPE_TXTBOX,
null,
false, 50);
62 $this->
initVar(
'template', XOBJ_DTYPE_OTHER,
null,
false);
63 $this->
initVar(
'bcachetime', XOBJ_DTYPE_INT, 0,
false);
64 $this->
initVar(
'last_modified', XOBJ_DTYPE_INT, time(),
false);
65 $initVars = $this->vars;
68 SHOW_SIDEBLOCK_LEFT => 0,
69 SHOW_SIDEBLOCK_RIGHT => 1,
70 SHOW_CENTERBLOCK_LEFT => 3,
71 SHOW_CENTERBLOCK_RIGHT => 4,
72 SHOW_CENTERBLOCK_CENTER => 5
84 $this->mBlockFlagMapping = $initMap;
119 if (
'H' == $c_type) {
120 $ret = str_replace(
'{X_SITEURL}', XOOPS_URL.
'/', $this->getVar(
'content',
'N'));
121 } elseif (
'P' == $c_type) {
123 echo eval($this->
get(
'content'));
124 $content = ob_get_contents();
126 $ret = str_replace(
'{X_SITEURL}', XOOPS_URL.
'/', $content);
127 } elseif (
'S' == $c_type) {
129 $ret = str_replace(
'{X_SITEURL}', XOOPS_URL.
'/', $myts->displayTarea($this->get(
'content'), 1, 1));
132 $ret = str_replace(
'{X_SITEURL}', XOOPS_URL.
'/', $myts->displayTarea($this->get(
'content'), 1, 0));
136 $ret = $this->
getVar(
'content',
'E');
139 $ret = $this->
get(
'content');
146 public function &buildBlock()
152 if (
'C' != $this->
get(
'block_type')) {
154 $show_func = $this->
getVar(
'show_func',
'N');
159 if (file_exists($path = XOOPS_ROOT_PATH.
'/modules/'.($dirname = $this->getVar(
'dirname',
'N')).
'/blocks/'.$this->getVar(
'func_file',
'N'))) {
160 $root=&XCube_Root::getSingleton();
161 $root->mLanguageManager->loadBlockMessageCatalog($dirname);
164 if (function_exists($show_func)) {
166 $params = explode(
'|', $this->
getVar(
'options',
'N'));
167 $block = $show_func($params);
180 if (empty($block[
'content'])) {
194 public function &buildContent($position, $content=
'', $contentdb=
'')
197 if (0 == $position) {
198 $ret = $contentdb.$content;
199 } elseif (1 == $position) {
200 $ret = $content.$contentdb;
205 public function &buildTitle($originaltitle, $newtitle=
'')
207 if (
'' != $newtitle) {
210 $ret = $originaltitle;
215 public function isCustom()
217 if (
'C' == $this->
get(
'block_type')) {
230 if (
'C' != $this->
get(
'block_type')) {
231 $edit_func = $this->
getVar(
'edit_func',
'N');
235 if (file_exists($path = XOOPS_ROOT_PATH.
'/modules/'.($dirname=$this->getVar(
'dirname',
'N')).
'/blocks/'.$this->getVar(
'func_file',
'N'))) {
236 $root =& XCube_Root::getSingleton();
237 $root->mLanguageManager->loadBlockMessageCatalog($dirname);
240 $options = explode(
'|', $this->
getVar(
'options',
'N'));
241 $edit_form = $edit_func($options);
257 public function load($id)
259 $handler = xoops_gethandler(
'block');
260 if ($obj =& $handler->get($id)) {
261 foreach ($obj->vars as $k => $v) {
267 public function store()
269 $handler = xoops_gethandler(
'block');
270 if ($handler->insert($this)) {
271 return $this->
getVar(
'bid',
'N');
277 public function delete()
279 $handler = xoops_gethandler(
'block');
280 return $handler->delete($this);
282 public function &getAllBlocksByGroup($groupid, $asobject=
true, $side=
null, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
284 $handler = xoops_gethandler(
'block');
285 $ret =& $handler->getAllBlocksByGroup($groupid, $asobject, $side, $visible, $orderby, $isactive);
288 public function &getAllBlocks($rettype=
'object', $side=
null, $visible=
null, $orderby=
'side,weight,bid', $isactive=1)
290 $handler = xoops_gethandler(
'block');
291 $ret =& $handler->getAllBlocks($rettype, $side, $visible, $orderby, $isactive);
294 public static function &sGetByModule($moduleid, $asobject=
true) {
295 $handler = xoops_gethandler(
'block');
296 $ret =& $handler->getByModule($moduleid, $asobject);
299 public function &getByModule($moduleid, $asobject=
true)
301 return self::sGetByModule($moduleid, $asobject);
303 public function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=
false, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
305 $handler = xoops_gethandler(
'block');
306 $ret =& $handler->getAllByGroupModule($groupid, $module_id, $toponlyblock, $visible, $orderby, $isactive);
309 public function &getBlocks($groupid, $mid=
false, $blockFlag=SHOW_BLOCK_ALL, $orderby=
'b.weight,b.bid')
311 $handler = xoops_gethandler(
'block');
312 $ret =& $handler->getBlocks($groupid, $mid, $blockFlag, $orderby);
315 public function &getNonGroupedBlocks($module_id=0, $toponlyblock=
false, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
317 $handler = xoops_gethandler(
'block');
318 $ret =& $handler->getNonGroupedBlocks($module_id, $toponlyblock, $visible, $orderby, $isactive);
321 public function countSimilarBlocks($moduleId, $funcNum, $showFunc =
null)
323 $handler = xoops_gethandler(
'block');
324 $ret =& $handler->countSimilarBlocks($moduleId, $funcNum, $showFunc);
370 $block =& $this->
create();
372 $options=$info[
'options'] ??
null;
373 $edit_func=$info[
'edit_func'] ??
null;
375 $block->setVar(
'options', $options);
376 $block->setVar(
'name', $info[
'name']);
377 $block->setVar(
'title', $info[
'name']);
378 $block->setVar(
'block_type',
'M');
379 $block->setVar(
'c_type', 1);
380 $block->setVar(
'func_file', $info[
'file']);
381 $block->setVar(
'show_func', $info[
'show_func']);
382 $block->setVar(
'edit_func', $edit_func);
383 $block->setVar(
'template', $info[
'template']);
384 $block->setVar(
'last_modified', time());
396 public function &
get($id)
401 $sql =
'SELECT * FROM '.$db->prefix(
'newblocks').
' WHERE bid='.$id;
402 if (!$result = $db->query($sql)) {
406 $numrows = $db->getRowsNum($result);
409 $block->assignVars($db->fetchArray($result));
425 public function insert(&$block, $autolink=
false)
446 if (
'xoopsblock' != strtolower(get_class($block))) {
449 if (!$block->isDirty()) {
452 if (!$block->cleanVars()) {
455 foreach ($block->cleanVars as $k => $v) {
462 if ($block->isNew()) {
464 $bid = $db->genId(
'newblocks_bid_seq');
465 $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)', $db->prefix(
'newblocks'), $bid, $mid, $func_num, $db->quoteString($options), $db->quoteString($name), $db->quoteString($title), $db->quoteString($content), $side, $weight, $visible, $db->quoteString($block_type), $db->quoteString($c_type), 1, $db->quoteString($dirname), $db->quoteString($func_file), $db->quoteString($show_func), $db->quoteString($edit_func), $db->quoteString($template), $bcachetime, time());
467 $sql = sprintf(
'UPDATE %s SET func_num = %u, options = %s, name = %s, title = %s, content = %s, side = %u, weight = %u, visible = %u, c_type = %s, isactive = %u, func_file = %s, show_func = %s, edit_func = %s, template = %s, bcachetime = %u, last_modified = %u WHERE bid = %u', $db->prefix(
'newblocks'), $func_num, $db->quoteString($options), $db->quoteString($name), $db->quoteString($title), $db->quoteString($content), $side, $weight, $visible, $db->quoteString($c_type), $isactive, $db->quoteString($func_file), $db->quoteString($show_func), $db->quoteString($edit_func), $db->quoteString($template), $bcachetime, time(), $bid);
469 if (!$result = $db->query($sql)) {
473 $bid = $db->getInsertId();
475 $block->assignVar(
'bid', $bid);
480 if ($isNew && $autolink) {
481 $link_sql =
'INSERT INTO ' . $db->prefix(
'block_module_link') .
' (block_id, module_id) VALUES ('.$bid.
', -1)';
482 return $db->query($link_sql);
494 public function delete(&$block)
496 if (
'xoopsblock' != strtolower(get_class($block))) {
499 $id = $block->get(
'bid');
501 $sql = sprintf(
'DELETE FROM %s WHERE bid = %u', $db->prefix(
'newblocks'), $id);
502 if (!$result = $db->query($sql)) {
505 $sql = sprintf(
'DELETE FROM %s WHERE block_id = %u', $db->prefix(
'block_module_link'), $id);
516 public function &
getObjects($criteria =
null, $id_as_key =
false)
520 $sql =
'SELECT DISTINCT(b.*) FROM '.$this->db->prefix(
'newblocks').
' b LEFT JOIN '.$this->db->prefix(
'block_module_link').
' l ON b.bid=l.block_id';
521 if (isset($criteria) && $criteria instanceof \criteriaelement) {
522 $sql .=
' '.$criteria->renderWhere();
523 $limit = $criteria->getLimit();
524 $start = $criteria->getStart();
526 $result = $this->db->query($sql, $limit, $start);
530 while ($myrow = $this->db->fetchArray($result)) {
531 $block =& $this->
create(
false);
532 $block->assignVars($myrow);
536 $ret[$myrow[
'bid']] =& $block;
543 public function &getObjectsDirectly($criteria =
null)
549 $sql =
'SELECT * FROM ' . $this->db->prefix(
'newblocks');
551 $sql .=
' ' . $criteria->renderWhere();
554 $result = $this->db->query($sql);
559 while ($row = $this->db->fetchArray($result)) {
560 $block =& $this->
create(
false);
561 $block->assignVars($row);
580 $blocks =& $this->
getObjects($criteria,
true);
582 foreach (array_keys($blocks) as $i) {
583 $name = (
'C' != $blocks[$i]->get(
'block_type')) ? $blocks[$i]->getVar(
'name') : $blocks[$i]->getVar(
'title');
605 public function &
getAllBlocksByGroup($groupid, $asobject=
true, $side=
null, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
609 $sql =
'SELECT b.bid ';
611 $sql =
'SELECT b.* ';
613 $sql .=
'FROM '.$this->db->prefix(
'newblocks').
' b LEFT JOIN '.$this->db->prefix(
'group_permission').
' l ON l.gperm_itemid=b.bid WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
614 if (is_array($groupid)) {
615 $sql .=
' AND (l.gperm_groupid='.(int)$groupid[0];
616 $size = count($groupid);
618 for ($i = 1; $i < $size; $i++) {
619 $sql .=
' OR l.gperm_groupid='.(int)$groupid[$i];
624 $sql .=
' AND l.gperm_groupid='.(int)$groupid;
626 $sql .=
' AND b.isactive='.(int)$isactive;
630 if (XOOPS_SIDEBLOCK_BOTH == $side) {
631 $side =
'(b.side=0 OR b.side=1)';
632 } elseif (XOOPS_CENTERBLOCK_ALL == $side) {
633 $side =
'(b.side=3 OR b.side=4 OR b.side=5)';
635 $side =
'b.side='.$side;
637 $sql .=
' AND '.$side;
639 if (isset($visible)) {
640 $sql .=
' AND b.visible='.(int)$visible;
642 $sql .=
' ORDER BY '.addslashes($orderby);
643 $result = $this->db->query($sql);
645 while ($myrow = $this->db->fetchArray($result)) {
646 if (!in_array($myrow[
'bid'], $added)) {
648 $ret[] = $myrow[
'bid'];
650 $block =& $this->
create(
false);
651 $block->assignVars($myrow);
654 array_push($added, $myrow[
'bid']);
659 public function &getAllBlocks($rettype=
'object', $side=
null, $visible=
null, $orderby=
'side,weight,bid', $isactive=1)
662 $where_query =
' WHERE isactive='.(int)$isactive;
667 $side =
'(side=0 OR side=1)';
668 } elseif (6 == $side) {
669 $side =
'(side=3 OR side=4 OR side=5)';
671 $side =
'side='.$side;
673 $where_query .=
' AND '.$side;
675 if (isset($visible)) {
676 $visible = (int)$visible;
677 $where_query .=
' AND visible='.$visible;
679 $where_query .=
' ORDER BY '.addslashes($orderby);
682 $sql =
'SELECT * FROM '.$this->db->prefix(
'newblocks').$where_query;
683 $result = $this->db->query($sql);
684 while ($myrow = $this->db->fetchArray($result)) {
685 $block =& $this->
create(
false);
686 $block->assignVars($myrow);
691 $sql =
'SELECT * FROM '.$this->db->prefix(
'newblocks').$where_query;
692 $result = $this->db->query($sql);
693 while ($myrow = $this->db->fetchArray($result)) {
694 $block =& $this->
create(
false);
695 $block->assignVars($myrow);
696 $name = (
'C' != $block->get(
'block_type')) ? $block->getVar(
'name') : $block->getVar(
'title');
697 $ret[$block->getVar(
'bid')] = $name;
702 $sql =
'SELECT bid FROM '.$this->db->prefix(
'newblocks').$where_query;
703 $result = $this->db->query($sql);
704 while ($myrow = $this->db->fetchArray($result)) {
705 $ret[] = $myrow[
'bid'];
713 public function &getByModule($moduleid, $asobject=
true)
715 $moduleid = (int)$moduleid;
716 if (
true == $asobject) {
717 $sql = $sql =
'SELECT * FROM '.$this->db->prefix(
'newblocks').
' WHERE mid='.$moduleid;
719 $sql =
'SELECT bid FROM '.$this->db->prefix(
'newblocks').
' WHERE mid='.$moduleid;
721 $result = $this->db->query($sql);
723 while ($myrow = $this->db->fetchArray($result)) {
725 $block =& $this->
create(
false);
726 $block->assignVars($myrow);
729 $ret[] = $myrow[
'bid'];
746 public function &
getAllByGroupModule($groupid, $module_id=0, $toponlyblock=
false, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
750 $sql =
'SELECT DISTINCT gperm_itemid FROM '.$db->prefix(
'group_permission').
' WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
751 if (is_array($groupid)) {
752 $sql .=
' AND gperm_groupid IN ('.addslashes(implode(
',', array_map(
'intval', $groupid))).
')';
754 $groupid = (int)$groupid;
756 $sql .=
' AND gperm_groupid='.$groupid;
759 $result = $db->query($sql);
761 while ($myrow = $db->fetchArray($result)) {
762 $blockids[] = $myrow[
'gperm_itemid'];
764 if (!empty($blockids)) {
765 $sql =
'SELECT b.* FROM '.$db->prefix(
'newblocks').
' b, '.$db->prefix(
'block_module_link').
' m WHERE m.block_id=b.bid';
766 $sql .=
' AND b.isactive='.$isactive;
767 if (isset($visible)) {
768 $sql .=
' AND b.visible='.(int)$visible;
770 if (
false !== $module_id) {
771 $sql .=
' AND m.module_id IN (0,'.(int)$module_id;
778 $sql .=
' AND m.module_id IN (0,-1)';
780 $sql .=
' AND m.module_id=0';
783 $sql .=
' AND b.bid IN ('.implode(
',', $blockids).
')';
784 $sql .=
' ORDER BY '.$orderby;
785 $result = $db->query($sql);
786 while ($myrow = $db->fetchArray($result)) {
787 $block =& $this->
create(
false);
788 $block->assignVars($myrow);
789 $ret[$myrow[
'bid']] =& $block;
806 public function &
getBlocks($groupid, $mid=
false, $blockFlag=SHOW_BLOCK_ALL, $orderby=
'b.weight,b.bid')
808 $root =& XCube_Root::getSingleton();
809 $db = $this->db =& $root->mController->getDB();
812 $sql =
'SELECT DISTINCT gperm_itemid FROM '.$db->prefix(
'group_permission').
' WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
813 if (is_array($groupid)) {
814 $sql .=
' AND gperm_groupid IN ('.addslashes(implode(
',', array_map(
'intval', $groupid))).
')';
816 $groupid = (int)$groupid;
818 $sql .=
' AND gperm_groupid='.$groupid;
821 $result = $db->query($sql);
823 while ([$itemid] = $db->fetchRow($result)) {
824 $blockids[] = $itemid;
826 if (!empty($blockids)) {
827 $sql =
'SELECT b.* FROM '.$db->prefix(
'newblocks').
' b, '.$db->prefix(
'block_module_link').
' m WHERE m.block_id=b.bid';
828 $sql .=
' AND b.isactive=1 AND b.visible=1';
829 if (
false !== $mid && 0 !== $mid) {
830 $sql .=
' AND m.module_id IN (0,'.(int)$mid.
')';
832 $sql .=
' AND m.module_id=0';
838 if (SHOW_BLOCK_ALL != $blockFlag) {
840 if ($blockFlag & SHOW_SIDEBLOCK_LEFT) {
841 $arr[] =
'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_LEFT];
843 if ($blockFlag & SHOW_SIDEBLOCK_RIGHT) {
844 $arr[] =
'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_RIGHT];
846 if ($blockFlag & SHOW_CENTERBLOCK_LEFT) {
847 $arr[] =
'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_LEFT];
849 if ($blockFlag & SHOW_CENTERBLOCK_CENTER) {
850 $arr[] =
'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_CENTER];
852 if ($blockFlag & SHOW_CENTERBLOCK_RIGHT) {
853 $arr[] =
'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_RIGHT];
856 $sql .=
' AND (' . implode(
' OR ', $arr) .
')';
859 $sql .=
' AND b.bid IN ('.implode(
',', $blockids).
')' .
' ORDER BY '.addslashes($orderby);
860 $result = $db->query($sql);
861 while ($myrow = $db->fetchArray($result)) {
862 $block =& $this->
create(
false);
863 $block->assignVars($myrow);
864 $ret[$myrow[
'bid']] =& $block;
880 public function &
getNonGroupedBlocks($module_id=0, $toponlyblock=
false, $visible=
null, $orderby=
'b.weight,b.bid', $isactive=1)
885 $sql =
'SELECT DISTINCT(bid) from '.$db->prefix(
'newblocks');
886 if ($result = $db->query($sql)) {
887 while ($myrow = $db->fetchArray($result)) {
888 $bids[] = $myrow[
'bid'];
891 $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\'';
893 if ($result = $db->query($sql)) {
894 while ($myrow = $db->fetchArray($result)) {
895 $grouped[] = $myrow[
'gperm_itemid'];
898 $non_grouped = array_diff($bids, $grouped);
899 if (!empty($non_grouped)) {
900 $sql =
'SELECT b.* FROM '.$db->prefix(
'newblocks').
' b, '.$db->prefix(
'block_module_link').
' m WHERE m.block_id=b.bid';
901 $sql .=
' AND b.isactive='.(int)$isactive;
902 if (isset($visible)) {
903 $sql .=
' AND b.visible='.(int)$visible;
905 $module_id = (int)$module_id;
906 if (!empty($module_id)) {
907 $sql .=
' AND m.module_id IN (0,'.$module_id.($toponlyblock?
',-1)':
')');
910 $sql .=
' AND m.module_id IN (0,-1)';
912 $sql .=
' AND m.module_id=0';
915 $sql .=
' AND b.bid IN ('.implode(
',', $non_grouped).
')';
916 $sql .=
' ORDER BY '.addslashes($orderby);
917 $result = $db->query($sql);
918 while ($myrow = $db->fetchArray($result)) {
919 $block =& $this->
create(
false);
920 $block->assignVars($myrow);
921 $ret[$myrow[
'bid']] =& $block;
928 public function countSimilarBlocks($moduleId, $funcNum, $showFunc =
null)
930 $funcNum = (int)$funcNum;
931 $moduleId = (int)$moduleId;
932 if ($funcNum < 1 || $moduleId < 1) {
937 if (isset($showFunc)) {
939 $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)));
941 $sql = sprintf(
'SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d', $db->prefix(
'newblocks'), $moduleId, $funcNum);
943 if (!$result = $db->query($sql)) {
946 [$count] = $db->fetchRow($result);
960 $db->prepare(
'UPDATE ' . $db->prefix(
'newblocks') .
' SET isactive=? WHERE mid=?');
961 $db->bind_param(
'ii', $isActive, $moduleId);