Passed
Push — 4 ( 8d0772...67ea95 )
by Loz
09:06
created

GridFieldDeleteAction::getRemoveRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\ValidationException;
7
8
/**
9
 * This class is a {@link GridField} component that adds a delete action for
10
 * objects.
11
 *
12
 * This component also supports unlinking a relation instead of deleting the
13
 * object.
14
 *
15
 * Use the {@link $removeRelation} property set in the constructor.
16
 *
17
 * <code>
18
 * $action = new GridFieldDeleteAction(); // delete objects permanently
19
 *
20
 * // removes the relation to object instead of deleting
21
 * $action = new GridFieldDeleteAction(true);
22
 * </code>
23
 */
24
class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider
25
{
26
27
    /**
28
     * If this is set to true, this {@link GridField_ActionProvider} will
29
     * remove the object from the list, instead of deleting.
30
     *
31
     * In the case of a has one, has many or many many list it will uncouple
32
     * the item from the list.
33
     *
34
     * @var boolean
35
     */
36
    protected $removeRelation = false;
37
38
    /**
39
     *
40
     * @param boolean $removeRelation - true if removing the item from the list, but not deleting it
41
     */
42
    public function __construct($removeRelation = false)
43
    {
44
        $this->setRemoveRelation($removeRelation);
45
    }
46
47
    /**
48
     * Add a column 'Delete'
49
     *
50
     * @param GridField $gridField
51
     * @param array $columns
52
     */
53
    public function augmentColumns($gridField, &$columns)
54
    {
55
        if (!in_array('Actions', $columns)) {
56
            $columns[] = 'Actions';
57
        }
58
    }
59
60
    /**
61
     * Return any special attributes that will be used for FormField::create_tag()
62
     *
63
     * @param GridField $gridField
64
     * @param DataObject $record
65
     * @param string $columnName
66
     * @return array
67
     */
68
    public function getColumnAttributes($gridField, $record, $columnName)
69
    {
70
        return array('class' => 'grid-field__col-compact');
71
    }
72
73
    /**
74
     * Add the title
75
     *
76
     * @param GridField $gridField
77
     * @param string $columnName
78
     * @return array
79
     */
80
    public function getColumnMetadata($gridField, $columnName)
81
    {
82
        if ($columnName == 'Actions') {
83
            return array('title' => '');
84
        }
85
    }
86
87
    /**
88
     * Which columns are handled by this component
89
     *
90
     * @param GridField $gridField
91
     * @return array
92
     */
93
    public function getColumnsHandled($gridField)
94
    {
95
        return array('Actions');
96
    }
97
98
    /**
99
     * Which GridField actions are this component handling
100
     *
101
     * @param GridField $gridField
102
     * @return array
103
     */
104
    public function getActions($gridField)
105
    {
106
        return array('deleterecord', 'unlinkrelation');
107
    }
108
109
    /**
110
     *
111
     * @param GridField $gridField
112
     * @param DataObject $record
113
     * @param string $columnName
114
     * @return string the HTML for the column
115
     */
116
    public function getColumnContent($gridField, $record, $columnName)
117
    {
118
        if ($this->getRemoveRelation()) {
119
            if (!$record->canEdit()) {
120
                return null;
121
            }
122
            $title = _t(__CLASS__.'.UnlinkRelation', "Unlink");
123
124
            $field = GridField_FormAction::create(
125
                $gridField,
0 ignored issues
show
Bug introduced by
$gridField of type SilverStripe\Forms\GridField\GridField is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

125
                /** @scrutinizer ignore-type */ $gridField,
Loading history...
126
                'UnlinkRelation'.$record->ID,
0 ignored issues
show
Bug introduced by
'UnlinkRelation' . $record->ID of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

126
                /** @scrutinizer ignore-type */ 'UnlinkRelation'.$record->ID,
Loading history...
127
                false,
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

127
                /** @scrutinizer ignore-type */ false,
Loading history...
128
                "unlinkrelation",
129
                array('RecordID' => $record->ID)
130
            )
131
                ->addExtraClass('btn btn--no-text btn--icon-md font-icon-link-broken grid-field__icon-action gridfield-button-unlink')
132
                ->setAttribute('title', $title)
133
                ->setAttribute('aria-label', $title);
134
        } else {
135
            if (!$record->canDelete()) {
136
                return null;
137
            }
138
139
            $field = GridField_FormAction::create(
140
                $gridField,
141
                'DeleteRecord'.$record->ID,
142
                false,
143
                "deleterecord",
144
                array('RecordID' => $record->ID)
145
            )
146
                ->addExtraClass('gridfield-button-delete btn--icon-md font-icon-trash-bin btn--no-text grid-field__icon-action')
147
                ->setAttribute('title', _t(__CLASS__.'.Delete', "Delete"))
148
                ->setDescription(_t(__CLASS__.'.DELETE_DESCRIPTION', 'Delete'));
149
        }
150
        return $field->Field();
151
    }
152
153
    /**
154
     * Handle the actions and apply any changes to the GridField
155
     *
156
     * @param GridField $gridField
157
     * @param string $actionName
158
     * @param array $arguments
159
     * @param array $data Form data
160
     * @throws ValidationException
161
     */
162
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
163
    {
164
        if ($actionName == 'deleterecord' || $actionName == 'unlinkrelation') {
165
            /** @var DataObject $item */
166
            $item = $gridField->getList()->byID($arguments['RecordID']);
0 ignored issues
show
Bug introduced by
The method byID() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Limitable. Are you sure you never get one of those? ( Ignorable by Annotation )

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

166
            $item = $gridField->getList()->/** @scrutinizer ignore-call */ byID($arguments['RecordID']);
Loading history...
167
            if (!$item) {
168
                return;
169
            }
170
171
            if ($actionName == 'deleterecord') {
172
                if (!$item->canDelete()) {
173
                    throw new ValidationException(
174
                        _t(__CLASS__.'.DeletePermissionsFailure', "No delete permissions")
175
                    );
176
                }
177
178
                $item->delete();
179
            } else {
180
                if (!$item->canEdit()) {
181
                    throw new ValidationException(
182
                        _t(__CLASS__.'.EditPermissionsFailure', "No permission to unlink record")
183
                    );
184
                }
185
186
                $gridField->getList()->remove($item);
187
            }
188
        }
189
    }
190
191
    /**
192
     * Get whether to remove or delete the relation
193
     *
194
     * @return bool
195
     */
196
    public function getRemoveRelation()
197
    {
198
        return $this->removeRelation;
199
    }
200
201
    /**
202
     * Set whether to remove or delete the relation
203
     * @param bool $removeRelation
204
     * @return $this
205
     */
206
    public function setRemoveRelation($removeRelation)
207
    {
208
        $this->removeRelation = (bool) $removeRelation;
209
        return $this;
210
    }
211
}
212