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