Completed
Pull Request — master (#4)
by Nic
06:53 queued 05:29
created

ManageableDataObjectExtension::doSaveObject()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 10
cts 15
cp 0.6667
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 2
crap 4.5923
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' => 'Add new ' . $object->singular_name(),
35
                'ManageableDataObjectForm' => $form
36 2
            ));
37
        }
38
39
        return Security::permissionFailure($this->owner, "You don't have permission to add records.");
40
    }
41
42
    /**
43
     * Edit object
44
     *
45
     * @return SS_HTTPResponse|ViewableData_Customised
46
     */
47 1
    public function edit()
48
    {
49 1
        if ($item = $this->getCurrentItem()) {
50 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...
51
52
                // get Form
53 1
                $form = $this->ManageableDataObjectForm($item);
54
55 1
                return $this->owner->customise(array(
56 1
                    'Title' => 'Edit ' . $item->singular_name(),
57 1
                    'ManageableDataObjectForm' => $form,
58
                    'Item' => $item
59 1
                ));
60
            }
61
62
            return Security::permissionFailure($this->owner, "You don't have permission to edit this record.");
63
        }
64
65 1
        return $this->owner->httpError(404);
66
    }
67
68
    /**
69
     * Delete Object
70
     *
71
     * @return SS_HTTPResponse
72
     */
73 1
    public function delete()
74
    {
75 1
        if ($item = $this->getCurrentItem()) {
76 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...
77 1
                $item->delete();
78
79 1
                return $this->owner->redirect($this->owner->Link());
80
            }
81
82
            return Security::permissionFailure($this->owner, "You don't have permission to delete this record.");
83
        }
84
85 1
        return $this->owner->httpError(404);
86
    }
87
88
    /**
89
     * Main GridObject Form. Fields loaded via getFrontEndFields method on each Object
90
     *
91
     * @param $object
92
     *
93
     * @return ManageableDataObjectForm
94
     */
95 3
    public function ManageableDataObjectForm($object = null)
96
    {
97 3
        $model = $this->owner->config()->get('managed_object');
98 3
        $field = ($this->owner->config()->get('query_field'))
99 3
            ? $this->owner->config()->get('query_field')
100 3
            : 'ID';
101 3
        $object = ($object !== null && $object instanceof $model && $object->exists())
102 3
            ? $object
103 3
            : Injector::inst()->create($model);
104
105 3
        $form = ManageableDataObjectForm::create(
106 3
            $this->owner,
107 3
            'ManageableDataObjectForm',
108
            $model
109 3
        );
110
111 3
        if ($object->exists()) {
112 1
            $form->Fields()->push(HiddenField::create($field, $object->$field));
113 1
            $form->loadDataFrom($object);
114 1
        }
115
116 3
        return $form;
117
    }
118
119
    /**
120
     * Save object
121
     *
122
     * @param $data
123
     * @param Form $form
124
     *
125
     * @return SS_HTTPResponse
126
     */
127 1
    public function doSaveObject($data, Form $form)
128
    {
129
130 1
        $model = $this->owner->config()->get('managed_object');
131
132 1
        if (isset($data['ID']) && $data['ID']) {
133
            $field = ($this->owner->config()->get('query_field'))
134
                ? $this->owner->config()->get('query_field')
135
                : 'ID';
136
            $object = $model::get()->filter($field, $data['ID'])->first();
137
        } else {
138 1
            $object = $model::create();
139
            // write on create to relations are saved on final write (needs ID)
140 1
            $object->write();
141
        }
142
143 1
        $form->saveInto($object);
144
145 1
        $this->owner->extend('updateObjectPreSave', $data, $object);
146
147 1
        $object->write();
148
149 1
        $this->owner->extend('updateObjectPostSave', $data, $object);
150
151 1
        return $this->owner->redirect($object->Link());
152
    }
153
154
155
    /**
156
     * @return bool|DataObject
157
     */
158 2
    protected function getCurrentItem()
159
    {
160 2
        if (!$id = $this->owner->request->param('ID')) {
161 2
            return false;
162
        }
163
164 2
        $class = $this->owner->config()->get('managed_object');
165 2
        $field = ($this->owner->config()->get('query_field'))
166 2
            ? $this->owner->config()->get('query_field')
167 2
            : 'ID';
168 2
        if (!$record = $class::get()->filter($field, $id)->first()) {
169
            return false;
170
        }
171
172 2
        return $record;
173
    }
174
175
}