Completed
Push — master ( a89a63...97b0bf )
by Nic
13s
created

ManageableDataObjectExtension::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 10
cp 0.7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 0
crap 4.432
1
<?php
2
3
/**
4
 * Class ManageableDataObjectExtension
5
 */
6
class ManageableDataObjectExtension extends Extension
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private static $allowed_actions = [
13
        'add',
14
        'edit',
15
        'delete',
16
        'ManageableDataObjectForm',
17
    ];
18
19
    /**
20
     * Add object
21
     *
22
     * @return SS_HTTPResponse|ViewableData_Customised
23
     */
24 2
    public function add()
25
    {
26 2
        $model = $this->owner->config()->get('managed_object');
27 2
        $object = Injector::inst()->get($model);
28 2
        if ($object->canCreate(Member::currentUser())) {
29
30 2
            $form = $this->ManageableDataObjectForm();
31 2
            $form->Actions()->push(new CancelFormAction($this->owner->Link(), 'Cancel'));
32
33 2
            return $this->owner->customise([
34 2
                'Title' => ($this->owner->config()->get('add_item_title'))
35 2
                    ? $this->owner->config()->get('add_item_title')
36 2
                    : 'Add new ' . $object->singular_name(),
37 2
                'ManageableDataObjectForm' => $form,
38 2
            ]);
39
        }
40
41
        return Security::permissionFailure($this->owner, "You don't have permission to add records.");
42
    }
43
44
    /**
45
     * Edit object
46
     *
47
     * @return SS_HTTPResponse|ViewableData_Customised
48
     */
49 1
    public function edit()
50
    {
51 1
        if ($item = $this->getCurrentItem()) {
52 1
            if ($item->canEdit(Member::currentUser())) {
0 ignored issues
show
Bug introduced by
It seems like \Member::currentUser() targeting Member::currentUser() can also be of type object<DataObject>; however, DataObject::canEdit() does only seem to accept object<Member>|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
54
                // get Form
55 1
                $form = $this->ManageableDataObjectForm($item);
56
57 1
                return $this->owner->customise([
58 1
                    'Title' => 'Edit ' . $item->singular_name(),
59 1
                    'ManageableDataObjectForm' => $form,
60 1
                    'Item' => $item,
61 1
                ]);
62
            }
63
64
            return Security::permissionFailure($this->owner, "You don't have permission to edit this record.");
65
        }
66
67 1
        return $this->owner->httpError(404);
68
    }
69
70
    /**
71
     * Delete Object
72
     *
73
     * @return SS_HTTPResponse
74
     */
75 1
    public function delete()
76
    {
77 1
        if ($item = $this->getCurrentItem()) {
78 1
            if ($item->canDelete(Member::currentUser())) {
0 ignored issues
show
Bug introduced by
It seems like \Member::currentUser() targeting Member::currentUser() can also be of type object<DataObject>; however, DataObject::canDelete() does only seem to accept object<Member>|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79 1
                if ($item->hasMethod('softDelete')) {
80
                    $item->softDelete();
0 ignored issues
show
Bug introduced by
The method softDelete() does not exist on DataObject. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
81
                } else {
82 1
                    $item->delete();
83
                }
84
85 1
                return $this->owner->redirect($this->owner->Link());
86
            }
87
88
            return Security::permissionFailure($this->owner, "You don't have permission to delete this record.");
89
        }
90
91 1
        return $this->owner->httpError(404);
92
    }
93
94
    /**
95
     * Main GridObject Form. Fields loaded via getFrontEndFields method on each Object
96
     *
97
     * @param $object
98
     *
99
     * @return ManageableDataObjectForm
100
     */
101 3
    public function ManageableDataObjectForm($object = null)
102
    {
103 3
        $model = $this->owner->config()->get('managed_object');
104 3
        $field = ($this->owner->config()->get('query_field'))
105 3
            ? $this->owner->config()->get('query_field')
106 3
            : 'ID';
107 3
        $object = ($object !== null && $object instanceof $model && $object->exists())
108 3
            ? $object
109 3
            : Injector::inst()->create($model);
110
111 3
        $form = ManageableDataObjectForm::create(
112 3
            $this->owner,
113 3
            'ManageableDataObjectForm',
114
            $object
115 3
        );
116
117 3
        if ($object->exists()) {
118 1
            $form->Fields()->push(HiddenField::create($field, $object->$field));
119 1
            $form->loadDataFrom($object);
120 1
        }
121
122 3
        return $form;
123
    }
124
125
    /**
126
     * Save object
127
     *
128
     * @param $data
129
     * @param Form $form
130
     *
131
     * @return SS_HTTPResponse
132
     */
133 1
    public function doSaveObject($data, Form $form)
134
    {
135
136 1
        $model = $this->owner->config()->get('managed_object');
137
138 1
        if (isset($data['ID']) && $data['ID']) {
139
            $field = ($this->owner->config()->get('query_field'))
140
                ? $this->owner->config()->get('query_field')
141
                : 'ID';
142
            $object = $model::get()->filter($field, $data['ID'])->first();
143
        } else {
144 1
            $object = $model::create();
145 1
            if ($object->hasDatabaseField('URLSegment')) {
146 1
                $object->URLSegment = Injector::inst()->create(SiteTree::class)->generateURLSegment($data['Title']);
147 1
            }
148
            // write on create to relations are saved on final write (needs ID)
149 1
            $object->write();
150
        }
151
152 1
        $form->saveInto($object);
153
154 1
        $this->owner->extend('updateObjectPreSave', $data, $object);
155
156 1
        $object->write();
157
158 1
        $this->owner->extend('updateObjectPostSave', $data, $object);
159
160 1
        return $this->owner->redirect($object->Link());
161
    }
162
163
164
    /**
165
     * @return bool|DataObject
166
     */
167 2
    protected function getCurrentItem()
168
    {
169 2
        if (!$id = $this->owner->request->param('ID')) {
170 2
            return false;
171
        }
172
173 2
        $class = $this->owner->config()->get('managed_object');
174 2
        $field = (Injector::inst()->get($class)->config()->get('query_field'))
175 2
            ? Injector::inst()->get($class)->config()->get('query_field')
176 2
            : 'ID';
177
178 2
        if (!$record = $class::get()->filter($field, $id)->first()) {
179
            return false;
180
        }
181
182 2
        return $record;
183
    }
184
185
}