Passed
Pull Request — master (#42)
by Loz
03:30
created

ElementForm::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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