1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalUserForms\Model; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use DNADesign\ElementalUserForms\Control\ElementFormController; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\Form; |
10
|
|
|
use SilverStripe\Forms\GridField\GridField; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
12
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
13
|
|
|
use SilverStripe\UserForms\UserForm; |
14
|
|
|
|
15
|
|
|
class ElementForm extends BaseElement |
16
|
|
|
{ |
17
|
|
|
use UserForm { |
|
|
|
|
18
|
|
|
getCMSFields as userFormGetCMSFields; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private static $table_name = 'ElementForm'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
private static $icon = 'font-icon-block-form'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $controller_class = ElementFormController::class; |
|
|
|
|
26
|
|
|
|
27
|
|
|
private static $singular_name = 'form'; |
|
|
|
|
28
|
|
|
|
29
|
|
|
private static $plural_name = 'forms'; |
|
|
|
|
30
|
|
|
|
31
|
|
|
private static $inline_editable = false; |
|
|
|
|
32
|
|
|
|
33
|
|
|
public function getCMSFields() |
34
|
|
|
{ |
35
|
|
|
$this->afterExtending('updateCMSFields', function (FieldList $fields) { |
36
|
|
|
/** @var GridField $recipientsGridField */ |
37
|
|
|
$recipientsGridField = $fields->dataFieldByName('EmailRecipients'); |
38
|
|
|
/** @var GridFieldDetailForm $detailForm */ |
39
|
|
|
$detailForm = $recipientsGridField->getConfig()->getComponentByType(GridFieldDetailForm::class); |
40
|
|
|
|
41
|
|
|
// Re-build CMS fields with "parent" form record populated |
42
|
|
|
$detailForm->setItemEditFormCallback(function (Form $form) { |
43
|
|
|
$record = $form->getRecord(); |
44
|
|
|
|
45
|
|
|
// See EmailRecipient::getFormParent() for why this is necessary |
46
|
|
|
$record->FormID = $this->ID; |
47
|
|
|
$record->FormClass = $this->ClassName; |
48
|
|
|
|
49
|
|
|
// Re-build CMS fields |
50
|
|
|
$form->setFields($record->getCMSFields()); |
51
|
|
|
$form->loadDataFrom($record, $record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT); |
52
|
|
|
|
53
|
|
|
if ($record->ID && !$record->canEdit()) { |
54
|
|
|
// Restrict editing of existing records |
55
|
|
|
$form->makeReadonly(); |
56
|
|
|
} elseif (!$record->ID && !$record->canCreate()) { |
57
|
|
|
// Restrict creation of new records |
58
|
|
|
$form->makeReadonly(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Use CMS tabset template which stops tabs from being rendered twice |
62
|
|
|
// Copied from GridFieldDetailForm_ItemRequest |
63
|
|
|
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
64
|
|
|
}); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
return $this->userFormGetCMSFields(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return UserForm |
72
|
|
|
*/ |
73
|
|
|
public function Form() |
74
|
|
|
{ |
75
|
|
|
$controller = UserDefinedFormController::create($this); |
76
|
|
|
$current = Controller::curr(); |
77
|
|
|
$controller->setRequest($current->getRequest()); |
78
|
|
|
|
79
|
|
|
if ($current && $current->getAction() == 'finished') { |
80
|
|
|
return $controller->renderWith(UserDefinedFormController::class .'_ReceivedFormSubmission'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$form = $controller->Form(); |
84
|
|
|
$form->setFormAction( |
85
|
|
|
Controller::join_links( |
86
|
|
|
$current->Link(), |
87
|
|
|
'element', |
88
|
|
|
$this->owner->ID, |
|
|
|
|
89
|
|
|
'Form' |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $form; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function Link($action = null) |
97
|
|
|
{ |
98
|
|
|
$current = Controller::curr(); |
99
|
|
|
|
100
|
|
|
if ($action === 'finished') { |
101
|
|
|
return Controller::join_links( |
102
|
|
|
$current->Link(), |
103
|
|
|
'finished' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return parent::Link($action); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getType() |
111
|
|
|
{ |
112
|
|
|
return _t(__CLASS__ . '.BlockType', 'Form'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|