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