1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Kernel\Handlers;
13:
14: use Xoops\Core\Database\Connection;
15: use Xoops\Core\Kernel\CriteriaElement;
16: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class XoopsBlockHandler extends XoopsPersistableObjectHandler
31: {
32: 33: 34: 35: 36:
37: public function __construct(Connection $db = null)
38: {
39: parent::__construct($db, 'system_block', '\Xoops\Core\Kernel\Handlers\XoopsBlock', 'bid', 'name');
40: }
41:
42: 43: 44: 45: 46: 47: 48: 49:
50: public function insertBlock(XoopsBlock $obj, $force = false)
51: {
52: $obj->setVar('last_modified', time());
53: return parent::insert($obj, $force);
54: }
55:
56: 57: 58: 59: 60: 61: 62:
63: public function deleteBlock(XoopsBlock $obj)
64: {
65: if (!parent::delete($obj)) {
66: return false;
67: }
68: $qb = $this->db2->createXoopsQueryBuilder();
69: $eb = $qb->expr();
70: $qb ->deletePrefix('system_permission', null)
71: ->where($eb->eq('gperm_name', $eb->literal('block_read')))
72: ->andWhere($eb->eq('gperm_itemid', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT)))
73: ->andWhere($eb->eq('gperm_modid', $qb->createNamedParameter(1, \PDO::PARAM_INT)))
74: ->execute();
75:
76: $qb ->deletePrefix('system_blockmodule', null)
77: ->where($eb->eq('block_id', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT)))
78: ->execute();
79:
80: return true;
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90:
91: public function getDistinctObjects(CriteriaElement $criteria = null, $id_as_key = false)
92: {
93: $ret = array();
94:
95: $qb = $this->db2->createXoopsQueryBuilder();
96: $eb = $qb->expr();
97: $qb ->select('DISTINCT(b.bid)')
98: ->addSelect('b.*')
99: ->fromPrefix('system_block', 'b')
100: ->leftJoinPrefix('b', 'system_blockmodule', 'l', $eb->eq('b.bid', 'l.block_id'));
101:
102: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
103: $criteria->renderQb($qb);
104: }
105:
106: $result = $qb->execute();
107: if (!$result) {
108: return $ret;
109: }
110: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
111: $block = new XoopsBlock();
112: $block->assignVars($myrow);
113: if (!$id_as_key) {
114: $ret[] = $block;
115: } else {
116: $ret[$myrow['bid']] = $block;
117: }
118: unset($block);
119: }
120: return $ret;
121:
122: }
123:
124: 125: 126: 127: 128: 129: 130:
131: public function getNameList(CriteriaElement $criteria = null)
132: {
133: $blocks = $this->getObjects($criteria, true);
134: $ret = array();
135: foreach (array_keys($blocks) as $i) {
136: $name = (!$blocks[$i]->isCustom()) ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
137: $ret[$i] = $name;
138: }
139: return $ret;
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160:
161: public function getAllBlocksByGroup(
162: $groupid,
163: $asobject = true,
164: $side = null,
165: $visible = null,
166: $orderby = "b.weight,b.bid",
167: $isactive = 1
168: ) {
169: $ret = array();
170: $qb = $this->db2->createXoopsQueryBuilder();
171: $eb = $qb->expr();
172: if ($asobject) {
173: $qb ->select('b.*');
174: } else {
175: $qb ->select('b.bid');
176: }
177: $qb ->fromPrefix('system_block', 'b')
178: ->leftJoinPrefix('b', 'system_permission', 'l', $eb->eq('b.bid', 'l.gperm_itemid'))
179: ->where($eb->eq('gperm_name', $eb->literal('block_read')))
180: ->andWhere($eb->eq('gperm_modid', 1));
181:
182: if (is_array($groupid)) {
183: if (count($groupid) > 1) {
184: $in=array();
185: foreach ($groupid as $gid) {
186: $in[] = $qb->createNamedParameter($gid, \PDO::PARAM_INT);
187: }
188: $qb->andWhere($eb->in('l.gperm_groupid', $in));
189: }
190: } else {
191: $qb->andWhere($eb->eq('l.gperm_groupid', $qb->createNamedParameter($groupid, \PDO::PARAM_INT)));
192: }
193: $qb->andWhere($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
194: if (isset($side)) {
195:
196: if ($side == XOOPS_SIDEBLOCK_BOTH) {
197: $qb->andWhere($eb->in('b.side', array(0,1)));
198: } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
199: $qb->andWhere($eb->in('b.side', array(3,4,5,7,8,9)));
200: } else {
201: $qb->andWhere($eb->eq('b.side', $qb->createNamedParameter($side, \PDO::PARAM_INT)));
202: }
203: }
204: if (isset($visible)) {
205: $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
206: }
207: $qb->orderBy($orderby);
208: $result = $qb->execute();
209: $added = array();
210: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
211: if (!in_array($myrow['bid'], $added)) {
212: if (!$asobject) {
213: $ret[] = $myrow['bid'];
214: } else {
215: $ret[] = new XoopsBlock($myrow);
216: }
217: array_push($added, $myrow['bid']);
218: }
219: }
220: return $ret;
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233:
234: public function getAllBlocks(
235: $rettype = "object",
236: $side = null,
237: $visible = null,
238: $orderby = "side,weight,bid",
239: $isactive = 1
240: ) {
241: $ret = array();
242: $qb = $this->db2->createXoopsQueryBuilder();
243: $eb = $qb->expr();
244:
245: $qb ->fromPrefix('system_block', null)
246: ->where($eb->eq('isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
247: if (isset($side)) {
248:
249: if ($side == XOOPS_SIDEBLOCK_BOTH) {
250: $qb->andWhere($eb->in('side', array(0,1)));
251: } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
252: $qb->andWhere($eb->in('side', array(3,4,5,7,8,9)));
253: } else {
254: $qb->andWhere($eb->eq('side', $qb->createNamedParameter($side, \PDO::PARAM_INT)));
255: }
256: }
257: if (isset($visible)) {
258: $qb->andWhere($eb->eq('visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
259: }
260: $qb->orderBy($orderby);
261: switch ($rettype) {
262: case "object":
263: $qb->select('*');
264: $result = $qb->execute();
265: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
266: $ret[] = new XoopsBlock($myrow);
267: }
268: break;
269: case "list":
270: $qb->select('*');
271: $result = $qb->execute();
272: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
273: $block = new XoopsBlock($myrow);
274: $title = $block->getVar("title");
275: $title = empty($title) ? $block->getVar("name") : $title;
276: $ret[$block->getVar("bid")] = $title;
277: }
278: break;
279: case "id":
280: $qb->select('bid');
281: $result = $qb->execute();
282: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
283: $ret[] = $myrow['bid'];
284: }
285: break;
286: }
287:
288: return $ret;
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298:
299: public function getByModule($moduleid, $asobject = true)
300: {
301: $qb = $this->db2->createXoopsQueryBuilder();
302: $eb = $qb->expr();
303:
304: $qb ->fromPrefix('system_block', null)
305: ->where($eb->eq('mid', $qb->createNamedParameter($moduleid, \PDO::PARAM_INT)));
306: if ($asobject == true) {
307: $qb->select('*');
308: } else {
309: $qb->select('bid');
310: }
311:
312: $ret = array();
313: $result = $qb->execute();
314: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
315: if ($asobject) {
316: $ret[] = new XoopsBlock($myrow);
317: } else {
318: $ret[] = $myrow['bid'];
319: }
320: }
321: return $ret;
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335:
336: public function getAllByGroupModule(
337: $groupid,
338: $module_id = 0,
339: $toponlyblock = false,
340: $visible = null,
341: $orderby = 'b.weight, m.block_id',
342: $isactive = 1
343: ) {
344: $ret = array();
345:
346: $qb = $this->db2->createXoopsQueryBuilder();
347: $eb = $qb->expr();
348:
349: $blockids=null;
350: if (isset($groupid)) {
351: $qb ->select('DISTINCT gperm_itemid')
352: ->fromPrefix('system_permission', null)
353: ->where($eb->eq('gperm_name', $eb->literal('block_read')))
354: ->andWhere('gperm_modid=1');
355:
356: if (is_array($groupid) && !empty($groupid)) {
357: $qb->andWhere($eb->in('gperm_groupid', $groupid));
358: } else {
359: if ((int)($groupid) > 0) {
360: $qb->andWhere($eb->eq('gperm_groupid', $groupid));
361: }
362: }
363: $result = $qb->execute();
364: $blockids = $result->fetchAll(\PDO::FETCH_COLUMN);
365: }
366:
367: $qb->resetQueryParts();
368:
369: $qb ->select('b.*')
370: ->fromPrefix('system_block', 'b')
371: ->where($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
372: if (isset($visible)) {
373: $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
374: }
375: if (isset($module_id)) {
376: $qb ->fromPrefix('system_blockmodule', 'm')
377: ->andWhere($eb->eq('m.block_id', 'b.bid'));
378: if (!empty($module_id)) {
379: $in=array();
380: $in[]=0;
381: $in[]=(int)($module_id);
382: if ($toponlyblock) {
383: $in[]=(int)(-1);
384: }
385: } else {
386: if ($toponlyblock) {
387: $in=array(0, -1);
388: } else {
389: $in=0;
390: }
391: }
392: if (is_array($in)) {
393: $qb->andWhere($eb->in('m.module_id', $in));
394: } else {
395: $qb->andWhere($eb->eq('m.module_id', $in));
396: }
397: }
398: if (!empty($blockids)) {
399: $qb->andWhere($eb->in('b.bid', $blockids));
400: }
401: $qb->orderBy($orderby);
402: $result = $qb->execute();
403: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
404: $block = new XoopsBlock($myrow);
405: $ret[$myrow['bid']] = $block;
406: unset($block);
407: }
408: return $ret;
409: }
410:
411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421:
422: public function getNonGroupedBlocks(
423: $module_id = 0,
424: $toponlyblock = false,
425: $visible = null,
426: $orderby = 'b.weight, m.block_id',
427: $isactive = 1
428: ) {
429: $ret = array();
430:
431: $qb = $this->db2->createXoopsQueryBuilder();
432: $eb = $qb->expr();
433:
434: $qb ->select('DISTINCT(bid)')
435: ->fromPrefix('system_block', null);
436: $result = $qb->execute();
437: $bids = $result->fetchAll(\PDO::FETCH_COLUMN);
438:
439: $qb->resetQueryParts();
440:
441: $qb ->select('DISTINCT(p.gperm_itemid)')
442: ->fromPrefix('system_permission', 'p')
443: ->fromPrefix('system_group', 'g')
444: ->where($eb->eq('g.groupid', 'p.gperm_groupid'))
445: ->andWhere($eb->eq('p.gperm_name', $eb->literal('block_read')));
446: $result = $qb->execute();
447: $grouped = $result->fetchAll(\PDO::FETCH_COLUMN);
448:
449: $non_grouped = array_diff($bids, $grouped);
450:
451: if (!empty($non_grouped)) {
452: $qb->resetQueryParts();
453:
454: $qb ->select('b.*')
455: ->fromPrefix('system_block', 'b')
456: ->where($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
457: if (isset($visible)) {
458: $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
459: }
460:
461: if (isset($module_id)) {
462: $qb ->fromPrefix('system_blockmodule', 'm')
463: ->andWhere($eb->eq('m.block_id', 'b.bid'));
464: if (!empty($module_id)) {
465: $in=array();
466: $in[]=0;
467: $in[]=(int)($module_id);
468: if ($toponlyblock) {
469: $in[]=(int)(-1);
470: }
471: } else {
472: if ($toponlyblock) {
473: $in=array(0, -1);
474: } else {
475: $in=0;
476: }
477: }
478: if (is_array($in)) {
479: $qb->andWhere($eb->in('m.module_id', $in));
480: } else {
481: $qb->andWhere($eb->eq('m.module_id', $in));
482: }
483: }
484: $qb->andWhere($eb->in('b.bid', $non_grouped));
485: $qb->orderBy($orderby);
486: $result = $qb->execute();
487: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
488: $block = new XoopsBlock($myrow);
489: $ret[$myrow['bid']] = $block;
490: unset($block);
491: }
492: }
493: return $ret;
494: }
495:
496: 497: 498: 499: 500: 501: 502: 503: 504:
505: public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
506: {
507: $funcNum = (int)($funcNum);
508: $moduleId = (int)($moduleId);
509: if ($funcNum < 1 || $moduleId < 1) {
510:
511: return 0;
512: }
513:
514: $qb = $this->db2->createXoopsQueryBuilder();
515: $eb = $qb->expr();
516:
517: $qb ->select('COUNT(*)')
518: ->fromPrefix('system_block', null)
519: ->where($eb->eq('mid', $qb->createNamedParameter($moduleId, \PDO::PARAM_INT)))
520: ->andWhere($eb->eq('func_num', $qb->createNamedParameter($funcNum, \PDO::PARAM_INT)));
521:
522: if (isset($showFunc)) {
523:
524: $qb->andWhere($eb->eq('show_func', $qb->createNamedParameter($showFunc, \PDO::PARAM_STR)));
525: }
526: if (!$result = $qb->execute()) {
527: return 0;
528: }
529: list ($count) = $result->fetch(\PDO::FETCH_NUM);
530: return $count;
531: }
532:
533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543:
544: public function buildContent($position, $content = "", $contentdb = "")
545: {
546: $ret = '';
547: if ($position == 0) {
548: $ret = $contentdb . $content;
549: } else {
550: if ($position == 1) {
551: $ret = $content . $contentdb;
552: }
553: }
554: return $ret;
555: }
556:
557: 558: 559: 560: 561: 562: 563: 564:
565: public function buildTitle($originaltitle, $newtitle = '')
566: {
567: if ($newtitle != '') {
568: $ret = $newtitle;
569: } else {
570: $ret = $originaltitle;
571: }
572: return $ret;
573: }
574:
575:
576:
577: 578: 579: 580: 581: 582: 583:
584: public function getBlockByPerm($groupid)
585: {
586: $ret = array();
587: if (isset($groupid)) {
588: $qb = $this->db2->createXoopsQueryBuilder();
589: $eb = $qb->expr();
590:
591: $qb ->select('DISTINCT(gperm_itemid)')
592: ->fromPrefix('system_permission', 'p')
593: ->fromPrefix('system_group', 'g')
594: ->where($eb->eq('p.gperm_name', $eb->literal('block_read')))
595: ->andWhere('gperm_modid=1');
596:
597: if (is_array($groupid)) {
598: $qb->andWhere($eb->in('gperm_groupid', $groupid));
599: } else {
600: if ((int)($groupid) > 0) {
601: $qb->andWhere($eb->eq('gperm_groupid', $groupid));
602: }
603: }
604:
605: $result = $qb->execute();
606: $blockids = $result->fetchAll(\PDO::FETCH_COLUMN);
607: return $blockids;
608: }
609: return $ret;
610: }
611: }
612: