1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* When using inline editing on a ModelAdmin, there is no save button |
12
|
|
|
* This allows saving the records |
13
|
|
|
* It needs a custom endpoint because somehow, new records are not sent along |
14
|
|
|
*/ |
15
|
|
|
class GridFieldSaveAllButton extends GridFieldTableButton |
16
|
|
|
{ |
17
|
|
|
protected $fontIcon = 'save'; |
18
|
|
|
public bool $submitData = true; |
19
|
|
|
/** |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
protected $noAjax = false; |
23
|
|
|
protected ?string $completeMessage = null; |
24
|
|
|
|
25
|
|
|
public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($targetFragment, $buttonLabel); |
28
|
|
|
$this->buttonLabel = $buttonLabel ?? _t('GridFieldSaveAllButton.SaveAll', 'Save all'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function handle(GridField $gridField, Controller $controller, $arguments = [], $data = []) |
32
|
|
|
{ |
33
|
|
|
$fieldName = $gridField->getName(); |
34
|
|
|
$list = $gridField->getList(); |
35
|
|
|
$model = $gridField->getModelClass(); |
36
|
|
|
|
37
|
|
|
// Without this, handleSave does not work |
38
|
|
|
$gridField->setSubmittedValue($data[$fieldName]); |
39
|
|
|
|
40
|
|
|
$updatedData = $data[$fieldName]['GridFieldEditableColumns'] ?? []; |
41
|
|
|
foreach ($updatedData as $id => $values) { |
42
|
|
|
/** @var DataObject $record */ |
43
|
|
|
$record = $list->byID($id); |
44
|
|
|
if (!$record) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class); |
|
|
|
|
48
|
|
|
$component->handleSave($gridField, $record); |
49
|
|
|
// foreach ($values as $k => $v) { |
50
|
|
|
// $record->$k = $v; |
51
|
|
|
// } |
52
|
|
|
// $record->write(); |
53
|
|
|
} |
54
|
|
|
$newData = $data[$fieldName]['GridFieldAddNewInlineButton'] ?? []; |
55
|
|
|
foreach ($newData as $idx => $values) { |
56
|
|
|
$record = new $model; |
57
|
|
|
foreach ($values as $k => $v) { |
58
|
|
|
$record->$k = $v; |
59
|
|
|
} |
60
|
|
|
$record->write(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$response = $controller->getResponse(); |
64
|
|
|
|
65
|
|
|
if (Director::is_ajax()) { |
66
|
|
|
if (!$this->completeMessage) { |
67
|
|
|
$this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'ALL SAVED!'); |
68
|
|
|
} |
69
|
|
|
// Reload for now since we mess up with the PJAX fragment |
70
|
|
|
$url = $controller->getReferer(); |
71
|
|
|
$response->addHeader('X-ControllerURL', $url); |
72
|
|
|
$response->addHeader('X-Reload', true); |
|
|
|
|
73
|
|
|
$response->addHeader('X-Status', rawurlencode($this->completeMessage)); |
74
|
|
|
} else { |
75
|
|
|
return $controller->redirectBack(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the value of completeMessage |
81
|
|
|
*/ |
82
|
|
|
public function getCompleteMessage(): string |
83
|
|
|
{ |
84
|
|
|
return $this->completeMessage; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the value of completeMessage |
89
|
|
|
* |
90
|
|
|
* @param string $completeMessage |
91
|
|
|
*/ |
92
|
|
|
public function setCompleteMessage($completeMessage): self |
93
|
|
|
{ |
94
|
|
|
$this->completeMessage = $completeMessage; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths