| Total Complexity | 50 |
| Total Lines | 335 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SysUtility 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 SysUtility, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class SysUtility |
||
| 36 | { |
||
| 37 | use VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
|
|
|||
| 38 | use ServerStats; // getServerStats Trait |
||
| 39 | use FilesManagement; // Files Management Trait |
||
| 40 | |||
| 41 | //--------------- Common module methods ----------------------------- |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Access the only instance of this class |
||
| 45 | * |
||
| 46 | * @return \XoopsModules\Marquee\Common\SysUtility |
||
| 47 | * |
||
| 48 | */ |
||
| 49 | public static function getInstance() |
||
| 50 | { |
||
| 51 | static $instance; |
||
| 52 | if (null === $instance) { |
||
| 53 | $instance = new static(); |
||
| 54 | } |
||
| 55 | |||
| 56 | return $instance; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $text |
||
| 61 | * @param $form_sort |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public static function selectSorting($text, $form_sort) |
||
| 65 | { |
||
| 66 | global $start, $order, $file_cat, $sort, $xoopsModule; |
||
| 67 | |||
| 68 | $select_view = ''; |
||
| 69 | $moduleDirName = basename(dirname(__DIR__)); |
||
| 70 | /** @var Helper $helper */ |
||
| 71 | $helper = Helper::getInstance(); |
||
| 72 | |||
| 73 | //$pathModIcon16 = XOOPS_URL . '/modules/' . $moduleDirName . '/' . $helper->getConfig('modicons16'); |
||
| 74 | $pathModIcon16 = $helper->url($helper->getModule()->getInfo('modicons16')); |
||
| 75 | |||
| 76 | $select_view = '<form name="form_switch" id="form_switch" action="' . Request::getString('REQUEST_URI', '', 'SERVER') . '" method="post"><span style="font-weight: bold;">' . $text . '</span>'; |
||
| 77 | //$sorts = $sort == 'asc' ? 'desc' : 'asc'; |
||
| 78 | if ($form_sort == $sort) { |
||
| 79 | $sel1 = 'asc' === $order ? 'selasc.png' : 'asc.png'; |
||
| 80 | $sel2 = 'desc' === $order ? 'seldesc.png' : 'desc.png'; |
||
| 81 | } else { |
||
| 82 | $sel1 = 'asc.png'; |
||
| 83 | $sel2 = 'desc.png'; |
||
| 84 | } |
||
| 85 | $select_view .= ' <a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=asc"><img src="' . $pathModIcon16 . '/' . $sel1 . '" title="ASC" alt="ASC"></a>'; |
||
| 86 | $select_view .= '<a href="' . Request::getString('SCRIPT_NAME', '', 'SERVER') . '?start=' . $start . '&sort=' . $form_sort . '&order=desc"><img src="' . $pathModIcon16 . '/' . $sel2 . '" title="DESC" alt="DESC"></a>'; |
||
| 87 | $select_view .= '</form>'; |
||
| 88 | |||
| 89 | return $select_view; |
||
| 90 | } |
||
| 91 | |||
| 92 | /***************Blocks***************/ |
||
| 93 | /** |
||
| 94 | * @param array $cats |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public static function blockAddCatSelect($cats) |
||
| 98 | { |
||
| 99 | $cat_sql = ''; |
||
| 100 | if (is_array($cats) && !empty($cats)) { |
||
| 101 | $cat_sql = '(' . current($cats); |
||
| 102 | array_shift($cats); |
||
| 103 | foreach ($cats as $cat) { |
||
| 104 | $cat_sql .= ',' . $cat; |
||
| 105 | } |
||
| 106 | $cat_sql .= ')'; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $cat_sql; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $content |
||
| 114 | */ |
||
| 115 | public static function metaKeywords($content) |
||
| 116 | { |
||
| 117 | global $xoopsTpl, $xoTheme; |
||
| 118 | $myts = \MyTextSanitizer::getInstance(); |
||
| 119 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 120 | if (null !== $xoTheme && is_object($xoTheme)) { |
||
| 121 | $xoTheme->addMeta('meta', 'keywords', strip_tags($content)); |
||
| 122 | } else { // Compatibility for old Xoops versions |
||
| 123 | $xoopsTpl->assign('xoops_metaKeywords', strip_tags($content)); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $content |
||
| 129 | */ |
||
| 130 | public static function metaDescription($content) |
||
| 131 | { |
||
| 132 | global $xoopsTpl, $xoTheme; |
||
| 133 | $myts = \MyTextSanitizer::getInstance(); |
||
| 134 | $content = $myts->undoHtmlSpecialChars($myts->displayTarea($content)); |
||
| 135 | if (null !== $xoTheme && is_object($xoTheme)) { |
||
| 136 | $xoTheme->addMeta('meta', 'description', strip_tags($content)); |
||
| 137 | } else { // Compatibility for old Xoops versions |
||
| 138 | $xoopsTpl->assign('xoops_metaDescription', strip_tags($content)); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $tableName |
||
| 144 | * @param $columnName |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public static function enumerate($tableName, $columnName) |
||
| 149 | { |
||
| 150 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 151 | |||
| 152 | // $result = $GLOBALS['xoopsDB']->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS |
||
| 153 | // WHERE TABLE_NAME = '" . $table . "' AND COLUMN_NAME = '" . $columnName . "'") |
||
| 154 | // || exit ($GLOBALS['xoopsDB']->error()); |
||
| 155 | |||
| 156 | $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "' . $table . '" AND COLUMN_NAME = "' . $columnName . '"'; |
||
| 157 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 158 | if (!$result) { |
||
| 159 | exit($GLOBALS['xoopsDB']->error()); |
||
| 160 | } |
||
| 161 | |||
| 162 | $row = $GLOBALS['xoopsDB']->fetchBoth($result); |
||
| 163 | $enumList = explode(',', str_replace("'", '', substr($row['COLUMN_TYPE'], 5, - 6))); |
||
| 164 | return $enumList; |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array|string $tableName |
||
| 170 | * @param int $id_field |
||
| 171 | * @param int $id |
||
| 172 | * |
||
| 173 | * @return mixed |
||
| 174 | */ |
||
| 175 | public static function cloneRecord($tableName, $id_field, $id) |
||
| 176 | { |
||
| 177 | $new_id = false; |
||
| 178 | $table = $GLOBALS['xoopsDB']->prefix($tableName); |
||
| 179 | // copy content of the record you wish to clone |
||
| 180 | $sql = "SELECT * FROM $table WHERE $id_field='$id' "; |
||
| 181 | $tempTable = $GLOBALS['xoopsDB']->fetchArray($GLOBALS['xoopsDB']->query($sql), MYSQLI_ASSOC); |
||
| 182 | if (!$tempTable) { |
||
| 183 | exit($GLOBALS['xoopsDB']->error()); |
||
| 184 | } |
||
| 185 | // set the auto-incremented id's value to blank. |
||
| 186 | unset($tempTable[$id_field]); |
||
| 187 | // insert cloned copy of the original record |
||
| 188 | $sql = "INSERT INTO $table (" . implode(', ', array_keys($tempTable)) . ") VALUES ('" . implode("', '", array_values($tempTable)) . "')"; |
||
| 189 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 190 | if (!$result) { |
||
| 191 | exit($GLOBALS['xoopsDB']->error()); |
||
| 192 | } |
||
| 193 | // Return the new id |
||
| 194 | $new_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 195 | |||
| 196 | return $new_id; |
||
| 197 | } |
||
| 198 | /** |
||
| 199 | * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
||
| 200 | * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
||
| 201 | * www.cakephp.org |
||
| 202 | * |
||
| 203 | * @param string $text String to truncate. |
||
| 204 | * @param int $length Length of returned string, including ellipsis. |
||
| 205 | * @param string $ending Ending to be appended to the trimmed string. |
||
| 206 | * @param bool $exact If false, $text will not be cut mid-word |
||
| 207 | * @param bool $considerHtml If true, HTML tags would be handled correctly |
||
| 208 | * |
||
| 209 | * @return string Trimmed string. |
||
| 210 | */ |
||
| 211 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param \Xmf\Module\Helper $helper |
||
| 305 | * @param array|null $options |
||
| 306 | * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor |
||
| 307 | */ |
||
| 308 | public static function getEditor($helper = null, $options = null) |
||
| 309 | { |
||
| 310 | /** @var Helper $helper */ |
||
| 311 | if (null === $options) { |
||
| 312 | $options = []; |
||
| 313 | $options['name'] = 'Editor'; |
||
| 314 | $options['value'] = 'Editor'; |
||
| 315 | $options['rows'] = 10; |
||
| 316 | $options['cols'] = '100%'; |
||
| 317 | $options['width'] = '100%'; |
||
| 318 | $options['height'] = '400px'; |
||
| 319 | } |
||
| 320 | |||
| 321 | if (null === $helper) { |
||
| 322 | $helper = Helper::getInstance(); |
||
| 323 | } |
||
| 324 | |||
| 325 | $isAdmin = $helper->isUserAdmin(); |
||
| 326 | |||
| 327 | if (class_exists('XoopsFormEditor')) { |
||
| 328 | if ($isAdmin) { |
||
| 329 | $descEditor = new \XoopsFormEditor(ucfirst($options['name']), $helper->getConfig('editorAdmin'), $options, $nohtml = false, $onfailure = 'textarea'); |
||
| 330 | } else { |
||
| 331 | $descEditor = new \XoopsFormEditor(ucfirst($options['name']), $helper->getConfig('editorUser'), $options, $nohtml = false, $onfailure = 'textarea'); |
||
| 332 | } |
||
| 333 | } else { |
||
| 334 | $descEditor = new \XoopsFormDhtmlTextArea(ucfirst($options['name']), $options['name'], $options['value'], '100%', '100%'); |
||
| 335 | } |
||
| 336 | |||
| 337 | // $form->addElement($descEditor); |
||
| 338 | |||
| 339 | return $descEditor; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $fieldname |
||
| 344 | * @param string $table |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public static function fieldExists($fieldname, $table) |
||
| 349 | { |
||
| 350 | global $xoopsDB; |
||
| 351 | $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
||
| 352 | |||
| 353 | return ($xoopsDB->getRowsNum($result) > 0); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 358 | * |
||
| 359 | * @param string $folder The full path of the directory to check |
||
| 360 | */ |
||
| 361 | public static function prepareFolder($folder) |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 |