Conditions | 11 |
Paths | 74 |
Total Lines | 96 |
Code Lines | 63 |
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 |
||
68 | public function __construct($target) |
||
69 | { |
||
70 | // global $helper; |
||
71 | $this->helper = $target->helper; |
||
|
|||
72 | $this->targetObject = $target; |
||
73 | |||
74 | $title = $this->targetObject->isNew() ? \sprintf(\AM_PEDIGREE_REGISTRY_ADD) : \sprintf(AM_PEDIGREE_REGISTRY_EDIT); |
||
75 | parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true); |
||
76 | $this->setExtra('enctype="multipart/form-data"'); |
||
77 | |||
78 | //include ID field, it's needed so the module knows if it is a new form or an edited form |
||
79 | |||
80 | $hidden = new \XoopsFormHidden('id', $this->targetObject->getVar('id')); |
||
81 | $this->addElement($hidden); |
||
82 | unset($hidden); |
||
83 | |||
84 | // Id |
||
85 | $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_ID, $this->targetObject->getVar('id'), 'id')); |
||
86 | // Pname |
||
87 | $this->addElement(new \XoopsFormTextArea(\AM_PEDIGREE_REGISTRY_PNAME, 'pname', $this->targetObject->getVar('pname'), 4, 47), false); |
||
88 | // Id_owner |
||
89 | $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_ID_OWNER, 'id_owner', 50, 255, $this->targetObject->getVar('id_owner')), false); |
||
90 | // Id_breeder |
||
91 | $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_ID_BREEDER, 'id_breeder', 50, 255, $this->targetObject->getVar('id_breeder')), false); |
||
92 | // User |
||
93 | $this->addElement(new \XoopsFormSelectUser(\AM_PEDIGREE_REGISTRY_USER, 'user', false, $this->targetObject->getVar('user'), 1, false), false); |
||
94 | // Roft |
||
95 | $roft = new \XoopsFormSelect(\AM_PEDIGREE_REGISTRY_ROFT, 'roft', $this->targetObject->getVar('roft')); |
||
96 | $optionsArray = Utility::enumerate('pedigree_registry', 'roft'); |
||
97 | if (!\is_array($optionsArray)) { |
||
98 | throw new RuntimeException($optionsArray . ' must be an array.'); |
||
99 | } |
||
100 | foreach ($optionsArray as $enum) { |
||
101 | $roft->addOption($enum, (\defined($enum) ? \constant($enum) : $enum)); |
||
102 | } |
||
103 | $this->addElement($roft, false); |
||
104 | // Mother |
||
105 | require_once XOOPS_ROOT_PATH . '/class/tree.php'; |
||
106 | /** @var \XoopsModules\Pedigree\RegistryHandler $Handler */ |
||
107 | $registryHandler = $this->helper->getHandler('Registry'); |
||
108 | |||
109 | $criteria = new CriteriaCompo(); |
||
110 | $categoryArray = $registryHandler->getObjects($criteria); |
||
111 | if ($categoryArray) { |
||
112 | $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid'); |
||
113 | |||
114 | if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) { |
||
115 | $registryPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', \AM_PEDIGREE_REGISTRY_MOTHER); |
||
116 | $this->addElement($registryPid); |
||
117 | } else { |
||
118 | $registryPid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true); |
||
119 | $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_MOTHER, $registryPid)); |
||
120 | } |
||
121 | } |
||
122 | // Father |
||
123 | require_once XOOPS_ROOT_PATH . '/class/tree.php'; |
||
124 | /** @var \XoopsModules\Pedigree\RegistryHandler $Handler */ |
||
125 | $registryHandler = $this->helper->getHandler('Registry'); |
||
126 | |||
127 | $criteria = new CriteriaCompo(); |
||
128 | $categoryArray = $registryHandler->getObjects($criteria); |
||
129 | if ($categoryArray) { |
||
130 | $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid'); |
||
131 | |||
132 | if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) { |
||
133 | $registryPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', \AM_PEDIGREE_REGISTRY_FATHER); |
||
134 | $this->addElement($registryPid); |
||
135 | } else { |
||
136 | $registryPid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true); |
||
137 | $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_FATHER, $registryPid)); |
||
138 | } |
||
139 | } |
||
140 | // Foto |
||
141 | $foto = $this->targetObject->getVar('foto') ?: 'blank.png'; |
||
142 | |||
143 | $uploadDir = '/uploads/pedigree/images/'; |
||
144 | $imgtray = new \XoopsFormElementTray(\AM_PEDIGREE_REGISTRY_FOTO, '<br>'); |
||
145 | $imgpath = \sprintf(\AM_PEDIGREE_FORMIMAGE_PATH, $uploadDir); |
||
146 | $imageselect = new \XoopsFormSelect($imgpath, 'foto', $foto); |
||
147 | $imageArray = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir); |
||
148 | foreach ($imageArray as $image) { |
||
149 | $imageselect->addOption((string)$image, $image); |
||
150 | } |
||
151 | $imageselect->setExtra("onchange='showImgSelected(\"image_foto\", \"foto\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'"); |
||
152 | $imgtray->addElement($imageselect); |
||
153 | $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $foto . "' name='image_foto' id='image_foto' alt='' >")); |
||
154 | $fileseltray = new \XoopsFormElementTray('', '<br>'); |
||
155 | $fileseltray->addElement(new \XoopsFormFile(\AM_PEDIGREE_FORMUPLOAD, 'foto', \xoops_getModuleOption('maxsize'))); |
||
156 | $fileseltray->addElement(new \XoopsFormLabel('')); |
||
157 | $imgtray->addElement($fileseltray); |
||
158 | $this->addElement($imgtray); |
||
159 | // Coi |
||
160 | $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_COI, 'coi', 50, 255, $this->targetObject->getVar('coi')), false); |
||
161 | |||
162 | $this->addElement(new \XoopsFormHidden('op', 'save')); |
||
163 | $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit')); |
||
164 | } |
||
166 |