Passed
Push — master ( 3a2152...658ebb )
by Goffy
04:30 queued 12s
created

myblocksadmin_update_block()   F

Complexity

Conditions 17
Paths 560

Size

Total Lines 86
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 17
eloc 52
nc 560
nop 10
dl 0
loc 86
rs 1.6611
c 3
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author      XOOPS Development Team, Kazumi Ono (AKA onokazu)
16
 */
17
18
use Xmf\Request;
19
20
if (!is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid())) {
21
    exit('Access Denied');
22
}
23
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
24
//mb require_once XOOPS_ROOT_PATH . '/modules/system/admin/blocksadmin/blocksadmin.php';
25
26
$op = 'list';
27
28
if (!empty($_POST['op'])) {
29
    $op = $_POST['op'];
30
}
31
if (!empty($_POST['bid'])) {
32
    $bid = Request::getInt('bid', 0, 'POST');
33
}
34
35
if (Request::hasVar('op', 'GET')) {
36
    if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op']
37
        || 'clone' === $_GET['op'] /* || $_GET['op'] == 'previewpopup'*/) {
38
        $op  = $_GET['op'];
39
        $bid = Request::getInt('bid', 0, 'GET');
40
    }
41
}
42
43
if (Request::hasVar('previewblock', 'POST')) {
44
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
45
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
46
    }
47
48
    if (empty($bid)) {
49
        exit('Invalid bid.');
50
    }
51
52
    $bside      = Request::getInt('bside', 0, 'POST');
53
    $bweight    = Request::getInt('bweight', 0, 'POST');
54
    $bvisible   = Request::getInt('bvisible', 0, 'POST');
55
    $bmodule    = Request::getArray('bmodule', [], 'POST');
56
    $btitle     = Request::getString('btitle', '', 'POST');
57
    $bcontent   = Request::getString('bcontent', '', 'POST');
58
    $bctype     = Request::getString('bctype', '', 'POST');
59
    $bcachetime = Request::getInt('bcachetime', 0, 'POST');
60
61
    xoops_cp_header();
62
    require_once XOOPS_ROOT_PATH . '/class/template.php';
63
    $xoopsTpl          = new \XoopsTpl();
64
    $xoopsTpl->caching = 0;
65
    $block['bid']      = $bid;
66
67
    if ('clone_ok' === $op) {
68
        $block['form_title']    = _AM_CLONEBLOCK;
69
        $block['submit_button'] = _CLONE;
70
        $myblock                = new \XoopsBlock();
71
        $myblock->setVar('block_type', 'C');
72
    } else {
73
        $op                     = 'update';
74
        $block['form_title']    = _AM_EDITBLOCK;
75
        $block['submit_button'] = _SUBMIT;
76
        $myblock                = new \XoopsBlock($bid);
77
        $block['name']          = $myblock->getVar('name');
78
    }
79
80
    $myts = \MyTextSanitizer::getInstance();
81
    $myblock->setVar('title', ($btitle));
82
    $myblock->setVar('content', ($bcontent));
83
84
    $block['edit_form'] = false;
85
    $block['template']  = '';
86
    $block['op']        = $op;
87
    $block['side']      = $bside;
88
    $block['weight']    = $bweight;
89
    $block['visible']   = $bvisible;
90
    $block['title']     = $myblock->getVar('title', 'E');
91
    $block['content']   = $myblock->getVar('content', 'n');
92
    $block['modules']   = $bmodule;
93
    $block['ctype']     = $bctype ?? $myblock->getVar('c_type');
94
    $block['is_custom'] = true;
95
    $block['cachetime'] = $bcachetime;
96
    echo '<a href="myblocksadmin.php">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . $block['form_title'] . '<br><br>';
97
    require_once dirname(__DIR__) . '/admin/myblockform.php'; //GIJ
98
99
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
100
    $form->display();
101
102
    $original_level = error_reporting(E_ALL);
103
    echo "
104
    <table width='100%' class='outer' cellspacing='1'>
105
      <tr>
106
        <th>" . $myblock->getVar('title') . "</th>
107
      </tr>
108
      <tr>
109
        <td class='odd'>" . $myblock->getContent('S', $bctype) . '</td>
110
      </tr>
111
    </table>';
112
    error_reporting($original_level);
113
114
    xoops_cp_footer();
