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