Total Complexity | 80 |
Total Lines | 667 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Blocksadmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Blocksadmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Blocksadmin |
||
28 | { |
||
29 | /** |
||
30 | * @var \XoopsMySQLDatabase|null |
||
31 | */ |
||
32 | public $db; |
||
33 | /** |
||
34 | * @var \Xmf\Module\Helper |
||
35 | */ |
||
36 | public $helper; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | public $moduleDirName; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $moduleDirNameUpper; |
||
45 | |||
46 | /** |
||
47 | * Blocksadmin constructor. |
||
48 | */ |
||
49 | public function __construct(?\XoopsDatabase $db, \Xmf\Module\Helper $helper) |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return void |
||
67 | */ |
||
68 | public function listBlocks(): void |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param int $bid |
||
245 | * @return void |
||
246 | */ |
||
247 | public function deleteBlock(int $bid): void |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @param int $bid |
||
267 | * @return void |
||
268 | */ |
||
269 | public function cloneBlock(int $bid): void |
||
311 | // xoops_cp_footer(); |
||
312 | // require_once __DIR__ . '/admin_footer.php'; |
||
313 | // exit(); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param int $bid |
||
318 | * @param string $bside |
||
319 | * @param string $bweight |
||
320 | * @param string $bvisible |
||
321 | * @param string $bcachetime |
||
322 | * @param array|null $bmodule |
||
323 | * @param array|null $options |
||
324 | * @param array|null $groups |
||
325 | * @return void |
||
326 | */ |
||
327 | public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void |
||
328 | { |
||
329 | \xoops_loadLanguage('admin', 'system'); |
||
330 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
331 | \xoops_loadLanguage('admin/groups', 'system'); |
||
332 | |||
333 | $block = new \XoopsBlock($bid); |
||
334 | $clone = $block->xoopsClone(); |
||
335 | if (empty($bmodule)) { |
||
336 | // \xoops_cp_header(); |
||
337 | \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN)); |
||
338 | \xoops_cp_footer(); |
||
339 | exit(); |
||
340 | } |
||
341 | $clone->setVar('side', $bside); |
||
342 | $clone->setVar('weight', $bweight); |
||
343 | $clone->setVar('visible', $bvisible); |
||
344 | //$clone->setVar('content', $_POST['bcontent']); |
||
345 | $clone->setVar('title', Request::getString('btitle', '', 'POST')); |
||
346 | $clone->setVar('bcachetime', $bcachetime); |
||
347 | if (\is_array($options) && (\count($options) > 0)) { |
||
348 | $options = \implode('|', $options); |
||
349 | $clone->setVar('options', $options); |
||
350 | } |
||
351 | $clone->setVar('bid', 0); |
||
352 | if (\in_array($block->getVar('block_type'), ['C', 'E'])) { |
||
353 | $clone->setVar('block_type', 'E'); |
||
354 | } else { |
||
355 | $clone->setVar('block_type', 'D'); |
||
356 | } |
||
357 | // $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105 |
||
358 | if ($clone->store()) { |
||
359 | $newid = $clone->id(); //get the id of the cloned block |
||
360 | } |
||
361 | if (!$newid) { |
||
362 | // \xoops_cp_header(); |
||
363 | $clone->getHtmlErrors(); |
||
364 | \xoops_cp_footer(); |
||
365 | exit(); |
||
366 | } |
||
367 | if ('' !== $clone->getVar('template')) { |
||
368 | /** @var \XoopsTplfileHandler $tplfileHandler */ |
||
369 | $tplfileHandler = \xoops_getHandler('tplfile'); |
||
370 | $btemplate = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid); |
||
371 | if (\count($btemplate) > 0) { |
||
372 | $tplclone = $btemplate[0]->xoopsClone(); |
||
373 | $tplclone->setVar('tpl_id', 0); |
||
374 | $tplclone->setVar('tpl_refid', $newid); |
||
375 | $tplfileHandler->insert($tplclone); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | foreach ($bmodule as $bmid) { |
||
380 | $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')'; |
||
381 | $this->db->query($sql); |
||
382 | } |
||
383 | //$groups = &$GLOBALS['xoopsUser']->getGroups(); |
||
384 | foreach ($groups as $iValue) { |
||
385 | $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')"; |
||
386 | $this->db->query($sql); |
||
387 | } |
||
388 | $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param string $bid |
||
393 | * @param string $title |
||
394 | * @param string $weight |
||
395 | * @param string $visible |
||
396 | * @param string $side |
||
397 | * @param string $bcachetime |
||
398 | * @param array|null $bmodule |
||
399 | * @return void |
||
400 | */ |
||
401 | public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null): void |
||
410 | // /** @var \XoopsBlockHandler $blockHandler */ |
||
411 | // $blockHandler = \xoops_getHandler('block'); |
||
412 | // return $blockHandler->insert($myblock); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @param int $bid |
||
417 | * @return void |
||
418 | */ |
||
419 | public function editBlock(int $bid): void |
||
420 | { |
||
421 | // require_once \dirname(__DIR__,2) . '/admin/admin_header.php'; |
||
422 | // \xoops_cp_header(); |
||
423 | \xoops_loadLanguage('admin', 'system'); |
||
424 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
425 | \xoops_loadLanguage('admin/groups', 'system'); |
||
426 | // mpu_adm_menu(); |
||
427 | $myblock = new \XoopsBlock($bid); |
||
428 | $sql = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid; |
||
429 | $result = $this->db->query($sql); |
||
430 | $modules = []; |
||
431 | if ($result instanceof \mysqli_result) { |
||
432 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
433 | $modules[] = (int)$row['module_id']; |
||
434 | } |
||
435 | } |
||
436 | $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']); |
||
437 | $block = [ |
||
438 | 'title' => $myblock->getVar('title'), |
||
439 | 'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK, |
||
440 | // 'name' => $myblock->getVar('name'), |
||
441 | 'side' => $myblock->getVar('side'), |
||
442 | 'weight' => $myblock->getVar('weight'), |
||
443 | 'visible' => $myblock->getVar('visible'), |
||
444 | 'content' => $myblock->getVar('content', 'N'), |
||
445 | 'modules' => $modules, |
||
446 | 'is_custom' => $isCustom, |
||
447 | 'ctype' => $myblock->getVar('c_type'), |
||
448 | 'bcachetime' => $myblock->getVar('bcachetime'), |
||
449 | 'op' => 'edit_ok', |
||
450 | 'bid' => $myblock->getVar('bid'), |
||
451 | 'edit_form' => $myblock->getOptions(), |
||
452 | 'template' => $myblock->getVar('template'), |
||
453 | 'options' => $myblock->getVar('options'), |
||
454 | ]; |
||
455 | echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a> <span style="font-weight:bold;">»»</span> ' . \_AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>'; |
||
456 | |||
457 | echo $this->render($block); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @param int $bid |
||
462 | * @param string $btitle |
||
463 | * @param string $bside |
||
464 | * @param string $bweight |
||
465 | * @param string $bvisible |
||
466 | * @param string $bcachetime |
||
467 | * @param array|null $bmodule |
||
468 | * @param array|null $options |
||
469 | * @param array|null $groups |
||
470 | * @return void |
||
471 | */ |
||
472 | public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void |
||
473 | { |
||
474 | $myblock = new \XoopsBlock($bid); |
||
475 | $myblock->setVar('title', $btitle); |
||
476 | $myblock->setVar('weight', $bweight); |
||
477 | $myblock->setVar('visible', $bvisible); |
||
478 | $myblock->setVar('side', $bside); |
||
479 | $myblock->setVar('bcachetime', $bcachetime); |
||
480 | //update block options |
||
481 | if (isset($options)) { |
||
482 | $optionsCount = \count($options); |
||
483 | if ($optionsCount > 0) { |
||
484 | //Convert array values to comma-separated |
||
485 | foreach ($options as $i => $iValue) { |
||
486 | if (\is_array($iValue)) { |
||
487 | $options[$i] = \implode(',', $iValue); |
||
488 | } |
||
489 | } |
||
490 | $options = \implode('|', $options); |
||
491 | $myblock->setVar('options', $options); |
||
492 | } |
||
493 | } |
||
494 | $myblock->store(); |
||
495 | // /** @var \XoopsBlockHandler $blockHandler */ |
||
496 | // $blockHandler = \xoops_getHandler('block'); |
||
497 | // $blockHandler->insert($myblock); |
||
498 | |||
499 | if (!empty($bmodule) && \count($bmodule) > 0) { |
||
500 | $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid); |
||
501 | $this->db->query($sql); |
||
502 | if (\in_array(0, $bmodule)) { |
||
503 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0); |
||
504 | $this->db->query($sql); |
||
505 | } else { |
||
506 | foreach ($bmodule as $bmid) { |
||
507 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid); |
||
508 | $this->db->query($sql); |
||
509 | } |
||
510 | } |
||
511 | } |
||
512 | $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid); |
||
513 | $this->db->query($sql); |
||
514 | if (!empty($groups)) { |
||
515 | foreach ($groups as $grp) { |
||
516 | $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid); |
||
517 | $this->db->query($sql); |
||
518 | } |
||
519 | } |
||
520 | $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS')); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param array $bid |
||
525 | * @param array $oldtitle |
||
526 | * @param array $oldside |
||
527 | * @param array $oldweight |
||
528 | * @param array $oldvisible |
||
529 | * @param array $oldgroups |
||
530 | * @param array $oldbcachetime |
||
531 | * @param array $oldbmodule |
||
532 | * @param array $title |
||
533 | * @param array $weight |
||
534 | * @param array $visible |
||
535 | * @param array $side |
||
536 | * @param array $bcachetime |
||
537 | * @param array $groups |
||
538 | * @param array $bmodule |
||
539 | * @return void |
||
540 | */ |
||
541 | public function orderBlock( |
||
542 | array $bid, array $oldtitle, array $oldside, array $oldweight, array $oldvisible, array $oldgroups, array $oldbcachetime, array $oldbmodule, array $title, array $weight, array $visible, array $side, array $bcachetime, array $groups, array $bmodule |
||
543 | ): void { |
||
544 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
545 | \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors())); |
||
546 | } |
||
547 | foreach (\array_keys($bid) as $i) { |
||
548 | if ($oldtitle[$i] !== $title[$i] |
||
549 | || $oldweight[$i] !== $weight[$i] |
||
550 | || $oldvisible[$i] !== $visible[$i] |
||
551 | || $oldside[$i] !== $side[$i] |
||
552 | || $oldbcachetime[$i] !== $bcachetime[$i] |
||
553 | || $oldbmodule[$i] !== $bmodule[$i]) { |
||
554 | $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]); |
||
555 | } |
||
556 | if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) { |
||
557 | $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]); |
||
558 | $this->db->query($sql); |
||
559 | if (\in_array(0, $bmodule[$i], true)) { |
||
560 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0); |
||
561 | $this->db->query($sql); |
||
562 | } else { |
||
563 | foreach ($bmodule[$i] as $bmid) { |
||
564 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid); |
||
565 | $this->db->query($sql); |
||
566 | } |
||
567 | } |
||
568 | } |
||
569 | $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]); |
||
570 | $this->db->query($sql); |
||
571 | if (!empty($groups[$i])) { |
||
572 | foreach ($groups[$i] as $grp) { |
||
573 | $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid[$i]); |
||
574 | $this->db->query($sql); |
||
575 | } |
||
576 | } |
||
577 | } |
||
578 | |||
579 | $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS')); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @param array|null $block |
||
584 | * @return void |
||
585 | */ |
||
586 | public function render(?array $block = null) |
||
694 | } |
||
695 | } |
||
696 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.