115
    /* echo '<script type="text/javascript">
116
    preview_window = openWithSelfMain("'.XOOPS_URL.'/modules/system/admin.php?fct=blocksadmin&op=previewpopup&file='.$dummyfile.'", "popup", 250, 200);
117
    </script>';*/
118
119
    exit();
120
}
121
122
/* if ($op == 'previewpopup') {
123
  if ( !admin_refcheck("/modules/$moduleDirName/admin/") ) {
124
    exit('Invalid Referer');
125
  }
126
  $file = str_replace('..', '', XOOPS_CACHE_PATH.'/'.trim($_GET['file']));
127
  if (file_exists($file)) {
128
    require $file;
129
    @unlink($file);
130
  }
131
  exit();
132
} */
133
134
/* if ($op == "list") {
135
  xoops_cp_header();
136
  list_blocks();
137
  xoops_cp_footer();
138
  exit();
139
} */
140
141
if ('order' === $op) {
142
    //if ( !admin_refcheck("/modules/$moduleDirName/admin/") ) {
143
    //  exit('Invalid Referer');
144
    //}
145
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
146
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
147
    }
148
    if (Request::hasVar('side', 'POST')) {
149
        $side = $_POST['side'];
150
    }
151
    //  if ( !empty($_POST['weight']) ) { $weight = $_POST['weight']; }
152
    if (Request::hasVar('visible', 'POST')) {
153
        $visible = $_POST['visible'];
154
    }
155
    //  if ( !empty($_POST['oldside']) ) { $oldside = $_POST['oldside']; }
156
    //  if ( !empty($_POST['oldweight']) ) { $oldweight = $_POST['oldweight']; }
157
    //  if ( !empty($_POST['oldvisible']) ) { $oldvisible = $_POST['oldvisible']; }
158
    if (Request::hasVar('bid', 'POST')) {
159
        $bid = $_POST['bid'];
160
    } else {
161
        $bid = [];
162
    }
163
    // GIJ start
164
    foreach (array_keys($bid) as $i) {
165
        if ($side[$i] < 0) {
166
            $visible[$i] = 0;
167
            $side[$i]    = -1;
168
        } else {
169
            $visible[$i] = 1;
170
        }
171
172
        $bmodule = (isset($_POST['bmodule'][$i])
173
                    && is_array($_POST['bmodule'][$i])) ? $_POST['bmodule'][$i] : [-1];
174
175
        myblocksadmin_update_block($i, $side[$i], $_POST['weight'][$i], $visible[$i], $_POST['title'][$i], null, null, $_POST['bcachetime'][$i], $bmodule, []);
176
177
        //    if ( $oldweight[$i] != $weight[$i] || $oldvisible[$i] != $visible[$i] || $oldside[$i] != $side[$i] )
178
        //    order_block($bid[$i], $weight[$i], $visible[$i], $side[$i]);
179
    }
180
    $query4redirect = '?dirname=' . urlencode(strip_tags(mb_substr($_POST['query4redirect'], 9)));
181
    redirect_header("myblocksadmin.php$query4redirect", 1, _AM_DBUPDATED);
182
    // GIJ end
183
}
184
185
if ('order2' === $op) {
186
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
187
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
188
    }
189
190
    if (Request::hasVar('addblock', 'POST') && is_array($_POST['addblock'])) {
191
        // addblock
192
        foreach ($_POST['addblock'] as $bid => $val) {
193
            myblocksadmin_update_blockinstance(0, 0, 0, 0, '', null, null, 0, [], [], (int)$bid);
194
        }
195
    } else {
196
        // else change order
197
        if (Request::hasVar('side', 'POST')) {
198
            $side = $_POST['side'];
199
        }
200
        if (Request::hasVar('visible', 'POST')) {
201
            $visible = $_POST['visible'];
202
        }
203
        if (Request::hasVar('id', 'POST')) {
204
            $id = $_POST['id'];
205
        } else {
206
            $id = [];
207
        }
208
209
        foreach (array_keys($id) as $i) {
210
            // separate side and visible
211
            if ($side[$i] < 0) {
212
                $visible[$i] = 0;
213
                $side[$i]    = -1;  // for not to destroy the original position
214
            } else {
215
                $visible[$i] = 1;
216
            }
217
218
            $bmodule = (isset($_POST['bmodule'][$i])
219
                        && is_array($_POST['bmodule'][$i])) ? $_POST['bmodule'][$i] : [-1];
220
221
            myblocksadmin_update_blockinstance($i, $side[$i], $_POST['weight'][$i], $visible[$i], $_POST['title'][$i], null, null, $_POST['bcachetime'][$i], $bmodule, []);
222
        }
223
    }
