Conditions | 41 |
Paths | 976 |
Total Lines | 204 |
Code Lines | 133 |
Lines | 0 |
Ratio | 0 % |
Tests | 60 |
CRAP Score | 292.9658 |
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 |
||
282 | 1 | function smarty_function_sugar_button($params, &$smarty) |
|
283 | { |
||
284 | 4 | if(empty($params['module'])) { |
|
285 | 1 | $smarty->trigger_error("sugar_button: missing required param (module)"); |
|
286 | 3 | } else if(empty($params['id'])) { |
|
287 | $smarty->trigger_error("sugar_button: missing required param (id)"); |
||
288 | 3 | } else if(empty($params['view'])) { |
|
289 | $smarty->trigger_error("sugar_button: missing required param (view)"); |
||
290 | } |
||
291 | |||
292 | 4 | $js_form = (empty($params['form_id'])) ? "var _form = (this.form) ? this.form : document.forms[0];" : "var _form = document.getElementById('{$params['form_id']}');"; |
|
293 | |||
294 | 4 | $type = $params['id']; |
|
295 | 4 | $location = (empty($params['location'])) ? "" : "_".$params['location']; |
|
296 | |||
297 | 4 | if(!is_array($type)) { |
|
298 | 4 | $module = $params['module']; |
|
299 | 4 | $view = $params['view']; |
|
300 | 4 | switch(strtoupper($type)) { |
|
301 | 4 | case "SEARCH": |
|
302 | 2 | $output = '<input tabindex="2" title="{$APP.LBL_SEARCH_BUTTON_TITLE}" onclick="SUGAR.savedViews.setChooser();" class="button" type="submit" name="button" value="{$APP.LBL_SEARCH_BUTTON_LABEL}" id="search_form_submit"/> '; |
|
303 | 2 | break; |
|
304 | |||
305 | 2 | case "CANCEL": |
|
|
|||
306 | |||
307 | //If the return action is not empty and the return action is detail view and the id is not empty |
||
308 | $cancelButton = '{if !empty($smarty.request.return_action) && ($smarty.request.return_action == "DetailView" && !empty($smarty.request.return_id))}'; |
||
309 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=DetailView&module={$smarty.request.return_module|escape:"url"}&record={$smarty.request.return_id|escape:"url"}\'); return false;" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" type="button" id="'.$type.$location.'"> '; |
||
310 | |||
311 | //If the return action is not empty and the return action is detail view and the id (from fields) is not empty |
||
312 | $cancelButton .= '{elseif !empty($smarty.request.return_action) && ($smarty.request.return_action == "DetailView" && !empty($fields.id.value))}'; |
||
313 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=DetailView&module={$smarty.request.return_module|escape:"url"}&record={$fields.id.value}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="'.$type.$location.'"> '; |
||
314 | |||
315 | //Bug 1057 If the return action is not empty and the return action is detail view and the id (from both locations) are empty, go to the modules listview |
||
316 | $cancelButton .= '{elseif !empty($smarty.request.return_action) && ($smarty.request.return_action == "DetailView" && empty($fields.id.value)) && empty($smarty.request.return_id)}'; |
||
317 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=ListView&module={$smarty.request.return_module|escape:"url"}&record={$fields.id.value}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="'.$type.$location.'"> '; |
||
318 | |||
319 | |||
320 | //Bug 893 if the return action is not empty and the return module is not empty, go back to that page |
||
321 | $cancelButton .= '{elseif !empty($smarty.request.return_action) && !empty($smarty.request.return_module)}'; |
||
322 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action={$smarty.request.return_action}&module={$smarty.request.return_module|escape:"url"}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="'.$type.$location.'"> '; |
||
323 | |||
324 | //If the return action is empty but the return id is in fields |
||
325 | $cancelButton .= '{elseif empty($smarty.request.return_action) || empty($smarty.request.return_id) && !empty($fields.id.value)}'; |
||
326 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=index&module='.$module.'\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="'.$type.$location.'"> '; |
||
327 | |||
328 | |||
329 | |||
330 | $cancelButton .= '{else}'; |
||
331 | $cancelButton .= '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="SUGAR.ajaxUI.loadContent(\'index.php?action=index&module={$smarty.request.return_module|escape:"url"}&record={$smarty.request.return_id|escape:"url"}\'); return false;" type="button" name="button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}" id="'.$type.$location.'"> '; |
||
332 | $cancelButton .= '{/if}'; |
||
333 | |||
334 | |||
335 | //$cancelButton = '{$smarty.request.return_action}'.'{$smarty.request.return_module}'; |
||
336 | |||
337 | |||
338 | $output = $cancelButton; |
||
339 | break; |
||
340 | |||
341 | 2 | case "DELETE": |
|
342 | $output = '{if $bean->aclAccess("delete")}<input title="{$APP.LBL_DELETE_BUTTON_TITLE}" accessKey="{$APP.LBL_DELETE_BUTTON_KEY}" class="button" onclick="'.$js_form.' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'ListView\'; _form.action.value=\'Delete\'; if(confirm(\'{$APP.NTC_DELETE_CONFIRMATION}\')) SUGAR.ajaxUI.submitForm(_form);" type="submit" name="Delete" value="{$APP.LBL_DELETE_BUTTON_LABEL}" id="delete_button">{/if} '; |
||
343 | break; |
||
344 | |||
345 | 2 | case "DUPLICATE": |
|
346 | $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_DUPLICATE_BUTTON_TITLE}" accessKey="{$APP.LBL_DUPLICATE_BUTTON_KEY}" class="button" onclick="'.$js_form.' _form.return_module.value=\''. $module . '\'; _form.return_action.value=\'DetailView\'; _form.isDuplicate.value=true; _form.action.value=\'' . $view . '\'; _form.return_id.value=\'{$id}\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Duplicate" value="{$APP.LBL_DUPLICATE_BUTTON_LABEL}" id="duplicate_button">{/if} '; |
||
347 | break; |
||
348 | |||
349 | 2 | case "EDIT"; |
|
350 | $output = '{if $bean->aclAccess("edit")}<input title="{$APP.LBL_EDIT_BUTTON_TITLE}" accessKey="{$APP.LBL_EDIT_BUTTON_KEY}" class="button primary" onclick="'.$js_form.' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'EditView\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Edit" id="edit_button" value="{$APP.LBL_EDIT_BUTTON_LABEL}">{/if} '; |
||
351 | break; |
||
352 | |||
353 | 2 | case "FIND_DUPLICATES": |
|
354 | $output = '{if $bean->aclAccess("edit") && $bean->aclAccess("delete")}<input title="{$APP.LBL_DUP_MERGE}" class="button" onclick="'.$js_form.' _form.return_module.value=\'' . $module . '\'; _form.return_action.value=\'DetailView\'; _form.return_id.value=\'{$id}\'; _form.action.value=\'Step1\'; _form.module.value=\'MergeRecords\';SUGAR.ajaxUI.submitForm(_form);" type="button" name="Merge" value="{$APP.LBL_DUP_MERGE}" id="merge_duplicate_button">{/if} '; |
||
355 | break; |
||
356 | |||
357 | 2 | case "SAVE": |
|
358 | $view = ($_REQUEST['action'] == 'EditView') ? 'EditView' : (($view == 'EditView') ? 'EditView' : $view); |
||
359 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="'.$js_form.' {if $isDuplicate}_form.return_id.value=\'\'; {/if}_form.action.value=\'Save\'; if(check_form(\'' . $view . '\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="{$APP.LBL_SAVE_BUTTON_LABEL}" id="'.$type.$location.'">{/if} '; |
||
360 | break; |
||
361 | |||
362 | 2 | case "SUBPANELSAVE": |
|
363 | if($view == 'QuickCreate' || (isset($_REQUEST['target_action']) && strtolower($_REQUEST['target_action']) == 'quickcreate')) $view = "form_SubpanelQuickCreate_{$module}"; |
||
364 | |||
365 | /* BEGIN - SECURITY GROUPS - redirect a subpanel save to the detail view if select popup and user in more than 1 group*/ |
||
366 | /** |
||
367 | global $current_user, $sugar_config; |
||
368 | if(isset($sugar_config['securitysuite_popup_select']) && $sugar_config['securitysuite_popup_select'] == true) { |
||
369 | require_once('modules/SecurityGroups/SecurityGroup.php'); |
||
370 | $groupFocus = new SecurityGroup(); |
||
371 | |||
372 | if($groupFocus->getMembershipCount($current_user->id) > 1) { |
||
373 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" class="button" onclick="'.$js_form.' disableOnUnloadEditView(); _form.action.value=\'Save\';_form.module.value=\'{$module}\';_form.return_module.value=\'{$module}\';_form.return_action.value=\'DetailView\';_form.return_id.value=\'\';" type="submit" name="' . $params['module'] . '_subpanel_save_button" id="' . $params['module'] . '_subpanel_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} '; |
||
374 | } |
||
375 | } |
||
376 | //if different here then change the group return right above |
||
377 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" class="button" onclick="'.$js_form.' disableOnUnloadEditView(); _form.action.value=\'Save\';if(check_form(\''.$view.'\'))return SUGAR.subpanelUtils.inlineSave(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_subpanel_save_button" id="' . $params['module'] . '_subpanel_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} '; |
||
378 | |||
379 | */ |
||
380 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" class="button" onclick="'.$js_form.' disableOnUnloadEditView(); _form.action.value=\'Save\';if(check_form(\''.$view.'\'))return SUGAR.subpanelUtils.inlineSave(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_subpanel_save_button" id="' . $params['module'] . '_subpanel_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} '; |
||
381 | /* END - SECURITY GROUPS */ |
||
382 | |||
383 | break; |
||
384 | 2 | case "SUBPANELCANCEL": |
|
385 | $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" class="button" onclick="return SUGAR.subpanelUtils.cancelCreate($(this).attr(\'id\'));return false;" type="submit" name="' . $params['module'] . '_subpanel_cancel_button" id="' . $params['module'] . '_subpanel_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> '; |
||
386 | |||
387 | break; |
||
388 | 2 | case "SUBPANELFULLFORM": |
|
389 | $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" class="button" onclick="'.$js_form.' disableOnUnloadEditView(_form); _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';" type="submit" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> '; |
||
390 | $output .= '<input type="hidden" name="full_form" value="full_form">'; |
||
391 | break; |
||
392 | 2 | case "DCMENUCANCEL": |
|
393 | 1 | $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" class="button" onclick="javascript:lastLoadedMenu=undefined;DCMenu.closeOverlay();return false;" type="submit" name="' . $params['module'] . '_dcmenu_cancel_button" id="' . $params['module'] . '_dcmenu_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> '; |
|
394 | 1 | break; |
|
395 | 2 | case "DCMENUSAVE": |
|
396 | 1 | if ($view == 'QuickCreate') { |
|
397 | 1 | $view = "form_DCQuickCreate_{$module}"; |
|
398 | } else if ($view == 'EditView') { |
||
399 | $view = "form_DCEditView_{$module}"; |
||
400 | } |
||
401 | 1 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" class="button primary" onclick="'.$js_form.' _form.action.value=\'Save\';if(check_form(\''.$view.'\'))return DCMenu.save(_form.id, \'' . $params['module'] . '_subpanel_save_button\');return false;" type="submit" name="' . $params['module'] . '_dcmenu_save_button" id="' . $params['module'] . '_dcmenu_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} '; |
|
402 | 1 | break; |
|
403 | 2 | case "DCMENUFULLFORM": |
|
404 | 1 | $output = '<input title="{$APP.LBL_FULL_FORM_BUTTON_TITLE}" accessKey="{$APP.LBL_FULL_FORM_BUTTON_KEY}" class="button" onclick="'.$js_form.' disableOnUnloadEditView(_form); _form.return_action.value=\'DetailView\'; _form.action.value=\'EditView\'; _form.return_module.value=\'' . $params['module'] . '\';_form.return_id.value=_form.record.value;if(typeof(_form.to_pdf)!=\'undefined\') _form.to_pdf.value=\'0\';SUGAR.ajaxUI.submitForm(_form,null,true);DCMenu.closeOverlay();" type="button" name="' . $params['module'] . '_subpanel_full_form_button" id="' . $params['module'] . '_subpanel_full_form_button" value="{$APP.LBL_FULL_FORM_BUTTON_LABEL}"> '; |
|
405 | 1 | $output .= '<input type="hidden" name="full_form" value="full_form">'; |
|
406 | 1 | $output .= '<input type="hidden" name="is_admin" value="">'; |
|
407 | 1 | break; |
|
408 | 2 | case "POPUPSAVE": |
|
409 | 1 | $view = ($view == 'QuickCreate') ? "form_QuickCreate_{$module}" : $view; |
|
410 | $output = '{if $bean->aclAccess("save")}<input title="{$APP.LBL_SAVE_BUTTON_TITLE}" accessKey="{$APP.LBL_SAVE_BUTTON_KEY}" ' |
||
411 | 1 | . 'class="button primary" onclick="'.$js_form.' _form.action.value=\'Popup\';' |
|
412 | 1 | . 'return check_form(\''.$view.'\')" type="submit" name="' . $params['module'] |
|
413 | 1 | . '_popupcreate_save_button" id="' . $params['module'] |
|
414 | 1 | . '_popupcreate_save_button" value="{$APP.LBL_SAVE_BUTTON_LABEL}">{/if} '; |
|
415 | 1 | break; |
|
416 | 2 | case "POPUPCANCEL": |
|
417 | $output = '<input title="{$APP.LBL_CANCEL_BUTTON_TITLE}" accessKey="{$APP.LBL_CANCEL_BUTTON_KEY}" ' |
||
418 | . 'class="button" onclick="toggleDisplay(\'addform\');return false;" ' |
||
419 | 1 | . 'name="' . $params['module'] . '_popup_cancel_button" type="submit"' |
|
420 | 1 | . 'id="' . $params['module'] . '_popup_cancel_button" value="{$APP.LBL_CANCEL_BUTTON_LABEL}"> '; |
|
421 | 1 | break; |
|
422 | 2 | case "AUDIT": |
|
423 | $popup_request_data = array( |
||
424 | 2 | 'call_back_function' => 'set_return', |
|
425 | 'form_name' => 'EditView', |
||
426 | 'field_to_name_array' => array(), |
||
427 | ); |
||
428 | 2 | $json = getJSONobj(); |
|
429 | |||
430 | 2 | require_once('include/SugarFields/Parsers/MetaParser.php'); |
|
431 | 2 | $encoded_popup_request_data = MetaParser::parseDelimiters($json->encode($popup_request_data)); |
|
432 | 2 | $audit_link = '<input id="btn_view_change_log" title="{$APP.LNK_VIEW_CHANGE_LOG}" class="button" onclick=\'open_popup("Audit", "600", "400", "&record={$fields.id.value}&module_name=' . $params['module'] . '", true, false, ' . $encoded_popup_request_data . '); return false;\' type="button" value="{$APP.LNK_VIEW_CHANGE_LOG}">'; |
|
433 | 2 | $output = '{if $bean->aclAccess("detail")}{if !empty($fields.id.value) && $isAuditEnabled}'.$audit_link.'{/if}{/if}'; |
|
434 | 2 | break; |
|
435 | |||
436 | |||
437 | } //switch |
||
438 | 4 | if(isset($params['appendTo'])) { |
|
439 | 2 | $smarty->append($params['appendTo'], $output); |
|
440 | 2 | return; |
|
441 | } |
||
442 | 2 | return $output; |
|
443 | } else if(is_array($type) && isset($type['sugar_html'])) { |
||
444 | require_once('include/SugarHtml/SugarHtml.php'); |
||
445 | |||
446 | $dom_tree = SugarHtml::parseSugarHtml($type['sugar_html']); |
||
447 | replaceFormClick($dom_tree, $js_form); |
||
448 | $output = SugarHtml::createHtml($dom_tree); |
||
449 | |||
450 | if(isset($params['appendTo'])) { |
||
451 | $smarty->append($params['appendTo'], $output); |
||
452 | return; |
||
453 | } |
||
454 | return $output; |
||
455 | } else if(is_array($type) && isset($type['customCode'])) { |
||
456 | require_once('include/SugarHtml/SugarHtml.php'); |
||
457 | |||
458 | $dom_tree = SugarHtml::parseHtmlTag($type['customCode']); |
||
459 | $hidden_exists = false; |
||
460 | |||
461 | replaceFormClick($dom_tree, $js_form, $hidden_exists); |
||
462 | if($hidden_exists) { |
||
463 | //If the customCode contains hidden fields, the extracted hidden fields need to append in the original form |
||
464 | $form = $smarty->get_template_vars('form'); |
||
465 | $hidden_fields = $dom_tree; |
||
466 | extractHiddenInputs($hidden_fields); |
||
467 | if(!isset($form)) { |
||
468 | $form = array(); |
||
469 | } |
||
470 | if(!isset($form['hidden'])) { |
||
471 | $form['hidden'] = array(); |
||
472 | } |
||
473 | $form['hidden'][] = SugarHtml::createHtml($hidden_fields); |
||
474 | $smarty->assign('form', $form); |
||
475 | } |
||
476 | $output = SugarHtml::createHtml($dom_tree); |
||
477 | |||
478 | if(isset($params['appendTo'])) { |
||
479 | $smarty->append($params['appendTo'], $output); |
||
480 | return; |
||
481 | } |
||
482 | return $output; |
||
483 | } |
||
484 | |||
485 | } |
||
486 | /** |
||
572 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.