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

ElementForm::Link()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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