|
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
|
|
|
// Use CMS tabset template which stops tabs from being rendered twice |
|
54
|
|
|
// Copied from GridFieldDetailForm_ItemRequest |
|
55
|
|
|
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
|
56
|
|
|
}); |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
return $this->userFormGetCMSFields(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return UserForm |
|
64
|
|
|
*/ |
|
65
|
|
|
public function Form() |
|
66
|
|
|
{ |
|
67
|
|
|
$controller = UserDefinedFormController::create($this); |
|
68
|
|
|
$current = Controller::curr(); |
|
69
|
|
|
$controller->setRequest($current->getRequest()); |
|
70
|
|
|
|
|
71
|
|
|
if ($current && $current->getAction() == 'finished') { |
|
72
|
|
|
return $controller->renderWith(UserDefinedFormController::class .'_ReceivedFormSubmission'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$form = $controller->Form(); |
|
76
|
|
|
$form->setFormAction( |
|
77
|
|
|
Controller::join_links( |
|
78
|
|
|
$current->Link(), |
|
79
|
|
|
'element', |
|
80
|
|
|
$this->owner->ID, |
|
|
|
|
|
|
81
|
|
|
'Form' |
|
82
|
|
|
) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $form; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function Link($action = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$current = Controller::curr(); |
|
91
|
|
|
|
|
92
|
|
|
if ($action === 'finished') { |
|
93
|
|
|
return Controller::join_links( |
|
94
|
|
|
$current->Link(), |
|
95
|
|
|
'finished' |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return parent::Link($action); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getType() |
|
103
|
|
|
{ |
|
104
|
|
|
return _t(__CLASS__ . '.BlockType', 'Form'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|