| Conditions | 11 |
| Paths | 74 |
| Total Lines | 100 |
| Code Lines | 65 |
| 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_TREE_ADD) : \sprintf(AM_PEDIGREE_TREE_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_TREE_ID, $this->targetObject->getVar('id'), 'id')); |
||
| 86 | // Pname |
||
| 87 | $this->addElement(new \XoopsFormTextArea(AM_PEDIGREE_TREE_PNAME, 'pname', $this->targetObject->getVar('pname'), 4, 47), false); |
||
| 88 | // Id_owner |
||
| 89 | $this->addElement(new \XoopsFormText(AM_PEDIGREE_TREE_ID_OWNER, 'id_owner', 50, 255, $this->targetObject->getVar('id_owner')), false); |
||
| 90 | // Id_breeder |
||
| 91 | $this->addElement(new \XoopsFormText(AM_PEDIGREE_TREE_ID_BREEDER, 'id_breeder', 50, 255, $this->targetObject->getVar('id_breeder')), false); |
||
| 92 | // User |
||
| 93 | $this->addElement(new \XoopsFormSelectUser(AM_PEDIGREE_TREE_USER, 'user', false, $this->targetObject->getVar('user'), 1, false), false); |
||
| 94 | // Roft |
||
| 95 | $roft = new \XoopsFormSelect(AM_PEDIGREE_TREE_ROFT, 'roft', $this->targetObject->getVar('roft')); |
||
| 96 | $optionsArray = Utility::enumerate('pedigree_tree', '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 | //$treeHandler = xoops_getModuleHandler('tree', 'pedigree' ); |
||
| 107 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 108 | /** @var \XoopsPersistableObjectHandler $Handler */ |
||
| 109 | $treeHandler = new Pedigree\TreeHandler($db); |
||
| 110 | |||
| 111 | $criteria = new CriteriaCompo(); |
||
| 112 | $categoryArray = $treeHandler->getObjects($criteria); |
||
| 113 | if ($categoryArray) { |
||
| 114 | $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid'); |
||
| 115 | |||
| 116 | if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) { |
||
| 117 | $treePid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', AM_PEDIGREE_TREE_MOTHER); |
||
| 118 | $this->addElement($treePid); |
||
| 119 | } else { |
||
| 120 | $treePid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true); |
||
| 121 | $this->addElement(new \XoopsFormLabel(AM_PEDIGREE_TREE_MOTHER, $treePid)); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | // Father |
||
| 125 | require_once XOOPS_ROOT_PATH . '/class/tree.php'; |
||
| 126 | //$treeHandler = xoops_getModuleHandler('tree', 'pedigree' ); |
||
| 127 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 128 | /** @var \XoopsPersistableObjectHandler $Handler */ |
||
| 129 | $treeHandler = new Pedigree\TreeHandler($db); |
||
| 130 | |||
| 131 | $criteria = new CriteriaCompo(); |
||
| 132 | $categoryArray = $treeHandler->getObjects($criteria); |
||
| 133 | if ($categoryArray) { |
||
| 134 | $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid'); |
||
| 135 | |||
| 136 | if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) { |
||
| 137 | $treePid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', AM_PEDIGREE_TREE_FATHER); |
||
| 138 | $this->addElement($treePid); |
||
| 139 | } else { |
||
| 140 | $treePid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true); |
||
| 141 | $this->addElement(new \XoopsFormLabel(AM_PEDIGREE_TREE_FATHER, $treePid)); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | // Foto |
||
| 145 | $foto = $this->targetObject->getVar('foto') ?: 'blank.png'; |
||
| 146 | |||
| 147 | $uploadDir = '/uploads/pedigree/images/'; |
||
| 148 | $imgtray = new \XoopsFormElementTray(AM_PEDIGREE_TREE_FOTO, '<br>'); |
||
| 149 | $imgpath = \sprintf(\AM_PEDIGREE_FORMIMAGE_PATH, $uploadDir); |
||
| 150 | $imageselect = new \XoopsFormSelect($imgpath, 'foto', $foto); |
||
| 151 | $imageArray = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir); |
||
| 152 | foreach ($imageArray as $image) { |
||
| 153 | $imageselect->addOption((string)$image, $image); |
||
| 154 | } |
||
| 155 | $imageselect->setExtra("onchange='showImgSelected(\"image_foto\", \"foto\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'"); |
||
| 156 | $imgtray->addElement($imageselect); |
||
| 157 | $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $foto . "' name='image_foto' id='image_foto' alt='' >")); |
||
| 158 | $fileseltray = new \XoopsFormElementTray('', '<br>'); |
||
| 159 | $fileseltray->addElement(new \XoopsFormFile(\AM_PEDIGREE_FORMUPLOAD, 'foto', \xoops_getModuleOption('maxsize'))); |
||
| 160 | $fileseltray->addElement(new \XoopsFormLabel('')); |
||
| 161 | $imgtray->addElement($fileseltray); |
||
| 162 | $this->addElement($imgtray); |
||
| 163 | // Coi |
||
| 164 | $this->addElement(new \XoopsFormText(AM_PEDIGREE_TREE_COI, 'coi', 50, 255, $this->targetObject->getVar('coi')), false); |
||
| 165 | |||
| 166 | $this->addElement(new \XoopsFormHidden('op', 'save')); |
||
| 167 | $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit')); |
||
| 168 | } |
||
| 170 |