Completed
Pull Request — master (#12)
by Simon
01:22
created

getPartialFormSubmission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\PartialUserforms\Controllers;
4
5
use Exception;
6
use Firesphere\PartialUserforms\Forms\PasswordForm;
7
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
8
use Page;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Control\HTTPResponse_Exception;
12
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
13
use SilverStripe\Forms\Form;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\ORM\FieldType\DBField;
16
use SilverStripe\ORM\FieldType\DBHTMLText;
17
use SilverStripe\UserForms\Control\UserDefinedFormController;
18
use SilverStripe\UserForms\Model\UserDefinedForm;
19
20
/**
21
 * Class PartialUserFormController
22
 *
23
 * @package Firesphere\PartialUserforms\Controllers
24
 */
25
class PartialUserFormController extends UserDefinedFormController
26
{
27
28
    /**
29
     * @var array
30
     */
31
    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...
32
        '$Key/$Token' => 'partial',
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
        'partial',
39
    ];
40
    /**
41
     * @var PartialFormSubmission
42
     */
43
    protected $partialFormSubmission;
44
45
    /**
46
     * Partial form
47
     *
48
     * @param HTTPRequest $request
49
     * @return HTTPResponse|DBHTMLText|void
50
     * @throws Exception
51
     */
52
    public function partial(HTTPRequest $request)
53
    {
54
        /** @var PartialFormSubmission $partial */
55
        $partial = $this->setData($request);
56
        if ($this->dataRecord->PasswordProtected &&
57
            $request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID
58
        ) {
59
            return $this->redirect('verify');
60
        }
61
62
        /** @var Form $form */
63
        $form = $this->Form();
64
        $fields = $partial->PartialFields()->map('Name', 'Value')->toArray();
65
        $form->loadDataFrom($fields);
66
67
        // Copied from {@link UserDefinedFormController}
68
        if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) {
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. 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...
69
            $hasLocation = stristr($this->Content, '$UserDefinedForm');
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. 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
            if ($hasLocation) {
71
                /** @see Requirements_Backend::escapeReplacement */
72
                $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$');
73
                $content = preg_replace(
74
                    '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i',
75
                    $formEscapedForRegex,
76
                    $this->Content
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. 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...
77
                );
78
79
                return $this->customise([
80
                    'Content'     => DBField::create_field('HTMLText', $content),
81
                    'Form'        => '',
82
                    'PartialLink' => $partial->getPartialLink()
83
                ])->renderWith([static::class, Page::class]);
84
            }
85
        }
86
87
        return $this->customise([
88
            'Content'     => DBField::create_field('HTMLText', $this->Content),
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. 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...
89
            'Form'        => $form,
90
            'PartialLink' => $partial->getPartialLink()
91
        ])->renderWith([static::class, Page::class]);
92
    }
93
94
    /**
95
     * A little abstraction to be more readable
96
     *
97
     * @param HTTPRequest $request
98
     * @return PartialFormSubmission|void
99
     * @throws HTTPResponse_Exception
100
     */
101
    public function setData($request)
102
    {
103
        // Ensure this URL doesn't get picked up by HTTP caches
104
        HTTPCacheControlMiddleware::singleton()->disableCache();
105
106
        $key = $request->param('Key');
107
        $token = $request->param('Token');
108
109
        /** @var PartialFormSubmission $partial */
110
        $partial = PartialFormSubmission::get()->find('Token', $token);
111
        if (!$token ||
112
            !$partial ||
113
            !$partial->UserDefinedFormID ||
114
            !hash_equals($partial->generateKey($token), $key)
115
        ) {
116
            return $this->httpError(404);
117
        }
118
        /** @var UserDefinedForm $dataRecord */
119
        $dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID);
0 ignored issues
show
Documentation introduced by
The property UserDefinedFormClass 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...
120
        // @todo look at to solve this. Also breaks on CircleCI
121
//        $this->setFailover($dataRecord);
122
        $this->dataRecord = $dataRecord;
123
124
        $session = $this->getRequest()->getSession();
125
        // Set the session if the last session has expired
126
        if (!$session->get(PartialSubmissionController::SESSION_KEY)) {
127
            $session->set(PartialSubmissionController::SESSION_KEY, $partial->ID);
128
        }
129
130
        $this->setPartialFormSubmission($partial);
131
        // Set data record and load the form
132
133
        /** @var UserDefinedForm dataRecord */
134
135
        return $partial;
136
    }
137
138
    /**
139
     * @return PartialFormSubmission
140
     */
141
    public function getPartialFormSubmission(): PartialFormSubmission
142
    {
143
        return $this->partialFormSubmission;
144
    }
145
146
    /**
147
     * @param PartialFormSubmission $partialFormSubmission
148
     */
149
    public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void
150
    {
151
        $this->partialFormSubmission = $partialFormSubmission;
152
    }
153
}
154