Conditions | 55 |
Paths | > 20000 |
Total Lines | 323 |
Code Lines | 169 |
Lines | 0 |
Ratio | 0 % |
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 |
||
241 | public function __form() |
||
242 | { |
||
243 | // Handle unknown context |
||
244 | if (!in_array($this->_context['action'], array('new', 'edit'))) { |
||
245 | Administration::instance()->errorPageNotFound(); |
||
246 | } |
||
247 | |||
248 | if ($this->_context['action'] === 'new' && !Symphony::Author()->isDeveloper() && !Symphony::Author()->isManager()) { |
||
249 | Administration::instance()->throwCustomError( |
||
250 | __('You are not authorised to access this page.'), |
||
251 | __('Access Denied'), |
||
252 | Page::HTTP_STATUS_UNAUTHORIZED |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | if (isset($this->_context['flag'])) { |
||
257 | $time = Widget::Time(); |
||
258 | |||
259 | switch ($this->_context['flag']) { |
||
260 | case 'saved': |
||
261 | $message = __('Author updated at %s.', array($time->generate())); |
||
262 | break; |
||
263 | case 'created': |
||
264 | $message = __('Author created at %s.', array($time->generate())); |
||
265 | } |
||
266 | |||
267 | $this->pageAlert( |
||
268 | $message |
||
269 | . ' <a href="' . SYMPHONY_URL . '/system/authors/new/" accesskey="c">' |
||
270 | . __('Create another?') |
||
271 | . '</a> <a href="' . SYMPHONY_URL . '/system/authors/" accesskey="a">' |
||
272 | . __('View all Authors') |
||
273 | . '</a>', |
||
274 | Alert::SUCCESS |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | $this->setPageType('form'); |
||
279 | $isOwner = false; |
||
280 | $isEditing = ($this->_context['action'] === 'edit'); |
||
281 | |||
282 | if (isset($_POST['fields'])) { |
||
283 | $author = $this->_Author; |
||
284 | } elseif ($isEditing) { |
||
285 | if (!$author_id = $this->_context['id']) { |
||
286 | redirect(SYMPHONY_URL . '/system/authors/'); |
||
287 | } |
||
288 | |||
289 | if (!$author = AuthorManager::fetchByID($author_id)) { |
||
290 | Administration::instance()->throwCustomError( |
||
291 | __('The author profile you requested does not exist.'), |
||
292 | __('Author not found'), |
||
293 | Page::HTTP_STATUS_NOT_FOUND |
||
294 | ); |
||
295 | } |
||
296 | } else { |
||
297 | $author = new Author; |
||
298 | } |
||
299 | |||
300 | if ($isEditing && $author->get('id') === Symphony::Author()->get('id')) { |
||
301 | $isOwner = true; |
||
302 | } |
||
303 | |||
304 | if ($isEditing && !$isOwner && !Symphony::Author()->isDeveloper() && !Symphony::Author()->isManager()) { |
||
305 | Administration::instance()->throwCustomError( |
||
306 | __('You are not authorised to edit other authors.'), |
||
307 | __('Access Denied'), |
||
308 | Page::HTTP_STATUS_FORBIDDEN |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | $this->setTitle(__(($this->_context['action'] === 'new' ? '%2$s – %3$s' : '%1$s – %2$s – %3$s'), array($author->getFullName(), __('Authors'), __('Symphony')))); |
||
313 | $this->appendSubheading(($this->_context['action'] === 'new' ? __('Untitled') : $author->getFullName())); |
||
314 | $this->insertBreadcrumbs(array( |
||
315 | Widget::Anchor(__('Authors'), SYMPHONY_URL . '/system/authors/'), |
||
316 | )); |
||
317 | |||
318 | // Essentials |
||
319 | $group = new XMLElement('fieldset'); |
||
320 | $group->setAttribute('class', 'settings'); |
||
321 | $group->appendChild(new XMLElement('legend', __('Essentials'))); |
||
322 | |||
323 | $div = new XMLElement('div'); |
||
324 | $div->setAttribute('class', 'two columns'); |
||
325 | |||
326 | $label = Widget::Label(__('First Name'), null, 'column'); |
||
327 | $label->appendChild(Widget::Input('fields[first_name]', $author->get('first_name'))); |
||
328 | $div->appendChild((isset($this->_errors['first_name']) ? Widget::Error($label, $this->_errors['first_name']) : $label)); |
||
329 | |||
330 | |||
331 | $label = Widget::Label(__('Last Name'), null, 'column'); |
||
332 | $label->appendChild(Widget::Input('fields[last_name]', $author->get('last_name'))); |
||
333 | $div->appendChild((isset($this->_errors['last_name']) ? Widget::Error($label, $this->_errors['last_name']) : $label)); |
||
334 | |||
335 | $group->appendChild($div); |
||
336 | |||
337 | $label = Widget::Label(__('Email Address')); |
||
338 | $label->appendChild(Widget::Input('fields[email]', $author->get('email'), 'text', array('autocomplete' => 'off'))); |
||
339 | $group->appendChild((isset($this->_errors['email']) ? Widget::Error($label, $this->_errors['email']) : $label)); |
||
340 | |||
341 | $this->Form->appendChild($group); |
||
342 | |||
343 | // Login Details |
||
344 | $group = new XMLElement('fieldset'); |
||
345 | $group->setAttribute('class', 'settings'); |
||
346 | $group->appendChild(new XMLElement('legend', __('Login Details'))); |
||
347 | |||
348 | $div = new XMLElement('div'); |
||
349 | |||
350 | $label = Widget::Label(__('Username')); |
||
351 | $label->appendChild(Widget::Input('fields[username]', $author->get('username'), 'text', array('autocomplete' => 'off'))); |
||
352 | $div->appendChild((isset($this->_errors['username']) ? Widget::Error($label, $this->_errors['username']) : $label)); |
||
353 | |||
354 | // Only developers can change the user type. Primary account should NOT be able to change this |
||
355 | if ((Symphony::Author()->isDeveloper() || Symphony::Author()->isManager()) && !$author->isPrimaryAccount()) { |
||
356 | |||
357 | // Create columns |
||
358 | $div->setAttribute('class', 'two columns'); |
||
359 | $label->setAttribute('class', 'column'); |
||
360 | |||
361 | // User type |
||
362 | $label = Widget::Label(__('User Type'), null, 'column'); |
||
363 | |||
364 | $options = array( |
||
365 | array('author', false, __('Author')), |
||
366 | array('manager', $author->isManager(), __('Manager')) |
||
367 | ); |
||
368 | |||
369 | if (Symphony::Author()->isDeveloper()) { |
||
370 | $options[] = array('developer', $author->isDeveloper(), __('Developer')); |
||
371 | } |
||
372 | |||
373 | $label->appendChild(Widget::Select('fields[user_type]', $options)); |
||
374 | $div->appendChild($label); |
||
375 | } |
||
376 | |||
377 | $group->appendChild($div); |
||
378 | |||
379 | // Password |
||
380 | $fieldset = new XMLElement('fieldset', null, array('class' => 'two columns', 'id' => 'password')); |
||
381 | $legend = new XMLElement('legend', __('Password')); |
||
382 | $help = new XMLElement('i', __('Leave password fields blank to keep the current password')); |
||
383 | $fieldset->appendChild($legend); |
||
384 | $fieldset->appendChild($help); |
||
385 | |||
386 | /* |
||
387 | Password reset rules: |
||
388 | - Primary account can edit all accounts. |
||
389 | - Developers can edit all developers, managers and authors, and their own. |
||
390 | - Managers can edit all Authors, and their own. |
||
391 | - Authors can edit their own. |
||
392 | */ |
||
393 | if ($isEditing && !( |
||
394 | // All accounts can edit their own |
||
395 | $isOwner |
||
396 | // Managers can edit all Authors, and their own. |
||
397 | || (Symphony::Author()->isManager() && $author->isAuthor()) |
||
398 | // Primary account can edit all accounts. |
||
399 | || Symphony::Author()->isPrimaryAccount() |
||
400 | // Developers can edit all developers, managers and authors, and their own. |
||
401 | || Symphony::Author()->isDeveloper() && $author->isPrimaryAccount() === false |
||
402 | )) { |
||
403 | $fieldset->setAttribute('class', 'three columns'); |
||
404 | |||
405 | $label = Widget::Label(null, null, 'column'); |
||
406 | $label->appendChild(Widget::Input('fields[old-password]', null, 'password', array('placeholder' => __('Old Password'), 'autocomplete' => 'off'))); |
||
407 | $fieldset->appendChild((isset($this->_errors['old-password']) ? Widget::Error($label, $this->_errors['old-password']) : $label)); |
||
408 | } |
||
409 | |||
410 | // New password |
||
411 | $placeholder = ($isEditing ? __('New Password') : __('Password')); |
||
412 | $label = Widget::Label(null, null, 'column'); |
||
413 | $label->appendChild(Widget::Input('fields[password]', null, 'password', array('placeholder' => $placeholder, 'autocomplete' => 'off'))); |
||
414 | $fieldset->appendChild((isset($this->_errors['password']) ? Widget::Error($label, $this->_errors['password']) : $label)); |
||
415 | |||
416 | // Confirm password |
||
417 | $label = Widget::Label(null, null, 'column'); |
||
418 | $label->appendChild(Widget::Input('fields[password-confirmation]', null, 'password', array('placeholder' => __('Confirm Password'), 'autocomplete' => 'off'))); |
||
419 | $fieldset->appendChild((isset($this->_errors['password-confirmation']) ? Widget::Error($label, $this->_errors['password']) : $label)); |
||
420 | |||
421 | $group->appendChild($fieldset); |
||
422 | |||
423 | // Auth token |
||
424 | if (Symphony::Author()->isDeveloper() || Symphony::Author()->isManager()) { |
||
425 | $label = Widget::Label(); |
||
426 | $group->appendChild(Widget::Input('fields[auth_token_active]', 'no', 'hidden')); |
||
427 | $input = Widget::Input('fields[auth_token_active]', 'yes', 'checkbox'); |
||
428 | |||
429 | if ($author->isTokenActive()) { |
||
430 | $input->setAttribute('checked', 'checked'); |
||
431 | } |
||
432 | |||
433 | $temp = SYMPHONY_URL . '/login/' . $author->createAuthToken() . '/'; |
||
434 | $label->setValue(__('%s Allow remote login via', array($input->generate())) . ' <a href="' . $temp . '">' . $temp . '</a>'); |
||
435 | $group->appendChild($label); |
||
436 | } |
||
437 | |||
438 | $label = Widget::Label(__('Default Area')); |
||
439 | |||
440 | $sections = SectionManager::fetch(null, 'ASC', 'sortorder'); |
||
441 | |||
442 | $options = array(); |
||
443 | |||
444 | // If the Author is the Developer, allow them to set the Default Area to |
||
445 | // be the Sections Index. |
||
446 | if ($author->isDeveloper()) { |
||
447 | $options[] = array('/blueprints/sections/', $author->get('default_area') === '/blueprints/sections/', __('Sections Index')); |
||
448 | } |
||
449 | |||
450 | if (is_array($sections) && !empty($sections)) { |
||
451 | foreach ($sections as $s) { |
||
452 | $options[] = array($s->get('id'), $author->get('default_area') === $s->get('id'), $s->get('name')); |
||
453 | } |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Allows injection or manipulation of the Default Area dropdown for an Author. |
||
458 | * Take care with adding in options that are only valid for Developers, as if a |
||
459 | * normal Author is set to that option, they will be redirected to their own |
||
460 | * Author record. |
||
461 | * |
||
462 | * |
||
463 | * @delegate AddDefaultAuthorAreas |
||
464 | * @since Symphony 2.2 |
||
465 | * @param string $context |
||
466 | * '/system/authors/' |
||
467 | * @param array $options |
||
468 | * An associative array of options, suitable for use for the Widget::Select |
||
469 | * function. By default this will be an array of the Sections in the current |
||
470 | * installation. New options should be the path to the page after the `SYMPHONY_URL` |
||
471 | * constant. |
||
472 | * @param string $default_area |
||
473 | * The current `default_area` for this Author. |
||
474 | */ |
||
475 | Symphony::ExtensionManager()->notifyMembers('AddDefaultAuthorAreas', '/system/authors/', array( |
||
476 | 'options' => &$options, |
||
477 | 'default_area' => $author->get('default_area') |
||
478 | )); |
||
479 | |||
480 | $label->appendChild(Widget::Select('fields[default_area]', $options)); |
||
481 | $group->appendChild($label); |
||
482 | |||
483 | $this->Form->appendChild($group); |
||
484 | |||
485 | // Custom Language Selection |
||
486 | $languages = Lang::getAvailableLanguages(); |
||
487 | if (count($languages) > 1) { |
||
488 | // Get language names |
||
489 | asort($languages); |
||
490 | |||
491 | $group = new XMLElement('fieldset'); |
||
492 | $group->setAttribute('class', 'settings'); |
||
493 | $group->appendChild(new XMLElement('legend', __('Custom Preferences'))); |
||
494 | |||
495 | $label = Widget::Label(__('Language')); |
||
496 | |||
497 | $options = array( |
||
498 | array(null, is_null($author->get('language')), __('System Default')) |
||
499 | ); |
||
500 | |||
501 | foreach ($languages as $code => $name) { |
||
502 | $options[] = array($code, $code === $author->get('language'), $name); |
||
503 | } |
||
504 | $select = Widget::Select('fields[language]', $options); |
||
505 | $label->appendChild($select); |
||
506 | $group->appendChild($label); |
||
507 | |||
508 | $this->Form->appendChild($group); |
||
509 | } |
||
510 | |||
511 | // Administration password double check |
||
512 | if ($isEditing && !$isOwner) { |
||
513 | $group = new XMLElement('fieldset'); |
||
514 | $group->setAttribute('class', 'settings'); |
||
515 | $group->setAttribute('id', 'confirmation'); |
||
516 | $group->appendChild(new XMLElement('legend', __('Confirmation'))); |
||
517 | $group->appendChild(new XMLELement('p', __('Please confirm changes to this author with your password.'), array('class' => 'help'))); |
||
518 | |||
519 | $label = Widget::Label(__('Password')); |
||
520 | $label->appendChild(Widget::Input('fields[confirm-change-password]', null, 'password', array( |
||
521 | 'autocomplete' => 'off', |
||
522 | 'placeholder' => __('Your Password') |
||
523 | ))); |
||
524 | $group->appendChild( |
||
525 | isset($this->_errors['confirm-change-password']) ? Widget::Error($label, $this->_errors['confirm-change-password']) : $label |
||
526 | ); |
||
527 | |||
528 | $this->Form->appendChild($group); |
||
529 | } |
||
530 | |||
531 | // Actions |
||
532 | $div = new XMLElement('div'); |
||
533 | $div->setAttribute('class', 'actions'); |
||
534 | |||
535 | $div->appendChild(Widget::Input('action[save]', ($this->_context['action'] === 'edit' ? __('Save Changes') : __('Create Author')), 'submit', array('accesskey' => 's'))); |
||
536 | |||
537 | if ($isEditing && !$isOwner && !$author->isPrimaryAccount()) { |
||
538 | $button = new XMLElement('button', __('Delete')); |
||
539 | $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this author'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this author?'))); |
||
540 | $div->appendChild($button); |
||
541 | } |
||
542 | |||
543 | $this->Form->appendChild($div); |
||
544 | |||
545 | /** |
||
546 | * Allows the injection of custom form fields given the current `$this->Form` |
||
547 | * object. Please note that this custom data should be saved in own extension |
||
548 | * tables and that modifying `tbl_authors` to house your data is highly discouraged. |
||
549 | * |
||
550 | * @delegate AddElementstoAuthorForm |
||
551 | * @since Symphony 2.2 |
||
552 | * @param string $context |
||
553 | * '/system/authors/' |
||
554 | * @param XMLElement $form |
||
555 | * The contents of `$this->Form` after all the default form elements have been appended. |
||
556 | * @param Author $author |
||
557 | * The current Author object that is being edited |
||
558 | */ |
||
559 | Symphony::ExtensionManager()->notifyMembers('AddElementstoAuthorForm', '/system/authors/', array( |
||
560 | 'form' => &$this->Form, |
||
561 | 'author' => $author |
||
562 | )); |
||
563 | } |
||
564 | |||
775 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.