Completed
Pull Request — master (#30)
by Lhalaa
07:31
created

PartialFormSubmission::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 25
cts 25
cp 1
rs 9.1127
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Firesphere\PartialUserforms\Models;
4
5
use Exception;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldButtonRow;
11
use SilverStripe\Forms\GridField\GridFieldConfig;
12
use SilverStripe\Forms\GridField\GridFieldDataColumns;
13
use SilverStripe\Forms\GridField\GridFieldExportButton;
14
use SilverStripe\Forms\GridField\GridFieldPrintButton;
15
use SilverStripe\Forms\ReadonlyField;
16
use SilverStripe\ORM\ArrayList;
17
use SilverStripe\ORM\DataList;
18
use SilverStripe\ORM\DataObject;
19
use SilverStripe\Security\Member;
20
use SilverStripe\Security\RandomGenerator;
21
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
22
23
/**
24
 * Class \Firesphere\PartialUserforms\Models\PartialFormSubmission
25
 *
26
 * @property boolean $IsSend
27
 * @property string $TokenSalt
28
 * @property string $Token
29
 * @property int $UserDefinedFormID
30
 * @method DataObject UserDefinedForm()
31
 * @method DataList|PartialFieldSubmission[] PartialFields()
32
 * @method DataList|PartialFileFieldSubmission[] PartialUploads()
33
 */
34
class PartialFormSubmission extends SubmittedForm
35
{
36
    private static $table_name = 'PartialFormSubmission';
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...
37
38
    private static $db = [
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...
39
        'IsSend'    => 'Boolean(false)',
40
        'TokenSalt' => 'Varchar(16)',
41
        'Token'     => 'Varchar(16)',
42
    ];
43
44
    private static $has_one = [
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...
45
        'UserDefinedForm' => DataObject::class
46
    ];
47
48
    private static $has_many = [
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...
49
        'PartialFields'  => PartialFieldSubmission::class,
50
        'PartialUploads' => PartialFileFieldSubmission::class
51
    ];
52
53
    private static $cascade_deletes = [
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...
54
        'PartialFields'
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    private static $summary_fields = [
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...
61
        'ID'          => 'ID',
62
        'PartialLink' => 'Link',
63
        'Created'     => 'Created',
64
        'LastEdited'  => 'Last Edited',
65
    ];
66
67 1
    public function getCMSFields()
68
    {
69
        /** @var FieldList $fields */
70 1
        $fields = parent::getCMSFields();
71 1
        $fields->removeByName([
72 1
            'Values',
73
            'IsSend',
74
            'PartialFields',
75
            'TokenSalt',
76
            'Token',
77
            'UserDefinedFormID',
78
            'Submitter',
79
            'PartialUploads'
80
        ]);
81
82 1
        $partialFields = $this->PartialFields();
83 1
        $fileFields = $this->PartialUploads();
84 1
        $list = ArrayList::create();
85 1
        $list->merge($partialFields);
86 1
        $list->merge($fileFields);
87
88 1
        $partialFields = GridField::create(
89 1
            'PartialFields',
90 1
            _t(static::class . '.PARTIALFIELDS', 'Partial fields'),
91 1
            $list->sort('Created', 'ASC')
92
        );
93
94
        $exportColumns = [
95 1
            'Title'       => 'Title',
96
            'ExportValue' => 'Value'
97
        ];
98
99 1
        $config = new GridFieldConfig();
100 1
        $config->addComponent(new GridFieldDataColumns());
101 1
        $config->addComponent(new GridFieldButtonRow('after'));
102 1
        $config->addComponent(new GridFieldExportButton('buttons-after-left', $exportColumns));
103 1
        $config->addComponent(new GridFieldPrintButton('buttons-after-left'));
104 1
        $partialFields->setConfig($config);
105
106 1
        $fields->addFieldsToTab(
107 1
            'Root.Main',
108
            [
109 1
                ReadonlyField::create('ReadonlyPartialLink', 'Partial Link', $this->getPartialLink()),
110 1
                $partialFields
111
            ]
112
        );
113
114 1
        return $fields;
115
    }
116
117 43
    public function getParent()
118
    {
119 43
        return $this->UserDefinedForm();
120
    }
121
122
    /**
123
     * @param Member $member
124
     * @param array $context
125
     * @return bool
126
     */
127 1
    public function canCreate($member = null, $context = [])
128
    {
129 1
        return false;
130
    }
131
132
    /**
133
     * @param Member
134
     *
135
     * @return boolean|string
136
     */
137 40
    public function canView($member = null)
138
    {
139 40
        if ($this->UserDefinedForm()) {
140 37
            return $this->UserDefinedForm()->canView($member);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->UserDefinedForm()->canView($member); of type boolean|string adds the type string to the return on line 140 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...\SubmittedForm::canView of type boolean.
Loading history...
141
        }
142
143 19
        return parent::canView($member);
144
    }
145
146
    /**
147
     * @param Member
148
     *
149
     * @return boolean|string
150
     */
151 3
    public function canEdit($member = null)
152
    {
153 3
        if ($this->UserDefinedForm()) {
154 3
            return $this->UserDefinedForm()->canEdit($member);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->UserDefinedForm()->canEdit($member); of type boolean|string adds the type string to the return on line 154 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...\SubmittedForm::canEdit of type boolean.
Loading history...
155
        }
156
157
        return parent::canEdit($member);
158
    }
159
160
    /**
161
     * @param Member
162
     *
163
     * @return boolean|string
164
     */
165 3
    public function canDelete($member = null)
166
    {
167 3
        if ($this->UserDefinedForm()) {
168 3
            return $this->UserDefinedForm()->canDelete($member);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->UserDefinedForm()->canDelete($member); of type boolean|string adds the type string to the return on line 168 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...ubmittedForm::canDelete of type boolean.
Loading history...
169
        }
170
171
        return parent::canDelete($member);
172
    }
173
174
    /**
175
     * Get the share link of the form
176
     *
177
     * @return string
178
     * @throws Exception
179
     */
180 4
    public function getPartialLink()
181
    {
182 4
        if (!$this->isInDB()) {
183 1
            return '(none)';
184
        }
185
186 4
        $token = $this->getPartialToken();
187
188 4
        return Controller::join_links(
189 4
            Director::absoluteBaseURL(),
190 4
            'partial',
191 4
            $this->generateKey($token),
0 ignored issues
show
Bug introduced by
It seems like $token defined by $this->getPartialToken() on line 186 can also be of type boolean; however, Firesphere\PartialUserfo...bmission::generateKey() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
192 4
            $token
193
        );
194
    }
195
196
    /**
197
     * Get the unique token for the share link
198
     *
199
     * @return bool|string|null
200
     * @throws Exception
201
     */
202 5
    protected function getPartialToken()
203
    {
204 5
        if (!$this->TokenSalt) {
205 3
            $this->TokenSalt = $this->generateToken();
206 3
            $this->Token = $this->generateToken();
207 3
            $this->write();
208
        }
209
210 5
        return $this->Token;
211
    }
212
213
    /**
214
     * Generate a new token
215
     *
216
     * @return bool|string
217
     * @throws Exception
218
     */
219 4
    protected function generateToken()
220
    {
221 4
        $generator = new RandomGenerator();
222
223 4
        return substr($generator->randomToken('sha256'), 0, 16);
224
    }
225
226
    /**
227
     * Generate a key based on the share token salt
228
     *
229
     * @param string $token
230
     * @return string|bool
231
     */
232 6
    public function generateKey($token)
233
    {
234 6
        return hash_pbkdf2('sha256', $token, $this->TokenSalt, 1000, 16);
235
    }
236
237
    /**
238
     * Get all partial fields for loading data into the form
239
     *
240
     * @return array
241
     */
242 2
    public function getFields()
243
    {
244 2
        $formFields = $this->PartialFields()->map('Name', 'Value')->toArray();
245 2
        $fileFields = $this->PartialUploads()->map('Name', 'FileName')->toArray();
246
247 2
        return array_merge($formFields, $fileFields);
248
    }
249
}
250