| Total Complexity | 46 |
| Total Lines | 338 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ModuleAdmin 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 ModuleAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ModuleAdmin { |
||
| 18 | |||
| 19 | private $_itemButton = []; |
||
| 20 | private $_itemInfoBox = []; |
||
|
|
|||
| 21 | private $_itemInfoBoxLine = []; |
||
| 22 | private $_itemConfigBoxLine = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var XoopsModule |
||
| 26 | */ |
||
| 27 | private $_obj; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * addAssets - add assets to theme, if it is established |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | private function addAssets() |
||
| 44 | { |
||
| 45 | static $added; |
||
| 46 | |||
| 47 | if (empty($added) && !empty($GLOBALS['xoTheme'])) { |
||
| 48 | $added = true; |
||
| 49 | $GLOBALS['xoTheme']->addStylesheet("modules/system/css/admin.css"); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add Config Line |
||
| 55 | * |
||
| 56 | * @param string $value |
||
| 57 | * @param string $status |
||
| 58 | * @param string $type |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public function addConfigBoxLine($value = '', $status = '', $type = 'default') |
||
| 63 | { |
||
| 64 | $line = ''; |
||
| 65 | |||
| 66 | switch ($type) { |
||
| 67 | default: |
||
| 68 | case 'default': |
||
| 69 | $this->_itemConfigBoxLine['items'][] = [ |
||
| 70 | 'type' => $status, |
||
| 71 | 'msg' => $value, |
||
| 72 | ]; |
||
| 73 | break; |
||
| 74 | |||
| 75 | case 'folder': |
||
| 76 | if (!is_dir($value)) { |
||
| 77 | $this->_itemConfigBoxLine['items'][] = [ |
||
| 78 | 'type' => 'error', |
||
| 79 | 'msg' => sprintf(_AM_MODULEADMIN_CONFIG_FOLDERKO, $value), |
||
| 80 | ]; |
||
| 81 | } else { |
||
| 82 | $this->_itemConfigBoxLine['items'][] = [ |
||
| 83 | 'type' => 'success', |
||
| 84 | 'msg' => sprintf(_AM_MODULEADMIN_CONFIG_FOLDERKO, $value), |
||
| 85 | ]; |
||
| 86 | } |
||
| 87 | break; |
||
| 88 | |||
| 89 | case 'chmod': |
||
| 90 | if (is_dir($value[0])) { |
||
| 91 | if (substr(decoct(fileperms($value[0])), 2) != $value[1]) { |
||
| 92 | $this->_itemConfigBoxLine['items'][] = [ |
||
| 93 | 'type' => 'error', |
||
| 94 | 'msg' => sprintf(_AM_MODULEADMIN_CONFIG_CHMOD, $value[0], $value[1], substr(decoct(fileperms($value[0])), 2)), |
||
| 95 | ]; |
||
| 96 | } else { |
||
| 97 | $this->_itemConfigBoxLine['items'][] = [ |
||
| 98 | 'type' => 'success', |
||
| 99 | 'msg' => sprintf(_AM_MODULEADMIN_CONFIG_CHMOD, $value[0], $value[1], substr(decoct(fileperms($value[0])), 2)), |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param $title |
||
| 111 | * @param $link |
||
| 112 | * @param string $icon |
||
| 113 | * @param string $extra |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function addItemButton($title, $link, $icon = 'add', $extra = '') |
||
| 118 | { |
||
| 119 | $ret = []; |
||
| 120 | $ret['title'] = $title; |
||
| 121 | $ret['link'] = $link; |
||
| 122 | $ret['icon'] = $icon . '.png'; |
||
| 123 | $ret['extra'] = $extra; |
||
| 124 | $this->_itemButton[] = $ret; |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add Nivagition Menu |
||
| 131 | * |
||
| 132 | * @param string $menu |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function addNavigation($menu = '') |
||
| 136 | { |
||
| 137 | global $xoops, $xoopsTpl; |
||
| 138 | |||
| 139 | $this->addAssets(); |
||
| 140 | $path = $xoops->url('modules/' . $this->_obj->getVar('dirname') . '/'); |
||
| 141 | |||
| 142 | $this->_obj->loadAdminMenu(); |
||
| 143 | |||
| 144 | $xoopsTpl->assign('path', $path); |
||
| 145 | $xoopsTpl->assign('menu', $menu); |
||
| 146 | |||
| 147 | |||
| 148 | foreach (array_keys((array) $this->_obj->adminmenu) as $i) { |
||
| 149 | if ($this->_obj->adminmenu[$i]['link'] == 'admin/' . $menu) { |
||
| 150 | $xoopsTpl->assign('icon', $this->_obj->adminmenu[$i]['icon']); |
||
| 151 | $xoopsTpl->assign('title', $this->_obj->adminmenu[$i]['title']); |
||
| 152 | $xoopsTpl->assign('link', $this->_obj->adminmenu[$i]['link']); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | // Display Navigation |
||
| 156 | $xoopsTpl->display('db:system_modules_navigation.tpl'); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Display Admin About page |
||
| 161 | * |
||
| 162 | * @param string $business the PAYPAL business email or Merchant Account ID |
||
| 163 | * @param bool $logo_xoops true to display XOOPS logo and link on page |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public function renderAbout($business = '', $logo_xoops = true) |
||
| 167 | { |
||
| 168 | global $xoops, $xoopsTpl; |
||
| 169 | |||
| 170 | $this->addAssets(); |
||
| 171 | |||
| 172 | $date = preg_replace('/-\\\/', '/', $this->_obj->getInfo('release_date')); // make format a little more forgiving |
||
| 173 | $date = explode('/', $date); |
||
| 174 | $author = explode(',', $this->_obj->getInfo('author')); |
||
| 175 | $nickname = explode(',', $this->_obj->getInfo('nickname')); |
||
| 176 | $release_date = formatTimestamp(mktime(0, 0, 0, $date[1], $date[2], $date[0]), 's'); |
||
| 177 | $module_dir = $this->_obj->getVar('dirname'); |
||
| 178 | |||
| 179 | $license_url = $this->_obj->getInfo('license_url'); |
||
| 180 | $license_url = preg_match('%^(https?:)?//%', $license_url) ? $license_url : 'http://' . $license_url; |
||
| 181 | $website = $this->_obj->getInfo('website'); |
||
| 182 | $website = preg_match('%^(https?:)?//%', $website) ? $website : 'http://' . $website; |
||
| 183 | |||
| 184 | $xoopsTpl->assign('module_dir', $module_dir); |
||
| 185 | $xoopsTpl->assign('module_name', $this->_obj->getVar('name')); |
||
| 186 | $xoopsTpl->assign('module_version', $this->_obj->getVar('version')); |
||
| 187 | $xoopsTpl->assign('module_img', $xoops->url('modules/' . $module_dir . '/' . $this->_obj->getInfo('image'))); |
||
| 188 | |||
| 189 | // Author |
||
| 190 | $authorArray = []; |
||
| 191 | foreach ( $author as $k => $aName ) { |
||
| 192 | $authorArray[$k] = ( isset( $nickname[$k] ) && ( '' != $nickname[$k] ) ) ? "{$aName} ({$nickname[$k]})" : (string)($aName); |
||
| 193 | } |
||
| 194 | $xoopsTpl->assign('author', implode(', ', $authorArray)); |
||
| 195 | $xoopsTpl->assign('release_date', $release_date); |
||
| 196 | $xoopsTpl->assign('license', $this->_obj->getInfo('license')); |
||
| 197 | $xoopsTpl->assign('license_url', $license_url); |
||
| 198 | |||
| 199 | // Module Info |
||
| 200 | $xoopsTpl->assign('module_description', $this->_obj->getInfo('description')); |
||
| 201 | $xoopsTpl->assign('module_last_update', formatTimestamp($this->_obj->getVar('last_update'), 'm')); |
||
| 202 | $xoopsTpl->assign('module_status', $this->_obj->getStatus()); |
||
| 203 | $xoopsTpl->assign('module_website_name', $this->_obj->getInfo('module_website_name')); |
||
| 204 | $xoopsTpl->assign('module_website_url', $this->_obj->getInfo('module_website_url')); |
||
| 205 | |||
| 206 | // Donation |
||
| 207 | if ((1 !== preg_match('/[^a-zA-Z0-9]/', $business)) || (false !== checkEmail($business))) { |
||
| 208 | $xoopsTpl->assign('business', $business); |
||
| 209 | } |
||
| 210 | |||
| 211 | // Changelog |
||
| 212 | $changelog = ''; |
||
| 213 | $language = empty( $GLOBALS['xoopsConfig']['language'] ) ? 'english' : $GLOBALS['xoopsConfig']['language']; |
||
| 214 | $file = XOOPS_ROOT_PATH . "/modules/{$module_dir}/language/{$language}/changelog.txt"; |
||
| 215 | if ( !is_file( $file ) && ( 'english' !== $language ) ) { |
||
| 216 | $file = XOOPS_ROOT_PATH . "/modules/{$module_dir}/language/english/changelog.txt"; |
||
| 217 | } |
||
| 218 | if ( is_readable( $file ) ) { |
||
| 219 | $changelog .= ( implode( '<br>', file( $file ) ) ) . "\n"; |
||
| 220 | } else { |
||
| 221 | $file = XOOPS_ROOT_PATH . "/modules/{$module_dir}/docs/changelog.txt"; |
||
| 222 | if ( is_readable( $file ) ) { |
||
| 223 | $changelog .= implode( '<br>', file( $file ) ) . "\n"; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | $xoopsTpl->assign('changelog', $changelog); |
||
| 227 | |||
| 228 | // Display page |
||
| 229 | $xoopsTpl->display('db:system_modules_about.tpl'); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $position |
||
| 234 | * @param string $delimeter |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function renderButton($position = 'right', $delimeter = ' ') |
||
| 239 | { |
||
| 240 | global $xoops, $xoopsTpl; |
||
| 241 | |||
| 242 | $icons = xoops_getModuleOption('typeicons', 'system'); |
||
| 243 | if ($icons == '') { |
||
| 244 | $icons = 'default'; |
||
| 245 | } |
||
| 246 | $path = $xoops->url('modules/system/images/icons/' . $icons . '/'); |
||
| 247 | |||
| 248 | |||
| 249 | $this->addAssets(); |
||
| 250 | |||
| 251 | $xoopsTpl->assign('path', $path); |
||
| 252 | $xoopsTpl->assign('buttons', $this->_itemButton ); |
||
| 253 | $xoopsTpl->assign('position', $position); |
||
| 254 | |||
| 255 | $xoopsTpl->display('db:system_modules_button.tpl'); |
||
| 256 | |||
| 257 | $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/32/'; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Display Admin Index page |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | public function renderIndex() |
||
| 355 | } |
||
| 356 | } |