Passed
Push — master ( f36477...115177 )
by Thomas
13:22
created

GridFieldSaveAllButton::setCompleteMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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);
0 ignored issues
show
Bug introduced by
The type Symbiote\GridFieldExtens...ridFieldEditableColumns was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of SilverStripe\Control\HTTPResponse::addHeader(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $response->addHeader('X-Reload', /** @scrutinizer ignore-type */ true);
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->completeMessage could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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