| Conditions | 5 |
| Paths | 8 |
| Total Lines | 93 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 58 | public function __construct($target) |
||
| 59 | { |
||
| 60 | $this->helper = $target->helper; |
||
| 61 | $this->targetObject = $target; |
||
| 62 | |||
| 63 | $title = $this->targetObject->isNew() ? \sprintf(\AM_YOGURT_IMAGES_ADD) : \sprintf(\AM_YOGURT_IMAGES_EDIT); |
||
| 64 | parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true); |
||
| 65 | $this->setExtra('enctype="multipart/form-data"'); |
||
| 66 | |||
| 67 | //include ID field, it's needed so the module knows if it is a new form or an edited form |
||
| 68 | |||
| 69 | $hidden = new XoopsFormHidden( |
||
| 70 | 'cod_img', $this->targetObject->getVar( |
||
| 71 | 'cod_img' |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | $this->addElement($hidden); |
||
| 75 | unset($hidden); |
||
| 76 | |||
| 77 | // Cod_img |
||
| 78 | $this->addElement( |
||
| 79 | new XoopsFormLabel(\AM_YOGURT_IMAGES_COD_IMG, $this->targetObject->getVar('cod_img'), 'cod_img') |
||
| 80 | ); |
||
| 81 | // Title |
||
| 82 | $this->addElement( |
||
| 83 | new XoopsFormText(\AM_YOGURT_IMAGES_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')), |
||
| 84 | false |
||
| 85 | ); |
||
| 86 | |||
| 87 | // Caption |
||
| 88 | $this->addElement( |
||
| 89 | new XoopsFormText(\AM_YOGURT_IMAGES_CAPTION, 'caption', 50, 255, $this->targetObject->getVar('caption')), |
||
| 90 | false |
||
| 91 | ); |
||
| 92 | |||
| 93 | // Data_creation |
||
| 94 | $this->addElement( |
||
| 95 | new XoopsFormTextDateSelect( |
||
| 96 | \AM_YOGURT_IMAGES_DATE_CREATED, 'date_created', 0, \formatTimestamp($this->targetObject->getVar('date_created'), 's') |
||
|
|
|||
| 97 | ) |
||
| 98 | ); |
||
| 99 | // Data_update |
||
| 100 | $this->addElement( |
||
| 101 | new XoopsFormTextDateSelect( |
||
| 102 | \AM_YOGURT_IMAGES_DATE_UPDATED, 'date_updated', 0, \formatTimestamp($this->targetObject->getVar('date_updated'), 's') |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | // Uid_owner |
||
| 106 | $this->addElement( |
||
| 107 | new XoopsFormSelectUser( |
||
| 108 | \AM_YOGURT_IMAGES_UID_OWNER, 'uid_owner', false, $this->targetObject->getVar( |
||
| 109 | 'uid_owner' |
||
| 110 | ), 1, false |
||
| 111 | ), |
||
| 112 | false |
||
| 113 | ); |
||
| 114 | // Url |
||
| 115 | // $this->addElement( |
||
| 116 | // new XoopsFormTextArea(AM_YOGURT_IMAGES_URL, 'filename', $this->targetObject->getVar('filename'), 4, 47), |
||
| 117 | // false |
||
| 118 | // ); |
||
| 119 | |||
| 120 | $filename = $this->targetObject->getVar('filename') ?: 'blank.png'; |
||
| 121 | |||
| 122 | $uploadDir = '/uploads/yogurt/images/'; |
||
| 123 | $imgtray = new \XoopsFormElementTray(\AM_YOGURT_IMAGES_URL, '<br>'); |
||
| 124 | $imgpath = \sprintf(\AM_YOGURT_FORMIMAGE_PATH, $uploadDir); |
||
| 125 | $imageselect = new \XoopsFormSelect($imgpath, 'filename', $filename); |
||
| 126 | $imageArray = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir); |
||
| 127 | foreach ($imageArray as $image) { |
||
| 128 | $imageselect->addOption($image, $image); |
||
| 129 | } |
||
| 130 | $imageselect->setExtra("onchange='showImgSelected(\"image_url\", \"filename\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'"); |
||
| 131 | $imgtray->addElement($imageselect); |
||
| 132 | $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $url . "' name='image_url' id='image_url' alt='' style='max-width:300px'>")); |
||
| 133 | $fileseltray = new \XoopsFormElementTray('', '<br>'); |
||
| 134 | $fileseltray->addElement(new \XoopsFormFile(\AM_YOGURT_FORMUPLOAD, 'filename', $this->helper->getConfig('maxsize'))); |
||
| 135 | $fileseltray->addElement(new \XoopsFormLabel('')); |
||
| 136 | $imgtray->addElement($fileseltray); |
||
| 137 | $this->addElement($imgtray); |
||
| 138 | // Private |
||
| 139 | // $this->addElement( |
||
| 140 | // new XoopsFormText(AM_YOGURT_IMAGES_PRIVATE, 'private', 50, 255, $this->targetObject->getVar('private')), |
||
| 141 | // false |
||
| 142 | // ); |
||
| 143 | |||
| 144 | $private = $this->targetObject->isNew() ? 0 : $this->targetObject->getVar('private'); |
||
| 145 | $check_private = new \XoopsFormCheckBox(\AM_YOGURT_IMAGES_PRIVATE, 'private', $private); |
||
| 146 | $check_private->addOption(1, ' '); |
||
| 147 | $this->addElement($check_private); |
||
| 148 | |||
| 149 | $this->addElement(new XoopsFormHidden('op', 'save')); |
||
| 150 | $this->addElement(new XoopsFormButton('', 'submit', \_SUBMIT, 'submit')); |
||
| 151 | } |
||
| 153 |