Completed
Push — master ( c07b10...3eba7c )
by Simon
13s
created

PartialUserFormController::partial()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace Firesphere\PartialUserforms\Controllers;
4
5
use Firesphere\PartialUserforms\Models\PartialFieldSubmission;
6
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
7
use SilverStripe\CMS\Controllers\ContentController;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\ValidationException;
11
use SilverStripe\UserForms\Control\UserDefinedFormController;
12
use SilverStripe\UserForms\Model\EditableFormField;
13
use SilverStripe\View\Requirements;
14
15
/**
16
 * Class PartialUserFormController
17
 * @package Firesphere\PartialUserforms\Controllers
18
 */
19
class PartialUserFormController extends ContentController
20
{
21
    /**
22
     * Session key name
23
     */
24
    const SESSION_KEY = 'PartialSubmissionID';
25
26
    /**
27
     * @var array
28
     */
29
    private static $url_handlers = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
30
        'save' => 'savePartialSubmission',
31
        '$Key/$Token' => 'partial',
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
38
        'savePartialSubmission',
39
        'partial',
40
    ];
41
42
    /**
43
     * @param HTTPRequest $request
44
     * @return int|mixed|void
45
     * @throws ValidationException
46
     * @throws \SilverStripe\Control\HTTPResponse_Exception
47
     */
48
    public function savePartialSubmission(HTTPRequest $request)
49
    {
50
        if (!$request->isPOST()) {
51
            return $this->httpError(404);
52
        }
53
54
        $postVars = $request->postVars();
55
        $editableField = null;
56
57
        // We don't want SecurityID and/or the process Action stored as a thing
58
        unset($postVars['SecurityID'], $postVars['action_process']);
59
        $submissionID = $request->getSession()->get(self::SESSION_KEY);
60
61
        /** @var PartialFormSubmission $partialSubmission */
62
        $partialSubmission = PartialFormSubmission::get()->byID($submissionID);
63
64
        if (!$submissionID || !$partialSubmission) {
65
            $partialSubmission = PartialFormSubmission::create();
66
            $submissionID = $partialSubmission->write();
67
        }
68
        $request->getSession()->set(self::SESSION_KEY, $submissionID);
69
        foreach ($postVars as $field => $value) {
70
            /** @var EditableFormField $editableField */
71
            $editableField = $this->createOrUpdateSubmission([
72
                'Name'            => $field,
73
                'Value'           => $value,
74
                'SubmittedFormID' => $submissionID
75
            ]);
76
        }
77
78
        if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) {
79
            $partialSubmission->update([
80
                'UserDefinedFormID'    => $editableField->Parent()->ID,
81
                'ParentID'             => $editableField->Parent()->ID,
82
                'ParentClass'          => $editableField->Parent()->ClassName,
83
                'UserDefinedFormClass' => $editableField->Parent()->ClassName
84
            ]);
85
            $partialSubmission->write();
86
        }
87
88
        return $submissionID;
89
    }
90
91
    /**
92
     * @param $formData
93
     * @return DataObject|EditableFormField
94
     * @throws ValidationException
95
     */
96
    protected function createOrUpdateSubmission($formData)
97
    {
98
        if (is_array($formData['Value'])) {
99
            $formData['Value'] = implode(', ', $formData['Value']);
100
        }
101
102
        $filter = [
103
            'Name'            => $formData['Name'],
104
            'SubmittedFormID' => $formData['SubmittedFormID'],
105
        ];
106
107
        $exists = PartialFieldSubmission::get()->filter($filter)->first();
108
        // Set the title
109
        $editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first();
110
        if ($editableField) {
111
            $formData['Title'] = $editableField->Title;
112
            $formData['ParentClass'] = $editableField->Parent()->ClassName;
113
        }
114
        if (!$exists) {
115
            $exists = PartialFieldSubmission::create($formData);
116
            $exists->write();
117
        } else {
118
            $exists->update($formData);
119
            $exists->write();
120
        }
121
122
        // Return the ParentID to link the PartialSubmission to it's proper thingy
123
        return $editableField;
124
    }
125
126
    /**
127
     * Partial form
128
     *
129
     * @param HTTPRequest $request
130
     * @return \SilverStripe\ORM\FieldType\DBHTMLText|void
131
     * @throws \SilverStripe\Control\HTTPResponse_Exception
132
     */
133
    public function partial(HTTPRequest $request)
134
    {
135
        $key = $request->param('Key');
136
        $token = $request->param('Token');
137
138
        $partial = PartialFormSubmission::get()->find('Token', $token);
139
        if (!$token || !$partial || !$partial->UserDefinedFormID) {
140
            return $this->httpError(404);
141
        }
142
143
        if ($partial->generateKey($token) === $key) {
144
            // Set the session if the last session has expired
145
            if (!$request->getSession()->get(self::SESSION_KEY)) {
146
                $request->getSession()->set(self::SESSION_KEY, $partial->ID);
147
            }
148
149
            // TODO: Recognize visitor with the password
150
            // TODO: Populate form values
151
152
            $record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID);
153
            $controller = new UserDefinedFormController($record);
154
            $controller->init();
0 ignored issues
show
Bug introduced by
The method init() cannot be called from this context as it is declared protected in class SilverStripe\UserForms\C...erDefinedFormController.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
155
156
            Requirements::javascript('firesphere/partialuserforms:client/dist/main.js');
157
158
            return $this->customise([
159
                'Title' => $record->Title,
160
                'Breadcrumbs' => $record->Breadcrumbs(),
161
                'Content' => $this->obj('Content'),
162
                'Form' => $controller->Form(),
163
                'Link' => $partial->getPartialLink()
164
            ])->renderWith(['PartialUserForm', 'Page']);
165
        } else {
166
            return $this->httpError(404);
167
        }
168
    }
169
}
170