Completed
Push — master ( 81a72c...02e7ff )
by
unknown
12s
created

ElementFormController::setUserFormController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\ElementalUserForms\Control;
4
5
use DNADesign\Elemental\Controllers\ElementController;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\UserForms\Control\UserDefinedFormController;
10
use SilverStripe\UserForms\Form\UserForm;
11
12
class ElementFormController extends ElementController
13
{
14
    private static $allowed_actions = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
        'Form',
16
        'process',
17
        'finished'
18
    ];
19
20
    /**
21
     * @var UserDefinedFormController
22
     */
23
    protected $userFormController;
24
25
    protected function init()
26
    {
27
        parent::init();
28
29
        $controller = $this->getUserFormController() ?: UserDefinedFormController::create($this->element);
30
        $controller->setRequest($this->getRequest());
31
        $controller->doInit();
32
33
        $this->setUserFormController($controller);
34
    }
35
36
    /**
37
     * @return UserForm
38
     */
39
    public function Form()
40
    {
41
        return $this->getUserFormController()->Form();
42
    }
43
44
    public function process($data)
45
    {
46
        $user = $this->getUserFormController();
47
48
        return $user->process($data, $user->Form());
49
    }
50
51
    public function finished()
52
    {
53
        $user = $this->getUserFormController();
54
55
        $user->finished();
56
57
        $page = $this->getPage();
0 ignored issues
show
Bug introduced by
The method getPage() does not exist on DNADesign\ElementalUserF...l\ElementFormController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $page = $this->getPage();
Loading history...
58
        $controller = Injector::inst()->create($page->getControllerName(), $this->element->getPage());
59
        $element = $this->element;
60
61
        return $controller->customise([
62
            'Content' => $element->renderWith($element->getRenderTemplates('_ReceivedFormSubmission')),
63
        ]);
64
    }
65
66
    /**
67
     * @param string $action
68
     *
69
     * @return string
70
     */
71
    public function Link($action = null)
72
    {
73
        $id = $this->element->ID;
74
        $segment = Controller::join_links('element', $id, $action);
75
        $page = Director::get_current_page();
76
77
        if ($page && !($page instanceof ElementController)) {
78
            return $page->Link($segment);
79
        }
80
81
        if ($controller = $this->getParentController()) {
0 ignored issues
show
Bug introduced by
The method getParentController() does not exist on DNADesign\ElementalUserF...l\ElementFormController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        if ($controller = $this->/** @scrutinizer ignore-call */ getParentController()) {
Loading history...
82
            return $controller->Link($segment);
83
        }
84
85
        return $segment;
86
    }
87
88
    /**
89
     * Return the associated UserDefinedFormController
90
     *
91
     * @return UserDefinedFormController
92
     */
93
    public function getUserFormController()
94
    {
95
        return $this->userFormController;
96
    }
97
98
    /**
99
     * Set the associated UserDefinedFormController
100
     *
101
     * @param UserDefinedFormController $controller
102
     * @return $this
103
     */
104
    public function setUserFormController(UserDefinedFormController $controller)
105
    {
106
        $this->userFormController = $controller;
107
        return $this;
108
    }
109
}
110