|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handles individual record data editing or deleting. |
|
4
|
|
|
* |
|
5
|
|
|
* @package silverstripe-advancedworkflow |
|
6
|
|
|
*/ |
|
7
|
|
|
class WorkflowFieldItemController extends Controller { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
public static $allowed_actions = array( |
|
10
|
|
|
'index', |
|
11
|
|
|
'edit', |
|
12
|
|
|
'delete', |
|
13
|
|
|
'Form' |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
protected $parent; |
|
17
|
|
|
protected $name; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($parent, $name, $record) { |
|
20
|
|
|
$this->parent = $parent; |
|
21
|
|
|
$this->name = $name; |
|
22
|
|
|
$this->record = $record; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
parent::__construct(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function index() { |
|
28
|
|
|
return $this->edit(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function edit() { |
|
32
|
|
|
return $this->Form()->forTemplate(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function Form() { |
|
36
|
|
|
$record = $this->record; |
|
|
|
|
|
|
37
|
|
|
$fields = $record->getCMSFields(); |
|
38
|
|
|
$validator = $record->hasMethod('getValidator') ? $record->getValidator() : null; |
|
39
|
|
|
|
|
40
|
|
|
$save = FormAction::create('doSave', _t('WorkflowReminderTask.SAVE', 'Save')); |
|
41
|
|
|
$save->addExtraClass('ss-ui-button ss-ui-action-constructive') |
|
42
|
|
|
->setAttribute('data-icon', 'accept') |
|
43
|
|
|
->setUseButtonTag(true); |
|
44
|
|
|
|
|
45
|
|
|
$form = new Form($this, 'Form', $fields, new FieldList($save), $validator); |
|
46
|
|
|
if($record && $record instanceof DataObject && $record->exists()){ |
|
47
|
|
|
$form->loadDataFrom($record); |
|
48
|
|
|
} |
|
49
|
|
|
return $form; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function doSave($data, $form) { |
|
|
|
|
|
|
53
|
|
|
$record = $form->getRecord(); |
|
54
|
|
|
|
|
55
|
|
|
if(!$record || !$record->exists()){ |
|
56
|
|
|
$record = $this->record; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if(!$record->canEdit()) { |
|
60
|
|
|
$this->httpError(403); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if(!$record->isInDb()) { |
|
64
|
|
|
$record->write(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$form->saveInto($record); |
|
68
|
|
|
$record->write(); |
|
69
|
|
|
|
|
70
|
|
|
return $this->RootField()->forTemplate(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function delete($request) { |
|
74
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) { |
|
75
|
|
|
$this->httpError(400); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if(!$request->isPOST()) { |
|
79
|
|
|
$this->httpError(400); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(!$this->record->canDelete()) { |
|
|
|
|
|
|
83
|
|
|
$this->httpError(403); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->record->delete(); |
|
|
|
|
|
|
87
|
|
|
return $this->RootField()->forTemplate(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function RootField() { |
|
91
|
|
|
return $this->parent->RootField(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function Link($action = null) { |
|
95
|
|
|
return Controller::join_links($this->parent->Link(), $this->name, $action); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.