224
225
    $query4redirect = '?dirname=' . urlencode(strip_tags(mb_substr($_POST['query4redirect'], 9)));
226
    redirect_header("myblocksadmin.php$query4redirect", 1, _MD_AM_DBUPDATED);
227
}
228
229
/* if ($op == 'save') {
230
  if ( !admin_refcheck("/modules/$moduleDirName/admin/") ) {
231
    exit('Invalid Referer');
232
  }
233
  if ( ! $GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin']) ) {
234
    redirect_header(XOOPS_URL.'/',3,$GLOBALS['xoopsSecurity']->getErrors());
235
  }
236
  if ( !empty($_POST['bside']) ) { $bside = (int)($_POST['bside']); } else { $bside = 0; }
237
  if ( !empty($_POST['bweight']) ) { $bweight = (int)($_POST['bweight']); } else { $bweight = 0; }
238
  if ( !empty($_POST['bvisible']) ) { $bvisible = (int)($_POST['bvisible']); } else { $bvisible = 0; }
239
  if ( !empty($_POST['bmodule']) ) { $bmodule = $_POST['bmodule']; } else { $bmodule = []; }
240
  if ( !empty($_POST['btitle']) ) { $btitle = $_POST['btitle']; } else { $btitle = ""; }
241
  if ( !empty($_POST['bcontent']) ) { $bcontent = $_POST['bcontent']; } else { $bcontent = ""; }
242
  if ( !empty($_POST['bctype']) ) { $bctype = $_POST['bctype']; } else { $bctype = ""; }
243
  if ( !empty($_POST['bcachetime']) ) { $bcachetime = (int)($_POST['bcachetime']); } else { $bcachetime = 0; }
244
  save_block($bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bmodule, $bcachetime);
245
  exit();
246
} */
247
248
if ('update' === $op) {
249
    //if ( !admin_refcheck("/modules/$moduleDirName/admin/") ) {
250
    //  exit('Invalid Referer');
251
    //}
252
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
253
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
254
    }
255
    /*  if ( !empty($_POST['bside']) ) { $bside = (int)($_POST['bside']); } else { $bside = 0; }
256
      if ( !empty($_POST['bweight']) ) { $bweight = (int)($_POST['bweight']); } else { $bweight = 0; }
257
      if ( !empty($_POST['bvisible']) ) { $bvisible = (int)($_POST['bvisible']); } else { $bvisible = 0; }
258
      if ( !empty($_POST['btitle']) ) { $btitle = $_POST['btitle']; } else { $btitle = ""; }
259
      if ( !empty($_POST['bcontent']) ) { $bcontent = $_POST['bcontent']; } else { $bcontent = ""; }
260
      if ( !empty($_POST['bctype']) ) { $bctype = $_POST['bctype']; } else { $bctype = ""; }
261
      if ( !empty($_POST['bcachetime']) ) { $bcachetime = (int)($_POST['bcachetime']); } else { $bcachetime = 0; }
262
      if ( !empty($_POST['bmodule']) ) { $bmodule = $_POST['bmodule']; } else { $bmodule = []; }
263
      if ( !empty($_POST['options']) ) { $options = $_POST['options']; } else { $options = []; }
264
      update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $bmodule, $options);*/
265
266
    $bcachetime = Request::getInt('bcachetime', 0, 'POST');
267
    $options    = $_POST['options'] ?? [];
268
    $bcontent   = Request::getString('bcontent', '', 'POST');
269
    $bctype     = Request::getString('bctype', '', 'POST');
270
    $bmodule    = (isset($_POST['bmodule']) && is_array($_POST['bmodule'])) ? $_POST['bmodule'] : [-1]; // GIJ +
271
    $msg        = myblocksadmin_update_block($_POST['bid'], $_POST['bside'], $_POST['bweight'], $_POST['bvisible'], $_POST['btitle'], $bcontent, $bctype, $bcachetime, $bmodule, $options); // GIJ !
272
    redirect_header('myblocksadmin.php', 1, $msg);
273
}
274
275
if ('delete_ok' === $op) {
276
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
277
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
278
    }
279
    // delete_block_ok($bid); GIJ imported from blocksadmin.php
280
    $myblock = new \XoopsBlock($bid);
