Completed
Push — master ( 55ac81...716a76 )
by Richard
17s
created

XoopsBlockHandler::getBlockByPerm()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 1
dl 0
loc 26
rs 9.6666
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444
crap 4.0027
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
 * XoopsBlockHandler
20
 *
21
 * @category  Xoops\Core\Kernel\Handlers\XoopsBlockHandler
22
 * @package   Xoops\Core\Kernel
23
 * @author    Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
24
 * @author    Gregory Mage (AKA Mage)
25
 * @author    trabis <[email protected]>
26
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
27
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28
 * @link      http://xoops.org
29
 */
30
class XoopsBlockHandler extends XoopsPersistableObjectHandler
31
{
32
    /**
33
     * Constructor
34
     *
35
     * @param Connection|null $db database
36
     */
37 20
    public function __construct(Connection $db = null)
38
    {
39 20
        parent::__construct($db, 'system_block', '\Xoops\Core\Kernel\Handlers\XoopsBlock', 'bid', 'name');
40 20
    }
41
42
    /**
43
     * Insert a block
44
     *
45
     * @param XoopsBlock $obj   block object to persist
46
     * @param bool       $force force insert even in 'safe' requests
47
     *
48
     * @return int|false id of insert, or false on error
49
     */
50 1
    public function insertBlock(XoopsBlock $obj, $force = false)
51
    {
52 1
        $obj->setVar('last_modified', time());
53 1
        return parent::insert($obj, $force);
54
    }
55
56
    /**
57
     * Delete a ID from the database
58
     *
59
     * @param XoopsBlock $obj object to delete
60
     *
61
     * @return bool
62
     */
63 1
    public function deleteBlock(XoopsBlock $obj)
64
    {
65 1
        if (!parent::delete($obj)) {
66
            return false;
67
        }
68 1
        $qb = $this->db2->createXoopsQueryBuilder();
69 1
        $eb = $qb->expr();
70 1
        $qb ->deletePrefix('system_permission', null)
71 1
            ->where($eb->eq('gperm_name', $eb->literal('block_read')))
72 1
            ->andWhere($eb->eq('gperm_itemid', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT)))
73 1
            ->andWhere($eb->eq('gperm_modid', $qb->createNamedParameter(1, \PDO::PARAM_INT)))
74 1
            ->execute();
75
76 1
        $qb ->deletePrefix('system_blockmodule', null)
77 1
            ->where($eb->eq('block_id', $qb->createNamedParameter($obj->getVar('bid'), \PDO::PARAM_INT)))
78 1
            ->execute();
79
80 1
        return true;
81
    }
82
83
    /**
84
     * retrieve array of XoopsBlock objects meeting certain conditions
85
     *
86
     * @param CriteriaElement|null $criteria  criteria to match
87
     * @param bool                 $id_as_key should the blocks' bid be the key for the returned array?
88
     *
89
     * @return XoopsBlock[]
90
     **/
91 2
    public function getDistinctObjects(CriteriaElement $criteria = null, $id_as_key = false)
92
    {
93 2
        $ret = array();
94
95 2
        $qb = $this->db2->createXoopsQueryBuilder();
96 2
        $eb = $qb->expr();
97 2
        $qb ->select('DISTINCT(b.bid)')
98 2
            ->addSelect('b.*')
99 2
            ->fromPrefix('system_block', 'b')
100 2
            ->leftJoinPrefix('b', 'system_blockmodule', 'l', $eb->eq('b.bid', 'l.block_id'));
101
102 2
        if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
103 1
            $criteria->renderQb($qb);
104
        }
105
106 2
        $result = $qb->execute();
107 2
        if (!$result) {
108
            return $ret;
109
        }
110 2
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
111 1
            $block = new XoopsBlock();
112 1
            $block->assignVars($myrow);
113 1
            if (!$id_as_key) {
114 1
                $ret[] = $block;
115
            } else {
116
                $ret[$myrow['bid']] = $block;
117
            }
118 1
            unset($block);
