| Conditions | 6 |
| Paths | 8 |
| Total Lines | 127 |
| Code Lines | 84 |
| Lines | 13 |
| Ratio | 10.24 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 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 |
||
| 26 | public function AddForm() { |
||
| 27 | $pageTypes = array(); |
||
| 28 | foreach($this->PageTypes() as $type) { |
||
| 29 | $html = sprintf('<span class="page-icon class-%s"></span><span class="title">%s</span><span class="form__field-description">%s</span>', |
||
| 30 | $type->getField('ClassName'), |
||
| 31 | $type->getField('AddAction'), |
||
| 32 | $type->getField('Description') |
||
| 33 | ); |
||
| 34 | $pageTypes[$type->getField('ClassName')] = DBField::create_field('HTMLFragment', $html); |
||
| 35 | } |
||
| 36 | // Ensure generic page type shows on top |
||
| 37 | if(isset($pageTypes['Page'])) { |
||
| 38 | $pageTitle = $pageTypes['Page']; |
||
| 39 | $pageTypes = array_merge(array('Page' => $pageTitle), $pageTypes); |
||
| 40 | } |
||
| 41 | |||
| 42 | $numericLabelTmpl = '<span class="step-label"><span class="flyout">Step %d. </span><span class="title">%s</span></span>'; |
||
| 43 | |||
| 44 | $topTitle = _t('CMSPageAddController.ParentMode_top', 'Top level'); |
||
| 45 | $childTitle = _t('CMSPageAddController.ParentMode_child', 'Under another page'); |
||
| 46 | |||
| 47 | $fields = new FieldList( |
||
| 48 | $parentModeField = new SelectionGroup( |
||
| 49 | "ParentModeField", |
||
| 50 | array( |
||
| 51 | new SelectionGroup_Item( |
||
| 52 | "top", |
||
| 53 | null, |
||
| 54 | $topTitle |
||
| 55 | ), |
||
| 56 | new SelectionGroup_Item( |
||
| 57 | 'child', |
||
| 58 | $parentField = new TreeDropdownField( |
||
| 59 | "ParentID", |
||
| 60 | "", |
||
| 61 | 'SiteTree', |
||
| 62 | 'ID', |
||
| 63 | 'TreeTitle' |
||
| 64 | ), |
||
| 65 | $childTitle |
||
| 66 | ) |
||
| 67 | ) |
||
| 68 | ), |
||
| 69 | new LiteralField( |
||
| 70 | 'RestrictedNote', |
||
| 71 | sprintf( |
||
| 72 | '<p class="message notice message-restricted">%s</p>', |
||
| 73 | _t( |
||
| 74 | 'CMSMain.AddPageRestriction', |
||
| 75 | 'Note: Some page types are not allowed for this selection' |
||
| 76 | ) |
||
| 77 | ) |
||
| 78 | ), |
||
| 79 | $typeField = new OptionsetField( |
||
| 80 | "PageType", |
||
| 81 | DBField::create_field( |
||
| 82 | 'HTMLFragment', |
||
| 83 | sprintf($numericLabelTmpl, 2, _t('CMSMain.ChoosePageType', 'Choose page type')) |
||
| 84 | ), |
||
| 85 | $pageTypes, |
||
| 86 | 'Page' |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | |||
| 90 | $parentModeField->setTitle(DBField::create_field( |
||
| 91 | 'HTMLFragment', |
||
| 92 | sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page')) |
||
| 93 | )); |
||
| 94 | |||
| 95 | $parentField->setSearchFunction(function ($sourceObject, $labelField, $search) { |
||
| 96 | return DataObject::get($sourceObject) |
||
| 97 | ->filterAny([ |
||
| 98 | 'MenuTitle:PartialMatch' => $search, |
||
| 99 | 'Title:PartialMatch' => $search, |
||
| 100 | ]); |
||
| 101 | }); |
||
| 102 | |||
| 103 | // TODO Re-enable search once it allows for HTML title display, |
||
| 104 | // see http://open.silverstripe.org/ticket/7455 |
||
| 105 | // $parentField->setShowSearch(true); |
||
| 106 | |||
| 107 | $parentModeField->addExtraClass('parent-mode'); |
||
| 108 | |||
| 109 | // CMSMain->currentPageID() automatically sets the homepage, |
||
| 110 | // which we need to counteract in the default selection (which should default to root, ID=0) |
||
| 111 | if($parentID = $this->getRequest()->getVar('ParentID')) { |
||
| 112 | $parentModeField->setValue('child'); |
||
| 113 | $parentField->setValue((int)$parentID); |
||
| 114 | } else { |
||
| 115 | $parentModeField->setValue('top'); |
||
| 116 | } |
||
| 117 | |||
| 118 | $actions = new FieldList( |
||
| 119 | FormAction::create("doAdd", _t('CMSMain.Create',"Create")) |
||
| 120 | ->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept') |
||
| 121 | ->setUseButtonTag(true), |
||
| 122 | FormAction::create("doCancel", _t('CMSMain.Cancel',"Cancel")) |
||
| 123 | ->addExtraClass('ss-ui-action-destructive ss-ui-action-cancel') |
||
| 124 | ->setUseButtonTag(true) |
||
| 125 | ); |
||
| 126 | |||
| 127 | $this->extend('updatePageOptions', $fields); |
||
| 128 | |||
| 129 | $negotiator = $this->getResponseNegotiator(); |
||
| 130 | $form = Form::create( |
||
| 131 | $this, "AddForm", $fields, $actions |
||
| 132 | )->setHTMLID('Form_AddForm'); |
||
| 133 | $form->setAttribute('data-hints', $this->SiteTreeHints()); |
||
| 134 | $form->setAttribute('data-childfilter', $this->Link('childfilter')); |
||
| 135 | View Code Duplication | $form->setValidationResponseCallback(function() use ($negotiator, $form) { |
|
| 136 | $request = $this->getRequest(); |
||
| 137 | if($request->isAjax() && $negotiator) { |
||
| 138 | $this->setupFormErrors(); |
||
| 139 | $result = $this->forTemplate(); |
||
| 140 | |||
| 141 | return $negotiator->respond($request, array( |
||
| 142 | 'CurrentForm' => function() use($result) { |
||
| 143 | return $result; |
||
| 144 | } |
||
| 145 | )); |
||
| 146 | } |
||
| 147 | }); |
||
| 148 | $form->addExtraClass('cms-add-form cms-content center cms-edit-form ' . $this->BaseCSSClasses()); |
||
| 149 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 150 | |||
| 151 | return $form; |
||
| 152 | } |
||
| 153 | |||
| 201 |