Completed
Push — master ( 2f705a...c06b8c )
by Simon
01:46
created

createOrUpdateSubmission()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 17
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\Model\EditableFormField;
12
13
/**
14
 * Class \Firesphere\PartialUserforms\Controllers\PartialUserFormController
15
 *
16
 */
17
class PartialUserFormController extends ContentController
18
{
19
    /**
20
     * Session key name
21
     */
22
    const SESSION_KEY = 'PartialSubmissionID';
23
24
    /**
25
     * @var array
26
     */
27
    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...
Unused Code introduced by
The property $url_handlers 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...
28
        '' => 'savePartialSubmission'
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    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...
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...
35
        'savePartialSubmission'
36
    ];
37
38
    /**
39
     * @param HTTPRequest $request
40
     * @return int
41
     * @throws ValidationException
42
     */
43
    public function savePartialSubmission(HTTPRequest $request)
44
    {
45
        $postVars = $request->postVars();
46
        $editableField = null;
47
48
        // We don't want SecurityID and/or the process Action stored as a thing
49
        unset($postVars['SecurityID'], $postVars['action_process']);
50
        $submissionID = $request->getSession()->get(self::SESSION_KEY);
51
52
        /** @var PartialFormSubmission $partialSubmission */
53
        $partialSubmission = PartialFormSubmission::get()->byID($submissionID);
54
55
        if (!$submissionID || !$partialSubmission) {
56
            $partialSubmission = PartialFormSubmission::create();
57
            $submissionID = $partialSubmission->write();
58
        }
59
        $request->getSession()->set(self::SESSION_KEY, $submissionID);
60
        foreach ($postVars as $field => $value) {
61
            /** @var EditableFormField $editableField */
62
            $editableField = $this->createOrUpdateSubmission([
63
                'Name'            => $field,
64
                'Value'           => $value,
65
                'SubmittedFormID' => $submissionID
66
            ]);
67
        }
68
69
        if ($editableField instanceof EditableFormField && !$partialSubmission->ParentID) {
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<Firesphere\Partia...\PartialFormSubmission>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
            $partialSubmission->update([
71
                'ParentID'    => $editableField->Parent()->ParentID,
72
                'ParentClass' => $editableField->Parent()->ClassName
73
            ])->write();
74
        }
75
76
        return $submissionID;
77
    }
78
79
    /**
80
     * @param $formData
81
     * @return DataObject|EditableFormField
82
     * @throws ValidationException
83
     */
84
    protected function createOrUpdateSubmission($formData)
85
    {
86
        if (is_array($formData['Value'])) {
87
            $formData['Value'] = implode(', ', $formData['Value']);
88
        }
89
90
        $filter = [
91
            'Name'            => $formData['Name'],
92
            'SubmittedFormID' => $formData['SubmittedFormID'],
93
        ];
94
95
        $exists = PartialFieldSubmission::get()->filter($filter)->first();
96
        // Set the title
97
        $editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first();
98
        $formData['Title'] = $editableField->Title;
99
        $formData['ParentClass'] = $editableField->Parent()->ClassName;
100
        if (!$exists) {
101
            $exists = PartialFieldSubmission::create($formData);
102
            $exists->write();
103
        } else {
104
            $exists->update($formData);
105
            $exists->write();
106
        }
107
108
        // Return the ParentID to link the PartialSubmission to it's proper thingy
109
        return $editableField;
110
    }
111
}
112