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