Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AssetAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AssetAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class AssetAdmin extends LeftAndMain implements PermissionProvider |
||
| 48 | { |
||
| 49 | private static $url_segment = 'assets'; |
||
| 50 | |||
| 51 | private static $url_rule = '/$Action/$ID'; |
||
| 52 | |||
| 53 | private static $menu_title = 'Files'; |
||
| 54 | |||
| 55 | private static $tree_class = 'SilverStripe\\Assets\\Folder'; |
||
| 56 | |||
| 57 | private static $url_handlers = [ |
||
| 58 | // Legacy redirect for SS3-style detail view |
||
| 59 | 'EditForm/field/File/item/$FileID/$Action' => 'legacyRedirectForEditView', |
||
| 60 | // Pass all URLs to the index, for React to unpack |
||
| 61 | 'show/$FolderID/edit/$FileID' => 'index', |
||
| 62 | // API access points with structured data |
||
| 63 | 'POST api/createFolder' => 'apiCreateFolder', |
||
| 64 | 'POST api/createFile' => 'apiCreateFile', |
||
| 65 | 'GET api/readFolder' => 'apiReadFolder', |
||
| 66 | 'PUT api/updateFolder' => 'apiUpdateFolder', |
||
| 67 | 'DELETE api/delete' => 'apiDelete', |
||
| 68 | 'GET api/search' => 'apiSearch', |
||
| 69 | 'GET api/history' => 'apiHistory' |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Amount of results showing on a single page. |
||
| 74 | * |
||
| 75 | * @config |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private static $page_length = 15; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @config |
||
| 82 | * @see Upload->allowedMaxFileSize |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | private static $allowed_max_file_size; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @config |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | private static $max_history_entries = 100; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $allowed_actions = array( |
||
| 98 | 'legacyRedirectForEditView', |
||
| 99 | 'apiCreateFolder', |
||
| 100 | 'apiCreateFile', |
||
| 101 | 'apiReadFolder', |
||
| 102 | 'apiUpdateFolder', |
||
| 103 | 'apiHistory', |
||
| 104 | 'apiDelete', |
||
| 105 | 'apiSearch', |
||
| 106 | 'fileEditForm', |
||
| 107 | 'fileHistoryForm', |
||
| 108 | 'addToCampaignForm', |
||
| 109 | 'fileInsertForm', |
||
| 110 | 'schema', |
||
| 111 | ); |
||
| 112 | |||
| 113 | private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin'; |
||
| 114 | |||
| 115 | private static $thumbnail_width = 400; |
||
| 116 | |||
| 117 | private static $thumbnail_height = 300; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Set up the controller |
||
| 121 | */ |
||
| 122 | public function init() |
||
| 136 | |||
| 137 | public function getClientConfig() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Fetches a collection of files by ParentID. |
||
| 197 | * |
||
| 198 | * @param HTTPRequest $request |
||
| 199 | * @return HTTPResponse |
||
| 200 | */ |
||
| 201 | public function apiReadFolder(HTTPRequest $request) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param HTTPRequest $request |
||
| 270 | * |
||
| 271 | * @return HTTPResponse |
||
| 272 | */ |
||
| 273 | public function apiSearch(HTTPRequest $request) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param HTTPRequest $request |
||
| 293 | * |
||
| 294 | * @return HTTPResponse |
||
| 295 | */ |
||
| 296 | public function apiDelete(HTTPRequest $request) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Creates a single file based on a form-urlencoded upload. |
||
| 337 | * |
||
| 338 | * @param HTTPRequest $request |
||
| 339 | * @return HTTPRequest|HTTPResponse |
||
| 340 | */ |
||
| 341 | public function apiCreateFile(HTTPRequest $request) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Returns a JSON array for history of a given file ID. Returns a list of all the history. |
||
| 416 | * |
||
| 417 | * @param HTTPRequest $request |
||
| 418 | * @return HTTPResponse |
||
| 419 | */ |
||
| 420 | public function apiHistory(HTTPRequest $request) |
||
| 421 | { |
||
| 422 | // CSRF check not required as the GET request has no side effects. |
||
| 423 | $fileId = $request->getVar('fileId'); |
||
| 424 | |||
| 425 | if (!$fileId || !is_numeric($fileId)) { |
||
| 426 | return new HTTPResponse(null, 400); |
||
| 427 | } |
||
| 428 | |||
| 429 | $class = File::class; |
||
| 430 | $file = DataObject::get($class)->byID($fileId); |
||
| 431 | |||
| 432 | if (!$file) { |
||
| 433 | return new HTTPResponse(null, 404); |
||
| 434 | } |
||
| 435 | |||
| 436 | if (!$file->canView()) { |
||
| 437 | return new HTTPResponse(null, 403); |
||
| 438 | } |
||
| 439 | |||
| 440 | $versions = Versioned::get_all_versions($class, $fileId) |
||
| 441 | ->limit($this->config()->max_history_entries) |
||
| 442 | ->sort('Version', 'DESC'); |
||
| 443 | |||
| 444 | $output = array(); |
||
| 445 | $next = array(); |
||
| 446 | $prev = null; |
||
| 447 | |||
| 448 | // swap the order so we can get the version number to compare against. |
||
| 449 | // i.e version 3 needs to know version 2 is the previous version |
||
| 450 | $copy = $versions->map('Version', 'Version')->toArray(); |
||
| 451 | foreach (array_reverse($copy) as $k => $v) { |
||
| 452 | if ($prev) { |
||
| 453 | $next[$v] = $prev; |
||
| 454 | } |
||
| 455 | |||
| 456 | $prev = $v; |
||
| 457 | } |
||
| 458 | |||
| 459 | $_cachedMembers = array(); |
||
| 460 | |||
| 461 | /** @var File $version */ |
||
| 462 | foreach ($versions as $version) { |
||
| 463 | $author = null; |
||
| 464 | |||
| 465 | if ($version->AuthorID) { |
||
| 466 | if (!isset($_cachedMembers[$version->AuthorID])) { |
||
| 467 | $_cachedMembers[$version->AuthorID] = DataObject::get(Member::class) |
||
| 468 | ->byID($version->AuthorID); |
||
| 469 | } |
||
| 470 | |||
| 471 | $author = $_cachedMembers[$version->AuthorID]; |
||
| 472 | } |
||
| 473 | |||
| 474 | if ($version->canView()) { |
||
| 475 | $published = true; |
||
| 476 | |||
| 477 | if (isset($next[$version->Version])) { |
||
| 478 | $summary = $version->humanizedChanges( |
||
| 479 | $version->Version, |
||
| 480 | $next[$version->Version] |
||
| 481 | ); |
||
| 482 | |||
| 483 | // if no summary returned by humanizedChanges, i.e we cannot work out what changed, just show a |
||
| 484 | // generic message |
||
| 485 | if (!$summary) { |
||
| 486 | $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.SAVEDFILE', "Saved file"); |
||
| 487 | } |
||
| 488 | } else { |
||
| 489 | $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file"); |
||
| 490 | } |
||
| 491 | |||
| 492 | $output[] = array( |
||
| 493 | 'versionid' => $version->Version, |
||
| 494 | 'date_ago' => $version->dbObject('LastEdited')->Ago(), |
||
| 495 | 'date_formatted' => $version->dbObject('LastEdited')->Nice(), |
||
| 496 | 'status' => ($version->WasPublished) ? _t('File.PUBLISHED', 'Published') : '', |
||
| 497 | 'author' => ($author) |
||
| 498 | ? $author->Name |
||
| 499 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNKNOWN', "Unknown"), |
||
| 500 | 'summary' => ($summary) |
||
| 501 | ? $summary |
||
| 502 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.NOSUMMARY', "No summary available") |
||
| 503 | ); |
||
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | return |
||
| 508 | (new HTTPResponse(json_encode($output)))->addHeader('Content-Type', 'application/json'); |
||
| 509 | } |
||
| 510 | |||
| 511 | |||
| 512 | /** |
||
| 513 | * Creates a single folder, within an optional parent folder. |
||
| 514 | * |
||
| 515 | * @param HTTPRequest $request |
||
| 516 | * @return HTTPRequest|HTTPResponse |
||
| 517 | */ |
||
| 518 | public function apiCreateFolder(HTTPRequest $request) |
||
| 519 | { |
||
| 520 | $data = $request->postVars(); |
||
| 521 | |||
| 522 | $class = 'SilverStripe\\Assets\\Folder'; |
||
| 523 | |||
| 524 | // CSRF check |
||
| 525 | $token = SecurityToken::inst(); |
||
| 526 | View Code Duplication | if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { |
|
| 527 | return new HTTPResponse(null, 400); |
||
| 528 | } |
||
| 529 | |||
| 530 | // check addchildren permissions |
||
| 531 | /** @var Folder $parentRecord */ |
||
| 532 | $parentRecord = null; |
||
| 533 | if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { |
||
| 534 | $parentRecord = DataObject::get_by_id($class, $data['ParentID']); |
||
| 535 | } |
||
| 536 | $data['Parent'] = $parentRecord; |
||
| 537 | $data['ParentID'] = $parentRecord ? (int)$parentRecord->ID : 0; |
||
| 538 | |||
| 539 | // Build filename |
||
| 540 | $baseFilename = isset($data['Name']) |
||
| 541 | ? basename($data['Name']) |
||
| 542 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.NEWFOLDER', "NewFolder"); |
||
| 543 | |||
| 544 | if ($parentRecord && $parentRecord->ID) { |
||
| 545 | $baseFilename = $parentRecord->getFilename() . '/' . $baseFilename; |
||
| 546 | } |
||
| 547 | |||
| 548 | // Ensure name is unique |
||
| 549 | $nameGenerator = $this->getNameGenerator($baseFilename); |
||
| 550 | $filename = null; |
||
| 551 | foreach ($nameGenerator as $filename) { |
||
| 552 | if (! File::find($filename)) { |
||
| 553 | break; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | $data['Name'] = basename($filename); |
||
| 557 | |||
| 558 | // Create record |
||
| 559 | /** @var Folder $record */ |
||
| 560 | $record = Injector::inst()->create($class); |
||
| 561 | |||
| 562 | // check create permissions |
||
| 563 | if (!$record->canCreate(null, $data)) { |
||
| 564 | return (new HTTPResponse(null, 403)) |
||
| 565 | ->addHeader('Content-Type', 'application/json'); |
||
| 566 | } |
||
| 567 | |||
| 568 | $record->ParentID = $data['ParentID']; |
||
| 569 | $record->Name = $record->Title = basename($data['Name']); |
||
| 570 | $record->write(); |
||
| 571 | |||
| 572 | $result = $this->getObjectFromData($record); |
||
| 573 | |||
| 574 | return (new HTTPResponse(json_encode($result)))->addHeader('Content-Type', 'application/json'); |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Redirects 3.x style detail links to new 4.x style routing. |
||
| 579 | * |
||
| 580 | * @param HTTPRequest $request |
||
| 581 | */ |
||
| 582 | public function legacyRedirectForEditView($request) |
||
| 583 | { |
||
| 584 | $fileID = $request->param('FileID'); |
||
| 585 | /** @var File $file */ |
||
| 586 | $file = File::get()->byID($fileID); |
||
| 587 | $link = $this->getFileEditLink($file) ?: $this->Link(); |
||
| 588 | $this->redirect($link); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Given a file return the CMS link to edit it |
||
| 593 | * |
||
| 594 | * @param File $file |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function getFileEditLink($file) |
||
| 598 | { |
||
| 599 | if (!$file || !$file->isInDB()) { |
||
| 600 | return null; |
||
| 601 | } |
||
| 602 | |||
| 603 | return Controller::join_links( |
||
| 604 | $this->Link('show'), |
||
| 605 | $file->ParentID, |
||
| 606 | 'edit', |
||
| 607 | $file->ID |
||
| 608 | ); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Get the search context from {@link File}, used to create the search form |
||
| 613 | * as well as power the /search API endpoint. |
||
| 614 | * |
||
| 615 | * @return SearchContext |
||
| 616 | */ |
||
| 617 | public function getSearchContext() |
||
| 618 | { |
||
| 619 | $context = File::singleton()->getDefaultSearchContext(); |
||
| 620 | |||
| 621 | // Customize fields |
||
| 622 | $dateHeader = HeaderField::create('Date', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4); |
||
| 623 | $dateFrom = DateField::create('CreatedFrom', _t('CMSSearch.FILTERDATEFROM', 'From')) |
||
| 624 | ->setConfig('showcalendar', true); |
||
| 625 | $dateTo = DateField::create('CreatedTo', _t('CMSSearch.FILTERDATETO', 'To')) |
||
| 626 | ->setConfig('showcalendar', true); |
||
| 627 | $dateGroup = FieldGroup::create( |
||
| 628 | $dateHeader, |
||
| 629 | $dateFrom, |
||
| 630 | $dateTo |
||
| 631 | ); |
||
| 632 | $context->addField($dateGroup); |
||
| 633 | /** @skipUpgrade */ |
||
| 634 | $appCategories = array( |
||
| 635 | 'archive' => _t( |
||
| 636 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryArchive', |
||
| 637 | 'Archive' |
||
| 638 | ), |
||
| 639 | 'audio' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryAudio', 'Audio'), |
||
| 640 | 'document' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryDocument', 'Document'), |
||
| 641 | 'flash' => _t( |
||
| 642 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryFlash', |
||
| 643 | 'Flash', |
||
| 644 | 'The fileformat' |
||
| 645 | ), |
||
| 646 | 'image' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryImage', 'Image'), |
||
| 647 | 'video' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryVideo', 'Video'), |
||
| 648 | ); |
||
| 649 | $context->addField( |
||
| 650 | $typeDropdown = new DropdownField( |
||
| 651 | 'AppCategory', |
||
| 652 | _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Filetype', 'File type'), |
||
| 653 | $appCategories |
||
| 654 | ) |
||
| 655 | ); |
||
| 656 | |||
| 657 | $typeDropdown->setEmptyString(' '); |
||
| 658 | |||
| 659 | $currentfolderLabel = _t( |
||
| 660 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CurrentFolderOnly', |
||
| 661 | 'Limit to current folder?' |
||
| 662 | ); |
||
| 663 | $context->addField( |
||
| 664 | new CheckboxField('CurrentFolderOnly', $currentfolderLabel) |
||
| 665 | ); |
||
| 666 | $context->getFields()->removeByName('Title'); |
||
| 667 | |||
| 668 | return $context; |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Get an asset renamer for the given filename. |
||
| 673 | * |
||
| 674 | * @param string $filename Path name |
||
| 675 | * @return AssetNameGenerator |
||
| 676 | */ |
||
| 677 | protected function getNameGenerator($filename) |
||
| 678 | { |
||
| 679 | return Injector::inst() |
||
| 680 | ->createWithArgs('AssetNameGenerator', array($filename)); |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @todo Implement on client |
||
| 685 | * |
||
| 686 | * @param bool $unlinked |
||
| 687 | * @return ArrayList |
||
| 688 | */ |
||
| 689 | public function breadcrumbs($unlinked = false) |
||
| 690 | { |
||
| 691 | return null; |
||
| 692 | } |
||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * Don't include class namespace in auto-generated CSS class |
||
| 697 | */ |
||
| 698 | public function baseCSSClasses() |
||
| 699 | { |
||
| 700 | return 'AssetAdmin LeftAndMain'; |
||
| 701 | } |
||
| 702 | |||
| 703 | public function providePermissions() |
||
| 704 | { |
||
| 705 | return array( |
||
| 706 | "CMS_ACCESS_AssetAdmin" => array( |
||
| 707 | 'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array( |
||
| 708 | 'title' => static::menu_title() |
||
| 709 | )), |
||
| 710 | 'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access') |
||
| 711 | ) |
||
| 712 | ); |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Build a form scaffolder for this model |
||
| 717 | * |
||
| 718 | * NOTE: Volatile api. May be moved to {@see LeftAndMain} |
||
| 719 | * |
||
| 720 | * @param File $file |
||
| 721 | * @return FormFactory |
||
| 722 | */ |
||
| 723 | public function getFormFactory(File $file) |
||
| 724 | { |
||
| 725 | // Get service name based on file class |
||
| 726 | $name = null; |
||
| 727 | if ($file instanceof Folder) { |
||
| 728 | $name = FolderFormFactory::class; |
||
| 729 | } elseif ($file instanceof Image) { |
||
| 730 | $name = ImageFormFactory::class; |
||
| 731 | } else { |
||
| 732 | $name = FileFormFactory::class; |
||
| 733 | } |
||
| 734 | return Injector::inst()->get($name); |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * The form is used to generate a form schema, |
||
| 739 | * as well as an intermediary object to process data through API endpoints. |
||
| 740 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
| 741 | * It handles both {@link File} and {@link Folder} records. |
||
| 742 | * |
||
| 743 | * @param int $id |
||
| 744 | * @return Form |
||
| 745 | */ |
||
| 746 | public function getFileEditForm($id) |
||
| 747 | { |
||
| 748 | /** @var File $file */ |
||
| 749 | $file = $this->getList()->byID($id); |
||
| 750 | |||
| 751 | View Code Duplication | if (!$file->canView()) { |
|
| 752 | $this->httpError(403, _t( |
||
| 753 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
| 754 | 'You don\'t have the necessary permissions to modify {ObjectTitle}', |
||
| 755 | '', |
||
| 756 | ['ObjectTitle' => $file->i18n_singular_name()] |
||
| 757 | )); |
||
| 758 | return null; |
||
| 759 | } |
||
| 760 | |||
| 761 | $scaffolder = $this->getFormFactory($file); |
||
| 762 | $form = $scaffolder->getForm($this, 'fileEditForm', [ |
||
| 763 | 'Record' => $file |
||
| 764 | ]); |
||
| 765 | |||
| 766 | // Configure form to respond to validation errors with form schema |
||
| 767 | // if requested via react. |
||
| 768 | View Code Duplication | $form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id) { |
|
| 769 | $schemaId = Controller::join_links($this->Link('schema/fileEditForm'), $id); |
||
| 770 | return $this->getSchemaResponse($schemaId, $form, $errors); |
||
| 771 | }); |
||
| 772 | |||
| 773 | return $form; |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Get file edit form |
||
| 778 | * |
||
| 779 | * @return Form |
||
| 780 | */ |
||
| 781 | View Code Duplication | public function fileEditForm() |
|
| 782 | { |
||
| 783 | // Get ID either from posted back value, or url parameter |
||
| 784 | $request = $this->getRequest(); |
||
| 785 | $id = $request->param('ID') ?: $request->postVar('ID'); |
||
| 786 | return $this->getFileEditForm($id); |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * The form is used to generate a form schema, |
||
| 791 | * as well as an intermediary object to process data through API endpoints. |
||
| 792 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
| 793 | * It handles both {@link File} and {@link Folder} records. |
||
| 794 | * |
||
| 795 | * @param int $id |
||
| 796 | * @return Form |
||
| 797 | */ |
||
| 798 | public function getFileInsertForm($id) |
||
| 799 | { |
||
| 800 | /** @var File $file */ |
||
| 801 | $file = $this->getList()->byID($id); |
||
| 802 | |||
| 803 | View Code Duplication | if (!$file->canView()) { |
|
| 804 | $this->httpError(403, _t( |
||
| 805 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
| 806 | 'You don\'t have the necessary permissions to modify {ObjectTitle}', |
||
| 807 | '', |
||
| 808 | ['ObjectTitle' => $file->i18n_singular_name()] |
||
| 809 | )); |
||
| 810 | return null; |
||
| 811 | } |
||
| 812 | |||
| 813 | $scaffolder = $this->getFormFactory($file); |
||
| 814 | $form = $scaffolder->getForm($this, 'fileInsertForm', [ |
||
| 815 | 'Record' => $file, |
||
| 816 | 'Type' => 'insert', |
||
| 817 | ]); |
||
| 818 | |||
| 819 | return $form; |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Get file insert form |
||
| 824 | * |
||
| 825 | * @return Form |
||
| 826 | */ |
||
| 827 | View Code Duplication | public function fileInsertForm() |
|
| 828 | { |
||
| 829 | // Get ID either from posted back value, or url parameter |
||
| 830 | $request = $this->getRequest(); |
||
| 831 | $id = $request->param('ID') ?: $request->postVar('ID'); |
||
| 832 | return $this->getFileInsertForm($id); |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param array $context |
||
| 837 | * @return Form |
||
| 838 | * @throws InvalidArgumentException |
||
| 839 | */ |
||
| 840 | public function getFileHistoryForm($context) |
||
| 841 | { |
||
| 842 | // Check context |
||
| 843 | if (!isset($context['RecordID']) || !isset($context['RecordVersion'])) { |
||
| 844 | throw new InvalidArgumentException("Missing RecordID / RecordVersion for this form"); |
||
| 845 | } |
||
| 846 | $id = $context['RecordID']; |
||
| 847 | $versionId = $context['RecordVersion']; |
||
| 848 | if (!$id || !$versionId) { |
||
| 849 | return $this->httpError(404); |
||
| 850 | } |
||
| 851 | |||
| 852 | /** @var File $file */ |
||
| 853 | $file = Versioned::get_version(File::class, $id, $versionId); |
||
| 854 | if (!$file) { |
||
| 855 | return $this->httpError(404); |
||
| 856 | } |
||
| 857 | |||
| 858 | View Code Duplication | if (!$file->canView()) { |
|
| 859 | $this->httpError(403, _t( |
||
| 860 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
| 861 | 'You don\'t have the necessary permissions to modify {ObjectTitle}', |
||
| 862 | '', |
||
| 863 | ['ObjectTitle' => $file->i18n_singular_name()] |
||
| 864 | )); |
||
| 865 | return null; |
||
| 866 | } |
||
| 867 | |||
| 868 | $effectiveContext = array_merge($context, ['Record' => $file]); |
||
| 869 | /** @var FormFactory $scaffolder */ |
||
| 870 | $scaffolder = Injector::inst()->get(FileHistoryFormFactory::class); |
||
| 871 | $form = $scaffolder->getForm($this, 'fileHistoryForm', $effectiveContext); |
||
| 872 | |||
| 873 | // Configure form to respond to validation errors with form schema |
||
| 874 | // if requested via react. |
||
| 875 | View Code Duplication | $form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id, $versionId) { |
|
| 876 | $schemaId = Controller::join_links($this->Link('schema/fileHistoryForm'), $id, $versionId); |
||
| 877 | return $this->getSchemaResponse($schemaId, $form, $errors); |
||
| 878 | }); |
||
| 879 | |||
| 880 | return $form; |
||
| 881 | } |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Gets a JSON schema representing the current edit form. |
||
| 885 | * |
||
| 886 | * WARNING: Experimental API. |
||
| 887 | * |
||
| 888 | * @param HTTPRequest $request |
||
| 889 | * @return HTTPResponse |
||
| 890 | */ |
||
| 891 | public function schema($request) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Get file history form |
||
| 917 | * |
||
| 918 | * @return Form |
||
| 919 | */ |
||
| 920 | public function fileHistoryForm() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @param array $data |
||
| 934 | * @param Form $form |
||
| 935 | * @return HTTPResponse |
||
| 936 | */ |
||
| 937 | public function save($data, $form) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @param array $data |
||
| 944 | * @param Form $form |
||
| 945 | * @return HTTPResponse |
||
| 946 | */ |
||
| 947 | public function publish($data, $form) |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Update thisrecord |
||
| 954 | * |
||
| 955 | * @param array $data |
||
| 956 | * @param Form $form |
||
| 957 | * @param bool $doPublish |
||
| 958 | * @return HTTPResponse |
||
| 959 | */ |
||
| 960 | protected function saveOrPublish($data, $form, $doPublish = false) |
||
| 961 | { |
||
| 962 | View Code Duplication | if (!isset($data['ID']) || !is_numeric($data['ID'])) { |
|
| 963 | return (new HTTPResponse(json_encode(['status' => 'error']), 400)) |
||
| 964 | ->addHeader('Content-Type', 'application/json'); |
||
| 965 | } |
||
| 966 | |||
| 992 | |||
| 993 | public function unpublish($data, $form) |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * @param File $file |
||
| 1020 | * |
||
| 1021 | * @return array |
||
| 1022 | */ |
||
| 1023 | public function getObjectFromData(File $file) |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Returns the files and subfolders contained in the currently selected folder, |
||
| 1094 | * defaulting to the root node. Doubles as search results, if any search parameters |
||
| 1095 | * are set through {@link SearchForm()}. |
||
| 1096 | * |
||
| 1097 | * @param array $params Unsanitised request parameters |
||
| 1098 | * @return DataList |
||
| 1099 | */ |
||
| 1100 | protected function getList($params = array()) |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Action handler for adding pages to a campaign |
||
| 1164 | * |
||
| 1165 | * @param array $data |
||
| 1166 | * @param Form $form |
||
| 1167 | * @return DBHTMLText|HTTPResponse |
||
| 1168 | */ |
||
| 1169 | public function addtocampaign($data, $form) |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Url handler for add to campaign form |
||
| 1188 | * |
||
| 1189 | * @param HTTPRequest $request |
||
| 1190 | * @return Form |
||
| 1191 | */ |
||
| 1192 | public function addToCampaignForm($request) |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * @param int $id |
||
| 1201 | * @return Form |
||
| 1202 | */ |
||
| 1203 | public function getAddToCampaignForm($id) |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * @return Upload |
||
| 1240 | */ |
||
| 1241 | protected function getUpload() |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Get response for successfully updated record |
||
| 1254 | * |
||
| 1255 | * @param File $record |
||
| 1256 | * @param Form $form |
||
| 1257 | * @return HTTPResponse |
||
| 1258 | */ |
||
| 1259 | protected function getRecordUpdatedResponse($record, $form) |
||
| 1266 | } |
||
| 1267 |
This check marks private properties in classes that are never used. Those properties can be removed.