Total Complexity | 81 |
Total Lines | 734 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 2 | 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 declare(strict_types=1); |
||
30 | class Blocksadmin |
||
31 | { |
||
32 | /** |
||
33 | * @var \XoopsMySQLDatabase|null |
||
34 | */ |
||
35 | public $db; |
||
36 | /** |
||
37 | * @var Helper |
||
38 | */ |
||
39 | public Helper $helper; |
||
40 | |||
41 | private \XoopsModule $xoopsModule; |
||
42 | private \XoopsSecurity $xoopsSecurity; |
||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | public string $moduleDirName; |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | public $moduleDirNameUpper; |
||
51 | |||
52 | /** |
||
53 | * Blocksadmin constructor. |
||
54 | * @param \XoopsMySQLDatabase|null $db |
||
55 | * @param Helper|null $helper |
||
56 | * @param \XoopsModule $xoopsModule |
||
57 | * @param \XoopsSecurity $xoopsSecurity |
||
58 | * |
||
59 | */ |
||
60 | public function __construct(?\XoopsMySQLDatabase $db, Helper $helper, \XoopsModule $xoopsModule, \XoopsSecurity $xoopsSecurity) |
||
61 | { |
||
62 | if (null === $db) { |
||
63 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
64 | } |
||
65 | $this->db = $db; |
||
66 | $this->helper = $helper; |
||
67 | $this->xoopsModule = $xoopsModule; |
||
68 | $this->xoopsSecurity = $xoopsSecurity; |
||
69 | $this->moduleDirName = \basename(\dirname(__DIR__, 2)); |
||
70 | $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName); |
||
71 | \xoops_loadLanguage('admin', 'system'); |
||
72 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
73 | \xoops_loadLanguage('admin/groups', 'system'); |
||
74 | $this->helper->loadLanguage('common'); |
||
75 | $this->helper->loadLanguage('blocksadmin'); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return void |
||
80 | */ |
||
81 | public function listBlocks(): void |
||
256 | </td></tr> |
||
257 | </table> |
||
258 | </form> |
||
259 | <br><br>"; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param int $bid |
||
264 | */ |
||
265 | public function deleteBlock(int $bid): void |
||
266 | { |
||
267 | // \xoops_cp_header(); |
||
268 | |||
269 | \xoops_loadLanguage('admin', 'system'); |
||
270 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
271 | \xoops_loadLanguage('admin/groups', 'system'); |
||
272 | |||
273 | // $myblock = new \XoopsBlock($bid); |
||
274 | |||
275 | $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid); |
||
276 | $result = $this->db->queryF($sql); |
||
277 | if (!$result) { |
||
278 | \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR); |
||
279 | } |
||
280 | $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid); |
||
281 | $result = $this->db->queryF($sql); |
||
282 | if (!$result) { |
||
283 | \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR); |
||
284 | } |
||
285 | |||
286 | $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param int $bid |
||
291 | */ |
||
292 | public function cloneBlock(int $bid) |
||
293 | { |
||
294 | //require __DIR__ . '/admin_header.php'; |
||
295 | // \xoops_cp_header(); |
||
296 | |||
297 | \xoops_loadLanguage('admin', 'system'); |
||
298 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
299 | \xoops_loadLanguage('admin/groups', 'system'); |
||
300 | |||
301 | $myblock = new \XoopsBlock($bid); |
||
302 | $sql = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid; |
||
303 | $result = $this->db->query($sql); |
||
304 | if (!$this->db->isResultSet($result)) { |
||
305 | $errorMsg = \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(); |
||
306 | $logger = \XoopsLogger::getInstance(); |
||
307 | $logger->handleError(E_USER_WARNING, $errorMsg, __FILE__, __LINE__); |
||
308 | $this->helper->redirect('admin/blocksadmin.php', 3, $errorMsg); |
||
309 | } |
||
310 | $modules = []; |
||
311 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
312 | $modules[] = (int)$row['module_id']; |
||
313 | } |
||
314 | |||
315 | $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']); |
||
316 | $block = [ |
||
317 | 'title' => $myblock->getVar('title') . ' Clone', |
||
318 | 'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'), |
||
319 | 'name' => $myblock->getVar('name'), |
||
320 | 'side' => $myblock->getVar('side'), |
||
321 | 'weight' => $myblock->getVar('weight'), |
||
322 | 'visible' => $myblock->getVar('visible'), |
||
323 | 'content' => $myblock->getVar('content', 'N'), |
||
324 | 'modules' => $modules, |
||
325 | 'is_custom' => $isCustom, |
||
326 | 'ctype' => $myblock->getVar('c_type'), |
||
327 | 'bcachetime' => $myblock->getVar('bcachetime'), |
||
328 | 'op' => 'clone_ok', |
||
329 | 'bid' => $myblock->getVar('bid'), |
||
330 | 'edit_form' => $myblock->getOptions(), |
||
331 | 'template' => $myblock->getVar('template'), |
||
332 | 'options' => $myblock->getVar('options'), |
||
333 | ]; |
||
334 | echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a> <span style="font-weight:bold;">»»</span> ' . \_AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>'; |
||
335 | // $form = new Blockform(); |
||
336 | // $form->render(); |
||
337 | |||
338 | $output = $this->render($block); |
||
339 | echo $output; |
||
340 | // xoops_cp_footer(); |
||
341 | // require_once __DIR__ . '/admin_footer.php'; |
||
342 | // exit(); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param int $bid |
||
347 | * @param string $bside |
||
348 | * @param string $bweight |
||
349 | * @param string $bvisible |
||
350 | * @param string $bcachetime |
||
351 | * @param array|null $bmodule |
||
352 | * @param array|null $options |
||
353 | * @param array|null $groups |
||
354 | * @param bool $redirect |
||
355 | */ |
||
356 | public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups, bool $redirect = true) |
||
357 | { |
||
358 | \xoops_loadLanguage('admin', 'system'); |
||
359 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
360 | \xoops_loadLanguage('admin/groups', 'system'); |
||
361 | |||
362 | $block = new \XoopsBlock($bid); |
||
363 | /** @var \XoopsBlock $clone */ |
||
364 | $clone = $block->xoopsClone($bid); |
||
365 | // if (empty($bmodule)) { |
||
366 | // // \xoops_cp_header(); |
||
367 | // \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN)); |
||
368 | // \xoops_cp_footer(); |
||
369 | // exit(); |
||
370 | // } |
||
371 | |||
372 | if (empty($bmodule)) { |
||
373 | throw new \InvalidArgumentException(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN)); |
||
374 | } |
||
375 | $clone->setVar('side', $bside); |
||
376 | $clone->setVar('weight', $bweight); |
||
377 | $clone->setVar('visible', $bvisible); |
||
378 | //$clone->setVar('content', $_POST['bcontent']); |
||
379 | $clone->setVar('title', Request::getString('btitle', '', 'POST')); |
||
380 | $clone->setVar('bcachetime', $bcachetime); |
||
381 | if (\is_array($options) && (\count($options) > 0)) { |
||
382 | $options = \implode('|', $options); |
||
383 | $clone->setVar('options', $options); |
||
384 | } |
||
385 | $clone->setVar('bid', 0); |
||
386 | if (\in_array($block->getVar('block_type'), ['C', 'E'])) { |
||
387 | $clone->setVar('block_type', 'E'); |
||
388 | } else { |
||
389 | $clone->setVar('block_type', 'D'); |
||
390 | } |
||
391 | $newid = null; |
||
392 | // $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105 |
||
393 | if ($clone->store()) { |
||
394 | $newid = $clone->id(); //get the id of the cloned block |
||
395 | } |
||
396 | // if (!$newid) { |
||
397 | // // \xoops_cp_header(); |
||
398 | // $clone->getHtmlErrors(); |
||
399 | // \xoops_cp_footer(); |
||
400 | // exit(); |
||
401 | // } |
||
402 | |||
403 | if (!$newid) { |
||
404 | throw new \RuntimeException($clone->getHtmlErrors()); |
||
405 | } |
||
406 | |||
407 | if ('' !== $clone->getVar('template')) { |
||
408 | |||
409 | /** @var \XoopsTplfileHandler $tplfileHandler */ |
||
410 | $tplfileHandler = \xoops_getHandler('tplfile'); |
||
411 | $configHandler = xoops_getHandler('config'); |
||
412 | /** @var \XoopsConfigHandler $xoopsConfig */ |
||
413 | $xoopsConfig = $configHandler->getConfigsByCat(XOOPS_CONF); |
||
414 | $btemplate = $tplfileHandler->find($xoopsConfig['template_set'], 'block', (string)$bid); |
||
415 | if (\count($btemplate) > 0) { |
||
416 | $tplclone = $btemplate[0]->xoopsClone(); |
||
417 | $tplclone->setVar('tpl_id', 0); |
||
418 | $tplclone->setVar('tpl_refid', $newid); |
||
419 | $tplfileHandler->insert($tplclone); |
||
420 | } |
||
421 | } |
||
422 | |||
423 | foreach ($bmodule as $bmid) { |
||
424 | $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')'; |
||
425 | $this->db->query($sql); |
||
426 | } |
||
427 | //$groups = &$this->xoopsUser->getGroups(); |
||
428 | foreach ($groups as $iValue) { |
||
429 | $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')"; |
||
430 | $this->db->query($sql); |
||
431 | } |
||
432 | |||
433 | if ($redirect) { |
||
434 | $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED); |
||
435 | } |
||
436 | |||
437 | return true; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @param string $bid |
||
442 | * @param string $title |
||
443 | * @param string $weight |
||
444 | * @param string $visible |
||
445 | * @param string $side |
||
446 | * @param string $bcachetime |
||
447 | * @param array|null $bmodule |
||
448 | */ |
||
449 | public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null): void |
||
450 | { |
||
451 | $myblock = new \XoopsBlock($bid); |
||
452 | $myblock->setVar('title', $title); |
||
453 | $myblock->setVar('weight', $weight); |
||
454 | $myblock->setVar('visible', $visible); |
||
455 | $myblock->setVar('side', $side); |
||
456 | $myblock->setVar('bcachetime', $bcachetime); |
||
457 | $myblock->store(); |
||
458 | // /** @var \XoopsBlockHandler $blockHandler */ |
||
459 | // $blockHandler = \xoops_getHandler('block'); |
||
460 | // return $blockHandler->insert($myblock); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param int $bid |
||
465 | * @return void |
||
466 | */ |
||
467 | public function editBlock(int $bid): void |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @param int $bid |
||
517 | * @param string $btitle |
||
518 | * @param string $bside |
||
519 | * @param string $bweight |
||
520 | * @param string $bvisible |
||
521 | * @param string $bcachetime |
||
522 | * @param array|null $bmodule |
||
523 | * @param array|null $options |
||
524 | * @param array|null $groups |
||
525 | */ |
||
526 | public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups) |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @param array $bid |
||
580 | * @param array $oldtitle |
||
581 | * @param array $oldside |
||
582 | * @param array $oldweight |
||
583 | * @param array $oldvisible |
||
584 | // * @param array $oldgroups |
||
585 | * @param array $oldbcachetime |
||
586 | * @param array $oldbmodule |
||
587 | * @param array $title |
||
588 | * @param array $weight |
||
589 | * @param array $visible |
||
590 | * @param array $side |
||
591 | * @param array $bcachetime |
||
592 | * @param array $groups |
||
593 | * @param array $bmodule |
||
594 | */ |
||
595 | public function orderBlock( |
||
596 | array $bid, |
||
597 | array $oldtitle, |
||
598 | array $oldside, |
||
599 | array $oldweight, |
||
600 | array $oldvisible, |
||
601 | // array $oldgroups, |
||
602 | array $oldbcachetime, |
||
603 | array $oldbmodule, |
||
604 | array $title, |
||
605 | array $weight, |
||
606 | array $visible, |
||
607 | array $side, |
||
608 | array $bcachetime, |
||
609 | array $groups, |
||
610 | array $bmodule |
||
611 | ): void { |
||
612 | if (!$this->xoopsSecurity->check()) { |
||
613 | \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $this->xoopsSecurity->getErrors())); |
||
614 | } |
||
615 | foreach (\array_keys($bid) as $i) { |
||
616 | if ($oldtitle[$i] !== $title[$i] |
||
617 | || $oldweight[$i] !== $weight[$i] |
||
618 | || $oldvisible[$i] !== $visible[$i] |
||
619 | || $oldside[$i] !== $side[$i] |
||
620 | || $oldbcachetime[$i] !== $bcachetime[$i] |
||
621 | || $oldbmodule[$i] !== $bmodule[$i]) { |
||
622 | $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]); |
||
623 | } |
||
624 | if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) { |
||
625 | $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]); |
||
626 | $this->db->query($sql); |
||
627 | if (\in_array(0, $bmodule[$i], true)) { |
||
628 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0); |
||
629 | $this->db->query($sql); |
||
630 | } else { |
||
631 | foreach ($bmodule[$i] as $bmid) { |
||
632 | $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid); |
||
633 | $this->db->query($sql); |
||
634 | } |
||
635 | } |
||
636 | } |
||
637 | $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]); |
||
638 | $this->db->query($sql); |
||
639 | if (!empty($groups[$i])) { |
||
640 | foreach ($groups[$i] as $grp) { |
||
641 | $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]); |
||
642 | $this->db->query($sql); |
||
643 | } |
||
644 | } |
||
645 | } |
||
646 | |||
647 | $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS')); |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * @param array|null $block |
||
652 | * @return void |
||
653 | */ |
||
654 | public function render(?array $block = null): void |
||
764 | } |
||
765 | } |
||
766 |