Conditions | 52 |
Paths | > 20000 |
Total Lines | 336 |
Code Lines | 215 |
Lines | 63 |
Ratio | 18.75 % |
Changes | 3 | ||
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 |
||
29 | public function __form($readonly = false) |
||
|
|||
30 | { |
||
31 | $formHasErrors = (is_array($this->_errors) && !empty($this->_errors)); |
||
32 | |||
33 | View Code Duplication | if ($formHasErrors) { |
|
34 | $this->pageAlert( |
||
35 | __('An error occurred while processing this form. See below for details.'), |
||
36 | Alert::ERROR |
||
37 | ); |
||
38 | |||
39 | // These alerts are only valid if the form doesn't have errors |
||
40 | } elseif (isset($this->_context['flag'])) { |
||
41 | $time = Widget::Time(); |
||
42 | |||
43 | switch ($this->_context['flag']) { |
||
44 | case 'saved': |
||
45 | $message = __('Event updated at %s.', array($time->generate())); |
||
46 | break; |
||
47 | case 'created': |
||
48 | $message = __('Event created at %s.', array($time->generate())); |
||
49 | } |
||
50 | |||
51 | $this->pageAlert( |
||
52 | $message |
||
53 | . ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">' |
||
54 | . __('Create another?') |
||
55 | . '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">' |
||
56 | . __('View all Events') |
||
57 | . '</a>', |
||
58 | Alert::SUCCESS |
||
59 | ); |
||
60 | } |
||
61 | |||
62 | $isEditing = ($readonly ? true : false); |
||
63 | $fields = array("name" => null, "filters" => null); |
||
64 | $about = array("name" => null); |
||
65 | $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT); |
||
66 | |||
67 | if (isset($_POST['fields'])) { |
||
68 | $fields = $_POST['fields']; |
||
69 | |||
70 | if ($this->_context['action'] === 'edit') { |
||
71 | $isEditing = true; |
||
72 | } |
||
73 | } elseif ($this->_context['action'] === 'edit' || $this->_context['action'] === 'info') { |
||
74 | $isEditing = true; |
||
75 | $handle = $this->_context['handle']; |
||
76 | $existing = EventManager::create($handle); |
||
77 | $about = $existing->about(); |
||
78 | |||
79 | if ($this->_context['action'] === 'edit' && !$existing->allowEditorToParse()) { |
||
80 | redirect(SYMPHONY_URL . '/blueprints/events/info/' . $handle . '/'); |
||
81 | } |
||
82 | |||
83 | $fields['name'] = $about['name']; |
||
84 | $fields['source'] = $existing->getSource(); |
||
85 | $provided = false; |
||
86 | |||
87 | View Code Duplication | if (!empty($providers)) { |
|
88 | foreach ($providers as $providerClass => $provider) { |
||
89 | if ($fields['source'] === call_user_func(array($providerClass, 'getClass'))) { |
||
90 | $fields = array_merge($fields, $existing->settings()); |
||
91 | $provided = true; |
||
92 | break; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if (!$provided) { |
||
98 | if (isset($existing->eParamFILTERS)) { |
||
99 | $fields['filters'] = $existing->eParamFILTERS; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // Handle name on edited changes, or from reading an edited datasource |
||
105 | View Code Duplication | if (isset($about['name'])) { |
|
106 | $name = $about['name']; |
||
107 | } elseif (isset($fields['name'])) { |
||
108 | $name = $fields['name']; |
||
109 | } |
||
110 | |||
111 | $this->setPageType('form'); |
||
112 | $this->setTitle(__(($isEditing ? '%1$s – %2$s – %3$s' : '%2$s – %3$s'), |
||
113 | array($name, __('Events'), __('Symphony')))); |
||
114 | $this->appendSubheading(($isEditing ? $about['name'] : __('Untitled'))); |
||
115 | $this->insertBreadcrumbs(array( |
||
116 | Widget::Anchor(__('Events'), SYMPHONY_URL . '/blueprints/events/'), |
||
117 | )); |
||
118 | |||
119 | if (!$readonly) { |
||
120 | $fieldset = new XMLElement('fieldset'); |
||
121 | $fieldset->setAttribute('class', 'settings'); |
||
122 | $fieldset->appendChild(new XMLElement('legend', __('Essentials'))); |
||
123 | |||
124 | // Target |
||
125 | $sources = new XMLElement('div', null, array('class' => 'apply actions')); |
||
126 | $div = new XMLElement('div'); |
||
127 | $label = Widget::Label(__('Target'), null, 'apply-label-left'); |
||
128 | $sources->appendChild($label); |
||
129 | $sources->appendChild($div); |
||
130 | |||
131 | $sections = SectionManager::fetch(null, 'ASC', 'name'); |
||
132 | $options = array(); |
||
133 | $section_options = array(); |
||
134 | $source = isset($fields['source']) ? $fields['source'] : null; |
||
135 | |||
136 | if (is_array($sections) && !empty($sections)) { |
||
137 | $section_options = array('label' => __('Sections'), 'options' => array()); |
||
138 | |||
139 | View Code Duplication | foreach ($sections as $s) { |
|
140 | $section_options['options'][] = array( |
||
141 | $s->get('id'), |
||
142 | $source === $s->get('id'), |
||
143 | General::sanitize($s->get('name')) |
||
144 | ); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $options[] = $section_options; |
||
149 | |||
150 | // Loop over the event providers |
||
151 | if (!empty($providers)) { |
||
152 | $p = array('label' => __('From extensions'), 'options' => array()); |
||
153 | |||
154 | foreach ($providers as $providerClass => $provider) { |
||
155 | $p['options'][] = array( |
||
156 | $providerClass, |
||
157 | ($fields['source'] === $providerClass), |
||
158 | $provider |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | $options[] = $p; |
||
163 | } |
||
164 | |||
165 | $div->appendChild( |
||
166 | Widget::Select('source', $options, array('id' => 'event-context')) |
||
167 | ); |
||
168 | |||
169 | if (isset($this->_errors['source'])) { |
||
170 | $this->Context->prependChild(Widget::Error($sources, $this->_errors['source'])); |
||
171 | } else { |
||
172 | $this->Context->prependChild($sources); |
||
173 | } |
||
174 | |||
175 | $this->Form->appendChild( |
||
176 | Widget::Input('fields[source]', $options[0]['options'][0][0], 'hidden', |
||
177 | array('id' => 'event-source')) |
||
178 | ); |
||
179 | |||
180 | // Name |
||
181 | $group = new XMLElement('div'); |
||
182 | $label = Widget::Label(__('Name')); |
||
183 | $label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name']))); |
||
184 | |||
185 | $div = new XMLElement('div'); |
||
186 | $div->setAttribute('class', 'column'); |
||
187 | |||
188 | View Code Duplication | if (isset($this->_errors['name'])) { |
|
189 | $div->appendChild(Widget::Error($label, $this->_errors['name'])); |
||
190 | } else { |
||
191 | $div->appendChild($label); |
||
192 | } |
||
193 | $group->appendChild($div); |
||
194 | $fieldset->appendChild($group); |
||
195 | $this->Form->appendChild($fieldset); |
||
196 | |||
197 | // Filters |
||
198 | $fieldset = new XMLElement('fieldset'); |
||
199 | $fieldset->setAttribute('class', 'settings pickable'); |
||
200 | $fieldset->appendChild(new XMLElement('legend', __('Filters'))); |
||
201 | $p = new XMLElement('p', __('Event Filters add additional conditions or actions to an event.')); |
||
202 | $p->setAttribute('class', 'help'); |
||
203 | $fieldset->appendChild($p); |
||
204 | |||
205 | $filters = isset($fields['filters']) ? $fields['filters'] : array(); |
||
206 | $options = array( |
||
207 | array('admin-only', in_array('admin-only', $filters), __('Admin Only')), |
||
208 | array('send-email', in_array('send-email', $filters), __('Send Notification Email')), |
||
209 | array('expect-multiple', in_array('expect-multiple', $filters), __('Allow Multiple')), |
||
210 | ); |
||
211 | |||
212 | /** |
||
213 | * Allows adding of new filter rules to the Event filter rule select box |
||
214 | * |
||
215 | * @delegate AppendEventFilter |
||
216 | * @param string $context |
||
217 | * '/blueprints/events/(edit|new|info)/' |
||
218 | * @param array $selected |
||
219 | * An array of all the selected filters for this Event |
||
220 | * @param array $options |
||
221 | * An array of all the filters that are available, passed by reference |
||
222 | */ |
||
223 | Symphony::ExtensionManager()->notifyMembers('AppendEventFilter', |
||
224 | '/blueprints/events/' . $this->_context['action'] . '/', array( |
||
225 | 'selected' => $filters, |
||
226 | 'options' => &$options |
||
227 | )); |
||
228 | |||
229 | $fieldset->appendChild(Widget::Select('fields[filters][]', $options, |
||
230 | array('multiple' => 'multiple', 'id' => 'event-filters'))); |
||
231 | $this->Form->appendChild($fieldset); |
||
232 | |||
233 | // Connections |
||
234 | $fieldset = new XMLElement('fieldset'); |
||
235 | $fieldset->setAttribute('class', 'settings'); |
||
236 | $fieldset->appendChild(new XMLElement('legend', __('Attach to Pages'))); |
||
237 | $p = new XMLElement('p', __('The event will only be available on the selected pages.')); |
||
238 | $p->setAttribute('class', 'help'); |
||
239 | $fieldset->appendChild($p); |
||
240 | |||
241 | $div = new XMLElement('div'); |
||
242 | $label = Widget::Label(__('Pages')); |
||
243 | |||
244 | $pages = PageManager::fetch(); |
||
245 | $event_handle = str_replace('-', '_', Lang::createHandle($fields['name'])); |
||
246 | $connections = ResourceManager::getAttachedPages(ResourceManager::RESOURCE_TYPE_EVENT, $event_handle); |
||
247 | $selected = array(); |
||
248 | |||
249 | foreach ($connections as $connection) { |
||
250 | $selected[] = $connection['id']; |
||
251 | } |
||
252 | |||
253 | $options = array(); |
||
254 | |||
255 | View Code Duplication | foreach ($pages as $page) { |
|
256 | $options[] = array( |
||
257 | $page['id'], |
||
258 | in_array($page['id'], $selected), |
||
259 | PageManager::resolvePageTitle($page['id']) |
||
260 | ); |
||
261 | } |
||
262 | |||
263 | $label->appendChild(Widget::Select('fields[connections][]', $options, array('multiple' => 'multiple'))); |
||
264 | $div->appendChild($label); |
||
265 | |||
266 | $fieldset->appendChild($div); |
||
267 | $this->Form->appendChild($fieldset); |
||
268 | |||
269 | // Providers |
||
270 | if (!empty($providers)) { |
||
271 | foreach ($providers as $providerClass => $provider) { |
||
272 | if ($isEditing && $fields['source'] !== call_user_func(array($providerClass, 'getSource'))) { |
||
273 | continue; |
||
274 | } |
||
275 | |||
276 | call_user_func_array(array($providerClass, 'buildEditor'), |
||
277 | array($this->Form, &$this->_errors, $fields, $handle)); |
||
278 | } |
||
279 | } |
||
280 | } else { |
||
281 | // Author |
||
282 | if (isset($about['author']['website'])) { |
||
283 | $link = Widget::Anchor($about['author']['name'], General::validateURL($about['author']['website'])); |
||
284 | } elseif (isset($about['author']['email'])) { |
||
285 | $link = Widget::Anchor($about['author']['name'], 'mailto:' . $about['author']['email']); |
||
286 | } else { |
||
287 | $link = $about['author']['name']; |
||
288 | } |
||
289 | |||
290 | if ($link) { |
||
291 | $fieldset = new XMLElement('fieldset'); |
||
292 | $fieldset->setAttribute('class', 'settings'); |
||
293 | $fieldset->appendChild(new XMLElement('legend', __('Author'))); |
||
294 | $fieldset->appendChild(new XMLElement('p', $link->generate(false))); |
||
295 | $this->Form->appendChild($fieldset); |
||
296 | } |
||
297 | |||
298 | // Version |
||
299 | $fieldset = new XMLElement('fieldset'); |
||
300 | $fieldset->setAttribute('class', 'settings'); |
||
301 | $fieldset->appendChild(new XMLElement('legend', __('Version'))); |
||
302 | $version = array_key_exists('version', $about) ? $about['version'] : null; |
||
303 | $release_date = array_key_exists('release-date', |
||
304 | $about) ? $about['release-date'] : filemtime(EventManager::__getDriverPath($handle)); |
||
305 | |||
306 | if (preg_match('/^\d+(\.\d+)*$/', $version)) { |
||
307 | $fieldset->appendChild( |
||
308 | new XMLElement('p', __('%1$s released on %2$s', |
||
309 | array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))) |
||
310 | ); |
||
311 | View Code Duplication | } elseif (!is_null($version)) { |
|
312 | $fieldset->appendChild( |
||
313 | new XMLElement('p', __('Created by %1$s at %2$s', |
||
314 | array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))) |
||
315 | ); |
||
316 | } else { |
||
317 | $fieldset->appendChild( |
||
318 | new XMLElement('p', |
||
319 | __('Last modified on %s', array(DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))) |
||
320 | ); |
||
321 | } |
||
322 | $this->Form->appendChild($fieldset); |
||
323 | } |
||
324 | |||
325 | // If we are editing an event, it assumed that the event has documentation |
||
326 | $fieldset = new XMLElement('fieldset'); |
||
327 | $fieldset->setAttribute('id', 'event-documentation'); |
||
328 | $fieldset->setAttribute('class', 'settings'); |
||
329 | |||
330 | if ($isEditing && method_exists($existing, 'documentation')) { |
||
331 | $doc = $existing->documentation(); |
||
332 | |||
333 | if ($doc) { |
||
334 | $fieldset->setValue( |
||
335 | '<legend>' . __('Documentation') . '</legend>' . PHP_EOL . |
||
336 | General::tabsToSpaces(is_object($doc) ? $doc->generate(true, 4) : $doc) |
||
337 | ); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | $this->Form->appendChild($fieldset); |
||
342 | |||
343 | $div = new XMLElement('div'); |
||
344 | $div->setAttribute('class', 'actions'); |
||
345 | $div->appendChild(Widget::Input('action[save]', ($isEditing ? __('Save Changes') : __('Create Event')), |
||
346 | 'submit', array('accesskey' => 's'))); |
||
347 | |||
348 | View Code Duplication | if ($isEditing) { |
|
349 | $button = new XMLElement('button', __('Delete')); |
||
350 | $button->setAttributeArray(array( |
||
351 | 'name' => 'action[delete]', |
||
352 | 'class' => 'button confirm delete', |
||
353 | 'title' => __('Delete this event'), |
||
354 | 'type' => 'submit', |
||
355 | 'accesskey' => 'd', |
||
356 | 'data-message' => __('Are you sure you want to delete this event?') |
||
357 | )); |
||
358 | $div->appendChild($button); |
||
359 | } |
||
360 | |||
361 | if (!$readonly) { |
||
362 | $this->Form->appendChild($div); |
||
363 | } |
||
364 | } |
||
365 | |||
720 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: