Completed
Push — master ( 4d30a6...0579e1 )
by Nic
03:24
created

ManageableDataObjectExtension::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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