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