diff --git a/src/Cms/DMSDocumentAddController.php b/src/Cms/DMSDocumentAddController.php index 1aced8a..d1aaff6 100644 --- a/src/Cms/DMSDocumentAddController.php +++ b/src/Cms/DMSDocumentAddController.php @@ -23,6 +23,7 @@ use SilverStripe\Security\Permission; use SilverStripe\Security\Security; use SilverStripe\Admin\LeftAndMain; +use Sunnysideup\DMS\Admin\DMSDocumentAdmin; /** * @package dms @@ -86,95 +87,96 @@ public function getCurrentDocumentSet() if ($id = $this->getRequest()->getVar('dsid')) { return DMSDocumentSet::get()->byId($id); } + return singleton(DMSDocumentSet::class); } /** * @return Form - * @todo what template is used here? AssetAdmin_UploadContent.ss doesn't seem to be used anymore */ public function getEditForm($id = null, $fields = null) { - - /** - * ### @@@@ START REPLACEMENT @@@@ ### - * WHY: upgrade to SS4 - * OLD: FRAMEWORK_DIR (ignore case) - * NEW: SilverStripe\Core\Manifest\ModuleLoader::getModule('silverstripe/framework')->getResource('UPGRADE-FIX-REQUIRED.php')->getRelativePath() (COMPLEX) - * EXP: Please review update and fix as required - * ### @@@@ STOP REPLACEMENT @@@@ ### - */ - Requirements::javascript(SilverStripe\Core\Manifest\ModuleLoader::getModule('silverstripe/framework')->getResource('UPGRADE-FIX-REQUIRED.php')->getRelativePath() . '/javascript/AssetUploadField.js'); - Requirements::css(SilverStripe\Core\Manifest\ModuleLoader::getModule('silverstripe/framework')->getResource('UPGRADE-FIX-REQUIRED.php')->getRelativePath() . '/css/AssetUploadField.css'); - Requirements::css(DMS_DIR . '/dist/css/cmsbundle.css'); - - /** @var SiteTree $page */ - $page = $this->currentPage(); - /** @var DMSDocumentSet $documentSet */ $documentSet = $this->getCurrentDocumentSet(); + if (!$documentSet) { + throw new \RuntimeException('No document set found'); + } - $uploadField = DMSUploadField::create('AssetUploadField', ''); - $uploadField->setConfig('previewMaxWidth', 40); - $uploadField->setConfig('previewMaxHeight', 30); - // Required to avoid Solr reindexing (often used alongside DMS) to - // return 503s because of too many concurrent reindex requests - $uploadField->setConfig('sequentialUploads', 1); - $uploadField->addExtraClass('ss-assetuploadfield'); - $uploadField->removeExtraClass('ss-uploadfield'); - $uploadField->setTemplate('AssetUploadField'); - $uploadField->setRecord($documentSet); - - $uploadField->getValidator()->setAllowedExtensions($this->getAllowedExtensions()); - $exts = $uploadField->getValidator()->getAllowedExtensions(); - - asort($exts); + // Configure upload field + $uploadField = DMSUploadField::create('AssetUploadField', '') + ->setConfig('previewMaxWidth', 40) + ->setConfig('previewMaxHeight', 30) + ->setConfig('sequentialUploads', 1) + ->addExtraClass('ss-assetuploadfield') + ->removeExtraClass('ss-uploadfield') + ->setTemplate('AssetUploadField') + ->setRecord($documentSet); + + // Set allowed extensions + $validator = $uploadField->getValidator(); + $validator->setAllowedExtensions($this->getAllowedExtensions()); + $extensions = $validator->getAllowedExtensions(); + asort($extensions); + + // Create back link button $backlink = $this->Backlink(); - $done = " - - " . _t('UploadField.DONE', 'DONE') . " - "; + $doneButton = LiteralField::create( + 'doneButton', + sprintf( + '%s', + $backlink, + _t('UploadField.DONE', 'DONE') + ) + ); - $addExistingField = new DMSDocumentAddExistingField( + // Create add existing field + $addExistingField = DMSDocumentAddExistingField::create( 'AddExisting', _t('DMSDocumentAddExistingField.ADDEXISTING', 'Add Existing') + )->setRecord($documentSet); + + // Create allowed extensions field + $allowedExtensionsField = LiteralField::create( + 'AllowedExtensions', + sprintf( + '
%s: %s
', + _t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'), + implode(', ', $extensions) + ) ); - $addExistingField->setRecord($documentSet); - $form = new Form( - $this, - 'getEditForm', - new FieldList( - new TabSet( - _t('DMSDocumentAddController.MAINTAB', 'Main'), - new Tab( - _t('UploadField.FROMCOMPUTER', 'From your computer'), - $uploadField, - new LiteralField( - 'AllowedExtensions', - sprintf( - '%s: %s
', - _t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'), - implode(', ', $exts) - ) - ) - ), - new Tab( - _t('UploadField.FROMCMS', 'From the CMS'), - $addExistingField - ) - ) + // Create tabs + $tabSet = TabSet::create( + _t('DMSDocumentAddController.MAINTAB', 'Main'), + Tab::create( + _t('UploadField.FROMCOMPUTER', 'From your computer'), + $uploadField, + $allowedExtensionsField ), - new FieldList( - new LiteralField('doneButton', $done) + Tab::create( + _t('UploadField.FROMCMS', 'From the CMS'), + $addExistingField ) ); - $form->addExtraClass('center cms-edit-form ' . $this->BaseCSSClasses()); - $form->Backlink = $backlink; - // Don't use AssetAdmin_EditForm, as it assumes a different panel structure - $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); + + // Create form + $form = Form::create( + $this, + 'getEditForm', + FieldList::create($tabSet), + FieldList::create($doneButton) + ); + + // Configure form + $form->addExtraClass(sprintf('center cms-edit-form %s', $this->BaseCSSClasses())) + ->setTemplate($this->getTemplatesWithSuffix('_EditForm')); + + // Add hidden fields $form->Fields()->push(HiddenField::create('ID', false, $documentSet->ID)); $form->Fields()->push(HiddenField::create('DSID', false, $documentSet->ID)); + // Set backlink + $form->Backlink = $backlink; + return $form; } @@ -210,24 +212,21 @@ public function Breadcrumbs($unlinked = false) * 1) Page context: page ID and document set ID provided, redirect back to the page and document set * 2) Document set context: no page ID, document set ID provided, redirect back to document set in ModelAdmin * 3) Document context: no page ID and no document set ID provided, redirect back to documents in ModelAdmin - * - * @return string */ - public function Backlink() + public function Backlink(): string { if (!$this->getRequest()->getVar('dsid') || !$this->currentPageID()) { - $modelAdmin = new DMSDocumentAdmin; - $modelAdmin->init(); + $admin = DMSDocumentAdmin::create(); if ($this->getRequest()->getVar('dsid')) { return Controller::join_links( - $modelAdmin->Link(DMSDocumentSet::class), + $admin->Link(DMSDocumentSet::class), 'EditForm/field/DMSDocumentSet/item', (int) $this->getRequest()->getVar('dsid'), 'edit' ); } - return $modelAdmin->Link(); + return $admin->Link(); } return $this->getPageEditLink($this->currentPageID(), (int) $this->getRequest()->getVar('dsid')); @@ -269,7 +268,7 @@ public function documentautocomplete() ); } - return Convert::raw2json($return); + return json_encode($return); } /** @@ -302,7 +301,7 @@ public function linkdocument() ); } - return Convert::raw2json($return); + return json_encode($return); } /** diff --git a/src/Cms/DMSGridFieldAddNewButton.php b/src/Cms/DMSGridFieldAddNewButton.php index 9e185b2..be8134a 100644 --- a/src/Cms/DMSGridFieldAddNewButton.php +++ b/src/Cms/DMSGridFieldAddNewButton.php @@ -6,8 +6,8 @@ use SilverStripe\Control\Controller; use Sunnysideup\DMS\Model\DMSDocumentSet; use SilverStripe\CMS\Controllers\CMSPageEditController; +use SilverStripe\Forms\GridField\GridField; use SilverStripe\View\ArrayData; -use Sunnysideup\DMS\Cms\DMSGridFieldAddNewButton; use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\Forms\GridField\GridField_HTMLProvider; @@ -21,48 +21,59 @@ class DMSGridFieldAddNewButton extends GridFieldAddNewButton implements GridFiel protected $documentSetId; /** - * Overriding the parent method to change the template that the DMS add button will be rendered with + * Get the HTML fragments for the add button * - * @param GridField $gridField - * @return array + * @param GridField $gridField */ - public function getHTMLFragments($gridField) + public function getHTMLFragments($gridField): array { - $singleton = singleton($gridField->getModelClass()); + $modelClass = $gridField->getModelClass(); + $singleton = $modelClass::singleton(); if (!$singleton->canCreate()) { - return array(); + return []; } - if (!$this->buttonName) { - // provide a default button name, can be changed by calling {@link setButtonName()} on this component + if (empty($this->buttonName)) { $objectName = $singleton->i18n_singular_name(); - $this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName)); + $this->buttonName = _t( + 'GridField.Add', + 'Add {name}', + ['name' => $objectName] + ); } - $link = singleton(DMSDocumentAddController::class)->Link(); - if ($this->getDocumentSetId()) { - $link = Controller::join_links($link, '?dsid=' . $this->getDocumentSetId()); + $link = DMSDocumentAddController::singleton()->Link(); + + // Add document set ID if available + $documentSetId = $this->getDocumentSetId(); + if ($documentSetId) { + $link = Controller::join_links($link, '?dsid=' . $documentSetId); // Look for an associated page, but only share it if we're editing in a page context - $set = DMSDocumentSet::get()->byId($this->getDocumentSetId()); - if ($set && $set->exists() && $set->Page()->exists() + $set = DMSDocumentSet::get()->byId($documentSetId); + + if ( + $set + && $set->exists() + && $set->Page()->exists() && Controller::curr() instanceof CMSPageEditController ) { $link = Controller::join_links($link, '?page_id=' . $set->Page()->ID); } } - $data = new ArrayData(array( + $data = ArrayData::create([ 'NewLink' => $link, 'ButtonName' => $this->buttonName, - )); + ]); - return array( - $this->targetFragment => $data->renderWith(DMSGridFieldAddNewButton::class), - ); + return [ + $this->targetFragment => $data->renderWith(self::class) + ]; } + /** * Set the document set ID that this document should be attached to * @@ -84,4 +95,4 @@ public function getDocumentSetId() { return $this->documentSetId; } -} +} \ No newline at end of file diff --git a/src/Cms/DMSUploadField.php b/src/Cms/DMSUploadField.php index bf5861c..8956e87 100644 --- a/src/Cms/DMSUploadField.php +++ b/src/Cms/DMSUploadField.php @@ -31,9 +31,7 @@ */ class DMSUploadField extends UploadField { - private static $allowed_actions = array( - "upload", - ); + private static $allowed_actions = ['upload']; /** * The temporary folder name to store files in during upload @@ -48,7 +46,7 @@ class DMSUploadField extends UploadField */ protected function attachFile($file) { - $dms = DMS::inst(); + $dms = singleton(DMS::class); $record = $this->getRecord(); if ($record instanceof DMSDocument) { @@ -171,7 +169,7 @@ public function upload(HTTPRequest $request) $document = $this->attachFile($file); // Collect all output data. - $return = array_merge($return, array( + $return = array_merge($return, [ 'id' => $document->ID, 'name' => $document->getTitle(), 'thumbnail_url' => $document->Icon($document->getExtension()), @@ -179,13 +177,11 @@ public function upload(HTTPRequest $request) 'size' => $document->getFileSizeFormatted(), 'buttons' => (string) $document->renderWith($this->getTemplateFileButtons()), 'showeditform' => true - )); - - // CUSTOM END + ]); } } } - $response = new HTTPResponse(Convert::raw2json(array($return))); + $response = new HTTPResponse(json_encode([$return])); $response->addHeader('Content-Type', 'text/plain'); return $response; } @@ -206,10 +202,10 @@ public function Field($properties = array()) // Replace the download template with a new one only when access the upload field through a GridField. // Needs to be enabled through setConfig('downloadTemplateName', 'ss-dmsuploadfield-downloadtemplate'); - Requirements::javascript(DMS_DIR . '/javascript/DMSUploadField_downloadtemplate.js'); + Requirements::javascript('./javascript/DMSUploadField_downloadtemplate.js'); // In the add dialog, add the addtemplate into the set of file that load. - Requirements::javascript(DMS_DIR . '/javascript/DMSUploadField_addtemplate.js'); + Requirements::javascript('./javascript/DMSUploadField_addtemplate.js'); return $fields; } @@ -218,7 +214,7 @@ public function Field($properties = array()) * @param int $itemID * @return UploadField_ItemHandler */ - public function getItemHandler($itemID) + public function getItemHandler(int $itemID): DMSUploadField_ItemHandler { return DMSUploadField_ItemHandler::create($this, $itemID); } diff --git a/src/Cms/DMSUploadField_ItemHandler.php b/src/Cms/DMSUploadField_ItemHandler.php index 8e8b3fb..7f6f6c6 100644 --- a/src/Cms/DMSUploadField_ItemHandler.php +++ b/src/Cms/DMSUploadField_ItemHandler.php @@ -2,51 +2,103 @@ namespace Sunnysideup\DMS\Cms; -// todo: UPGRADE: we need to replace this ... // use UploadField_ItemHandler; - +use SilverStripe\Core\Config\Configurable; +use SilverStripe\Core\Extensible; +use SilverStripe\Core\Injector\Injectable; use Sunnysideup\DMS\Model\DMSDocument; use SilverStripe\Forms\Form; class DMSUploadField_ItemHandler { - private static $allowed_actions = array( + use Configurable; + use Extensible; + use Injectable; + + /** + * @var array