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

ElementForm::getCMSFields()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 1
nop 0
dl 0
loc 35
rs 9.2222
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
                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,
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...
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