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