| Total Complexity | 42 |
| Total Lines | 275 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class File extends \XoopsObject |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | public $start = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public $limit = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | public function __construct() |
||
| 51 | { |
||
| 52 | $this->initVar('id', \XOBJ_DTYPE_INT); |
||
| 53 | $this->initVar('directory_id', \XOBJ_DTYPE_INT); |
||
| 54 | $this->initVar('name', \XOBJ_DTYPE_TXTBOX); |
||
| 55 | $this->initVar('description', \XOBJ_DTYPE_OTHER); |
||
| 56 | $this->initVar('mimetype', \XOBJ_DTYPE_TXTBOX); |
||
| 57 | $this->initVar('size', \XOBJ_DTYPE_INT); |
||
| 58 | $this->initVar('ctime', \XOBJ_DTYPE_INT); |
||
| 59 | $this->initVar('mtime', \XOBJ_DTYPE_INT); |
||
| 60 | $this->initVar('ip', \XOBJ_DTYPE_TXTBOX); |
||
| 61 | $this->initVar('status', \XOBJ_DTYPE_INT); |
||
| 62 | $this->initVar('date_created', \XOBJ_DTYPE_INT); |
||
| 63 | $this->initVar('submitter', \XOBJ_DTYPE_INT); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @static function &getInstance |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | public static function getInstance() |
||
| 71 | { |
||
| 72 | static $instance = false; |
||
| 73 | if (!$instance) { |
||
| 74 | $instance = new self(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The new inserted $Id |
||
| 80 | * @return integer |
||
| 81 | */ |
||
| 82 | public function getNewInsertedId() |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @public function getForm |
||
| 89 | * @param bool $action |
||
| 90 | * @return \XoopsThemeForm |
||
| 91 | */ |
||
| 92 | public function getForm($action = false) |
||
| 93 | { |
||
| 94 | $helper = \XoopsModules\Wgfilemanager\Helper::getInstance(); |
||
| 95 | $fileHandler = $helper->getHandler('File'); |
||
| 96 | $permissionsHandler = $helper->getHandler('Permissions'); |
||
| 97 | if (!$action) { |
||
| 98 | $action = $_SERVER['REQUEST_URI']; |
||
| 99 | } |
||
| 100 | $isAdmin = \is_object($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid()); |
||
| 101 | // Title |
||
| 102 | $title = $this->isNew() ? \_MA_WGFILEMANAGER_FILE_ADD : \_MA_WGFILEMANAGER_FILE_EDIT; |
||
| 103 | // Get Theme Form |
||
| 104 | \xoops_load('XoopsFormLoader'); |
||
| 105 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 106 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 107 | //$form->addElement(new \XoopsFormHidden('formtype', $formType)); |
||
| 108 | // Form Table directory |
||
| 109 | $directoryId = (int)$this->getVar('directory_id'); |
||
| 110 | $directoryHandler = $helper->getHandler('Directory'); |
||
| 111 | $fileDirectory_idSelect = new \XoopsFormSelect(\_MA_WGFILEMANAGER_FILE_DIRECTORY_ID, 'directory_id', $directoryId); |
||
| 112 | $dirListSelect = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($directoryHandler->getDirListFormSelect(0))); |
||
| 113 | foreach ($dirListSelect as $key => $value) { |
||
| 114 | $fileDirectory_idSelect->addOption($key, $value); |
||
|
|
|||
| 115 | } |
||
| 116 | $form->addElement($fileDirectory_idSelect, true); |
||
| 117 | $form->addElement(new \XoopsFormHidden('directory_id_old', $directoryId)); |
||
| 118 | // Form File: Upload fileName |
||
| 119 | $fileName = $this->isNew() ? '' : $this->getVar('name'); |
||
| 120 | if ($this->isNew()) { |
||
| 121 | if ($permissionsHandler->getPermUploadFileToDir($directoryId)) { |
||
| 122 | $fileUploadTray = new \XoopsFormElementTray(\_MA_WGFILEMANAGER_FILE_NAME, '<br>'); |
||
| 123 | //$fileDirectory = '/uploads/wgfilemanager/files/file'; |
||
| 124 | //$fileUploadTray->addElement(new \XoopsFormLabel(\sprintf(\_MA_WGFILEMANAGER_FILE_NAME_UPLOADS, ".$fileDirectory/"), $fileName)); |
||
| 125 | $maxsize = $helper->getConfig('maxsize_file'); |
||
| 126 | $fileUploadTray->addElement(new \XoopsFormFile('', 'name', $maxsize)); |
||
| 127 | $fileUploadTray->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FORM_UPLOAD_SIZE, ($maxsize / 1048576) . ' ' . \_MA_WGFILEMANAGER_FORM_UPLOAD_SIZE_MB)); |
||
| 128 | $form->addElement($fileUploadTray, true); |
||
| 129 | } else { |
||
| 130 | $form->addElement(new \XoopsFormHidden('name', $fileName)); |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | $form->addElement(new \XoopsFormText(\_MA_WGFILEMANAGER_FILE_NAME, 'name', 100, 150, $fileName)); |
||
| 134 | $form->addElement(new \XoopsFormHidden('name_old', $fileName)); |
||
| 135 | } |
||
| 136 | // Form Editor DhtmlTextArea fileDescription |
||
| 137 | $editorConfigs = []; |
||
| 138 | if ($isAdmin) { |
||
| 139 | $editor = $helper->getConfig('editor_admin'); |
||
| 140 | } else { |
||
| 141 | $editor = $helper->getConfig('editor_user'); |
||
| 142 | } |
||
| 143 | $editorConfigs['name'] = 'description'; |
||
| 144 | $editorConfigs['value'] = $this->getVar('description', 'e'); |
||
| 145 | $editorConfigs['rows'] = 5; |
||
| 146 | $editorConfigs['cols'] = 40; |
||
| 147 | $editorConfigs['width'] = '100%'; |
||
| 148 | $editorConfigs['height'] = '400px'; |
||
| 149 | $editorConfigs['editor'] = $editor; |
||
| 150 | $form->addElement(new \XoopsFormEditor(\_MA_WGFILEMANAGER_FILE_DESCRIPTION, 'description', $editorConfigs)); |
||
| 151 | // Form Text fileType |
||
| 152 | $fileType = $this->isNew() ? '' : $this->getVar('mimetype'); |
||
| 153 | if (!$this->isNew()) { |
||
| 154 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_MIMETYPE, $fileType)); |
||
| 155 | } |
||
| 156 | //$form->addElement(new \XoopsFormHidden('mimetype', $fileType)); |
||
| 157 | // Form Text Date file modification date |
||
| 158 | $fileSize = $this->isNew() ? \time() : $this->getVar('size'); |
||
| 159 | if (!$this->isNew()) { |
||
| 160 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_SIZE, $fileHandler->FileSizeConvert($fileSize))); |
||
| 161 | } |
||
| 162 | // Form Text Date file modification date |
||
| 163 | $fileMtime = $this->isNew() ? \time() : $this->getVar('mtime'); |
||
| 164 | if (!$this->isNew()) { |
||
| 165 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_MTIME, \formatTimestamp($fileMtime, 's'))); |
||
| 166 | } |
||
| 167 | // Form Text Date file creation date |
||
| 168 | $fileCtime = $this->isNew() ? \time() : $this->getVar('ctime'); |
||
| 169 | if (!$this->isNew()) { |
||
| 170 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_CTIME, \formatTimestamp($fileCtime, 's'))); |
||
| 171 | } |
||
| 172 | //$form->addElement(new \XoopsFormHidden('mtime', $fileMtime)); |
||
| 173 | //$form->addElement(new \XoopsFormTextDateSelect(\_MA_WGFILEMANAGER_FILE_MTIME, 'mtime', '', $fileMtime)); |
||
| 174 | // Form Text IP fileIp |
||
| 175 | $fileIp = $_SERVER['REMOTE_ADDR']; |
||
| 176 | if ($isAdmin) { |
||
| 177 | $form->addElement(new \XoopsFormText(\_MA_WGFILEMANAGER_FILE_IP, 'ip', 20, 150, $fileIp)); |
||
| 178 | } else { |
||
| 179 | $form->addElement(new \XoopsFormHidden('ip', $fileIp)); |
||
| 180 | } |
||
| 181 | // Form Select Status fileStatus |
||
| 182 | $fileStatus = $this->isNew() ? Constants::STATUS_SUBMITTED : $this->getVar('status'); |
||
| 183 | if ($isAdmin) { |
||
| 184 | $fileStatusSelect = new \XoopsFormSelect(\_MA_WGFILEMANAGER_FILE_STATUS, 'status', $fileStatus); |
||
| 185 | $fileStatusSelect->addOption(Constants::STATUS_NONE, \_AM_WGFILEMANAGER_STATUS_NONE); |
||
| 186 | //$fileStatusSelect->addOption(Constants::STATUS_OFFLINE, \_AM_WGFILEMANAGER_STATUS_OFFLINE); |
||
| 187 | $fileStatusSelect->addOption(Constants::STATUS_SUBMITTED, \_AM_WGFILEMANAGER_STATUS_SUBMITTED); |
||
| 188 | //$fileStatusSelect->addOption(Constants::STATUS_APPROVED, \_AM_WGFILEMANAGER_STATUS_APPROVED); |
||
| 189 | $fileStatusSelect->addOption(Constants::STATUS_BROKEN, \_AM_WGFILEMANAGER_STATUS_BROKEN); |
||
| 190 | $form->addElement($fileStatusSelect); |
||
| 191 | } else { |
||
| 192 | $form->addElement(new \XoopsFormHidden('status', $fileStatus)); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Form Text Date Select fileDate_created |
||
| 196 | $fileDate_created = $this->isNew() ? \time() : $this->getVar('date_created'); |
||
| 197 | if ($isAdmin) { |
||
| 198 | $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGFILEMANAGER_FILE_DATE_CREATED, 'date_created', '', $fileDate_created)); |
||
| 199 | } elseif (!$this->isNew()) { |
||
| 200 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_DATE_CREATED, \formatTimestamp($fileDate_created, 's'))); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Form Select User fileSubmitter |
||
| 204 | $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0; |
||
| 205 | $fileSubmitter = $this->isNew() ? $uidCurrent : $this->getVar('submitter'); |
||
| 206 | if ($isAdmin) { |
||
| 207 | $form->addElement(new \XoopsFormSelectUser(\_MA_WGFILEMANAGER_FILE_SUBMITTER, 'submitter', false, $fileSubmitter)); |
||
| 208 | } else { |
||
| 209 | $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_SUBMITTER, \XoopsUser::getUnameFromId($fileSubmitter))); |
||
| 210 | $form->addElement(new \XoopsFormHidden('submitter', $fileSubmitter)); |
||
| 211 | } |
||
| 212 | // To Save |
||
| 213 | $form->addElement(new \XoopsFormHidden('op', 'save')); |
||
| 214 | $form->addElement(new \XoopsFormHidden('start', $this->start)); |
||
| 215 | $form->addElement(new \XoopsFormHidden('limit', $this->limit)); |
||
| 216 | $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false)); |
||
| 217 | return $form; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get Values |
||
| 222 | * @param null $keys |
||
| 223 | * @param null $format |
||
| 224 | * @param null $maxDepth |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getValuesFile($keys = null, $format = null, $maxDepth = null) |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns an array representation of the object |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function toArrayFile() |
||
| 309 | } |
||
| 310 | } |
||
| 311 |