281
    if ('D' !== $myblock->getVar('block_type') && 'C' !== $myblock->getVar('block_type')) {
282
        redirect_header('myblocksadmin.php', 4, 'Invalid block');
283
    }
284
    $myblock->delete();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::delete() has been deprecated. ( Ignorable by Annotation )

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

284
    /** @scrutinizer ignore-deprecated */ $myblock->delete();
Loading history...
285
    if ('' != $myblock->getVar('template') && !defined('XOOPS_ORETEKI')) {
286
        /** @var \XoopsTplfileHandler $tplfileHandler */
287
        $tplfileHandler = xoops_getHandler('tplfile');
288
        $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
289
        if (count($btemplate) > 0) {
290
            $tplman->delete($btemplate[0]);
291
        }
292
    }
293
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
294
    // end of delete_block_ok() GIJ
295
}
296
297
if ('delete' === $op) {
298
    xoops_cp_header();
299
    // delete_block($bid); GIJ imported from blocksadmin.php
300
    $myblock = new \XoopsBlock($bid);
301
    if ('S' === $myblock->getVar('block_type')) {
302
        $message = _AM_SYSTEMCANT;
303
        redirect_header('admin.php?fct=blocksadmin', 4, $message);
304
    } elseif ('M' === $myblock->getVar('block_type')) {
305
        $message = _AM_MODULECANT;
306
        redirect_header('admin.php?fct=blocksadmin', 4, $message);
307
    } else {
308
        //xoops_confirm(['fct' => 'blocksadmin', 'op'  => 'delete_ok', 'bid' => $myblock->getVar('bid')] + $xoopsGTicket->getTicketArray(__LINE__, 1800, 'myblocksadmin'), 'admin.php', sprintf(_AM_RUSUREDEL, $myblock->getVar('title')));
309
        xoops_confirm(['fct' => 'blocksadmin', 'op' => 'delete_ok', 'bid' => $myblock->getVar('bid')], 'admin.php', sprintf(_AM_RUSUREDEL, $myblock->getVar('title')));
0 ignored issues
show
Bug introduced by
It seems like $myblock->getVar('title') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

309
        xoops_confirm(['fct' => 'blocksadmin', 'op' => 'delete_ok', 'bid' => $myblock->getVar('bid')], 'admin.php', sprintf(_AM_RUSUREDEL, /** @scrutinizer ignore-type */ $myblock->getVar('title')));
Loading history...
310
    }
311
    // end of delete_block() GIJ
312
    xoops_cp_footer();
313
    exit();
314
}
315
316
if ('edit' === $op) {
317
    xoops_cp_header();
318
    // edit_block($bid); GIJ imported from blocksadmin.php
319
    $myblock = new \XoopsBlock($bid);
320
321
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
322
    $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
323
    $result  = $db->query($sql);
324
    $modules = [];
325
    while (false !== ($row = $db->fetchArray($result))) {
326
        $modules[] = (int)$row['module_id'];
327
    }
328
    $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type')) ? true : false;
329
    $block     = [
330
        'form_title'    => _AM_EDITBLOCK,
331
        'name'          => $myblock->getVar('name'),
332
        'side'          => $myblock->getVar('side'),
333
        'weight'        => $myblock->getVar('weight'),
334
        'visible'       => $myblock->getVar('visible'),
335
        'title'         => $myblock->getVar('title', 'E'),
336
        'content'       => $myblock->getVar('content', 'n'),
337
        'modules'       => $modules,
338
        'is_custom'     => $is_custom,
339
        'ctype'         => $myblock->getVar('c_type'),
340
        'cachetime'     => $myblock->getVar('bcachetime'),
341
        'op'            => 'update',
342
        'bid'           => $myblock->getVar('bid'),
343
        'edit_form'     => $myblock->getOptions(),
344
        'template'      => $myblock->getVar('template'),
345
        'options'       => $myblock->getVar('options'),
346
        'submit_button' => _SUBMIT,
347
    ];
348
349
    echo '<a href="myblocksadmin.php">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_EDITBLOCK . '<br><br>';
350
    require dirname(__DIR__) . '/admin/myblockform.php'; //GIJ
351
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
352
    $form->display();
353
    // end of edit_block() GIJ
354
    xoops_cp_footer();
355
    exit();
356
}
357
358
if ('clone' === $op) {
359
    xoops_cp_header();
360
    $myblock = new \XoopsBlock($bid);
361
362
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
363
    $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
364
    $result  = $db->query($sql);
365
    $modules = [];
366
    while (false !== ($row = $db->fetchArray($result))) {
367
        $modules[] = (int)$row['module_id'];
368
    }
369
    $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type')) ? true : false;
370
    $block     = [
371
        'form_title'    => _AM_CLONEBLOCK,
372
        'name'          => $myblock->getVar('name'),
373
        'side'          => $myblock->getVar('side'),
374
        'weight'        => $myblock->getVar('weight'),
375
        'visible'       => $myblock->getVar('visible'),
376
        'content'       => $myblock->getVar('content', 'N'),
377
        'title'         => $myblock->getVar('title', 'E'),
378
        'modules'       => $modules,
379
        'is_custom'     => $is_custom,
380
        'ctype'         => $myblock->getVar('c_type'),
381
        'cachetime'     => $myblock->getVar('bcachetime'),
382
        'op'            => 'clone_ok',
383
        'bid'           => $myblock->getVar('bid'),
384
        'edit_form'     => $myblock->getOptions(),
385
        'template'      => $myblock->getVar('template'),
386
        'options'       => $myblock->getVar('options'),
387
        'submit_button' => _CLONE,
388
    ];
389
    echo '<a href="myblocksadmin.php">' . _AM_SYSTEM_BLOCKS_ADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>';
390
    require dirname(__DIR__) . '/admin/myblockform.php';
391
    //    $xoopsGTicket->addTicketXoopsFormElement($form, __LINE__, 1800, 'myblocksadmin'); //GIJ
392
    $form->display();
393
    xoops_cp_footer();
394
    exit();
395
}
396
397
if ('clone_ok' === $op) {
398
    // Ticket Check
399
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
400
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
401
    }
