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