Skip to content

Commit

Permalink
feat: Upgraded CMS classes for ss 5 and php8.3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-nzl committed Jan 8, 2025
1 parent 4198724 commit a67288c
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 156 deletions.
151 changes: 75 additions & 76 deletions src/Cms/DMSDocumentAddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\Admin\LeftAndMain;
use Sunnysideup\DMS\Admin\DMSDocumentAdmin;

/**
* @package dms
Expand Down Expand Up @@ -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 = "
<a class=\"ss-ui-button ss-ui-action-constructive cms-panel-link ui-corner-all\" href=\"" . $backlink . "\">
" . _t('UploadField.DONE', 'DONE') . "
</a>";
$doneButton = LiteralField::create(
'doneButton',
sprintf(
'<a class="ss-ui-button ss-ui-action-constructive cms-panel-link ui-corner-all" href="%s">%s</a>',
$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(
'<p>%s: %s</p>',
_t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'),
implode('<em>, </em>', $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(
'<p>%s: %s</p>',
_t('AssetAdmin.ALLOWEDEXTS', 'Allowed extensions'),
implode('<em>, </em>', $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;
}

Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -269,7 +268,7 @@ public function documentautocomplete()
);
}

return Convert::raw2json($return);
return json_encode($return);
}

/**
Expand Down Expand Up @@ -302,7 +301,7 @@ public function linkdocument()
);
}

return Convert::raw2json($return);
return json_encode($return);
}

/**
Expand Down
53 changes: 32 additions & 21 deletions src/Cms/DMSGridFieldAddNewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
*
Expand All @@ -84,4 +95,4 @@ public function getDocumentSetId()
{
return $this->documentSetId;
}
}
}
20 changes: 8 additions & 12 deletions src/Cms/DMSUploadField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -171,21 +169,19 @@ 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()),
'edit_url' => $this->getItemHandler($document->ID)->EditLink(),
'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;
}
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit a67288c

Please sign in to comment.