119
        }
120 2
        return $ret;
121
122
    }
123
124
    /**
125
     * get a list of blocks matching certain conditions
126
     *
127
     * @param CriteriaElement|null $criteria conditions to match
128
     *
129
     * @return array array of blocks matching the conditions
130
     **/
131 1
    public function getNameList(CriteriaElement $criteria = null)
132
    {
133 1
        $blocks = $this->getObjects($criteria, true);
134 1
        $ret = array();
135 1
        foreach (array_keys($blocks) as $i) {
136 1
            $name = (!$blocks[$i]->isCustom()) ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
137 1
            $ret[$i] = $name;
138
        }
139 1
        return $ret;
140
    }
141
142
    /**
143
     * get all the blocks that match the supplied parameters
144
     *
145
     * @param int|int[] $groupid  groupid (can be an array)
146
     * @param bool      $asobject retrieve as objects
147
     * @param int       $side     values:
148
     *                               0: sideblock - left
149
     *                               1: sideblock - right
150
     *                               2: sideblock - left and right
151
     *                               3: centerblock - left
152
     *                               4: centerblock - right
153
     *                               5: centerblock - center
154
     *                               6: centerblock - left, right, center
155
     * @param int|null  $visible  0: not visible 1: visible
156
     * @param string    $orderby  order of the blocks
157
     * @param int       $isactive 1: active or 0:inactive blocks
158
     *
159
     * @return array of block objects
160
     */
161 1
    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 1
        $ret = array();
170 1
        $qb = $this->db2->createXoopsQueryBuilder();
171 1
        $eb = $qb->expr();
172 1
        if ($asobject) {
173 1
            $qb ->select('b.*');
174
        } else {
175 1
            $qb ->select('b.bid');
176
        }
177 1
        $qb ->fromPrefix('system_block', 'b')
178 1
            ->leftJoinPrefix('b', 'system_permission', 'l', $eb->eq('b.bid', 'l.gperm_itemid'))
179 1
            ->where($eb->eq('gperm_name', $eb->literal('block_read')))
180 1
            ->andWhere($eb->eq('gperm_modid', 1));
181
182 1
        if (is_array($groupid)) {
183 1
            if (count($groupid) > 1) {
184 1
                $in=array();
185 1
                foreach ($groupid as $gid) {
186 1
                    $in[] = $qb->createNamedParameter($gid, \PDO::PARAM_INT);
187
                }
188 1
                $qb->andWhere($eb->in('l.gperm_groupid', $in));
189
            }
190
        } else {
191 1
            $qb->andWhere($eb->eq('l.gperm_groupid', $qb->createNamedParameter($groupid, \PDO::PARAM_INT)));
192
        }
193 1
        $qb->andWhere($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
194 1
        if (isset($side)) {
195
            // get both sides in sidebox? (some themes need this)
196 1
            if ($side == XOOPS_SIDEBLOCK_BOTH) {
197 1
                $qb->andWhere($eb->in('b.side', array(0,1)));
198 1
            } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
199 1
                $qb->andWhere($eb->in('b.side', array(3,4,5,7,8,9)));
200
            } else {
201 1
                $qb->andWhere($eb->eq('b.side', $qb->createNamedParameter($side, \PDO::PARAM_INT)));
202
            }
203
        }
204 1
        if (isset($visible)) {
205 1
            $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
206
        }
207 1
        $qb->orderBy($orderby);
208 1
        $result = $qb->execute();
209 1
        $added = array();
210 1
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
211 1
            if (!in_array($myrow['bid'], $added)) {
212 1
                if (!$asobject) {
213 1
                    $ret[] = $myrow['bid'];
214
                } else {
215 1
                    $ret[] = new XoopsBlock($myrow);
216
                }
217 1
                array_push($added, $myrow['bid']);
218
            }
219
        }
220 1
        return $ret;
221
    }