402
403
    $block = new \XoopsBlock($bid);
404
405
    // block type check
406
    $block_type = $block->getVar('block_type');
407
    if ('C' !== $block_type && 'M' !== $block_type && 'D' !== $block_type) {
408
        redirect_header('myblocksadmin.php', 4, 'Invalid block');
409
    }
410
411
    if (empty($_POST['options'])) {
412
        $options = [];
413
    } elseif (is_array($_POST['options'])) {
414
        $options = $_POST['options'];
415
    } else {
416
        $options = explode('|', $_POST['options']);
417
    }
418
419
    // for backward compatibility
420
    // $cblock =& $block->clone(); or $cblock =& $block->xoopsClone();
421
    $cblock = new \XoopsBlock();
422
    foreach ($block->vars as $k => $v) {
423
        $cblock->assignVar($k, $v['value']);
424
    }
425
    $cblock->setNew();
426
427
    $myts = \MyTextSanitizer::getInstance();
428
    $cblock->setVar('side', $_POST['bside']);
429
    $cblock->setVar('weight', $_POST['bweight']);
430
    $cblock->setVar('visible', $_POST['bvisible']);
431
    $cblock->setVar('title', $_POST['btitle']);
432
    $cblock->setVar('content', @$_POST['bcontent']);
433
    $cblock->setVar('c_type', @$_POST['bctype']);
434
    $cblock->setVar('bcachetime', $_POST['bcachetime']);
435
    if (isset($options) && (count($options) > 0)) {
436
        $options = implode('|', $options);
437
        $cblock->setVar('options', $options);
438
    }
439
    $cblock->setVar('bid', 0);
440
    $cblock->setVar('block_type', 'C' === $block_type ? 'C' : 'D');
441
    $cblock->setVar('func_num', 255);
442
    $newid = $cblock->store();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::store() has been deprecated. ( Ignorable by Annotation )

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

442
    $newid = /** @scrutinizer ignore-deprecated */ $cblock->store();
Loading history...
443
    if (!$newid) {
444
        xoops_cp_header();
445
        $cblock->getHtmlErrors();
446
        xoops_cp_footer();
447
        exit();
448
    }
449
    /*  if ($cblock->getVar('template') != '') {
450
            $tplfileHandler = xoops_getHandler('tplfile');
451
            $btemplate = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
452
            if (count($btemplate) > 0) {
453
                $tplclone =& $btemplate[0]->clone();
454
                $tplclone->setVar('tpl_id', 0);
455
                $tplclone->setVar('tpl_refid', $newid);
456
                $tplman->insert($tplclone);
457
            }
458
        } */
