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