XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
block.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
17if (!defined('SHOW_SIDEBLOCK_LEFT')) {
18 define('SHOW_SIDEBLOCK_LEFT', 1);
19 define('SHOW_SIDEBLOCK_RIGHT', 2);
20 define('SHOW_CENTERBLOCK_LEFT', 4);
21 define('SHOW_CENTERBLOCK_RIGHT', 8);
22 define('SHOW_CENTERBLOCK_CENTER', 16);
23 define('SHOW_BLOCK_ALL', 31);
24}
25
31class XoopsBlock extends XoopsObject
32{
33 public $mBlockFlagMapping = [];
34
38 public function __construct($id = null)
39 {
40 static $initVars, $initMap;
41 if (isset($initVars)) {
42 $this->vars = $initVars;
43 } else {
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);
49 //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
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;
66 $initMap = [
67 0 => false,
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
73 ];
74 }
75
76 // for backward compatibility
77 if (isset($id)) {
78 if (is_array($id)) {
79 $this->assignVars($id);
80 } else {
81 $this->load($id);
82 }
83 }
84 $this->mBlockFlagMapping = $initMap;
85 }
86 public function XoopsBlock($id = null)
87 {
88 return self::__construct($id);
89 }
90
107 public function &getContent($format = 'S', $c_type = 'T')
108 {
109 $ret = null;
110
111 switch ($format) {
112 case 'S':
113
114 // check the type of content
115 // H : custom HTML block
116 // P : custom PHP block
117 // S : use text sanitizater (smilies enabled)
118 // T : use text sanitizater (smilies disabled)
119 if ('H' == $c_type) {
120 $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
121 } elseif ('P' == $c_type) {
122 ob_start();
123 echo eval($this->get('content'));
124 $content = ob_get_contents();
125 ob_end_clean();
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));
130 } else {
132 $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->get('content'), 1, 0));
133 }
134 break;
135 case 'E':
136 $ret = $this->getVar('content', 'E');
137 break;
138 default:
139 $ret = $this->get('content');
140 break;
141 }
142
143 return $ret;
144 }
145
146 public function &buildBlock()
147 {
148 $ret = false;
149
150 $block = [];
151 // M for module block, S for system block C for Custom
152 if ('C' != $this->get('block_type')) {
153 // get block display function
154 $show_func = $this->getVar('show_func', 'N');
155 if (!$show_func) {
156 return $ret;
157 }
158 // must get lang files b4 execution of the function
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);
162
163 require_once $path;
164 if (function_exists($show_func)) {
165 // execute the function
166 $params = explode('|', $this->getVar('options', 'N'));
167 $block = $show_func($params);
168 if (!$block) {
169 return $ret;
170 }
171 } else {
172 return $ret;
173 }
174 } else {
175 return $ret;
176 }
177 } else {
178 // it is a custom block, so just return the contents
179 $block['content'] = $this->getContent('S', $this->getVar('c_type', 'N'));
180 if (empty($block['content'])) {
181 return $ret;
182 }
183 }
184 return $block;
185 }
186
187 /*
188 * Aligns the content of a block
189 * If position is 0, content in DB is positioned
190 * before the original content
191 * If position is 1, content in DB is positioned
192 * after the original content
193 */
194 public function &buildContent($position, $content='', $contentdb='')
195 {
196 $ret = null;
197 if (0 == $position) {
198 $ret = $contentdb.$content;
199 } elseif (1 == $position) {
200 $ret = $content.$contentdb;
201 }
202 return $ret;
203 }
204
205 public function &buildTitle($originaltitle, $newtitle='')
206 {
207 if ('' != $newtitle) {
208 $ret = $newtitle;
209 } else {
210 $ret = $originaltitle;
211 }
212 return $ret;
213 }
214
215 public function isCustom()
216 {
217 if ('C' == $this->get('block_type')) {
218 return true;
219 }
220 return false;
221 }
222
228 public function getOptions()
229 {
230 if ('C' != $this->get('block_type')) {
231 $edit_func = $this->getVar('edit_func', 'N');
232 if (!$edit_func) {
233 return false;
234 }
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);
238
239 include_once $path;
240 $options = explode('|', $this->getVar('options', 'N'));
241 $edit_form = $edit_func($options);
242 if (!$edit_form) {
243 return false;
244 }
245 return $edit_form;
246 } else {
247 return false;
248 }
249 } else {
250 return false;
251 }
252 }
253
254 // Some functions for for backward compatibility
255 // @deprecated
256
257 public function load($id)
258 {
259 $handler = xoops_gethandler('block');
260 if ($obj =& $handler->get($id)) {
261 foreach ($obj->vars as $k => $v) {
262 $this->assignVar($k, $v['value']);
263 }
264 }
265 }
266
267 public function store()
268 {
269 $handler = xoops_gethandler('block');
270 if ($handler->insert($this)) {
271 return $this->getVar('bid', 'N');
272 } else {
273 return false;
274 }
275 }
276
277 public function delete()
278 {
279 $handler = xoops_gethandler('block');
280 return $handler->delete($this);
281 }
282 public function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
283 {
284 $handler = xoops_gethandler('block');
285 $ret =& $handler->getAllBlocksByGroup($groupid, $asobject, $side, $visible, $orderby, $isactive);
286 return $ret;
287 }
288 public function &getAllBlocks($rettype='object', $side=null, $visible=null, $orderby='side,weight,bid', $isactive=1)
289 {
290 $handler = xoops_gethandler('block');
291 $ret =& $handler->getAllBlocks($rettype, $side, $visible, $orderby, $isactive);
292 return $ret;
293 }
294 public static function &sGetByModule($moduleid, $asobject=true) {
295 $handler = xoops_gethandler('block');
296 $ret =& $handler->getByModule($moduleid, $asobject);
297 return $ret;
298 }
299 public function &getByModule($moduleid, $asobject=true)
300 {
301 return self::sGetByModule($moduleid, $asobject);
302 }
303 public function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
304 {
305 $handler = xoops_gethandler('block');
306 $ret =& $handler->getAllByGroupModule($groupid, $module_id, $toponlyblock, $visible, $orderby, $isactive);
307 return $ret;
308 }
309 public function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid')
310 {
311 $handler = xoops_gethandler('block');
312 $ret =& $handler->getBlocks($groupid, $mid, $blockFlag, $orderby);
313 return $ret;
314 }
315 public function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
316 {
317 $handler = xoops_gethandler('block');
318 $ret =& $handler->getNonGroupedBlocks($module_id, $toponlyblock, $visible, $orderby, $isactive);
319 return $ret;
320 }
321 public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
322 {
323 $handler = xoops_gethandler('block');
324 $ret =& $handler->countSimilarBlocks($moduleId, $funcNum, $showFunc);
325 return $ret;
326 }
327}
328
329
342{
343
351 public function &create($isNew = true)
352 {
353 $block = new XoopsBlock();
354 if ($isNew) {
355 $block->setNew();
356 }
357 return $block;
358 }
359
368 public function &createByInfo($info)
369 {
370 $block =& $this->create();
371
372 $options=$info['options'] ?? null;
373 $edit_func=$info['edit_func'] ?? null;
374
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());
385
386 return $block;
387 }
388
396 public function &get($id)
397 {
398 $id = (int)$id;
399 if ($id > 0) {
400 $db = $this->db;
401 $sql = 'SELECT * FROM '.$db->prefix('newblocks').' WHERE bid='.$id;
402 if (!$result = $db->query($sql)) {
403 $ret = false; //< You may think this should be null. But this is the compatibility with X2.
404 return $ret;
405 }
406 $numrows = $db->getRowsNum($result);
407 if (1 == $numrows) {
408 $block = new XoopsBlock();
409 $block->assignVars($db->fetchArray($result));
410 return $block;
411 }
412 }
413
414 $ret = false; //< You may think this should be null. But this is the compatibility with X2.
415 return $ret;
416 }
417
425 public function insert(&$block, $autolink=false)
426 {
427 $mid = null;
428 $func_num = null;
429 $options = null;
430 $name = null;
431 $title = null;
432 $content = null;
433 $side = null;
434 $weight = null;
435 $visible = null;
436 $block_type = null;
437 $c_type = null;
438 $dirname = null;
439 $func_file = null;
440 $show_func = null;
441 $edit_func = null;
442 $template = null;
443 $bcachetime = null;
444 $isactive = null;
445 $bid = null;
446 if ('xoopsblock' != strtolower(get_class($block))) {
447 return false;
448 }
449 if (!$block->isDirty()) {
450 return true;
451 }
452 if (!$block->cleanVars()) {
453 return false;
454 }
455 foreach ($block->cleanVars as $k => $v) {
456 ${$k} = $v;
457 }
458
459 $isNew = false;
460
461 $db = $this->db;
462 if ($block->isNew()) {
463 $isNew = true;
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());
466 } else {
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);
468 }
469 if (!$result = $db->query($sql)) {
470 return false;
471 }
472 if (empty($bid)) {
473 $bid = $db->getInsertId();
474 }
475 $block->assignVar('bid', $bid);
476
477 //
478 // $autolink is temp variable.
479 //
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);
483 }
484
485 return true;
486 }
487
494 public function delete(&$block)
495 {
496 if ('xoopsblock' != strtolower(get_class($block))) {
497 return false;
498 }
499 $id = $block->get('bid');
500 $db = $this->db;
501 $sql = sprintf('DELETE FROM %s WHERE bid = %u', $db->prefix('newblocks'), $id);
502 if (!$result = $db->query($sql)) {
503 return false;
504 }
505 $sql = sprintf('DELETE FROM %s WHERE block_id = %u', $db->prefix('block_module_link'), $id);
506 $db->query($sql);
507 return true;
508 }
509
516 public function &getObjects($criteria = null, $id_as_key = false)
517 {
518 $ret = [];
519 $limit = $start = 0;
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();
525 }
526 $result = $this->db->query($sql, $limit, $start);
527 if (!$result) {
528 return $ret;
529 }
530 while ($myrow = $this->db->fetchArray($result)) {
531 $block =& $this->create(false);
532 $block->assignVars($myrow);
533 if (!$id_as_key) {
534 $ret[] =& $block;
535 } else {
536 $ret[$myrow['bid']] =& $block;
537 }
538 unset($block);
539 }
540 return $ret;
541 }
542
543 public function &getObjectsDirectly($criteria = null)
544 {
545 $ret = [];
546 $limit = 0;
547 $start = 0;
548
549 $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks');
550 if ($criteria) {
551 $sql .= ' ' . $criteria->renderWhere();
552 }
553
554 $result = $this->db->query($sql);
555 if (!$result) {
556 return $ret;
557 }
558
559 while ($row = $this->db->fetchArray($result)) {
560 $block =& $this->create(false);
561 $block->assignVars($row);
562
563 $ret[] =& $block;
564
565 unset($block);
566 }
567
568 return $ret;
569 }
570
571
578 public function &getList($criteria = null)
579 {
580 $blocks =& $this->getObjects($criteria, true);
581 $ret = [];
582 foreach (array_keys($blocks) as $i) {
583 $name = ('C' != $blocks[$i]->get('block_type')) ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
584 $ret[$i] = $name;
585 }
586 return $ret;
587 }
588
605 public function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
606 {
607 $ret = [];
608 if (!$asobject) {
609 $sql = 'SELECT b.bid ';
610 } else {
611 $sql = 'SELECT b.* ';
612 }
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);
617 if ($size > 1) {
618 for ($i = 1; $i < $size; $i++) {
619 $sql .= ' OR l.gperm_groupid='.(int)$groupid[$i];
620 }
621 }
622 $sql .= ')';
623 } else {
624 $sql .= ' AND l.gperm_groupid='.(int)$groupid;
625 }
626 $sql .= ' AND b.isactive='.(int)$isactive;
627 if (isset($side)) {
628 $side = (int)$side;
629 // get both sides in sidebox? (some themes need this)
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)';
634 } else {
635 $side = 'b.side='.$side;
636 }
637 $sql .= ' AND '.$side;
638 }
639 if (isset($visible)) {
640 $sql .= ' AND b.visible='.(int)$visible;
641 }
642 $sql .= ' ORDER BY '.addslashes($orderby);
643 $result = $this->db->query($sql);
644 $added = [];
645 while ($myrow = $this->db->fetchArray($result)) {
646 if (!in_array($myrow['bid'], $added)) {
647 if (!$asobject) {
648 $ret[] = $myrow['bid'];
649 } else {
650 $block =& $this->create(false);
651 $block->assignVars($myrow);
652 $ret[] =& $block;
653 }
654 array_push($added, $myrow['bid']);
655 }
656 }
657 return $ret;
658 }
659 public function &getAllBlocks($rettype='object', $side=null, $visible=null, $orderby='side,weight,bid', $isactive=1)
660 {
661 $ret = [];
662 $where_query = ' WHERE isactive='.(int)$isactive;
663 if (isset($side)) {
664 $side = (int)$side;
665 // get both sides in sidebox? (some themes need this)
666 if (2 == $side) {
667 $side = '(side=0 OR side=1)';
668 } elseif (6 == $side) {
669 $side = '(side=3 OR side=4 OR side=5)';
670 } else {
671 $side = 'side='.$side;
672 }
673 $where_query .= ' AND '.$side;
674 }
675 if (isset($visible)) {
676 $visible = (int)$visible;
677 $where_query .= ' AND visible='.$visible;
678 }
679 $where_query .= ' ORDER BY '.addslashes($orderby);
680 switch ($rettype) {
681 case 'object':
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);
687 $ret[] =& $block;
688 }
689 break;
690 case 'list':
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;
698 unset($block);
699 }
700 break;
701 case 'id':
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'];
706 }
707 break;
708 }
709 //echo $sql;
710 return $ret;
711 }
712
713 public function &getByModule($moduleid, $asobject=true)
714 {
715 $moduleid = (int)$moduleid;
716 if (true == $asobject) {
717 $sql = $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE mid='.$moduleid;
718 } else {
719 $sql = 'SELECT bid FROM '.$this->db->prefix('newblocks').' WHERE mid='.$moduleid;
720 }
721 $result = $this->db->query($sql);
722 $ret = [];
723 while ($myrow = $this->db->fetchArray($result)) {
724 if ($asobject) {
725 $block =& $this->create(false);
726 $block->assignVars($myrow);
727 $ret[] =& $block;
728 } else {
729 $ret[] = $myrow['bid'];
730 }
731 }
732 return $ret;
733 }
734
746 public function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
747 {
748 $ret = [];
749 $db = $this->db;
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))).')';
753 } else {
754 $groupid = (int)$groupid;
755 if ($groupid > 0) {
756 $sql .= ' AND gperm_groupid='.$groupid;
757 }
758 }
759 $result = $db->query($sql);
760 $blockids = [];
761 while ($myrow = $db->fetchArray($result)) {
762 $blockids[] = $myrow['gperm_itemid'];
763 }
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;
769 }
770 if (false !== $module_id) {
771 $sql .= ' AND m.module_id IN (0,'.(int)$module_id;
772 if ($toponlyblock) {
773 $sql .= ',-1';
774 }
775 $sql .= ')';
776 } else {
777 if ($toponlyblock) {
778 $sql .= ' AND m.module_id IN (0,-1)';
779 } else {
780 $sql .= ' AND m.module_id=0';
781 }
782 }
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;
790 unset($block);
791 }
792 }
793 return $ret;
794 }
795
806 public function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid')
807 {
808 $root =& XCube_Root::getSingleton();
809 $db = $this->db =& $root->mController->getDB();
810
811 $ret = [];
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))).')';
815 } else {
816 $groupid = (int)$groupid;
817 if ($groupid > 0) {
818 $sql .= ' AND gperm_groupid='.$groupid;
819 }
820 }
821 $result = $db->query($sql);
822 $blockids = [];
823 while ([$itemid] = $db->fetchRow($result)) {
824 $blockids[] = $itemid;
825 }
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.')';
831 } else {
832 $sql .= ' AND m.module_id=0';
833 }
834
835 //
836 // SIDE
837 //
838 if (SHOW_BLOCK_ALL != $blockFlag) {
839 $arr = [];
840 if ($blockFlag & SHOW_SIDEBLOCK_LEFT) {
841 $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_LEFT];
842 }
843 if ($blockFlag & SHOW_SIDEBLOCK_RIGHT) {
844 $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_RIGHT];
845 }
846 if ($blockFlag & SHOW_CENTERBLOCK_LEFT) {
847 $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_LEFT];
848 }
849 if ($blockFlag & SHOW_CENTERBLOCK_CENTER) {
850 $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_CENTER];
851 }
852 if ($blockFlag & SHOW_CENTERBLOCK_RIGHT) {
853 $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_RIGHT];
854 }
855
856 $sql .= ' AND (' . implode(' OR ', $arr) . ')';
857 }
858
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;
865 unset($block);
866 }
867 }
868 return $ret;
869 }
870
880 public function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
881 {
882 $ret = [];
883 $bids = [];
884 $db = $this->db;
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'];
889 }
890 }
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\'';
892 $grouped = [];
893 if ($result = $db->query($sql)) {
894 while ($myrow = $db->fetchArray($result)) {
895 $grouped[] = $myrow['gperm_itemid'];
896 }
897 }
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;
904 }
905 $module_id = (int)$module_id;
906 if (!empty($module_id)) {
907 $sql .= ' AND m.module_id IN (0,'.$module_id.($toponlyblock?',-1)':')');
908 } else {
909 if ($toponlyblock) {
910 $sql .= ' AND m.module_id IN (0,-1)';
911 } else {
912 $sql .= ' AND m.module_id=0';
913 }
914 }
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;
922 unset($block);
923 }
924 }
925 return $ret;
926 }
927
928 public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
929 {
930 $funcNum = (int)$funcNum;
931 $moduleId = (int)$moduleId;
932 if ($funcNum < 1 || $moduleId < 1) {
933 // invalid query
934 return 0;
935 }
936 $db = $this->db;
937 if (isset($showFunc)) {
938 // showFunc is set for more strict comparison
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)));
940 } else {
941 $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d', $db->prefix('newblocks'), $moduleId, $funcNum);
942 }
943 if (!$result = $db->query($sql)) {
944 return 0;
945 }
946 [$count] = $db->fetchRow($result);
947 return $count;
948 }
949
957 public function syncIsActive($moduleId, $isActive, $force = false)
958 {
959 $db = $this->db;
960 $db->prepare('UPDATE ' . $db->prefix('newblocks') . ' SET isactive=? WHERE mid=?');
961 $db->bind_param('ii', $isActive, $moduleId);
962
963 if ($force) {
964 $db->executeF();
965 } else {
966 $db->execute();
967 }
968 }
969}
& getObjects($criteria=null, $id_as_key=false)
Definition block.php:516
& createByInfo($info)
Definition block.php:368
& getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight, b.bid')
Definition block.php:806
& getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby='b.weight, b.bid', $isactive=1)
Definition block.php:605
& getList($criteria=null)
Definition block.php:578
& getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight, b.bid', $isactive=1)
Definition block.php:746
syncIsActive($moduleId, $isActive, $force=false)
Definition block.php:957
& getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight, b.bid', $isactive=1)
Definition block.php:880
insert(&$block, $autolink=false)
Definition block.php:425
& create($isNew=true)
Definition block.php:351
getOptions()
Definition block.php:228
& getContent($format='S', $c_type='T')
Definition block.php:107
__construct($id=null)
Definition block.php:38
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317
assignVar($key, $value)
Definition object.php:218
assignVars($var_arr)
Definition object.php:232