459
    $db      = \XoopsDatabaseFactory::getDatabaseConnection();
460
    $bmodule = (isset($_POST['bmodule']) && is_array($_POST['bmodule'])) ? $_POST['bmodule'] : [-1]; // GIJ +
461
    foreach ($bmodule as $bmid) {
462
        $sql = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
463
        $db->query($sql);
464
    }
465
466
    /*  global $xoopsUser;
467
        $groups =& $xoopsUser->getGroups();
468
        $count = count($groups);
469
        for ($i = 0; $i < $count; ++$i) {
470
            $sql = "INSERT INTO ".$db->prefix('group_permission')." (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (".$groups[$i].", ".$newid.", 1, 'block_read')";
471
            $db->query($sql);
472
        }
473
    */
474
475
    $sql    = 'SELECT gperm_groupid FROM ' . $db->prefix('group_permission') . " WHERE gperm_name='block_read' AND gperm_modid='1' AND gperm_itemid='$bid'";
476
    $result = $db->query($sql);
477
    while (list($gid) = $db->fetchRow($result)) {
478
        $sql = 'INSERT INTO ' . $db->prefix('group_permission') . " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES ($gid, $newid, 1, 'block_read')";
479
        $db->query($sql);
480
    }
481
482
    redirect_header('myblocksadmin.php', 1, _AM_DBUPDATED);
483
}
484
485
// import from modules/system/admin/blocksadmin/blocksadmin.php
486
/**
487
 * @param         $bid
488
 * @param         $bside
489
 * @param         $bweight
490
 * @param         $bvisible
491
 * @param         $btitle
492
 * @param         $bcontent
493
 * @param         $bctype
494
 * @param         $bcachetime
495
 * @param         $bmodule
496
 * @param array   $options
497
 * @return string
498
 */
499
function myblocksadmin_update_block(
500
    $bid,
501
    $bside,
502
    $bweight,
503
    $bvisible,
504
    $btitle,
505
    $bcontent,
506
    $bctype,
507
    $bcachetime,
508
    $bmodule,
509
    $options = []
510
) {
511
    global $xoopsConfig;
512
    /* if (empty($bmodule)) {
513
        xoops_cp_header();
514
        xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
515
        xoops_cp_footer();
516
        exit();
517
    } */
518
    $myblock = new \XoopsBlock($bid);
519
    // $myblock->setVar('side', $bside); GIJ -
520
    if ($bside >= 0) {
521
        $myblock->setVar('side', $bside);
522
    } // GIJ +
523
    $myblock->setVar('weight', $bweight);
524
    $myblock->setVar('visible', $bvisible);
525
    $myblock->setVar('title', $btitle);
526
    if (isset($bcontent)) {
527
        $myblock->setVar('content', $bcontent);
528
    }
529
    if (isset($bctype)) {
530
        $myblock->setVar('c_type', $bctype);
531
    }
532
    $myblock->setVar('bcachetime', $bcachetime);
533
    if (isset($options) && (count($options) > 0)) {
534
        $options = implode('|', $options);
535
        $myblock->setVar('options', $options);
536
    }
537
    if ('C' === $myblock->getVar('block_type')) {
538
        switch ($myblock->getVar('c_type')) {
539
            case 'H':
540
                $name = _AM_CUSTOMHTML;
541
                break;
542
            case 'P':
543
                $name = _AM_CUSTOMPHP;
544
                break;
545
            case 'S':
546
                $name = _AM_CUSTOMSMILE;
547
                break;
548
            default:
549
                $name = _AM_CUSTOMNOSMILE;
550
                break;
551
        }
552
        $myblock->setVar('name', $name);
553
    }
554
    $msg = _AM_DBUPDATED;
555
    if (false !== $myblock->store()) {
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::store() has been deprecated. ( Ignorable by Annotation )

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

555
    if (false !== /** @scrutinizer ignore-deprecated */ $myblock->store()) {
Loading history...
introduced by
The condition false !== $myblock->store() is always true.
Loading history...
556
        $db  = \XoopsDatabaseFactory::getDatabaseConnection();
557
        $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $db->prefix('block_module_link'), $bid);
558
        $db->query($sql);
559
        foreach ($bmodule as $bmid) {
560
            $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $db->prefix('block_module_link'), $bid, (int)$bmid);
561
            $db->query($sql);
562
        }
563
        require_once XOOPS_ROOT_PATH . '/class/template.php';
564
        $xoopsTpl          = new \XoopsTpl();
565
        $xoopsTpl->caching = 2;
566
        if ('' != $myblock->getVar('template')) {
567
            if ($xoopsTpl->is_cached('db:' . $myblock->getVar('template'))) {
568
                if (!$xoopsTpl->clear_cache('db:' . $myblock->getVar('template'))) {
569
                    $msg = 'Unable to clear cache for block ID' . $bid;
570
                }
571
            }
572
        } else {
573
            if ($xoopsTpl->is_cached('db:system_dummy.tpl', 'block' . $bid)) {
574
                if (!$xoopsTpl->clear_cache('db:system_dummy.tpl', 'block' . $bid)) {
575
                    $msg = 'Unable to clear cache for block ID' . $bid;
576
                }
577
            }
578
        }
579
    } else {
580
        $msg = 'Failed update of block. ID:' . $bid;
581
    }
