Conditions | 14 |
Paths | 832 |
Total Lines | 250 |
Code Lines | 171 |
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 |
||
81 | function editpartner($showmenu = false, $id = 0) |
||
82 | { |
||
83 | global $xoopsDB, $smartPartnerPartnerHandler, $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule; |
||
84 | if (!isset($smartPartnerPartnerHandler)) { |
||
85 | $smartPartnerPartnerHandler = smartpartner_gethandler('partner'); |
||
86 | } |
||
87 | include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
88 | // If there is a parameter, and the id exists, retrieve data: we're editing a partner |
||
89 | if ($id != 0) { |
||
90 | // Creating the partner object |
||
91 | $partnerObj = new SmartpartnerPartner($id); |
||
92 | |||
93 | if ($partnerObj->notLoaded()) { |
||
94 | redirect_header('partner.php', 1, _AM_SPARTNER_NOPARTNERSELECTED); |
||
95 | } |
||
96 | |||
97 | switch ($partnerObj->status()) { |
||
98 | |||
99 | case _SPARTNER_STATUS_SUBMITTED: |
||
100 | $breadcrumb_action1 = _AM_SPARTNER_SUBMITTED_PARTNERS; |
||
101 | $breadcrumb_action2 = _AM_SPARTNER_APPROVING; |
||
102 | $page_title = _AM_SPARTNER_SUBMITTED_TITLE; |
||
103 | $page_info = _AM_SPARTNER_SUBMITTED_INFO; |
||
104 | $button_caption = _AM_SPARTNER_APPROVE; |
||
105 | $new_status = _SPARTNER_STATUS_ACTIVE; |
||
106 | break; |
||
107 | |||
108 | case _SPARTNER_STATUS_ACTIVE: |
||
109 | $breadcrumb_action1 = _AM_SPARTNER_ACTIVE_PARTNERS; |
||
110 | $breadcrumb_action2 = _AM_SPARTNER_EDITING; |
||
111 | $page_title = _AM_SPARTNER_ACTIVE_EDITING; |
||
112 | $page_info = _AM_SPARTNER_ACTIVE_EDITING_INFO; |
||
113 | $button_caption = _AM_SPARTNER_MODIFY; |
||
114 | $new_status = _SPARTNER_STATUS_ACTIVE; |
||
115 | break; |
||
116 | |||
117 | case _SPARTNER_STATUS_INACTIVE: |
||
118 | $breadcrumb_action1 = _AM_SPARTNER_INACTIVE_PARTNERS; |
||
119 | $breadcrumb_action2 = _AM_SPARTNER_EDITING; |
||
120 | $page_title = _AM_SPARTNER_INACTIVE_EDITING; |
||
121 | $page_info = _AM_SPARTNER_INACTIVE_EDITING_INFO; |
||
122 | $button_caption = _AM_SPARTNER_MODIFY; |
||
123 | $new_status = _SPARTNER_STATUS_INACTIVE; |
||
124 | break; |
||
125 | |||
126 | case _SPARTNER_STATUS_REJECTED: |
||
127 | $breadcrumb_action1 = _AM_SPARTNER_REJECTED_PARTNERS; |
||
128 | $breadcrumb_action2 = _AM_SPARTNER_EDITING; |
||
129 | $page_title = _AM_SPARTNER_REJECTED_EDITING; |
||
130 | $page_info = _AM_SPARTNER_REJECTED_EDITING_INFO; |
||
131 | $button_caption = _AM_SPARTNER_MODIFY; |
||
132 | $new_status = _SPARTNER_STATUS_REJECTED; |
||
133 | break; |
||
134 | |||
135 | case 'default': |
||
136 | default: |
||
137 | break; |
||
138 | } |
||
139 | |||
140 | echo "<br>\n"; |
||
141 | smartpartner_collapsableBar('editpartner', 'editpartmericon', $page_title, $page_info); |
||
142 | } else { |
||
143 | // there's no parameter, so we're adding a partner |
||
144 | $partnerObj = $smartPartnerPartnerHandler->create(); |
||
145 | $breadcrumb_action1 = _AM_SPARTNER_PARTNERS; |
||
146 | $breadcrumb_action2 = _AM_SPARTNER_CREATE; |
||
147 | $button_caption = _AM_SPARTNER_CREATE; |
||
148 | $new_status = _SPARTNER_STATUS_ACTIVE; |
||
149 | smartpartner_collapsableBar('addpartner', 'addpartnericon', _AM_SPARTNER_PARTNER_CREATING, _AM_SPARTNER_PARTNER_CREATING_DSC); |
||
150 | } |
||
151 | |||
152 | // PARTNER FORM |
||
153 | $sform = new XoopsThemeForm(_AM_SPARTNER_PARTNERS, 'op', xoops_getenv('PHP_SELF')); |
||
154 | $sform->setExtra('enctype="multipart/form-data"'); |
||
155 | |||
156 | // TITLE |
||
157 | $title_text = new XoopsFormText(_AM_SPARTNER_TITLE, 'title', 50, 255, $partnerObj->title('e')); |
||
158 | $sform->addElement($title_text, true); |
||
159 | |||
160 | // Parent Category |
||
161 | $mytree = new SmartTree($xoopsDB->prefix('smartpartner_categories'), 'categoryid', 'parentid'); |
||
162 | ob_start(); |
||
163 | $mytree->makeMySelBox('name', 'weight', explode('|', $partnerObj->categoryid()), 0, 'categoryid', '', true); |
||
164 | //makeMySelBox($title,$order="",$preset_id=0, $none=0, $sel_name="", $onchange="") |
||
165 | $parent_cat_select = new XoopsFormLabel(_AM_SPARTNER_CATEGORY_BELONG, ob_get_contents()); |
||
166 | $parent_cat_select->setDescription(_AM_SPARTNER_BELONG_CATEGORY_DSC); |
||
167 | $sform->addElement($parent_cat_select); |
||
168 | ob_end_clean(); |
||
169 | |||
170 | // LOGO |
||
171 | $logo_array = XoopsLists:: getImgListAsArray(smartpartner_getImageDir()); |
||
172 | $logo_select = new XoopsFormSelect('', 'image', $partnerObj->image()); |
||
173 | $logo_select->addOption('-1', '---------------'); |
||
174 | $logo_select->addOptionArray($logo_array); |
||
175 | $logo_select->setExtra("onchange='showImgSelected(\"image3\", \"image\", \"" . 'uploads/' . SMARTPARTNER_DIRNAME . '/images' . "\", \"\", \"" . XOOPS_URL . "\")'"); |
||
176 | $logo_tray = new XoopsFormElementTray(_AM_SPARTNER_LOGO, ' '); |
||
177 | $logo_tray->addElement($logo_select); |
||
178 | $logo_tray->addElement(new XoopsFormLabel('', "<br><br><img src='" . smartpartner_getImageDir('', false) . $partnerObj->image() . "' name='image3' id='image3' alt='' />")); |
||
179 | $logo_tray->setDescription(_AM_SPARTNER_LOGO_DSC); |
||
180 | $sform->addElement($logo_tray); |
||
181 | |||
182 | // LOGO UPLOAD |
||
183 | $max_size = 5000000; |
||
184 | $file_box = new XoopsFormFile(_AM_SPARTNER_LOGO_UPLOAD, 'logo_file', $max_size); |
||
185 | $file_box->setExtra("size ='45'"); |
||
186 | $file_box->setDescription(sprintf(_AM_SPARTNER_LOGO_UPLOAD_DSC, $xoopsModuleConfig['img_max_width'], $xoopsModuleConfig['img_max_height'])); |
||
187 | $sform->addElement($file_box); |
||
188 | |||
189 | // IMAGE_URL |
||
190 | $image_url_text = new XoopsFormText(_CO_SPARTNER_IMAGE_URL, 'image_url', 50, 255, $partnerObj->image_url()); |
||
191 | $image_url_text->setDescription(_CO_SPARTNER_IMAGE_URL_DSC); |
||
192 | $sform->addElement($image_url_text, false); |
||
193 | |||
194 | // URL |
||
195 | $url_text = new XoopsFormText(_AM_SPARTNER_URL, 'url', 50, 255, $partnerObj->url()); |
||
196 | $url_text->setDescription(_AM_SPARTNER_URL_DSC); |
||
197 | $sform->addElement($url_text, false); |
||
198 | |||
199 | // SUMMARY |
||
200 | $summary_text = new XoopsFormTextArea(_AM_SPARTNER_SUMMARY, 'summary', $partnerObj->summary(0, 'e'), 7, 60); |
||
201 | $summary_text->setDescription(_AM_SPARTNER_SUMMARY_DSC); |
||
202 | $sform->addElement($summary_text, true); |
||
203 | |||
204 | // SHOW summary on partner page |
||
205 | $showsum_radio = new XoopsFormRadioYN(_AM_SPARTNER_SHOW_SUMMARY, 'showsummary', $partnerObj->getVar('showsummary')); |
||
206 | $showsum_radio->setDescription(_AM_SPARTNER_SHOW_SUMMARY_DSC); |
||
207 | $sform->addElement($showsum_radio); |
||
208 | |||
209 | // DESCRIPTION |
||
210 | $description_text = new XoopsFormDhtmlTextArea(_AM_SPARTNER_DESCRIPTION, 'description', $partnerObj->description(0, 'e'), 15, 60); |
||
211 | $description_text->setDescription(_AM_SPARTNER_DESCRIPTION_DSC); |
||
212 | $sform->addElement($description_text, false); |
||
213 | |||
214 | // CONTACT_NAME |
||
215 | $contact_name_text = new XoopsFormText(_CO_SPARTNER_CONTACT_NAME, 'contact_name', 50, 255, $partnerObj->contact_name('e')); |
||
216 | $contact_name_text->setDescription(_CO_SPARTNER_CONTACT_NAME_DSC); |
||
217 | $sform->addElement($contact_name_text, false); |
||
218 | |||
219 | // CONTACT_EMAIL |
||
220 | $contact_email_text = new XoopsFormText(_CO_SPARTNER_CONTACT_EMAIL, 'contact_email', 50, 255, $partnerObj->contact_email('e')); |
||
221 | $contact_email_text->setDescription(_CO_SPARTNER_CONTACT_EMAIL_DSC); |
||
222 | $sform->addElement($contact_email_text, false); |
||
223 | |||
224 | // EMAIL_PRIV |
||
225 | $email_priv_radio = new XoopsFormRadioYN(_CO_SPARTNER_CONTACT_EMAILPRIV, 'email_priv', $partnerObj->email_priv('e')); |
||
226 | $email_priv_radio->setDescription(_CO_SPARTNER_CONTACT_EMAILPRIV_DSC); |
||
227 | $sform->addElement($email_priv_radio); |
||
228 | |||
229 | // CONTACT_PHONE |
||
230 | $contact_phone_text = new XoopsFormText(_CO_SPARTNER_CONTACT_PHONE, 'contact_phone', 50, 255, $partnerObj->contact_phone('e')); |
||
231 | $contact_phone_text->setDescription(_CO_SPARTNER_CONTACT_PHONE_DSC); |
||
232 | $sform->addElement($contact_phone_text, false); |
||
233 | |||
234 | // PHONE_PRIV |
||
235 | $phone_priv_radio = new XoopsFormRadioYN(_CO_SPARTNER_CONTACT_PHONEPRIV, 'phone_priv', $partnerObj->phone_priv('e')); |
||
236 | $phone_priv_radio->setDescription(_CO_SPARTNER_CONTACT_PHONEPRIV_DSC); |
||
237 | $sform->addElement($phone_priv_radio); |
||
238 | |||
239 | // ADRESS |
||
240 | //$adress_text = new XoopsFormText(_CO_SPARTNER_ADRESS, 'adress', 50, 255, $partnerObj->adress('e')); |
||
241 | $adress_text = new XoopsFormTextArea(_CO_SPARTNER_ADRESS, 'adress', $partnerObj->adress('e')); |
||
242 | $adress_text->setDescription(_CO_SPARTNER_ADRESS_DSC); |
||
243 | $sform->addElement($adress_text, false); |
||
244 | |||
245 | // ADRESS_PRIV |
||
246 | $adress_priv_radio = new XoopsFormRadioYN(_CO_SPARTNER_CONTACT_ADRESSPRIV, 'adress_priv', $partnerObj->adress_priv('e')); |
||
247 | $adress_priv_radio->setDescription(_CO_SPARTNER_CONTACT_ADRESSPRIV_DSC); |
||
248 | $sform->addElement($adress_priv_radio); |
||
249 | |||
250 | // STATUS |
||
251 | $options = $partnerObj->getAvailableStatus(); |
||
252 | $status_select = new XoopsFormSelect(_AM_SPARTNER_STATUS, 'status', $new_status); |
||
253 | $status_select->addOptionArray($options); |
||
254 | $status_select->setDescription(_AM_SPARTNER_STATUS_DSC); |
||
255 | $sform->addElement($status_select); |
||
256 | |||
257 | // WEIGHT |
||
258 | $weight_text = new XoopsFormText(_AM_SPARTNER_WEIGHT, 'weight', 4, 4, $partnerObj->weight()); |
||
259 | $weight_text->setDescription(_AM_SPARTNER_WEIGHT_DSC); |
||
260 | $sform->addElement($weight_text); |
||
261 | |||
262 | //perms |
||
263 | global $smartPermissionsHandler; |
||
264 | include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobjectpermission.php'; |
||
265 | $smartPermissionsHandler = new SmartobjectPermissionHandler($smartPartnerPartnerHandler); |
||
266 | |||
267 | if ($partnerObj->id() != 0) { |
||
268 | $grantedGroups = $smartPermissionsHandler->getGrantedGroups('full_view', $partnerObj->id()); |
||
269 | } else { |
||
270 | $grantedGroups = $xoopsModuleConfig['default_full_view']; |
||
271 | } |
||
272 | $full_view_select = new XoopsFormSelectGroup(_CO_SPARTNER_FULL_PERM_READ, 'full_view', true, $grantedGroups, 5, true); |
||
273 | $full_view_select->setDescription(_CO_SPARTNER_FULL_PERM_READ_DSC); |
||
274 | $sform->addElement($full_view_select); |
||
275 | |||
276 | if ($partnerObj->id() != 0) { |
||
277 | $partGrantedGroups = $smartPermissionsHandler->getGrantedGroups('partial_view', $partnerObj->id()); |
||
278 | } else { |
||
279 | $partGrantedGroups = $xoopsModuleConfig['default_part_view']; |
||
280 | } |
||
281 | $part_view_select = new XoopsFormSelectGroup(_CO_SPARTNER_PART_PERM_READ, 'partial_view', true, $partGrantedGroups, 5, true); |
||
282 | $part_view_select->setDescription(_CO_SPARTNER_PART_PERM_READ_DSC); |
||
283 | $sform->addElement($part_view_select); |
||
284 | |||
285 | // Partner id |
||
286 | $sform->addElement(new XoopsFormHidden('id', $partnerObj->id())); |
||
287 | |||
288 | $button_tray = new XoopsFormElementTray('', ''); |
||
289 | $hidden = new XoopsFormHidden('op', 'addpartner'); |
||
290 | $button_tray->addElement($hidden); |
||
291 | |||
292 | $sform->addElement(new XoopsFormHidden('original_status', $partnerObj->status())); |
||
293 | |||
294 | if (!$id) { |
||
295 | // there's no id? Then it's a new partner |
||
296 | // $button_tray -> addElement( new XoopsFormButton( '', 'mod', _AM_SPARTNER_CREATE, 'submit' ) ); |
||
297 | $butt_create = new XoopsFormButton('', '', _AM_SPARTNER_CREATE, 'submit'); |
||
298 | $butt_create->setExtra('onclick="this.form.elements.op.value=\'addpartner\'"'); |
||
299 | $button_tray->addElement($butt_create); |
||
300 | |||
301 | $butt_clear = new XoopsFormButton('', '', _AM_SPARTNER_CLEAR, 'reset'); |
||
302 | $button_tray->addElement($butt_clear); |
||
303 | |||
304 | $butt_cancel = new XoopsFormButton('', '', _AM_SPARTNER_CANCEL, 'button'); |
||
305 | $butt_cancel->setExtra('onclick="history.go(-1)"'); |
||
306 | $button_tray->addElement($butt_cancel); |
||
307 | } else { |
||
308 | // else, we're editing an existing partner |
||
309 | // $button_tray -> addElement( new XoopsFormButton( '', 'mod', _AM_SPARTNER_MODIFY, 'submit' ) ); |
||
310 | $butt_create = new XoopsFormButton('', '', $button_caption, 'submit'); |
||
311 | $butt_create->setExtra('onclick="this.form.elements.op.value=\'addpartner\'"'); |
||
312 | $button_tray->addElement($butt_create); |
||
313 | |||
314 | $butt_cancel = new XoopsFormButton('', '', _AM_SPARTNER_CANCEL, 'button'); |
||
315 | $butt_cancel->setExtra('onclick="history.go(-1)"'); |
||
316 | $button_tray->addElement($butt_cancel); |
||
317 | } |
||
318 | |||
319 | $sform->addElement($button_tray); |
||
320 | $sform->display(); |
||
321 | unset($hidden); |
||
322 | if (!$id) { |
||
323 | smartpartner_close_collapsable('addpartner', 'addpartnericon'); |
||
324 | } else { |
||
325 | smartpartner_close_collapsable('editpartner', 'editpartnericon'); |
||
326 | } |
||
327 | if ($id != 0) { |
||
328 | showfiles($partnerObj); |
||
329 | } |
||
330 | } |
||
331 | |||
587 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.