-
Notifications
You must be signed in to change notification settings - Fork 70
/
WorkflowFieldItemController.php
114 lines (91 loc) · 2.62 KB
/
WorkflowFieldItemController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Symbiote\AdvancedWorkflow\FormFields;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\SecurityToken;
/**
* Handles individual record data editing or deleting.
*
* @package silverstripe-advancedworkflow
*/
class WorkflowFieldItemController extends Controller
{
private static $allowed_actions = array(
'index',
'edit',
'delete',
'Form'
);
protected $parent;
protected $name;
public function __construct($parent, $name, $record)
{
$this->parent = $parent;
$this->name = $name;
$this->record = $record;
parent::__construct();
}
public function index()
{
return $this->edit();
}
public function edit()
{
return $this->Form()->forTemplate();
}
public function Form()
{
$record = $this->record;
$fields = $record->getCMSFields();
$validator = $record->hasMethod('getValidator') ? $record->getValidator() : null;
$save = FormAction::create('doSave', _t('WorkflowReminderTask.SAVE', 'Save'));
$save->addExtraClass('btn btn-primary font-icon-save')
->setUseButtonTag(true);
$form = Form::create($this, 'Form', $fields, FieldList::create($save), $validator);
if ($record && $record instanceof DataObject && $record->exists()) {
$form->loadDataFrom($record);
}
return $form;
}
public function doSave($data, $form)
{
$record = $form->getRecord();
if (!$record || !$record->exists()) {
$record = $this->record;
}
if (!$record->canEdit()) {
$this->httpError(403);
}
if (!$record->isInDb()) {
$record->write();
}
$form->saveInto($record);
$record->write();
return $this->RootField()->forTemplate();
}
public function delete($request)
{
if (!SecurityToken::inst()->checkRequest($request)) {
$this->httpError(400);
}
if (!$request->isPOST()) {
$this->httpError(400);
}
if (!$this->record->canDelete()) {
$this->httpError(403);
}
$this->record->delete();
return $this->RootField()->forTemplate();
}
public function RootField()
{
return $this->parent->RootField();
}
public function Link($action = null)
{
return Controller::join_links($this->parent->Link(), $this->name, $action);
}
}