cms-niceties/src/Forms/GridField/SaveAllButton.php

173 lines
5.2 KiB
PHP
Raw Normal View History

2022-02-12 02:17:13 +01:00
<?php
namespace A2nt\CMSNiceties\Forms\GridField;
2021-06-19 21:30:03 +02:00
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Forms\GridField\GridField_SaveHandler;
use SilverStripe\Control\Controller;
2022-02-23 13:28:51 +01:00
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
2021-06-19 21:30:03 +02:00
class SaveAllButton implements GridField_HTMLProvider, GridField_ActionProvider
{
protected $targetFragment;
protected $actionName = 'saveallrecords';
public $buttonName;
public $publish = true;
public $completeMessage;
public $removeChangeFlagOnFormOnSave = false;
public function setButtonName($name)
{
$this->buttonName = $name;
return $this;
}
public function setRemoveChangeFlagOnFormOnSave($flag)
{
$this->removeChangeFlagOnFormOnSave = $flag;
return $this;
}
public function __construct($targetFragment = 'before', $publish = true, $action = 'saveallrecords')
{
$this->targetFragment = $targetFragment;
$this->publish = $publish;
$this->actionName = $action;
}
public function getHTMLFragments($gridField)
{
2023-03-24 19:34:45 +01:00
$class = $gridField->getModelClass();
$singleton = singleton($class);
2021-06-19 21:30:03 +02:00
if (!$singleton->canEdit() && !$singleton->canCreate()) {
return [];
}
if (!$this->buttonName) {
2023-03-24 19:34:45 +01:00
if ($this->publish && $singleton->hasExtension(Versioned::class)) {
$this->buttonName = _t('GridField.SAVE_ALL_AND_PUBLISH', 'Save all and Publish');
2021-06-19 21:30:03 +02:00
} else {
$this->buttonName = _t('GridField.SAVE_ALL', 'Save all');
}
}
$button = GridField_FormAction::create(
$gridField,
$this->actionName,
$this->buttonName,
$this->actionName,
null
);
2022-02-12 02:17:13 +01:00
$button
->setAttribute('style', 'float:right')
->addExtraClass('action action-detail btn btn-primary font-icon-disk new new-link font-icon-check-mark');
2021-06-19 21:30:03 +02:00
if ($this->removeChangeFlagOnFormOnSave) {
$button->addExtraClass('js-mwm-gridfield--saveall');
}
return [
$this->targetFragment => $button->Field(),
];
}
public function getActions($gridField)
{
return [$this->actionName];
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName == $this->actionName) {
return $this->saveAllRecords($gridField, $arguments, $data);
}
}
protected function saveAllRecords(GridField $grid, $arguments, $data)
{
2023-03-24 19:34:45 +01:00
if (!isset($data[$grid->Name])
|| !isset($data[$grid->Name]['GridFieldEditableColumns'])
) {
2022-02-23 13:28:51 +01:00
return;
}
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
$currValue = $grid->Value();
$grid->setValue($data[$grid->Name]);
$model = singleton($grid->List->dataClass());
$cfg = $grid->getConfig();
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
foreach ($cfg->getComponents() as $component) {
if ($component instanceof GridField_SaveHandler) {
$component->handleSave($grid, $model);
}
}
// Only use the viewable list items, since bulk publishing can take a toll on the system
2023-03-24 19:34:45 +01:00
$paginator = $cfg->getComponentByType(GridFieldPaginator::class);
2022-02-23 13:28:51 +01:00
$list = $paginator
? $paginator->getManipulatedData($grid, $grid->List)
: $grid->List;
// add missing checkbox fields
$cols = $cfg->getComponentByType(GridFieldEditableColumns::class);
if ($cols) {
$fields = $cols->getFields($grid, $model);
$colsData = $data[$grid->Name]['GridFieldEditableColumns'];
if (isset($colsData)) {
$list->each(function ($item) use ($colsData, $grid, $fields) {
/* @var $item \SilverStripe\ORM\DataObject */
if (!isset($colsData[$item->ID])) {
foreach ($fields as $field) {
/* @var $field \SilverStripe\Forms\FormField */
$fieldName = $field->getName();
$item->setField($fieldName, '');
2021-06-19 21:30:03 +02:00
}
2022-02-23 13:28:51 +01:00
$item->write();
2021-06-19 21:30:03 +02:00
}
2022-02-23 13:28:51 +01:00
});
2021-06-19 21:30:03 +02:00
}
2022-02-23 13:28:51 +01:00
}
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
if ($this->publish) {
$list->each(function ($item) {
2023-03-24 19:34:45 +01:00
if ($item->hasExtension(Versioned::class)) {
2022-02-23 13:28:51 +01:00
$item->writeToStage('Stage');
2023-03-24 19:34:45 +01:00
if (!$item->stagesDiffer()) {
$item->copyVersionToStage('Stage', 'Live');
}
2022-02-23 13:28:51 +01:00
}
});
}
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
if ($model->exists()) {
$model->delete();
$model->destroy();
}
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
$grid->setValue($currValue);
2021-06-19 21:30:03 +02:00
2022-02-23 13:28:51 +01:00
$curr = Controller::curr();
$response = $curr->Response;
if ($curr && $response) {
if (!$this->completeMessage) {
$this->completeMessage = _t('GridField.DONE', 'Done.');
2021-06-19 21:30:03 +02:00
}
2022-02-23 13:28:51 +01:00
$response->addHeader('X-Status', rawurlencode($this->completeMessage));
2021-06-19 21:30:03 +02:00
}
}
}