Conditions | 29 |
Paths | 4608 |
Total Lines | 324 |
Code Lines | 224 |
Lines | 35 |
Ratio | 10.8 % |
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 |
||
51 | function manageFields() |
||
52 | { |
||
53 | global $imagearray; |
||
54 | |||
55 | $session =& Session::singleton(); |
||
56 | $regex_array =& _getRegexArray(); |
||
57 | $hFields =& xhelpGetHandler('ticketField'); |
||
58 | |||
59 | $start = $limit = 0; |
||
60 | |||
61 | if (isset($_GET['limit'])) { |
||
62 | $limit = intval($_GET['limit']); |
||
63 | } |
||
64 | |||
65 | if (isset($_GET['start'])) { |
||
66 | $start = intval($_GET['start']); |
||
67 | } |
||
68 | |||
69 | if (!$limit) { |
||
70 | $limit = 15; |
||
71 | } |
||
72 | |||
73 | if (!isset($_POST['addField'])) { |
||
74 | $crit = new Criteria('',''); |
||
75 | $crit->setLimit($limit); |
||
76 | $crit->setStart($start); |
||
77 | $crit->setSort('weight'); |
||
78 | $crit->setOrder('ASC'); |
||
79 | |||
80 | $count = $hFields->getCount($crit); |
||
81 | $fields =& $hFields->getObjects($crit); |
||
82 | |||
83 | //Display List of Current Fields, form for new field |
||
84 | xoops_cp_header(); |
||
85 | //echo $oAdminButton->renderButtons('manfields'); |
||
86 | $indexAdmin = new ModuleAdmin(); |
||
87 | echo $indexAdmin->addNavigation('fields.php'); |
||
88 | |||
89 | if ($count) { |
||
90 | $nav = new XoopsPageNav($count, $limit, $start, 'start', "op=manageFields&limit=$limit"); |
||
91 | |||
92 | echo "<table width='100%' cellspacing='1' class='outer'> |
||
93 | <tr><th colspan='7'><label>"._AM_XHELP_TEXT_MANAGE_FIELDS."</label></th></tr>"; |
||
94 | echo "<tr class='head'> |
||
95 | <td>"._AM_XHELP_TEXT_ID."</td> |
||
96 | <td>"._AM_XHELP_TEXT_NAME."</td> |
||
97 | <td>"._AM_XHELP_TEXT_DESCRIPTION."</td> |
||
98 | <td>"._AM_XHELP_TEXT_FIELDNAME."</td> |
||
99 | <td>"._AM_XHELP_TEXT_CONTROLTYPE."</td> |
||
100 | <td>"._AM_XHELP_TEXT_REQUIRED."</td> |
||
101 | <td>"._AM_XHELP_TEXT_ACTIONS."</td> |
||
102 | </tr>"; |
||
103 | |||
104 | $req_link_params = array('op' => 'setFieldRequired', |
||
105 | 'setrequired' => 1, |
||
106 | 'id' => 0); |
||
107 | |||
108 | foreach ($fields as $field) { |
||
109 | $req_link_params['id'] = $field->getVar('id'); |
||
110 | |||
111 | if ($field->getVar('required')) { |
||
112 | $req_link_params['setrequired'] = 0; |
||
113 | $req_img = $imagearray['online']; |
||
114 | $req_title = _AM_XHELP_MESSAGE_DEACTIVATE; |
||
115 | } else { |
||
116 | $req_link_params['setrequired'] = 1; |
||
117 | $req_img = $imagearray['offline']; |
||
118 | $req_title = _AM_XHELP_MESSAGE_ACTIVATE; |
||
119 | } |
||
120 | |||
121 | $edit_url = xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', |
||
122 | array('op' => 'editfield', 'id' => $field->getVar('id'))); |
||
123 | $del_url = xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', |
||
124 | array('op' => 'delfield', 'id' => $field->getVar('id'))); |
||
125 | |||
126 | echo "<tr class='even'><td>".$field->getVar('id')."</td> |
||
127 | <td>".$field->getVar('name')."</td> |
||
128 | <td>".$field->getVar('description')."</td> |
||
129 | <td>".$field->getVar('fieldname')."</td> |
||
130 | <td>".xhelpGetControlLabel($field->getVar('controltype'))."</td> |
||
131 | <td><a href='".xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', $req_link_params)."' title='$req_title'>$req_img</a></td> |
||
132 | <td><a href='$edit_url'>{$imagearray['editimg']}</a> |
||
133 | <a href='$del_url'>{$imagearray['deleteimg']}</a></td> |
||
134 | </tr>"; |
||
135 | |||
136 | } |
||
137 | echo '</table>'; |
||
138 | //Render Page Nav |
||
139 | echo "<div id='pagenav'>". $nav->renderNav(). "</div><br />"; |
||
140 | } |
||
141 | |||
142 | //Get Custom Field From session (if exists) |
||
143 | $field_info = $session->get('xhelp_addField'); |
||
144 | $field_errors = $session->get('xhelp_addFieldErrors'); |
||
145 | |||
146 | $hDepts =& xhelpGetHandler('department'); |
||
147 | $depts =& $hDepts->getObjects(); |
||
148 | $deptarr = array(); |
||
149 | |||
150 | foreach($depts as $obj) { |
||
151 | $deptarr[$obj->getVar('id')] = $obj->getVar('department'); |
||
152 | } |
||
153 | |||
154 | if (! $field_info === false) { |
||
155 | //extract($field_info , EXTR_PREFIX_ALL , 'fld_'); |
||
156 | $fld_controltype = $field_info['controltype']; |
||
157 | $fld_datatype = $field_info['datatype']; |
||
158 | $fld_departments = $field_info['departments']; |
||
159 | $fld_name = $field_info['name']; |
||
160 | $fld_fieldname = $field_info['fieldname']; |
||
161 | $fld_description = $field_info['description']; |
||
162 | $fld_required = $field_info['required']; |
||
163 | $fld_length = $field_info['length']; |
||
164 | $fld_weight = $field_info['weight']; |
||
165 | $fld_defaultvalue = $field_info['defaultvalue']; |
||
166 | $fld_values = $field_info['values']; |
||
167 | $fld_validation = $field_info['validation']; |
||
168 | } else { |
||
169 | $fld_controltype = ''; |
||
170 | $fld_datatype = ''; |
||
171 | $fld_departments = array_keys($deptarr); |
||
172 | $fld_name = ''; |
||
173 | $fld_fieldname = ''; |
||
174 | $fld_description = ''; |
||
175 | $fld_required = ''; |
||
176 | $fld_length = ''; |
||
177 | $fld_weight = ''; |
||
178 | $fld_defaultvalue = ''; |
||
179 | $fld_values = ''; |
||
180 | $fld_validation = ''; |
||
181 | } |
||
182 | |||
183 | View Code Duplication | if (! $field_errors === false) { |
|
184 | xhelpRenderErrors($field_errors, xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'clearAddSession'))); |
||
185 | } |
||
186 | |||
187 | //Add Field Form |
||
188 | $controls = xhelpGetControlArray(); |
||
189 | $control_select = new XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype); |
||
190 | foreach($controls as $key=>$control) { |
||
191 | $control_select->addOption($key, $control['label']); |
||
192 | } |
||
193 | |||
194 | $datatypes = array( |
||
195 | _XHELP_DATATYPE_TEXT => _XHELP_DATATYPE_TEXT, |
||
196 | _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT, |
||
197 | _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC); |
||
198 | |||
199 | $datatype_select = new XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype); |
||
200 | $datatype_select->addOptionArray($datatypes); |
||
201 | |||
202 | $dept_select = new XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true); |
||
203 | foreach($depts as $obj) { |
||
204 | $dept_select->addOptionArray($deptarr); |
||
205 | } |
||
206 | unset($depts); |
||
207 | |||
208 | $form = new xhelpForm(_AM_XHELP_ADD_FIELD, 'add_field', xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'managefields'))); |
||
209 | $nameEle = new XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name); |
||
210 | $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC); |
||
211 | $form->addElement($nameEle); |
||
212 | |||
213 | $fieldnameEle = new XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname); |
||
214 | $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC); |
||
215 | $form->addElement($fieldnameEle); |
||
216 | |||
217 | $descriptionEle = new XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60); |
||
218 | $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC); |
||
219 | $form->addElement($descriptionEle); |
||
220 | |||
221 | $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC); |
||
222 | $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC); |
||
223 | $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC); |
||
224 | |||
225 | $form->addElement($dept_select); |
||
226 | $form->addElement($control_select); |
||
227 | $form->addElement($datatype_select); |
||
228 | |||
229 | $required = new XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required); |
||
230 | $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC); |
||
231 | $form->addElement($required); |
||
232 | |||
233 | $lengthEle = new XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length); |
||
234 | $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC); |
||
235 | $weightEle = new XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight); |
||
236 | $weightEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC); |
||
237 | |||
238 | $form->addElement($lengthEle); |
||
239 | $form->addElement($weightEle); |
||
240 | |||
241 | $regex_control = new xhelpFormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation); |
||
242 | $regex_control->addOptionArray($regex_array); |
||
243 | $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC); |
||
244 | |||
245 | $form->addElement($regex_control); |
||
246 | |||
247 | $defaultValueEle = new XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue); |
||
248 | $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC); |
||
249 | $form->addElement($defaultValueEle); |
||
250 | $values = new XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60); |
||
251 | $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC); |
||
252 | $form->addElement($values); |
||
253 | |||
254 | $btn_tray = new XoopsFormElementTray(''); |
||
255 | $btn_tray->addElement(new XoopsFormButton('', 'addField', _AM_XHELP_BUTTON_SUBMIT, 'submit')); |
||
256 | |||
257 | $form->addElement($btn_tray); |
||
258 | echo $form->render(); |
||
259 | |||
260 | include_once "admin_footer.php"; |
||
261 | |||
262 | } else { |
||
263 | //Validate Field Information |
||
264 | $has_errors = false; |
||
265 | $hField =& xhelpGetHandler('ticketField'); |
||
266 | |||
267 | $values =& _parseValues($_POST['fld_values']); |
||
268 | |||
269 | View Code Duplication | if (!$control = xhelpGetControl($_POST['fld_controltype'])) { |
|
270 | $has_errors = true; |
||
271 | $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE; |
||
272 | } |
||
273 | |||
274 | $fld_needslength = $control['needs_length']; |
||
275 | $fld_needsvalues = $control['needs_values']; |
||
276 | |||
277 | //name field filled? |
||
278 | View Code Duplication | if (trim($_POST['fld_name']) == '') { |
|
279 | $has_errors = true; |
||
280 | $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME; |
||
281 | } |
||
282 | |||
283 | $fld_fieldname = sanitizeFieldName($_POST['fld_fieldname']); |
||
284 | |||
285 | //fieldname filled |
||
286 | View Code Duplication | if (trim($fld_fieldname) == '') { |
|
287 | $has_errors = true; |
||
288 | $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME; |
||
289 | } |
||
290 | |||
291 | //fieldname unique? |
||
292 | $crit = new CriteriaCompo(new Criteria('fieldname', $fld_fieldname)); |
||
293 | if ($hField->getCount($crit)) { |
||
294 | $has_errors = true; |
||
295 | $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE; |
||
296 | } |
||
297 | |||
298 | //Length filled |
||
299 | if (intval($_POST['fld_length']) == 0 && $fld_needslength == true) { |
||
300 | $has_errors = true; |
||
301 | $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, 2, 16777215); |
||
302 | } |
||
303 | |||
304 | //Departments Chosen? |
||
305 | |||
306 | //default value in value set? |
||
307 | View Code Duplication | if (count($values)) { |
|
308 | if (!in_array($_POST['fld_defaultvalue'], $values, true) && !array_key_exists($_POST['fld_defaultvalue'], $values) ) { |
||
309 | $has_errors = true; |
||
310 | $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE; |
||
311 | } |
||
312 | |||
313 | //length larger than longest value? |
||
314 | $length = intval($_POST['fld_length']); |
||
315 | foreach($values as $key=>$value) { |
||
316 | if (strlen($key) > $length) { |
||
317 | $has_errors = true; |
||
318 | $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key), $length); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | //Values are all of the correct datatype? |
||
323 | } elseif ($fld_needsvalues) { |
||
324 | $has_errors = true; |
||
325 | $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE; |
||
326 | } |
||
327 | |||
328 | if ($has_errors) { |
||
329 | $afield = array(); |
||
330 | |||
331 | $afield['name'] = $_POST['fld_name']; |
||
332 | $afield['description'] = $_POST['fld_description']; |
||
333 | $afield['fieldname'] = $fld_fieldname; |
||
334 | $afield['departments'] = $_POST['fld_departments']; |
||
335 | $afield['controltype'] = $_POST['fld_controltype']; |
||
336 | $afield['datatype'] = $_POST['fld_datatype']; |
||
337 | $afield['required'] = $_POST['fld_required']; |
||
338 | $afield['weight'] = $_POST['fld_weight']; |
||
339 | $afield['defaultvalue'] = $_POST['fld_defaultvalue']; |
||
340 | $afield['values'] = $_POST['fld_values']; |
||
341 | $afield['length'] = $_POST['fld_length']; |
||
342 | $afield['validation'] = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']); |
||
343 | $session->set('xhelp_addField', $afield); |
||
344 | $session->set('xhelp_addFieldErrors', $errors); |
||
345 | header('Location: '. xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php')); |
||
346 | exit(); |
||
347 | } |
||
348 | |||
349 | //Save field |
||
350 | $hField =& xhelpGetHandler('ticketField'); |
||
351 | $field =& $hField->create(); |
||
352 | $field->setVar('name', $_POST['fld_name']); |
||
353 | $field->setVar('description', $_POST['fld_description']); |
||
354 | $field->setVar('fieldname', $fld_fieldname); |
||
355 | $field->setVar('controltype', $_POST['fld_controltype']); |
||
356 | $field->setVar('datatype', $_POST['fld_datatype']); |
||
357 | $field->setVar('fieldlength', $_POST['fld_length']); |
||
358 | $field->setVar('required', $_POST['fld_required']); |
||
359 | $field->setVar('weight', $_POST['fld_weight']); |
||
360 | $field->setVar('defaultvalue', $_POST['fld_defaultvalue']); |
||
361 | $field->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox'])); |
||
362 | $field->addValues($values); |
||
363 | $field->addDepartments($_POST['fld_departments']); |
||
364 | |||
365 | if ($hField->insert($field)) { |
||
366 | _clearAddSessionVars(); |
||
367 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_ADD_OK); |
||
368 | |||
369 | } else { |
||
370 | $errors = $field->getHtmlErrors(); |
||
371 | redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_ADD_ERR . $errors); |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | |||
772 |
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.