Test Failed
Branch master (19f93b)
by Michael
10:44
created

XoopsBlock::getAllBlocksByGroup()   D

Complexity

Conditions 13
Paths 240

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 4.8754
c 0
b 0
f 0
cc 13
eloc 39
nc 240
nop 6

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * XOOPS Block management
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/
17
 * @author              Skalpa Keo <[email protected]>
18
 * @author              Taiwen Jiang <[email protected]>
19
 */
20
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22
23
include_once $GLOBALS['xoops']->path('kernel/object.php');
24
25
/**
26
 * Class XoopsBlock
27
 */
28
class XoopsBlock extends XoopsObject
29
{
30
    public $db;
31
32
    /**
33
     * @param null|array $id
34
     */
35
    public function __construct($id = null)
36
    {
37
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
38
        $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
39
        $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
40
        $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
41
        $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
42
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
43
        //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
44
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
45
        $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
46
        $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
47
        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
48
        $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
49
        // The block_type is in a mess, let's say:
50
        // S - generated by system module
51
        // M - generated by a non-system module
52
        // C - Custom block
53
        // D - cloned system/module block
54
        // E - cloned custom block, DON'T use it
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
59
        $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
60
        $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
61
        $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
62
        $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
63
64
        $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
65
        $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
66
        $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
67
68
        if (!empty($id)) {
69
            if (is_array($id)) {
0 ignored issues
show
introduced by
The condition is_array($id) is always true.
Loading history...
70
                $this->assignVars($id);
71
            } else {
72
                $this->load((int)$id);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Load $id
79
     *
80
     * @param int $id
81
     */
82
    public function load($id)
83
    {
84
        $id  = (int)$id;
85
        $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks') . ' WHERE bid = ' . $id;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on XoopsBlock. Did you maybe forget to declare it?
Loading history...
86
        $arr = $this->db->fetchArray($this->db->query($sql));
87
        $this->assignVars($arr);
88
    }
89
90
    /**
91
     * Store Block Data to Database
92
     *
93
     * @return int $id
94
     */
95
    public function store()
96
    {
97
        if (!$this->cleanVars()) {
98
            return false;
99
        }
100
        foreach ($this->cleanVars as $k => $v) {
101
            ${$k} = $v;
102
        }
103
        if (empty($bid)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bid seems to never exist and therefore empty should always be true.
Loading history...
104
            $bid = $this->db->genId($this->db->prefix('newblocks') . '_bid_seq');
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on XoopsBlock. Did you maybe forget to declare it?
Loading history...
105
            $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)', $this->db->prefix('newblocks'), $bid, $mid, $func_num, $this->db->quoteString($options), $this->db->quoteString($name), $this->db->quoteString($title), $this->db->quoteString($content), $side, $weight, $visible, $this->db->quoteString($block_type), $this->db->quoteString($c_type), 1, $this->db->quoteString($dirname), $this->db->quoteString($func_file), $this->db->quoteString($show_func), $this->db->quoteString($edit_func), $this->db->quoteString($template), $bcachetime, time());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $template seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $edit_func seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $func_num seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dirname seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $c_type seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $side seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $content seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $show_func seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $visible seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $func_file seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $mid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $bcachetime seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $block_type seems to be never defined.
Loading history...
106
        } else {
107
            $sql = 'UPDATE ' . $this->db->prefix('newblocks') . ' SET options=' . $this->db->quoteString($options);
108
            // a custom block needs its own name
109
            if ($this->isCustom() /* in_array( $block_type , array( 'C' , 'E' ) ) */) {
110
                $sql .= ', name=' . $this->db->quoteString($name);
111
            }
112
            $sql .= ', isactive=' . $isactive . ', title=' . $this->db->quoteString($title) . ', content=' . $this->db->quoteString($content) . ', side=' . $side . ', weight=' . $weight . ', visible=' . $visible . ', c_type=' . $this->db->quoteString($c_type) . ', template=' . $this->db->quoteString($template) . ', bcachetime=' . $bcachetime . ', last_modified=' . time() . ' WHERE bid=' . $bid;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $isactive seems to be never defined.
Loading history...
113
        }
114
        if (!$this->db->query($sql)) {
115
            $this->setErrors('Could not save block data into database');
116
117
            return false;
118
        }
119
        if (empty($bid)) {
120
            $bid = $this->db->getInsertId();
121
        }
122
123
        return $bid;
124
    }
125
126
    /**
127
     * Delete a ID from the database
128
     *
129
     * @return bool
130
     */
131
    public function delete()
132
    {
133
        $sql = sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $this->getVar('bid'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('bid') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
        $sql = sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), /** @scrutinizer ignore-type */ $this->getVar('bid'));
Loading history...
Bug Best Practice introduced by
The property db does not exist on XoopsBlock. Did you maybe forget to declare it?
Loading history...
134
        if (!$this->db->query($sql)) {
135
            return false;
136
        }
137
        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'block_read' AND gperm_itemid = %u AND gperm_modid = 1", $this->db->prefix('group_permission'), $this->getVar('bid'));
138
        $this->db->query($sql);
139
        $sql = sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $this->getVar('bid'));
140
        $this->db->query($sql);
141
142
        return true;
143
    }
144
145
    /**
146
     * do stripslashes/htmlspecialchars according to the needed output
147
     *
148
     * @param string $format output use: 's' for Show and 'e' for Edit
149
     * @param string $c_type type of block content
150
     *
151
     * @returns string
152
     */
153
    public function getContent($format = 's', $c_type = 't')
154
    {
155
        switch ($format) {
156
            case 's':
157
                // check the type of content
158
                // H : custom HTML block
159
                // P : custom PHP block
160
                // S : use text sanitizater (smilies enabled)
161
                // T : use text sanitizater (smilies disabled)
162
                if ($c_type === 'H') {
163
                    return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
164
                } elseif ($c_type === 'P') {
165
                    ob_start();
166
                    echo eval($this->getVar('content', 'n'));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
167
                    $content = ob_get_contents();
168
                    ob_end_clean();
169
170
                    return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content);
171
                } elseif ($c_type === 'S') {
172
                    $myts    = MyTextSanitizer::getInstance();
173
                    $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
174
175
                    return $myts->displayTarea($content, 1, 1);
176
                } else {
177
                    $myts    = MyTextSanitizer::getInstance();
178
                    $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
179
180
                    return $myts->displayTarea($content, 1, 0);
181
                }
182
                break;
183
            case 'e':
184
                return $this->getVar('content', 'e');
185
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
186
            default:
187
                return $this->getVar('content', 'n');
188
                break;
189
        }
190
    }
