| Conditions | 9 |
| Paths | 32 |
| Total Lines | 103 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 71 | public function getForm($action = false) |
||
| 72 | { |
||
| 73 | /** @var \XoopsModules\Tdmdownloads\Helper $helper */ |
||
| 74 | |||
| 75 | $helper = \XoopsModules\Tdmdownloads\Helper::getInstance(); |
||
| 76 | |||
| 77 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 78 | |||
| 79 | if (!$action) { |
||
| 80 | $action = $_SERVER['REQUEST_URI']; |
||
| 81 | } |
||
| 82 | |||
| 83 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 84 | |||
| 85 | //nom du formulaire selon l'action (editer ou ajouter): |
||
| 86 | |||
| 87 | $title = $this->isNew() ? \sprintf(_AM_TDMDOWNLOADS_FORMADD) : \sprintf(_AM_TDMDOWNLOADS_FORMEDIT); |
||
| 88 | |||
| 89 | //création du formulaire |
||
| 90 | |||
| 91 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 92 | |||
| 93 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 94 | |||
| 95 | //titre |
||
| 96 | |||
| 97 | if (1 == $this->getVar('status_def')) { |
||
| 98 | $form->addElement(new \XoopsFormLabel(_AM_TDMDOWNLOADS_FORMTITLE, $this->getVar('title'))); |
||
| 99 | |||
| 100 | $form->addElement(new \XoopsFormHidden('title', $this->getVar('title'))); |
||
| 101 | } else { |
||
| 102 | $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, $this->getVar('title')), true); |
||
| 103 | } |
||
| 104 | |||
| 105 | //image |
||
| 106 | |||
| 107 | $downloadsfield_img = $this->getVar('img') ?: 'blank.gif'; |
||
| 108 | |||
| 109 | $uploadirectory = '/uploads/' . $moduleDirName . '/images/field'; |
||
| 110 | |||
| 111 | $imgtray = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMAGE, '<br>'); |
||
| 112 | |||
| 113 | $imgpath = \sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory); |
||
| 114 | |||
| 115 | $imageselect = new \XoopsFormSelect($imgpath, 'downloadsfield_img', $downloadsfield_img); |
||
| 116 | |||
| 117 | $topics_array = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadirectory); |
||
| 118 | |||
| 119 | foreach ($topics_array as $image) { |
||
| 120 | $imageselect->addOption($image, $image); |
||
| 121 | } |
||
| 122 | |||
| 123 | $imageselect->setExtra("onchange='showImgSelected(\"image3\", \"downloadsfield_img\", \"" . $uploadirectory . '", "", "' . XOOPS_URL . "\")'"); |
||
| 124 | |||
| 125 | $imgtray->addElement($imageselect, false); |
||
| 126 | |||
| 127 | $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadirectory . '/' . $downloadsfield_img . "' name='image3' id='image3' alt=''><br>")); |
||
| 128 | |||
| 129 | $fileseltray = new \XoopsFormElementTray('', '<br>'); |
||
| 130 | |||
| 131 | $fileseltray->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxuploadsize')), false); |
||
| 132 | |||
| 133 | $fileseltray->addElement(new \XoopsFormLabel(''), false); |
||
| 134 | |||
| 135 | $imgtray->addElement($fileseltray); |
||
| 136 | |||
| 137 | $form->addElement($imgtray); |
||
| 138 | |||
| 139 | //poids du champ |
||
| 140 | |||
| 141 | $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMWEIGHT, 'weight', 5, 5, $this->getVar('weight', 'e')), false); |
||
| 142 | |||
| 143 | // affiché? |
||
| 144 | |||
| 145 | $status = $this->getVar('status') ?: 0; |
||
| 146 | |||
| 147 | $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHE, 'status', $status)); |
||
| 148 | |||
| 149 | // affiché dans le champ de recherche? |
||
| 150 | |||
| 151 | $search = $this->getVar('search') ?: 0; |
||
| 152 | |||
| 153 | $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHESEARCH, 'search', $search)); |
||
| 154 | |||
| 155 | // pour passer "fid" si on modifie le champ |
||
| 156 | |||
| 157 | if (!$this->isNew()) { |
||
| 158 | $form->addElement(new \XoopsFormHidden('fid', $this->getVar('fid'))); |
||
| 159 | |||
| 160 | $form->addElement(new \XoopsFormHidden('status_def', $this->getVar('status_def'))); |
||
| 161 | } else { |
||
| 162 | $form->addElement(new \XoopsFormHidden('status_def', 0)); |
||
| 163 | } |
||
| 164 | |||
| 165 | //pour enregistrer le formulaire |
||
| 166 | |||
| 167 | $form->addElement(new \XoopsFormHidden('op', 'save_field')); |
||
| 168 | |||
| 169 | //boutton d'envoi du formulaire |
||
| 170 | |||
| 171 | $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', 'submit', false)); |
||
| 172 | |||
| 173 | return $form; |
||
| 174 | } |
||
| 176 |