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