191
192
    /**
193
     * Build Block
194
     *
195
     * @return unknown
0 ignored issues
show
Bug introduced by
The type unknown was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
196
     */
197
    public function buildBlock()
198
    {
199
        global $xoopsConfig, $xoopsOption, $xoTheme;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
200
        $block = array();
201
        if (!$this->isCustom()) {
202
            // get block display function
203
            $show_func = $this->getVar('show_func');
204
            if (!$show_func) {
205
                return false;
206
            }
207
            if (!file_exists($func_file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
208
                return false;
209
            }
210
            // must get lang files b4 including the file
211
            // some modules require it for code that is outside the function
212
            xoops_loadLanguage('blocks', $this->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('dirname') can also be of type array and array; however, parameter $domain of xoops_loadLanguage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
            xoops_loadLanguage('blocks', /** @scrutinizer ignore-type */ $this->getVar('dirname'));
Loading history...
213
            include_once $func_file;
214
215
            if (function_exists($show_func)) {
216
                // execute the function
217
                $options = explode('|', $this->getVar('options'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('options') can also be of type array and array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
                $options = explode('|', /** @scrutinizer ignore-type */ $this->getVar('options'));
Loading history...
218
                $block   = $show_func($options);
219
                if (!$block) {
220
                    return false;
221
                }
222
            } else {
223
                return false;
224
            }
225
        } else {
226
            // it is a custom block, so just return the contents
227
            $block['content'] = $this->getContent('s', $this->getVar('c_type'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('c_type') can also be of type array and array; however, parameter $c_type of XoopsBlock::getContent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
            $block['content'] = $this->getContent('s', /** @scrutinizer ignore-type */ $this->getVar('c_type'));
Loading history...
228
            if (empty($block['content'])) {
229
                return false;
230
            }
231
        }
232
233
        return $block;
234
    }
235
236
    /*
237
    * Aligns the content of a block
238
    * If position is 0, content in DB is positioned
239
    * before the original content
240
    * If position is 1, content in DB is positioned
241
    * after the original content
242
    */
243
    /**
244
     * @param        $position
245
     * @param string $content
246
     * @param string $contentdb
247
     *
248
     * @return string
249
     */
250
    public function buildContent($position, $content = '', $contentdb = '')
251
    {
252
        if ($position == 0) {
253
            $ret = $contentdb . $content;
254
        } elseif ($position == 1) {
255
            $ret = $content . $contentdb;
256
        }
257
258
        return $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
259
    }
260
261
    /**
262
     * Enter description here...
263
     *
264
     * @param  string $originaltitle
265
     * @param  string $newtitle
266
     * @return string title
267
     */
268
    public function buildTitle($originaltitle, $newtitle = '')
269
    {
270
        $ret = $originaltitle;
271
        if ($newtitle != '') {
272
            $ret = $newtitle;
273
        }
274
275
        return $ret;
276
    }
277
278
    /**
279
     * XoopsBlock::isCustom()
280
     *
281
     * @return bool
282
     */
283
    public function isCustom()
284
    {
285
        return in_array($this->getVar('block_type'), array(
286
            'C',
287
            'E'));
288
    }
289
290
    /**
291
     * XoopsBlock::getOptions()
292
     *
293
     * @return bool
294
     */
295
    public function getOptions()
296
    {
297
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
298
        if (!$this->isCustom()) {
299
            $edit_func = $this->getVar('edit_func');
300
            if (!$edit_func) {
301
                return false;
302
            }
303
            if (file_exists($GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) {
304
                if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/blocks.php'))) {
305
                    include_once $file;
306
                } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/language/english/blocks.php'))) {
307
                    include_once $file;
308
                }
309
                include_once $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'));
310
                $options   = explode('|', $this->getVar('options'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('options') can also be of type array and array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

310
                $options   = explode('|', /** @scrutinizer ignore-type */ $this->getVar('options'));
Loading history...
311
                $edit_form = $edit_func($options);
312
                if (!$edit_form) {
313
                    return false;
314
                }
315
316
                return $edit_form;
317
            } else {
318
                return false;
319
            }
320
        } else {
321
            return false;
322
        }
323
    }
324
325
    /**
326
     * get all the blocks that match the supplied parameters
327
     * @param int|array $groupid  groupid (can be an array)
328
     * @param bool   $asobject
329
     * @param null|string $side     0: sideblock - left
330
     *                         1: sideblock - right
331
     *                         2: sideblock - left and right
332
     *                         3: centerblock - left
333
     *                         4: centerblock - right
334
     *                         5: centerblock - center
335
     *                         6: centerblock - left, right, center
336
     * @param        $visible  0: not visible 1: visible
337
     * @param string $orderby  order of the blocks
338
     * @param int    $isactive
339
     * @returns array of block objects
340
     */
341
    public static function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = 'b.weight,b.bid', $isactive = 1)
342
    {
343
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
344
        $ret = array();
345
        $sql = 'SELECT b.* ';
346
        if (!$asobject) {
347
            $sql = 'SELECT b.bid ';
348
        }
349
        $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";
350
        if (is_array($groupid)) {
351
            $sql .= ' AND (l.gperm_groupid=' . $groupid[0] . '';
352
            $size = count($groupid);
353
            if ($size > 1) {
354
                for ($i = 1; $i < $size; ++$i) {
355
                    $sql .= ' OR l.gperm_groupid=' . $groupid[$i] . '';
356
                }
357
            }
358
            $sql .= ')';
359
        } else {
360
            $sql .= ' AND l.gperm_groupid=' . $groupid . '';
361
        }
362
        $sql .= ' AND b.isactive=' . $isactive;
363
        if (isset($side)) {
364
            // get both sides in sidebox? (some themes need this)
365
            if ($side == XOOPS_SIDEBLOCK_BOTH) {
366
                $side = '(b.side=0 OR b.side=1)';
367
            } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
368
                $side = '(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )';
369
            } elseif ($side == XOOPS_FOOTERBLOCK_ALL) {
370
                $side = '(b.side=10 OR b.side=11 OR b.side=12 )';
371
            } else {
372
                $side = 'b.side=' . $side;
373
            }
374
            $sql .= ' AND ' . $side;
375
        }
376
        if (isset($visible)) {
377
            $sql .= " AND b.visible=$visible";
378
        }
379
        $sql .= " ORDER BY $orderby";
380
        $result = $db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

380
        /** @scrutinizer ignore-call */ 
381
        $result = $db->query($sql);
Loading history...
381
        $added  = array();
382
        while (false !== ($myrow = $db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

382
        while (false !== ($myrow = $db->/** @scrutinizer ignore-call */ fetchArray($result))) {
Loading history...
383
            if (!in_array($myrow['bid'], $added)) {
384
                if (!$asobject) {
385
                    $ret[] = $myrow['bid'];
386
                } else {
387
                    $ret[] = new XoopsBlock($myrow);
388
                }
389
                $added[] = $myrow['bid'];
390
            }
391
        }
392
393
        return $ret;
394
    }
395
396
    /**
397
     * XoopsBlock::getAllBlocks()
398
     *
399
     * @param  string  $rettype
400
     * @param  mixed   $side
401
     * @param  mixed   $visible
402
     * @param  string  $orderby
403
     * @param  integer $isactive
404
     * @return array
405
     */
406
    public function getAllBlocks($rettype = 'object', $side = null, $visible = null, $orderby = 'side,weight,bid', $isactive = 1)
407
    {
408
        $db          = XoopsDatabaseFactory::getDatabaseConnection();
409
        $ret         = array();
410
        $where_query = ' WHERE isactive=' . $isactive;
411
        if (isset($side)) {
412
            // get both sides in sidebox? (some themes need this)
413
            if ($side == XOOPS_SIDEBLOCK_BOTH) {
414
                $side = '(side=0 OR side=1)';
415
            } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
416
                $side = '(side=3 OR side=4 OR side=5 OR side=7 OR side=8 OR side=9)';
417
            } elseif ($side == XOOPS_FOOTERBLOCK_ALL) {
418
                $side = '(side=10 OR side=11 OR side=12)';
419
            } else {
420
                $side = 'side=' . $side;
421
            }
422
            $where_query .= ' AND ' . $side;
423
        }
424
        if (isset($visible)) {
425
            $where_query .= ' AND visible=.' . $visible;
426
        }
427
        $where_query .= ' ORDER BY ' . $orderby;
428
        switch ($rettype) {
429
            case 'object':
430
                $sql    = 'SELECT * FROM ' . $db->prefix('newblocks') . '' . $where_query;
431
                $result = $db->query($sql);
432
                while (false !== ($myrow = $db->fetchArray($result))) {
433
                    $ret[] = new XoopsBlock($myrow);
434
                }
435
                break;
436
            case 'list':
437
                $sql    = 'SELECT * FROM ' . $db->prefix('newblocks') . '' . $where_query;
438
                $result = $db->query($sql);
439
                while (false !== ($myrow = $db->fetchArray($result))) {
440
                    $block                      = new XoopsBlock($myrow);
441
                    $title                      = $block->getVar('title');
442
                    $title                      = empty($title) ? $block->getVar('name') : $title;
443
                    $ret[$block->getVar('bid')] = $title;
444
                }
445
                break;
446
            case 'id':
447
                $sql    = 'SELECT bid FROM ' . $db->prefix('newblocks') . '' . $where_query;
448
                $result = $db->query($sql);
449
                while (false !== ($myrow = $db->fetchArray($result))) {
450
                    $ret[] = $myrow['bid'];
451
                }
452
                break;
453
        }
454
455
        //echo $sql;
456
        return $ret;
457
    }
458
459
    /**
460
     * XoopsBlock::getByModule()
461
     *
462
     * @param  mixed $moduleid
463
     * @param  mixed $asobject
464
     * @return array
465
     */
466
    public static function getByModule($moduleid, $asobject = true)
467
    {
468
        $moduleid = (int)$moduleid;
469
        $db       = XoopsDatabaseFactory::getDatabaseConnection();
470
        if ($asobject == true) {
471
            $sql = $sql = 'SELECT * FROM ' . $db->prefix('newblocks') . ' WHERE mid=' . $moduleid;
0 ignored issues
show
Unused Code introduced by
The assignment to $sql is dead and can be removed.
Loading history...
472
        } else {
473
            $sql = 'SELECT bid FROM ' . $db->prefix('newblocks') . ' WHERE mid=' . $moduleid;
474
        }
475
        $result = $db->query($sql);
476
        $ret    = array();
477
        while (false !== ($myrow = $db->fetchArray($result))) {
478
            if ($asobject) {
479
                $ret[] = new XoopsBlock($myrow);
480
            } else {
481
                $ret[] = $myrow['bid'];
482
            }
483
        }
484
485
        return $ret;
486
    }
487
488
    /**
489
     * XoopsBlock::getAllByGroupModule()
490
     *
491
     * @param  mixed   $groupid
492
     * @param  integer $module_id
493
     * @param  mixed   $toponlyblock
494
     * @param  mixed   $visible
495
     * @param  string  $orderby
496
     * @param  integer $isactive
497
     * @return array
498
     */
499
    public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
500
    {
501
        $isactive = (int)$isactive;
502
        $db       = XoopsDatabaseFactory::getDatabaseConnection();
503
        $ret      = array();
504
        if (isset($groupid)) {
505
            $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1";
506
            if (is_array($groupid)) {
507
                $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')';
508
            } else {
509
                if ((int)$groupid > 0) {
510
                    $sql .= ' AND gperm_groupid=' . (int)$groupid;
511
                }
512
            }
513
            $result   = $db->query($sql);
514
            $blockids = array();
515
            while (false !== ($myrow = $db->fetchArray($result))) {
516
                $blockids[] = $myrow['gperm_itemid'];
517
            }
518
            if (empty($blockids)) {
519
                return $blockids;
520
            }
521
        }
522
        $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
523
        $sql .= ' AND b.isactive=' . $isactive;
524
        if (isset($visible)) {
525
            $sql .= ' AND b.visible=' . (int)$visible;
526
        }
527
        if (!isset($module_id)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
528
        } elseif (!empty($module_id)) {
529
            $sql .= ' AND m.module_id IN (0,' . (int)$module_id;
530
            if ($toponlyblock) {
531
                $sql .= ',-1';
532
            }
533
            $sql .= ')';
534
        } else {
535
            if ($toponlyblock) {
536
                $sql .= ' AND m.module_id IN (0,-1)';
537
            } else {
538
                $sql .= ' AND m.module_id=0';
539
            }
540
        }
541
        if (!empty($blockids)) {
542
            $sql .= ' AND b.bid IN (' . implode(',', $blockids) . ')';
543
        }
544
        $sql .= ' ORDER BY ' . $orderby;
545
        $result = $db->query($sql);
546
        while (false !== ($myrow = $db->fetchArray($result))) {
547
            $block              = new XoopsBlock($myrow);
548
            $ret[$myrow['bid']] = &$block;
549
            unset($block);
550
        }
551
552
        return $ret;
553
    }
554
555
    /**
556
     * XoopsBlock::getNonGroupedBlocks()
557
     *
558
     * @param  integer $module_id
559
     * @param  mixed   $toponlyblock
560
     * @param  mixed   $visible
561
     * @param  string  $orderby
562
     * @param  integer $isactive
563
     * @return array
564
     */
565
    public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)
566
    {
567
        $db   = XoopsDatabaseFactory::getDatabaseConnection();
568
        $ret  = array();
569
        $bids = array();
570
        $sql  = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks');
571
        if ($result = $db->query($sql)) {
572
            while (false !== ($myrow = $db->fetchArray($result))) {
573
                $bids[] = $myrow['bid'];
574
            }
575
        }
576
        $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'";
577
        $grouped = array();
578
        if ($result = $db->query($sql)) {
579
            while (false !== ($myrow = $db->fetchArray($result))) {
580
                $grouped[] = $myrow['gperm_itemid'];
581
            }
582
        }
583
        $non_grouped = array_diff($bids, $grouped);
584
        if (!empty($non_grouped)) {
585
            $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid';
586
            $sql .= ' AND b.isactive=' . (int)$isactive;
587
            if (isset($visible)) {
588
                $sql .= ' AND b.visible=' . (int)$visible;
589
            }
590
            if (!isset($module_id)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
591
            } elseif (!empty($module_id)) {
592
                $sql .= ' AND m.module_id IN (0,' . (int)$module_id;
593
                if ($toponlyblock) {
594
                    $sql .= ',-1';
595
                }
596
                $sql .= ')';
597
            } else {
598
                if ($toponlyblock) {
599
                    $sql .= ' AND m.module_id IN (0,-1)';
600
                } else {
601
                    $sql .= ' AND m.module_id=0';
602
                }
603
            }
604
            $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')';
605
            $sql .= ' ORDER BY ' . $orderby;
606
            $result = $db->query($sql);
607
            while (false !== ($myrow = $db->fetchArray($result))) {
608
                $block              = new XoopsBlock($myrow);
609
                $ret[$myrow['bid']] =& $block;
610
                unset($block);
611
            }
612
        }
613
614
        return $ret;
615
    }
616
617
    /**
618
     * XoopsBlock::countSimilarBlocks()
619
     *
620
     * @param  mixed $moduleId
621
     * @param  mixed $funcNum
622
     * @param  mixed $showFunc
623
     * @return int
624
     */
625
    public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
626
    {
627
        $funcNum  = (int)$funcNum;
628
        $moduleId = (int)$moduleId;
629
        if ($funcNum < 1 || $moduleId < 1) {
630
            // invalid query
631
            return 0;
632
        }
633
        $db = XoopsDatabaseFactory::getDatabaseConnection();
634
        if (isset($showFunc)) {
635
            // showFunc is set for more strict comparison
636
            $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)));
0 ignored issues
show
Bug introduced by
The method quoteString() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

636
            $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s', $db->prefix('newblocks'), $moduleId, $funcNum, $db->/** @scrutinizer ignore-call */ quoteString(trim($showFunc)));
Loading history...
637
        } else {
638
            $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d', $db->prefix('newblocks'), $moduleId, $funcNum);
639
        }
640
        if (!$result = $db->query($sql)) {
641
            return 0;
642
        }
643
        list($count) = $db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

643
        /** @scrutinizer ignore-call */ 
644
        list($count) = $db->fetchRow($result);
Loading history...
644
645
        return $count;
646
    }
647
}
648