222
223
    /**
224
     * getAllBlocks matching selection criteria
225
     *
226
     * @param string   $rettype  what to return, values can be object, list or id
227
     * @param int      $side     block location (side)
228
     * @param int|null $visible  null for all, 0 not visible, 1 for visible only
229
     * @param string   $orderby  comma separated columns to order by
230
     * @param int      $isactive 1: active or 0:inactive blocks
231
     *
232
     * @return array
233
     */
234 1
    public function getAllBlocks(
235
        $rettype = "object",
236
        $side = null,
237
        $visible = null,
238
        $orderby = "side,weight,bid",
239
        $isactive = 1
240
    ) {
241 1
        $ret = array();
242 1
        $qb = $this->db2->createXoopsQueryBuilder();
243 1
        $eb = $qb->expr();
244
245 1
        $qb ->fromPrefix('system_block', null)
246 1
            ->where($eb->eq('isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
247 1
        if (isset($side)) {
248
            // get both sides in sidebox? (some themes need this)
249 1
            if ($side == XOOPS_SIDEBLOCK_BOTH) {
250 1
                $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 1
        if (isset($visible)) {
258 1
            $qb->andWhere($eb->eq('visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
259
        }
260 1
        $qb->orderBy($orderby);
261 1
        switch ($rettype) {
262 1
            case "object":
263 1
                $qb->select('*');
264 1
                $result = $qb->execute();
265 1
                while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
266 1
                    $ret[] = new XoopsBlock($myrow);
267
                }
268 1
                break;
269 1
            case "list":
270 1
                $qb->select('*');
271 1
                $result = $qb->execute();
272 1
                while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
273 1
                    $block = new XoopsBlock($myrow);
274 1
                    $title = $block->getVar("title");
275 1
                    $title = empty($title) ? $block->getVar("name") : $title;
276 1
                    $ret[$block->getVar("bid")] = $title;
277
                }
278 1
                break;
279 1
            case "id":
280 1
                $qb->select('bid');
281 1
                $result = $qb->execute();
282 1
                while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
283 1
                    $ret[] = $myrow['bid'];
284
                }
285 1
                break;
286
        }
287
288 1
        return $ret;
289
    }
290
291
    /**
292
     * get blocks by module id
293
     *
294
     * @param int  $moduleid module id
295
     * @param bool $asobject true to fetch as objects, otherwise associative array
296
     *
297
     * @return array of block information
298
     */
299 1
    public function getByModule($moduleid, $asobject = true)
300
    {
301 1
        $qb = $this->db2->createXoopsQueryBuilder();
302 1
        $eb = $qb->expr();
303
304 1
        $qb ->fromPrefix('system_block', null)
305 1
            ->where($eb->eq('mid', $qb->createNamedParameter($moduleid, \PDO::PARAM_INT)));
306 1
        if ($asobject == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
307 1
            $qb->select('*');
308
        } else {
309 1
            $qb->select('bid');
310
        }
311
312 1
        $ret = array();
313 1
        $result = $qb->execute();
314 1
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
315
            if ($asobject) {
316
                $ret[] = new XoopsBlock($myrow);
317
            } else {
318
                $ret[] = $myrow['bid'];
319
            }
320
        }
321 1
        return $ret;
322
    }
323
324
    /**
325
     * XoopsBlock::getAllByGroupModule()
326
     *
327
     * @param mixed   $groupid      int group id, int[] of group ids,
328
     * @param integer $module_id    module id
329
     * @param boolean $toponlyblock only for top block
330
     * @param mixed   $visible      restrict by visible values
331
     * @param string  $orderby      comma separated list of columns to order by
332
     * @param integer $isactive     restrict by isactive values
333
     *
334
     * @return array
335
     */
336 3
    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 3
        $ret = array();
345
346 3
        $qb = $this->db2->createXoopsQueryBuilder();
347 3
        $eb = $qb->expr();
348
349 3
        $blockids=null;
350 3
        if (isset($groupid)) {
351 3
            $qb ->select('DISTINCT gperm_itemid')
352 3
                ->fromPrefix('system_permission', null)
353 3
                ->where($eb->eq('gperm_name', $eb->literal('block_read')))
354 3
                ->andWhere('gperm_modid=1');
355
356 3
            if (is_array($groupid) && !empty($groupid)) {
357 3
                $qb->andWhere($eb->in('gperm_groupid', $groupid));
358
            } else {
359 1
                if ((int)($groupid) > 0) {
360 1
                    $qb->andWhere($eb->eq('gperm_groupid', $groupid));
361
                }
362
            }
363 3
            $result = $qb->execute();
364 3
            $blockids = $result->fetchAll(\PDO::FETCH_COLUMN);
365
        }
366
367 3
        $qb->resetQueryParts();
368
369 3
        $qb ->select('b.*')
370 3
            ->fromPrefix('system_block', 'b')
371 3
            ->where($eb->eq('b.isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
372 3
        if (isset($visible)) {
373 2
            $qb->andWhere($eb->eq('b.visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
374
        }
375 3
        if (isset($module_id)) {
376 3
            $qb ->fromPrefix('system_blockmodule', 'm')
377 3
                ->andWhere($eb->eq('m.block_id', 'b.bid'));
378 3
            if (!empty($module_id)) {
379 1
                $in=array();
380 1
                $in[]=0;
381 1
                $in[]=(int)($module_id);
382 1
                if ($toponlyblock) {
383 1
                    $in[]=(int)(-1);
384
                }
385
            } else {
386 3
                if ($toponlyblock) {
387
                    $in=array(0, -1);
388
                } else {
389 3
                    $in=0;
390
                }
391
            }
392 3
            if (is_array($in)) {
393 1
                $qb->andWhere($eb->in('m.module_id', $in));
394
            } else {
395 3
                $qb->andWhere($eb->eq('m.module_id', $in));
396
            }
397
        }
398 3
        if (!empty($blockids)) {
399 3
            $qb->andWhere($eb->in('b.bid', $blockids));
400
        }
401 3
        $qb->orderBy($orderby);
402 3
        $result = $qb->execute();
403 3
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
404 3
            $block = new XoopsBlock($myrow);
405 3
            $ret[$myrow['bid']] = $block;
406 3
            unset($block);
407
        }
408 3
        return $ret;
409
    }
410
411
    /**
412
     * XoopsBlock::getNonGroupedBlocks()
413
     *
414
     * @param integer $module_id    module id
415
     * @param boolean $toponlyblock only for top block
416
     * @param mixed   $visible      restrict by visible values
417
     * @param string  $orderby      comma separated list of columns to order by
418
     * @param integer $isactive     restrict by isactive values
419
     *
420
     * @return array
421
     */
422 1
    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 1
        $ret = array();
430
431 1
        $qb = $this->db2->createXoopsQueryBuilder();
432 1
        $eb = $qb->expr();
433
434 1
        $qb ->select('DISTINCT(bid)')
435 1
            ->fromPrefix('system_block', null);
436 1
        $result = $qb->execute();
437 1
        $bids = $result->fetchAll(\PDO::FETCH_COLUMN);
438
439 1
        $qb->resetQueryParts();
440
441 1
        $qb ->select('DISTINCT(p.gperm_itemid)')
442 1
            ->fromPrefix('system_permission', 'p')
443 1
            ->fromPrefix('system_group', 'g')
444 1
            ->where($eb->eq('g.groupid', 'p.gperm_groupid'))
445 1
            ->andWhere($eb->eq('p.gperm_name', $eb->literal('block_read')));
446 1
        $result = $qb->execute();
447 1
        $grouped = $result->fetchAll(\PDO::FETCH_COLUMN);
448
449 1
        $non_grouped = array_diff($bids, $grouped);
450
451 1
        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 1
        return $ret;
494
    }
495
496
    /**
497
     * XoopsBlock::countSimilarBlocks()
498
     *
499
     * @param int    $moduleId module id
500
     * @param string $funcNum  func number
501
     * @param string $showFunc show function
502
     *
503
     * @return int count
504
     */
505 1
    public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
506
    {
507 1
        $funcNum = (int)($funcNum);
508 1
        $moduleId = (int)($moduleId);
509 1
        if ($funcNum < 1 || $moduleId < 1) {
510
            // invalid query
511
            return 0;
512
        }
513
514 1
        $qb = $this->db2->createXoopsQueryBuilder();
515 1
        $eb = $qb->expr();
516
517 1
        $qb ->select('COUNT(*)')
518 1
            ->fromPrefix('system_block', null)
519 1
            ->where($eb->eq('mid', $qb->createNamedParameter($moduleId, \PDO::PARAM_INT)))
520 1
            ->andWhere($eb->eq('func_num', $qb->createNamedParameter($funcNum, \PDO::PARAM_INT)));
521
522 1
        if (isset($showFunc)) {
523
            // showFunc is set for more strict comparison
524 1
            $qb->andWhere($eb->eq('show_func', $qb->createNamedParameter($showFunc, \PDO::PARAM_STR)));
525
        }
526 1
        if (!$result = $qb->execute()) {
527
            return 0;
528
        }
529 1
        list ($count) = $result->fetch(\PDO::FETCH_NUM);
530 1
        return $count;
531
    }
532
533
    /**
534
     * Aligns the content of a block
535
     *
536
     * @param integer $position  order of content
537
     *                              0 -> content in DB is positioned before the original content
538
     *                              1 -> content in DB is positioned after the original content
539
     * @param string  $content   content
540
     * @param string  $contentdb content from database
541
     *
542
     * @return string
543
     */
544 1
    public function buildContent($position, $content = "", $contentdb = "")
545
    {
546 1
        $ret = '';
547 1
        if ($position == 0) {
548 1
            $ret = $contentdb . $content;
549
        } else {
550 1
            if ($position == 1) {
551 1
                $ret = $content . $contentdb;
552
            }
553
        }
554 1
        return $ret;
555
    }
556
557
    /**
558
     * Enter description here... appears to be unused?
559
     *
560
     * @param string $originaltitle original title
561
     * @param string $newtitle      new title
562
     *
563
     * @return string title winner of the title war?
564
     */
565 1
    public function buildTitle($originaltitle, $newtitle = '')
566
    {
567 1
        if ($newtitle != '') {
568 1
            $ret = $newtitle;
569
        } else {
570 1
            $ret = $originaltitle;
571
        }
572 1
        return $ret;
573
    }
574
575
    /************ system ***************/
576
577
    /**
578
     * get list of ids of block that a group has permission to views
579
     *
580
     * @param null|integer $groupid group
581
     *
582
     * @return int[]
583
     */
584 1
    public function getBlockByPerm($groupid)
585
    {
586 1
        $ret = array();
587 1
        if (isset($groupid)) {
588 1
            $qb = $this->db2->createXoopsQueryBuilder();
589 1
            $eb = $qb->expr();
590
591 1
            $qb ->select('DISTINCT(gperm_itemid)')
592 1
                ->fromPrefix('system_permission', 'p')
593 1
                ->fromPrefix('system_group', 'g')
594 1
                ->where($eb->eq('p.gperm_name', $eb->literal('block_read')))
595 1
                ->andWhere('gperm_modid=1');
596
597 1
            if (is_array($groupid)) {
0 ignored issues
show
introduced by
The condition is_array($groupid) is always false.
Loading history...
598
                $qb->andWhere($eb->in('gperm_groupid', $groupid));
599
            } else {
600 1
                if ((int)($groupid) > 0) {
601 1
                    $qb->andWhere($eb->eq('gperm_groupid', $groupid));
602
                }
603
            }
604
605 1
            $result = $qb->execute();
606 1
            $blockids = $result->fetchAll(\PDO::FETCH_COLUMN);
607 1
            return $blockids;
608
        }
609 1
        return $ret;
610
    }
611
}
612