| Conditions | 26 |
| Paths | > 20000 |
| Total Lines | 173 |
| 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 |
||
| 67 | public function getForm($action = false, $admin_aerea = false) |
||
| 68 | { |
||
| 69 | global $xoopsDB, $xoopsUser, $pathImageIcon; |
||
| 70 | |||
| 71 | if ($action === false) { |
||
| 72 | $action = $_SERVER["REQUEST_URI"]; |
||
| 73 | } |
||
| 74 | |||
| 75 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_LETTER_ADD) : sprintf(_AM_XNEWSLETTER_LETTER_EDIT); |
||
| 76 | |||
| 77 | include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php"; |
||
| 78 | $form = new XoopsThemeForm($title, "form", $action, "post", true); |
||
| 79 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 80 | |||
| 81 | $form->addElement(new XoopsFormText(_AM_XNEWSLETTER_LETTER_TITLE, "letter_title", 50, 255, $this->getVar("letter_title", 'e')), true); |
||
| 82 | |||
| 83 | $editor_configs = array(); |
||
| 84 | $editor_configs["name"] = "letter_content"; |
||
| 85 | $editor_configs["value"] = $this->getVar("letter_content", "e"); |
||
| 86 | $editor_configs["rows"] = 10; |
||
| 87 | $editor_configs["cols"] = 80; |
||
| 88 | $editor_configs["width"] = "100%"; |
||
| 89 | $editor_configs["height"] = "400px"; |
||
| 90 | $editor_configs["editor"] = $this->xnewsletter->getConfig('xnewsletter_editor'); |
||
| 91 | $letter_content_editor = new XoopsFormEditor(_AM_XNEWSLETTER_LETTER_CONTENT, "letter_content", $editor_configs); |
||
| 92 | $letter_content_editor->setDescription(_AM_XNEWSLETTER_LETTER_CONTENT_DESC); |
||
| 93 | $form->addElement($letter_content_editor, true); |
||
| 94 | |||
| 95 | $letter_template = $this->getVar("letter_template") == "" ? "basic" : $this->getVar("letter_template"); |
||
| 96 | |||
| 97 | $template_select = new XoopsFormSelect(_AM_XNEWSLETTER_LETTER_TEMPLATE, "letter_template", $letter_template); |
||
| 98 | // get template files in /modules/xnewsletter/language/{language}/templates/ directory |
||
| 99 | $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/'; |
||
| 100 | if (!is_dir($template_path)) { |
||
| 101 | $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/'; |
||
| 102 | } |
||
| 103 | $templateFiles = array(); |
||
| 104 | if (!$dirHandler = @opendir($template_path)) die(str_replace("%p", $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH)); |
||
| 105 | while ($filename = readdir($dirHandler)) { |
||
| 106 | if (($filename != ".") and ($filename != "..") and ($filename != "index.html")) { |
||
| 107 | $info = pathinfo($filename); |
||
| 108 | $templateFiles[] = basename($filename, '.' . $info['extension']); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | closedir($dirHandler); |
||
| 112 | foreach ($templateFiles as $templateFile) { |
||
| 113 | $template_select->addOption($templateFile, "file:" . $templateFile); |
||
| 114 | } |
||
| 115 | // get template objects from database |
||
| 116 | $templateCriteria = new CriteriaCompo(); |
||
| 117 | $templateCriteria->setSort("template_title DESC, template_id"); |
||
| 118 | $templateCriteria->setOrder("DESC"); |
||
| 119 | $templateCount = $this->xnewsletter->getHandler('template')->getCount(); |
||
| 120 | if($templateCount > 0) { |
||
| 121 | $templateObjs = $this->xnewsletter->getHandler('template')->getAll($templateCriteria); |
||
| 122 | foreach($templateObjs as $templateObj) { |
||
| 123 | $template_select->addOption("db:" . $templateObj->getVar('template_id'), "db:" . $templateObj->getVar('template_title')); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | $form->addElement($template_select, false); |
||
| 127 | |||
| 128 | $gperm_handler = xoops_gethandler('groupperm'); |
||
| 129 | $member_handler = xoops_gethandler('member'); |
||
| 130 | $my_group_ids = $member_handler->getGroupsByUser($xoopsUser->uid()); |
||
| 131 | |||
| 132 | $catCriteria = new CriteriaCompo(); |
||
| 133 | $catCriteria->setSort('cat_id'); |
||
| 134 | $catCriteria->setOrder('ASC'); |
||
| 135 | $letter_cats = explode("|", $this->getVar("letter_cats")); |
||
| 136 | $cat_select = new XoopsFormCheckBox(_AM_XNEWSLETTER_LETTER_CATS, "letter_cats", $letter_cats); |
||
| 137 | $catObjs = $this->xnewsletter->getHandler('cat')->getAll($catCriteria); |
||
| 138 | foreach ($catObjs as $cat_id => $catObj) { |
||
| 139 | $cat_name = $catObj->getVar("cat_name"); |
||
| 140 | $show = $gperm_handler->checkRight('newsletter_create_cat', $cat_id, $my_group_ids, $this->xnewsletter->getModule()->mid()); |
||
| 141 | if ($show == 1) { |
||
| 142 | $cat_select->addOption($cat_id, $cat_name); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $form->addElement($cat_select, true); |
||
| 146 | |||
| 147 | $attachment_tray = new XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ATTACHMENT, '<br>'); |
||
| 148 | if ($this->isNew()) { |
||
| 149 | $attachmentObjs = array(); |
||
| 150 | } else { |
||
| 151 | $attachmentCriteria = new CriteriaCompo(); |
||
| 152 | $attachmentCriteria->add(new Criteria('attachment_letter_id', $this->getVar("letter_id"))); |
||
| 153 | $attachmentCriteria->setSort("attachment_id"); |
||
| 154 | $attachmentCriteria->setOrder("ASC"); |
||
| 155 | $attachmentObjs = $this->xnewsletter->getHandler('attachment')->getAll($attachmentCriteria); |
||
| 156 | } |
||
| 157 | $i = 1; |
||
| 158 | $remove_attachment_tray = array(); |
||
| 159 | foreach ($attachmentObjs as $attachment_id => $attachmentObj) { |
||
| 160 | $remove_attachment_tray[$attachment_id] = new XoopsFormElementTray("", ' '); |
||
| 161 | $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormLabel("", $attachmentObj->getVar("attachment_name"))); |
||
| 162 | $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormButton("", "delete_attachment_{$i}", _DELETE, "submit")); |
||
| 163 | $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormHidden("attachment_{$i}", $attachment_id)); |
||
| 164 | $attachment_tray->addElement($remove_attachment_tray[$attachment_id]); |
||
| 165 | ++$i; |
||
| 166 | } |
||
| 167 | //$add_att_tray = array(); |
||
| 168 | for ($j = $i; $j < 6; ++$j) { |
||
| 169 | $attachment_tray->addElement(new XoopsFormFile("", "letter_attachment_{$j}", $this->xnewsletter->getConfig('xn_maxsize'))); |
||
| 170 | } |
||
| 171 | $form->addElement($attachment_tray); |
||
| 172 | |||
| 173 | $opt_nextaction = new XoopsFormRadio("", "letter_action", "0", "<br />"); |
||
| 174 | $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_NO, _AM_XNEWSLETTER_LETTER_ACTION_NO); |
||
| 175 | $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW, _AM_XNEWSLETTER_LETTER_ACTION_PREVIEW); |
||
| 176 | $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_SEND, _AM_XNEWSLETTER_LETTER_ACTION_SEND); |
||
| 177 | $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST, _AM_XNEWSLETTER_LETTER_ACTION_SENDTEST); |
||
| 178 | $opt_tray = new XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ACTION, '<br/>'); |
||
| 179 | $opt_tray->addElement($opt_nextaction); |
||
| 180 | |||
| 181 | $letter_email_test = $this->isNew() ? $xoopsUser->email() : $this->getVar("letter_email_test"); |
||
| 182 | if ($letter_email_test == '') { |
||
| 183 | $letter_email_test = $xoopsUser->email(); |
||
| 184 | } |
||
| 185 | $opt_tray->addElement(new XoopsFormText(_AM_XNEWSLETTER_LETTER_EMAIL_TEST . ": ", "letter_email_test", 50, 255, $letter_email_test), false); |
||
| 186 | $form->addElement($opt_tray); |
||
| 187 | |||
| 188 | $accountsCriteria = new CriteriaCompo(); |
||
| 189 | $accountsCriteria->setSort("accounts_id"); |
||
| 190 | $accountsCriteria->setOrder("ASC"); |
||
| 191 | $numrows_accounts = $this->xnewsletter->getHandler('accounts')->getCount($accountsCriteria); |
||
| 192 | $account_default = 0; |
||
| 193 | if ($this->isNew()) { |
||
| 194 | $accountsObjs = $this->xnewsletter->getHandler('accounts')->getAll($accountsCriteria); |
||
| 195 | foreach ($accountsObjs as $accountsObj) { |
||
| 196 | if ($accountsObj->getVar("accounts_default") == 1) { |
||
| 197 | $account_default = $accountsObj->getVar("accounts_id"); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $account_default = $this->getVar("letter_account"); |
||
| 202 | } |
||
| 203 | if ($numrows_accounts == 1) { |
||
| 204 | $form->addElement(new XoopsFormHidden("letter_account", $account_default)); |
||
| 205 | } else { |
||
| 206 | $accounts_list = $this->xnewsletter->getHandler('accounts')->getList($accountsCriteria); |
||
| 207 | |||
| 208 | if ($admin_aerea == true) { |
||
| 209 | $opt_accounts = new XoopsFormRadio(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, "letter_account", $account_default); |
||
| 210 | $opt_accounts->addOptionArray($accounts_list); |
||
| 211 | $form->addElement($opt_accounts, false); |
||
| 212 | } else { |
||
| 213 | $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, $accounts_list[$account_default])); |
||
| 214 | $form->addElement(new XoopsFormHidden("letter_account", $account_default)); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($this->isNew()) { |
||
| 219 | $time = time(); |
||
| 220 | $submitter_uid = $GLOBALS['xoopsUser']->uid(); |
||
| 221 | $submitter_name = $GLOBALS['xoopsUser']->uname(); |
||
| 222 | } else { |
||
| 223 | $time = $this->getVar("letter_created"); |
||
| 224 | $submitter_uid = $this->getVar("letter_submitter"); |
||
| 225 | xoops_load("xoopsuserutility"); |
||
| 226 | $submitter_name = XoopsUserUtility::getUnameFromId($submitter_uid); |
||
| 227 | } |
||
| 228 | |||
| 229 | $form->addElement(new XoopsFormHidden("letter_submitter", $submitter_uid)); |
||
| 230 | $form->addElement(new XoopsFormHidden("letter_created", $time)); |
||
| 231 | |||
| 232 | $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_SUBMITTER, $submitter_name)); |
||
| 233 | $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_CREATED, formatTimestamp($time, 's'))); |
||
| 234 | |||
| 235 | $form->addElement(new XoopsFormHidden("op", "save_letter")); |
||
| 236 | $form->addElement(new XoopsFormButton("", "submit", _AM_XNEWSLETTER_SAVE, "submit")); |
||
| 237 | |||
| 238 | return $form; |
||
| 239 | } |
||
| 240 | } |
||
| 262 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.