Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ss 5 compatibility updates #2

Merged
merged 8 commits into from
Jan 20, 2025
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
7 changes: 7 additions & 0 deletions src/Cms/DMSDocumentAddExistingField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class DMSDocumentAddExistingField extends CompositeField
{
public $useFieldContext = true;

/**
* @var DataObject
*/
protected $record;


public function __construct($name, $title = null)
{
$this->name = $name;
Expand All @@ -37,6 +43,7 @@ public function setRecord($record)
$this->record = $record;
return $this;
}

/**
* Get the record to use as "Parent" for uploaded Files (eg a Page with a has_one to File) If none is set, it
* will use Form->getRecord() or Form->Controller()->data()
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;
}
}
}
Loading
Loading