582
    // redirect_header('admin.php?fct=blocksadmin&amp;t='.time(),1,$msg);
583
    // exit(); GIJ -
584
    return $msg; // GIJ +
585
}
586
587
// update block instance for 2.2
588
/**
589
 * @param         $id
590
 * @param         $bside
591
 * @param         $bweight
592
 * @param         $bvisible
593
 * @param         $btitle
594
 * @param         $bcontent
595
 * @param         $bctype
596
 * @param         $bcachetime
597
 * @param         $bmodule
598
 * @param array   $options
599
 * @param null    $bid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bid is correct as it would always require null to be passed?
Loading history...
600
 * @return string
601
 */
602
function myblocksadmin_update_blockinstance(
603
    $id,
604
    $bside,
605
    $bweight,
606
    $bvisible,
607
    $btitle,
608
    $bcontent,
0 ignored issues
show
Unused Code introduced by
The parameter $bcontent is not used and could be removed. ( Ignorable by Annotation )

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

608
    /** @scrutinizer ignore-unused */ $bcontent,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
609
    $bctype,
0 ignored issues
show
Unused Code introduced by
The parameter $bctype is not used and could be removed. ( Ignorable by Annotation )

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

609
    /** @scrutinizer ignore-unused */ $bctype,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
610
    $bcachetime,
611
    $bmodule,
612
    $options = [],
613
    $bid = null
614
) {
615
    global $xoopsDB;
616
617
    $instanceHandler = xoops_getHandler('blockinstance');
618
    $blockHandler    = xoops_getHandler('block');
619
    if ($id > 0) {
620
        // update
621
        $instance = $instanceHandler->get($id);
622
        if ($bside >= 0) {
623
            $instance->setVar('side', $bside);
624
        }
625
        if (!empty($options)) {
626
            $instance->setVar('options', $options);
627
        }
628
    } else {
629
        // insert
630
        $instance = $instanceHandler->create();
631
        $instance->setVar('bid', $bid);
632
        $instance->setVar('side', $bside);
633
        $block = $blockHandler->get($bid);
634
        $instance->setVar('options', $block->getVar('options'));
635
        if (empty($btitle)) {
636
            $btitle = $block->getVar('name');
637
        }
638
    }
639
    $instance->setVar('weight', $bweight);
640
    $instance->setVar('visible', $bvisible);
641
    $instance->setVar('title', $btitle);
642
    // if( isset( $bcontent ) ) $instance->setVar('content', $bcontent);
643
    // if( isset( $bctype ) ) $instance->setVar('c_type', $bctype);
644
    $instance->setVar('bcachetime', $bcachetime);
645
646
    if ($instanceHandler->insert($instance)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $instanceHandler->insert($instance) targeting XoopsObjectHandler::insert() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
647
        $GLOBALS['xoopsDB']->query('DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('block_module_link') . ' WHERE block_id=' . $instance->getVar('instanceid'));
648
        foreach ($bmodule as $mid) {
649
            $page   = explode('-', $mid);
650
            $mid    = $page[0];
651
            $pageid = $page[1];
652
            $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('block_module_link') . ' VALUES (' . $instance->getVar('instanceid') . ', ' . (int)$mid . ', ' . (int)$pageid . ')');
653
        }
654
655
        return _MD_AM_DBUPDATED;
656
    }
657
658
    return 'Failed update of block instance. ID:' . $id;
659
}
660
661
// TODO  edit2, delete2, customblocks
662