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