| Total Complexity | 81 |
| Total Lines | 734 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 82 | { |
||
| 83 | $pathIcon16 = Admin::iconUrl('', '16'); |
||
| 84 | |||
| 85 | require_once XOOPS_ROOT_PATH . '/class/xoopslists.php'; |
||
| 86 | |||
| 87 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 88 | $moduleHandler = \xoops_getHandler('module'); |
||
| 89 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 90 | $memberHandler = \xoops_getHandler('member'); |
||
| 91 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 92 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
| 93 | $groups = $memberHandler->getGroups(); |
||
| 94 | $criteria = new \CriteriaCompo(new \Criteria('hasmain', '1')); |
||
| 95 | $criteria->add(new \Criteria('isactive', '1')); |
||
| 96 | $moduleList = $moduleHandler->getList($criteria); |
||
| 97 | $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE; |
||
| 98 | $moduleList[0] = \_AM_SYSTEM_BLOCKS_ALLPAGES; |
||
| 99 | \ksort($moduleList); |
||
| 100 | echo "<h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>'; |
||
| 101 | echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>"; |
||
| 102 | echo $this->xoopsSecurity->getTokenHTML(); |
||
| 103 | echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'> |
||
| 104 | <tr valign='middle'><th align='center'>" . \_AM_SYSTEM_BLOCKS_TITLE . "</th><th align='center' nowrap='nowrap'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE') . '<br>' . _LEFT . '-' . _CENTER . '-' . _RIGHT . "</th> |
||
| 105 | <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT') . "</th> |
||
| 106 | <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE') . "</th><th align='center'>" . \_AM_SYSTEM_BLOCKS_VISIBLEIN . "</th> |
||
| 107 | <th align='center'>" . \_AM_SYSTEM_ADGS . "</th> |
||
| 108 | <th align='center'>" . \_AM_SYSTEM_BLOCKS_BCACHETIME . "</th> |
||
| 109 | <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION') . '</th> |
||
| 110 | </tr>'; |
||
| 111 | $blockArray = \XoopsBlock::getByModule($this->xoopsModule->mid()); |
||
| 112 | $blockCount = \count($blockArray); |
||
|
|
|||
| 113 | $class = 'even'; |
||
| 114 | $cachetimes = [ |
||
| 115 | 0 => \_NOCACHE, |
||
| 116 | 30 => \sprintf(\_SECONDS, 30), |
||
| 117 | 60 => \_MINUTE, |
||
| 118 | 300 => \sprintf(\_MINUTES, 5), |
||
| 119 | 1800 => \sprintf(\_MINUTES, 30), |
||
| 120 | 3600 => \_HOUR, |
||
| 121 | 18000 => \sprintf(\_HOURS, 5), |
||
| 122 | 86400 => \_DAY, |
||
| 123 | 259200 => \sprintf(\_DAYS, 3), |
||
| 124 | 604800 => \_WEEK, |
||
| 125 | 2592000 => \_MONTH, |
||
| 126 | ]; |
||
| 127 | foreach ($blockArray as $i) { |
||
| 128 | $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid')); |
||
| 129 | $sql = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid'); |
||
| 130 | $result = $this->db->query($sql); |
||
| 131 | if (!$this->db->isResultSet($result)) { |
||
| 132 | // \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR); |
||
| 133 | $errorMsg = \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(); |
||
| 134 | $logger = \XoopsLogger::getInstance(); |
||
| 135 | $logger->handleError(E_USER_WARNING, $errorMsg, __FILE__, __LINE__); |
||
| 136 | $this->helper->redirect('admin/blocksadmin.php', 3, $errorMsg); |
||
| 137 | } |
||
| 138 | $modules = []; |
||
| 139 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 140 | $modules[] = (int)$row['module_id']; |
||
| 141 | } |
||
| 142 | |||
| 143 | $cachetimeOptions = ''; |
||
| 144 | foreach ($cachetimes as $cachetime => $cachetimeName) { |
||
| 145 | if ($i->getVar('bcachetime') == $cachetime) { |
||
| 146 | $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n"; |
||
| 147 | } else { |
||
| 148 | $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n"; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $ssel7 = ''; |
||
| 153 | $ssel6 = $ssel7; |
||
| 154 | $ssel5 = $ssel6; |
||
| 155 | $ssel4 = $ssel5; |
||
| 156 | $ssel3 = $ssel4; |
||
| 157 | $ssel2 = $ssel3; |
||
| 158 | $ssel1 = $ssel2; |
||
| 159 | $ssel0 = $ssel1; |
||
| 160 | $sel1 = $ssel0; |
||
| 161 | $sel0 = $sel1; |
||
| 162 | if (1 === $i->getVar('visible')) { |
||
| 163 | $sel1 = ' checked'; |
||
| 164 | } else { |
||
| 165 | $sel0 = ' checked'; |
||
| 166 | } |
||
| 167 | if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) { |
||
| 168 | $ssel0 = ' checked'; |
||
| 169 | } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) { |
||
| 170 | $ssel1 = ' checked'; |
||
| 171 | } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) { |
||
| 172 | $ssel2 = ' checked'; |
||
| 173 | } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) { |
||
| 174 | $ssel4 = ' checked'; |
||
| 175 | } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) { |
||
| 176 | $ssel3 = ' checked'; |
||
| 177 | } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) { |
||
| 178 | $ssel5 = ' checked'; |
||
| 179 | } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) { |
||
| 180 | $ssel6 = ' checked'; |
||
| 181 | } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) { |
||
| 182 | $ssel7 = ' checked'; |
||
| 183 | } |
||
| 184 | if ('' === $i->getVar('title')) { |
||
| 185 | $title = ' '; |
||
| 186 | }else { |
||
| 187 | $title = $i->getVar('title'); |
||
| 188 | } |
||
| 189 | $name = $i->getVar('name'); |
||
| 190 | echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title[" . $i->getVar('bid') . "]' value='" . $title . "'></td> |
||
| 191 | <td class='$class' align='center' nowrap='nowrap'><div align='center' > |
||
| 192 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_LEFT . "'$ssel2> |
||
| 193 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_CENTER . "'$ssel3> |
||
| 194 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_RIGHT . "'$ssel4> |
||
| 195 | </div> |
||
| 196 | <div> |
||
| 197 | <span style='float:right;'><input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_SIDEBLOCK_RIGHT . "'$ssel1></span> |
||
| 198 | <div align='left'><input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_SIDEBLOCK_LEFT . "'$ssel0></div> |
||
| 199 | </div> |
||
| 200 | <div align='center'> |
||
| 201 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOMLEFT . "'$ssel5> |
||
| 202 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOM . "'$ssel7> |
||
| 203 | <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOMRIGHT . "'$ssel6> |
||
| 204 | </div> |
||
| 205 | </td> |
||
| 206 | <td class='$class' align='center'><input type='text' name='weight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "' size='5' maxlength='5'></td> |
||
| 207 | <td class='$class' align='center' nowrap><input type='radio' name='visible[" . $i->getVar('bid') . "]' value='1'$sel1>" . \_YES . " <input type='radio' name='visible[" . $i->getVar('bid') . "]' value='0'$sel0>" . \_NO . '</td>'; |
||
| 208 | |||
| 209 | echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>"; |
||
| 210 | foreach ($moduleList as $k => $v) { |
||
| 211 | echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>"; |
||
| 212 | } |
||
| 213 | echo '</select></td>'; |
||
| 214 | |||
| 215 | echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>"; |
||
| 216 | foreach ($groups as $grp) { |
||
| 217 | echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>'; |
||
| 218 | } |
||
| 219 | echo '</select></td>'; |
||
| 220 | |||
| 221 | // Cache lifetime |
||
| 222 | echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select> |
||
| 223 | </td>'; |
||
| 224 | |||
| 225 | // Actions |
||
| 226 | |||
| 227 | echo "<td class='$class' align='center'> |
||
| 228 | <a href='blocksadmin.php?op=edit&bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . \_EDIT . "' title='" . \_EDIT . "'></a> |
||
| 229 | <a href='blocksadmin.php?op=clone&bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . \_CLONE . "' title='" . \_CLONE . "'></a>"; |
||
| 230 | // if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) { |
||
| 231 | // echo " <a href='" . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&op=delete&bid=' . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'> |
||
| 232 | // </a>"; |
||
| 233 | // } |
||
| 234 | |||
| 235 | // if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) { |
||
| 236 | if (!\in_array($i->getVar('block_type'), ['M', 'S'])) { |
||
| 237 | echo " |
||
| 238 | <a href='blocksadmin.php?op=delete&bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . \_DELETE . "' title='" . \_DELETE . "'> |
||
| 239 | </a>"; |
||
| 240 | } |
||
| 241 | echo " |
||
| 242 | <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'> |
||
| 243 | <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'> |
||
| 244 | <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'> |
||
| 245 | <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'> |
||
| 246 | <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'> |
||
| 247 | <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'> |
||
| 248 | <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'> |
||
| 249 | </td></tr> |
||
| 250 | "; |
||
| 251 | $class = ('even' === $class) ? 'odd' : 'even'; |
||
| 252 | } |
||
| 253 | echo "<tr><td class='foot' align='center' colspan='8'> |
||
| 254 | <input type='hidden' name='op' value='order'>" . $this->xoopsSecurity->getTokenHTML() . " |
||
| 255 | <input type='submit' name='submit' value='" . \_SUBMIT . "'> |
||
| 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) |
||
| 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 |
||
| 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 |
||
| 468 | { |
||
| 469 | // require_once \dirname(__DIR__,2) . '/admin/admin_header.php'; |
||
| 470 | // \xoops_cp_header(); |
||
| 471 | \xoops_loadLanguage('admin', 'system'); |
||
| 472 | \xoops_loadLanguage('admin/blocksadmin', 'system'); |
||
| 473 | \xoops_loadLanguage('admin/groups', 'system'); |
||
| 474 | // mpu_adm_menu(); |
||
| 475 | $myblock = new \XoopsBlock($bid); |
||
| 476 | $sql = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid; |
||
| 477 | $result = $this->db->query($sql); |
||
| 478 | if (!$this->db->isResultSet($result)) { |
||
| 479 | // \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR); |
||
| 480 | $errorMsg = \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(); |
||
| 481 | $logger = \XoopsLogger::getInstance(); |
||
| 482 | $logger->handleError(E_USER_WARNING, $errorMsg, __FILE__, __LINE__); |
||
| 483 | $this->helper->redirect('admin/blocksadmin.php', 3, $errorMsg); |
||
| 484 | } |
||
| 485 | $modules = []; |
||
| 486 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 487 | $modules[] = (int)$row['module_id']; |
||
| 488 | } |
||
| 489 | |||
| 490 | $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']); |
||
| 491 | $block = [ |
||
| 492 | 'title' => $myblock->getVar('title'), |
||
| 493 | 'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK, |
||
| 494 | // 'name' => $myblock->getVar('name'), |
||
| 495 | 'side' => $myblock->getVar('side'), |
||
| 496 | 'weight' => $myblock->getVar('weight'), |
||
| 497 | 'visible' => $myblock->getVar('visible'), |
||
| 498 | 'content' => $myblock->getVar('content', 'N'), |
||
| 499 | 'modules' => $modules, |
||
| 500 | 'is_custom' => $isCustom, |
||
| 501 | 'ctype' => $myblock->getVar('c_type'), |
||
| 502 | 'bcachetime' => $myblock->getVar('bcachetime'), |
||
| 503 | 'op' => 'edit_ok', |
||
| 504 | 'bid' => $myblock->getVar('bid'), |
||
| 505 | 'edit_form' => $myblock->getOptions(), |
||
| 506 | 'template' => $myblock->getVar('template'), |
||
| 507 | 'options' => $myblock->getVar('options'), |
||
| 508 | ]; |
||
| 509 | echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a> <span style="font-weight:bold;">»»</span> ' . \_AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>'; |
||
| 510 | |||
| 511 | $output = $this->render($block); |
||
| 512 | echo $output; |
||
| 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) |
||
| 527 | { |
||
| 528 | $myblock = new \XoopsBlock($bid); |
||
| 529 | $myblock->setVar('title', $btitle); |
||
| 530 | $myblock->setVar('weight', $bweight); |
||
| 531 | $myblock->setVar('visible', $bvisible); |
||
| 532 | $myblock->setVar('side', $bside); |
||
| 533 | $myblock->setVar('bcachetime', $bcachetime); |
||
| 534 | //update block options |
||
| 535 | if (isset($options)) { |
||
| 536 | $optionsCount = \count($options); |
||
| 537 | if ($optionsCount > 0) { |
||
| 538 | //Convert array values to comma-separated |
||
| 539 | foreach ($options as $i => $iValue) { |
||
| 540 | if (\is_array($iValue)) { |
||
| 541 | $options[$i] = \implode(',', $iValue); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | $options = \implode('|', $options); |
||
| 545 | $myblock->setVar('options', $options); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | $newbid = $myblock->store(); |
||
| 549 | |||
| 550 | /** @var \XoopsBlockHandler $blockHandler */ |
||
| 551 | $blockHandler = \xoops_getHandler('block'); |
||
| 552 | $blockHandler->insert($myblock); |
||
| 553 | |||
| 554 | if (!empty($bmodule) && \count($bmodule) > 0) { |
||
| 555 | $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid); |
||
| 556 | $this->db->query($sql); |
||
| 557 | if (\in_array(0, $bmodule)) { |
||
| 558 | $sql = \sprintf('INSERT INTO %s (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0); |
||
| 559 | $this->db->query($sql); |
||
| 560 | } else { |
||
| 561 | foreach ($bmodule as $bmid) { |
||
| 562 | $sql = \sprintf('INSERT INTO %s (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid); |
||
| 563 | $this->db->query($sql); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | $sql = \sprintf('DELETE FROM %s WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid); |
||
| 568 | $this->db->query($sql); |
||
| 569 | if (!empty($groups)) { |
||
| 570 | foreach ($groups as $grp) { |
||
| 571 | $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); |
||
| 572 | $this->db->query($sql); |
||
| 573 | } |
||
| 574 | } |
||
| 575 | $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS')); |
||
| 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( |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param array|null $block |
||
| 652 | * @return void |
||
| 653 | */ |
||
| 654 | public function render(?array $block = null): void |
||
| 764 | } |
||
| 765 | } |
||
| 766 |