Completed
Push — master ( 016a0b...25fc98 )
by Franco
12s
created

SubmittedForm::canDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 7
Ratio 87.5 %

Importance

Changes 0
Metric Value
dl 7
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\Model\Submission;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\GridField\GridFieldConfig;
7
use SilverStripe\Forms\GridField\GridFieldDataColumns;
8
use SilverStripe\Forms\GridField\GridFieldExportButton;
9
use SilverStripe\Forms\GridField\GridFieldPrintButton;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\Security\Member;
13
use SilverStripe\UserForms\Model\UserDefinedForm;
14
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
15
16
/**
17
 * Contents of an UserDefinedForm submission
18
 *
19
 * @package userforms
20
 */
21
class SubmittedForm extends DataObject
22
{
23
    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...
Unused Code introduced by
The property $has_one 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...
24
        'SubmittedBy' => Member::class,
25
        'Parent' => UserDefinedForm::class,
26
    ];
27
28
    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...
Unused Code introduced by
The property $has_many 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...
29
        'Values' => SubmittedFormField::class
30
    ];
31
32
    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...
Unused Code introduced by
The property $cascade_deletes 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...
33
        'Values',
34
    ];
35
36
    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...
Unused Code introduced by
The property $summary_fields 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...
37
        'ID',
38
        'Created'
39
    ];
40
41
    private static $table_name = 'SubmittedForm';
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 $table_name 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...
42
43
    /**
44
     * Returns the value of a relation or, in the case of this form, the value
45
     * of a given child {@link SubmittedFormField}
46
     *
47
     * @param string
48
     *
49
     * @return mixed
50
     */
51
    public function relField($fieldName)
52
    {
53
        // default case
54
        if ($value = parent::relField($fieldName)) {
55
            return $value;
56
        }
57
58
        // check values for a form field with the matching name.
59
        $formField = SubmittedFormField::get()->filter(array(
60
            'ParentID' => $this->ID,
61
            'Name' => $fieldName
62
        ))->first();
63
64
        if ($formField) {
65
            return $formField->getFormattedValue();
66
        }
67
    }
68
69
    /**
70
     * @return FieldList
71
     */
72
    public function getCMSFields()
73
    {
74
        $this->beforeUpdateCMSFields(function ($fields) {
75
            $fields->removeByName('Values');
76
77
            //check to ensure there is a Member to extract an Email from else null value
78
            if ($this->SubmittedBy() && $this->SubmittedBy()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method SubmittedBy does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
                $submitter = $this->SubmittedBy()->Email;
0 ignored issues
show
Documentation Bug introduced by
The method SubmittedBy does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
            } else {
81
                $submitter = null;
82
            }
83
84
            //replace scaffolded field with readonly submitter
85
            $fields->replaceField(
86
                'SubmittedByID',
87
                ReadonlyField::create(
88
                    'Submitter',
89
                    'Submitter',
90
                    $submitter
91
                )
92
            );
93
94
            $values = GridField::create(
95
                'Values',
96
                SubmittedFormField::class,
97
                $this->Values()->sort('Created', 'ASC')
0 ignored issues
show
Bug introduced by
The method Values() does not exist on SilverStripe\UserForms\M...ubmission\SubmittedForm. Did you maybe mean getXMLValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
            );
99
100
            $exportColumns = array(
101
                'Title' => 'Title',
102
                'ExportValue' => 'Value'
103
            );
104
105
            $config = new GridFieldConfig();
106
            $config->addComponent(new GridFieldDataColumns());
107
            $config->addComponent(new GridFieldExportButton('after', $exportColumns));
108
            $config->addComponent(new GridFieldPrintButton());
109
            $values->setConfig($config);
110
111
            $fields->addFieldToTab('Root.Main', $values);
112
        });
113
114
        $fields = parent::getCMSFields();
115
116
        return $fields;
117
    }
118
119
    /**
120
     * @param Member
121
     *
122
     * @return boolean
123
     */
124 View Code Duplication
    public function canCreate($member = null, $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $extended = $this->extendedCan(__FUNCTION__, $member);
127
        if ($extended !== null) {
128
            return $extended;
129
        }
130
        return $this->Parent()->canCreate();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
131
    }
132
133
    /**
134
     * @param Member
135
     *
136
     * @return boolean
137
     */
138 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $extended = $this->extendedCan(__FUNCTION__, $member);
141
        if ($extended !== null) {
142
            return $extended;
143
        }
144
        return $this->Parent()->canView();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
145
    }
146
147
    /**
148
     * @param Member
149
     *
150
     * @return boolean
151
     */
152 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $extended = $this->extendedCan(__FUNCTION__, $member);
155
        if ($extended !== null) {
156
            return $extended;
157
        }
158
        return $this->Parent()->canEdit();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
159
    }
160
161
    /**
162
     * @param Member
163
     *
164
     * @return boolean
165
     */
166 View Code Duplication
    public function canDelete($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        $extended = $this->extendedCan(__FUNCTION__, $member);
169
        if ($extended !== null) {
170
            return $extended;
171
        }
172
        return $this->Parent()->canDelete();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...bmission\SubmittedForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
173
    }
174
175
    /**
176
     * Before we delete this form make sure we delete all the
177
     * field values so that we don't leave old data round
178
     *
179
     * @return void
180
     */
181
    protected function onBeforeDelete()
182
    {
183
        if ($this->Values()) {
0 ignored issues
show
Bug introduced by
The method Values() does not exist on SilverStripe\UserForms\M...ubmission\SubmittedForm. Did you maybe mean getXMLValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
184
            foreach ($this->Values() as $value) {
0 ignored issues
show
Bug introduced by
The method Values() does not exist on SilverStripe\UserForms\M...ubmission\SubmittedForm. Did you maybe mean getXMLValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
185
                $value->delete();
186
            }
187
        }
188
189
        parent::onBeforeDelete();
190
    }
191
}
192