Completed
Pull Request — master (#4)
by Nic
09:10
created

ManageableDataObjectExtension::doSaveObject()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 10
cts 13
cp 0.7692
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 2
crap 3.1105
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
            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
                $form = $this->ManageableDataObjectForm($item);
54
55
                return $this->owner->customise(array(
56
                    'Title' => 'Edit ' . $item->singular_name(),
57
                    'ManageableDataObjectForm' => $form,
58
                    'Item' => $item
59
                ));
60
            }
61
62
            return Security::permissionFailure($this->owner, "You don't have permission to edit this record.");
63
        }
64
65
        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
            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
                $item->delete();
78
79
                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 2
    public function ManageableDataObjectForm($object = null)
96
    {
97 2
        $model = $this->owner->config()->get('managed_object');
98 2
        $field = $this->owner->config()->get('query_field');
99 2
        $object = ($object !== null && $object instanceof $model && $object->exists())
100 2
            ? $object
101 2
            : Injector::inst()->create($model);
102
103 2
        $form = ManageableDataObjectForm::create(
104 2
            $this->owner,
105 2
            'ManageableDataObjectForm',
106
            $model
107 2
        );
108
109 2
        if ($object->exists()) {
110
            $form->Fields()->push(HiddenField::create($field, $object->$field));
111
            $form->loadDataFrom($object);
112
        }
113
114 2
        return $form;
115
    }
116
117
    /**
118
     * Save object
119
     *
120
     * @param $data
121
     * @param Form $form
122
     *
123
     * @return SS_HTTPResponse
124
     */
125 1
    public function doSaveObject($data, Form $form)
126
    {
127
128 1
        $model = $this->owner->config()->get('managed_object');
129
130 1
        if (isset($data['ID']) && $data['ID']) {
131
            $field = $this->owner->config()->get('query_field');
132
            $object = $model::get()->filter($field, $data['ID'])->first();
133
        } else {
134 1
            $object = $model::create();
135
            // write on create to relations are saved on final write (needs ID)
136 1
            $object->write();
137
        }
138
139 1
        $form->saveInto($object);
140
141 1
        $this->owner->extend('updateObjectPreSave', $data, $object);
142
143 1
        $object->write();
144
145 1
        $this->owner->extend('updateObjectPostSave', $data, $object);
146
147 1
        return $this->owner->redirect($object->Link());
148
    }
149
150
151
    /**
152
     * @return bool|DataObject
153
     */
154 2
    protected function getCurrentItem()
155
    {
156 2
        if (!$id = $this->owner->request->param('ID')) {
157 1
            return false;
158
        }
159
160 2
        $class = $this->owner->config()->get('managed_object');
161 2
        $field = $this->owner->config()->get('query_field');
162 2
        if (!$record = $class::get()->filter($field, $id)->first()) {
163
            return false;
164
        }
165
166
        return $record;
167
    }
168
169
}