| Conditions | 18 |
| Paths | 18432 |
| Total Lines | 183 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 69 | public function getForm($action = false, $admin_aerea = false) |
||
| 70 | { |
||
| 71 | global $xoopsDB, $xoopsUser, $pathImageIcon; |
||
| 72 | |||
| 73 | if (false === $action) { |
||
| 74 | $action = $_SERVER['REQUEST_URI']; |
||
| 75 | } |
||
| 76 | |||
| 77 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_LETTER_ADD) : sprintf(_AM_XNEWSLETTER_LETTER_EDIT); |
||
| 78 | |||
| 79 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 80 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 81 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 82 | |||
| 83 | // letter_title |
||
| 84 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_TITLE, 'letter_title', 50, 255, $this->getVar('letter_title', 'e')), true); |
||
| 85 | |||
| 86 | // letter_content |
||
| 87 | $editor_configs = []; |
||
| 88 | $editor_configs['name'] = 'letter_content'; |
||
| 89 | $editor_configs['value'] = $this->getVar('letter_content', 'e'); |
||
| 90 | $editor_configs['rows'] = 40; |
||
| 91 | $editor_configs['cols'] = 80; |
||
| 92 | $editor_configs['width'] = '100%'; |
||
| 93 | $editor_configs['height'] = '800px'; |
||
| 94 | $editor_configs['editor'] = $this->helper->getConfig('xnewsletter_editor'); |
||
| 95 | $letter_content_editor = new \XoopsFormEditor(_AM_XNEWSLETTER_LETTER_CONTENT, 'letter_content', $editor_configs); |
||
| 96 | $letter_content_editor->setDescription(_AM_XNEWSLETTER_LETTER_CONTENT_DESC); |
||
| 97 | $form->addElement($letter_content_editor, true); |
||
| 98 | |||
| 99 | // letter_template |
||
| 100 | $letterTemplateid = $this->isNew() ? 1 : $this->getVar('letter_templateid'); |
||
| 101 | $template_select = new \XoopsFormSelect(_AM_XNEWSLETTER_LETTER_TEMPLATE, 'letter_templateid', $letterTemplateid); |
||
| 102 | // get template objects from database |
||
| 103 | $templateCriteria = new \CriteriaCompo(); |
||
| 104 | $templateCriteria->add(new \Criteria('template_online', 1)); |
||
| 105 | $templateCriteria->setSort('template_title ASC, template_id'); |
||
| 106 | $templateCriteria->setOrder('DESC'); |
||
| 107 | $templateCount = $this->helper->getHandler('Template')->getCount($templateCriteria); |
||
| 108 | if ($templateCount > 0) { |
||
| 109 | $template_select->addOptionArray($this->helper->getHandler('Template')->getList($templateCriteria)); |
||
| 110 | } else { |
||
| 111 | redirect_header('letter.php?op=list', 3, _MA_XNEWSLETTER_NOTEMPLATE_ONLINE); |
||
| 112 | } |
||
| 113 | $form->addElement($template_select, false); |
||
| 114 | |||
| 115 | // letter_cats |
||
| 116 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 117 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
| 118 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 119 | $memberHandler = xoops_getHandler('member'); |
||
| 120 | $groups = $memberHandler->getGroupsByUser($xoopsUser->uid()); |
||
| 121 | $catCriteria = new \CriteriaCompo(); |
||
| 122 | $catCriteria->setSort('cat_id'); |
||
| 123 | $catCriteria->setOrder('ASC'); |
||
| 124 | $letter_cats = explode('|', $this->getVar('letter_cats')); |
||
| 125 | $cat_select = new \XoopsFormCheckBox(_AM_XNEWSLETTER_LETTER_CATS, 'letter_cats', $letter_cats); |
||
| 126 | $catObjs = $this->helper->getHandler('Cat')->getAll($catCriteria); |
||
| 127 | foreach ($catObjs as $cat_id => $catObj) { |
||
| 128 | $cat_name = $catObj->getVar('cat_name'); |
||
| 129 | $show = $grouppermHandler->checkRight('newsletter_create_cat', $cat_id, $groups, $this->helper->getModule()->mid()); |
||
| 130 | if (1 == $show) { |
||
| 131 | $cat_select->addOption($cat_id, $cat_name); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | $form->addElement($cat_select, true); |
||
| 135 | |||
| 136 | // attachments |
||
| 137 | $attachment_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ATTACHMENT, '<br>'); |
||
| 138 | $attachment_tray->addElement(new \XoopsFormHidden('deleted_attachment_id', '')); |
||
| 139 | // existing_attachments |
||
| 140 | if ($this->isNew()) { |
||
| 141 | $attachmentObjs = []; |
||
| 142 | } else { |
||
| 143 | $attachmentCriteria = new \CriteriaCompo(); |
||
| 144 | $attachmentCriteria->add(new \Criteria('attachment_letter_id', $this->getVar('letter_id'))); |
||
| 145 | $attachmentCriteria->setSort('attachment_id'); |
||
| 146 | $attachmentCriteria->setOrder('ASC'); |
||
| 147 | $attachmentObjs = $this->helper->getHandler('Attachment')->getAll($attachmentCriteria); |
||
| 148 | } |
||
| 149 | $i = 1; |
||
| 150 | $remove_attachment_tray = []; |
||
|
|
|||
| 151 | foreach ($attachmentObjs as $attachment_id => $attachmentObj) { |
||
| 152 | $delete_attachment_tray = new \XoopsFormElementTray('', ' '); |
||
| 153 | $mode_select = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "existing_attachments_mode[{$attachment_id}]", $attachmentObj->getVar('attachment_mode'), ' '); |
||
| 154 | $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT); |
||
| 155 | $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK); |
||
| 156 | //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features |
||
| 157 | $delete_attachment_tray->addElement($mode_select); |
||
| 158 | $delete_attachment_tray->addElement(new \XoopsFormLabel('', $attachmentObj->getVar('attachment_name'))); |
||
| 159 | $delete_button = new \XoopsFormButton('', "delete_attachment_{$i}", _DELETE, 'submit'); |
||
| 160 | $delete_button->setExtra("onclick='this.form.elements.op.value=\"delete_attachment\";this.form.elements.deleted_attachment_id.value=\"{$attachment_id}\";'"); |
||
| 161 | $delete_attachment_tray->addElement($delete_button); |
||
| 162 | $attachment_tray->addElement($delete_attachment_tray); |
||
| 163 | ++$i; |
||
| 164 | unset($mode_select); |
||
| 165 | unset($delete_attachment_tray); |
||
| 166 | } |
||
| 167 | // new_attachments |
||
| 168 | for ($j = $i; $j < ($this->helper->getConfig('xn_maxattachments') + 1); ++$j) { |
||
| 169 | $add_attachment_tray = new \XoopsFormElementTray('', ' '); |
||
| 170 | $mode_select = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "new_attachments_mode[{$j}]", _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, ' '); |
||
| 171 | $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT); |
||
| 172 | $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK); |
||
| 173 | //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features |
||
| 174 | $add_attachment_tray->addElement($mode_select); |
||
| 175 | $add_attachment_tray->addElement(new \XoopsFormFile('', "new_attachment_index={$j}", $this->helper->getConfig('xn_maxsize'))); |
||
| 176 | $attachment_tray->addElement($add_attachment_tray); |
||
| 177 | unset($mode_select); |
||
| 178 | unset($add_attachment_tray); |
||
| 179 | } |
||
| 180 | $form->addElement($attachment_tray); |
||
| 181 | |||
| 182 | // letter_action |
||
| 183 | $opt_nextaction = new \XoopsFormRadio('', 'letter_action', _AM_XNEWSLETTER_LETTER_ACTION_NO, '<br>'); |
||
| 184 | $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_NO, _AM_XNEWSLETTER_LETTER_ACTION_NO); |
||
| 185 | $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW, _AM_XNEWSLETTER_LETTER_ACTION_PREVIEW); |
||
| 186 | $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SEND, _AM_XNEWSLETTER_LETTER_ACTION_SEND); |
||
| 187 | $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST, _AM_XNEWSLETTER_LETTER_ACTION_SENDTEST); |
||
| 188 | $opt_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ACTION, '<br>'); |
||
| 189 | $opt_tray->addElement($opt_nextaction); |
||
| 190 | |||
| 191 | // letter_email_test |
||
| 192 | $letter_email_test = $this->isNew() ? $xoopsUser->email() : $this->getVar('letter_email_test'); |
||
| 193 | if ('' == $letter_email_test) { |
||
| 194 | $letter_email_test = $xoopsUser->email(); |
||
| 195 | } |
||
| 196 | $opt_tray->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_EMAIL_TEST . ': ', 'letter_email_test', 50, 255, $letter_email_test), false); |
||
| 197 | $form->addElement($opt_tray); |
||
| 198 | |||
| 199 | // letter_account |
||
| 200 | $accountsCriteria = new \CriteriaCompo(); |
||
| 201 | $accountsCriteria->setSort('accounts_id'); |
||
| 202 | $accountsCriteria->setOrder('ASC'); |
||
| 203 | $accountsCount = $this->helper->getHandler('Accounts')->getCount($accountsCriteria); |
||
| 204 | $account_default = 0; |
||
| 205 | if ($this->isNew()) { |
||
| 206 | $accountsObjs = $this->helper->getHandler('Accounts')->getAll($accountsCriteria); |
||
| 207 | foreach ($accountsObjs as $accountsObj) { |
||
| 208 | if (1 == $accountsObj->getVar('accounts_default')) { |
||
| 209 | $account_default = $accountsObj->getVar('accounts_id'); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } else { |
||
| 213 | $account_default = $this->getVar('letter_account'); |
||
| 214 | } |
||
| 215 | if (1 == $accountsCount) { |
||
| 216 | $form->addElement(new \XoopsFormHidden('letter_account', $account_default)); |
||
| 217 | } else { |
||
| 218 | $accounts_list = $this->helper->getHandler('Accounts')->getList($accountsCriteria); |
||
| 219 | |||
| 220 | if (true === $admin_aerea) { |
||
| 221 | $opt_accounts = new \XoopsFormRadio(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, 'letter_account', $account_default); |
||
| 222 | $opt_accounts->addOptionArray($accounts_list); |
||
| 223 | $form->addElement($opt_accounts, false); |
||
| 224 | } else { |
||
| 225 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, $accounts_list[$account_default])); |
||
| 226 | $form->addElement(new \XoopsFormHidden('letter_account', $account_default)); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($this->isNew()) { |
||
| 231 | $time = time(); |
||
| 232 | $submitter_uid = $GLOBALS['xoopsUser']->uid(); |
||
| 233 | $submitter_name = $GLOBALS['xoopsUser']->uname(); |
||
| 234 | } else { |
||
| 235 | $time = $this->getVar('letter_created'); |
||
| 236 | $submitter_uid = $this->getVar('letter_submitter'); |
||
| 237 | xoops_load('xoopsuserutility'); |
||
| 238 | $submitter_name = \XoopsUserUtility::getUnameFromId($submitter_uid); |
||
| 239 | } |
||
| 240 | |||
| 241 | $form->addElement(new \XoopsFormHidden('letter_submitter', $submitter_uid)); |
||
| 242 | $form->addElement(new \XoopsFormHidden('letter_created', $time)); |
||
| 243 | |||
| 244 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $submitter_name)); |
||
| 245 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's'))); |
||
| 246 | |||
| 247 | $form->addElement(new \XoopsFormHidden('op', 'save_letter')); |
||
| 248 | $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); |
||
| 249 | |||
| 250 | return $form; |
||
| 251 | } |
||
| 252 | |||
| 283 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.