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

PartialFormSubmission   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 0
loc 165
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 28 1
A getParent() 0 4 1
A onBeforeWrite() 0 17 4
A canCreate() 0 8 2
A canView() 0 8 2
A canEdit() 0 8 2
A canDelete() 0 8 2
1
<?php
2
3
namespace Firesphere\PartialUserforms\Models;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig;
9
use SilverStripe\Forms\GridField\GridFieldDataColumns;
10
use SilverStripe\Forms\GridField\GridFieldExportButton;
11
use SilverStripe\Forms\GridField\GridFieldPrintButton;
12
use SilverStripe\ORM\DataList;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Security\Member;
15
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
16
use SilverStripe\UserForms\Model\UserDefinedForm;
17
18
/**
19
 * Class \Firesphere\PartialUserforms\Models\PartialFormSubmission
20
 *
21
 * @property boolean $IsSend
22
 * @property string $Password
23
 * @property int $UserDefinedFormID
24
 * @method DataObject UserDefinedForm()
25
 * @method DataList|PartialFieldSubmission[] PartialFields()
26
 */
27
class PartialFormSubmission extends SubmittedForm
28
{
29
    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...
30
31
    /**
32
     * @var array
33
     */
34
    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...
35
        'IsSend'   => 'Boolean(false)',
36
        'Password' => 'Varchar(64)',
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    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...
43
        'UserDefinedForm' => DataObject::class
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    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...
50
        'PartialFields' => PartialFieldSubmission::class
51
    ];
52
53
    private static $special_characters = [
54
        '!',
55
        '@',
56
        '#',
57
        '$',
58
        '%',
59
        '^',
60
        '&',
61
        '*',
62
        '(',
63
        ')',
64
        '_',
65
        '-',
66
        '=',
67
        '+',
68
        ';',
69
        ':',
70
        ',',
71
        '.',
72
        '?'
73
    ];
74
75
    /**
76
     * @return FieldList
77
     */
78
    public function getCMSFields()
79
    {
80
        /** @var FieldList $fields */
81
        $fields = parent::getCMSFields();
82
        $fields->removeByName(['Values', 'IsSend', 'PartialFields']);
83
84
        $values = GridField::create(
85
            'PartialFields',
86
            _t(static::class . '.PARTIALFIELDS', 'Partial fields'),
87
            $this->PartialFields()->sort('Created', 'ASC')
88
        );
89
90
        $exportColumns = array(
91
            'Title'       => 'Title',
92
            'ExportValue' => 'Value'
93
        );
94
95
        $config = new GridFieldConfig();
96
        $config->addComponent(new GridFieldDataColumns());
97
        $config->addComponent(new GridFieldExportButton('after', $exportColumns));
98
        $config->addComponent(new GridFieldPrintButton());
99
100
        $values->setConfig($config);
101
102
        $fields->addFieldToTab('Root.Main', $values);
103
104
        return $fields;
105
    }
106
107
    /**
108
     * @return DataObject|UserDefinedForm
109
     */
110
    public function getParent()
111
    {
112
        return $this->UserDefinedForm();
113
    }
114
115
    /**
116
     * If the submission is password protected, generate a password.
117
     */
118
    public function onBeforeWrite()
119
    {
120
        parent::onBeforeWrite();
121
        if (!$this->Password &&
122
            $this->getParent() &&
123
            $this->getParent()->PasswordProtected
124
        ) {
125
            $chars = range('A', 'Z');
126
            $chars = array_merge($chars, range('a', 'z'));
127
            $chars = array_merge($chars, range(0, 9));
128
            $chars = array_merge($chars, self::$special_characters);
129
            shuffle($chars);
130
            $pwd = implode(array_slice($chars, 0, 10));
131
            $this->Password = hash('SHA256', $pwd);
132
            Controller::curr()->getRequest()->getSession()->set('FormPassword', $pwd);
133
        }
134
    }
135
136
    /**
137
     * @param Member
138
     *
139
     * @return boolean|string
140
     */
141
    public function canCreate($member = null, $context = [])
142
    {
143
        if ($this->UserDefinedForm()) {
144
            return $this->UserDefinedForm()->canCreate($member, $context);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->UserDefinedForm()...ate($member, $context); of type boolean|string adds the type string to the return on line 144 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...ubmittedForm::canCreate of type boolean.
Loading history...
145
        }
146
147
        return parent::canCreate($member);
148
    }
149
150
    /**
151
     * @param Member
152
     *
153
     * @return boolean|string
154
     */
155
    public function canView($member = null)
156
    {
157
        if ($this->UserDefinedForm()) {
158
            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 158 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...\SubmittedForm::canView of type boolean.
Loading history...
159
        }
160
161
        return parent::canView($member);
162
    }
163
164
    /**
165
     * @param Member
166
     *
167
     * @return boolean|string
168
     */
169
    public function canEdit($member = null)
170
    {
171
        if ($this->UserDefinedForm()) {
172
            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 172 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...\SubmittedForm::canEdit of type boolean.
Loading history...
173
        }
174
175
        return parent::canEdit($member);
176
    }
177
178
    /**
179
     * @param Member
180
     *
181
     * @return boolean|string
182
     */
183
    public function canDelete($member = null)
184
    {
185
        if ($this->UserDefinedForm()) {
186
            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 186 which is incompatible with the return type of the parent method SilverStripe\UserForms\M...ubmittedForm::canDelete of type boolean.
Loading history...
187
        }
188
189
        return parent::canDelete($member);
190
    }
191
}
192