Passed
Pull Request — master (#42)
by Loz
04:00 queued 01:28
created

ElementForm::getCMSFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 1
nop 0
dl 0
loc 27
rs 9.9
c 0
b 0
f 0
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 {
0 ignored issues
show
introduced by
The trait SilverStripe\UserForms\UserForm requires some properties which are not provided by DNADesign\ElementalUserForms\Model\ElementForm: $Name, $SubmitButtonText, $OnCompleteMessage, $ShowInSummary, $ShowClearButton, $ClearButtonText
Loading history...
18
        getCMSFields as userFormGetCMSFields;
19
    }
20
21
    private static $table_name = 'ElementForm';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    private static $icon = 'font-icon-block-form';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
24
25
    private static $controller_class = ElementFormController::class;
0 ignored issues
show
introduced by
The private property $controller_class is not used, and could be removed.
Loading history...
26
27
    private static $singular_name = 'form';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
28
29
    private static $plural_name = 'forms';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
30
31
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
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,
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on DNADesign\ElementalUserForms\Model\ElementForm. Since you implemented __get, consider adding a @property annotation.
Loading history...
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