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