Completed
Push — master ( 2f1b03...8d4cd0 )
by Robbie
33:42
created

SubmittedForm::getCMSFields()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 28
cp 0
rs 9.125
c 0
b 0
f 0
cc 3
eloc 29
nc 1
nop 0
crap 12
1
<?php
2
/**
3
 * Contents of an UserDefinedForm submission
4
 *
5
 * @package userforms
6
 */
7
8
class SubmittedForm extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    private static $has_one = array(
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...
12
        "SubmittedBy" => "Member",
13
        "Parent" => "UserDefinedForm",
14
    );
15
16
    private static $has_many = array(
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...
17
        "Values" => "SubmittedFormField"
18
    );
19
20
    private static $summary_fields = array(
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...
21
        'ID',
22
        'Created'
23
    );
24
25
    /**
26
     * Returns the value of a relation or, in the case of this form, the value
27
     * of a given child {@link SubmittedFormField}
28
     *
29
     * @param string
30
     *
31
     * @return mixed
32
     */
33
    public function relField($fieldName)
34
    {
35
        // default case
36
        if ($value = parent::relField($fieldName)) {
37
            return $value;
38
        }
39
40
        // check values for a form field with the matching name.
41
        $formField = SubmittedFormField::get()->filter(array(
42
            'ParentID' => $this->ID,
43
            'Name' => $fieldName
44
        ))->first();
45
46
        if ($formField) {
47
            return $formField->getFormattedValue();
48
        }
49
    }
50
51
    /**
52
     * @return FieldList
53
     */
54
    public function getCMSFields()
55
    {
56
        $self = $this;
57
58
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
59
            $fields->removeByName('Values');
60
61
            //check to ensure there is a Member to extract an Email from else null value
62
            if($self->SubmittedBy() && $self->SubmittedBy()->exists()){
0 ignored issues
show
Documentation Bug introduced by
The method SubmittedBy does not exist on object<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...
63
                $submitter =  $self->SubmittedBy()->Email;
0 ignored issues
show
Documentation Bug introduced by
The method SubmittedBy does not exist on object<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...
64
            } else {
65
                $submitter = null;
66
            }
67
68
            //replace scaffolded field with readonly submitter
69
            $fields->replaceField(
70
                'SubmittedByID',
71
                ReadonlyField::create(
72
                    'Submitter',
73
                    'Submitter',
74
                    $submitter
75
                )
76
            );
77
78
            $values = GridField::create(
79
                'Values',
80
                'SubmittedFormField',
81
                $self->Values()->sort('Created', 'ASC')
0 ignored issues
show
Bug introduced by
The method Values() does not exist on 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...
82
            );
83
84
            $exportColumns = array(
85
                'Title' => 'Title',
86
                'ExportValue' => 'Value'
87
            );
88
89
            $config = new GridFieldConfig();
90
            $config->addComponent(new GridFieldDataColumns());
91
            $config->addComponent(new GridFieldExportButton('after', $exportColumns));
92
            $config->addComponent(new GridFieldPrintButton());
93
            $values->setConfig($config);
94
95
            $fields->addFieldToTab('Root.Main', $values);
96
        });
97
98
        $fields = parent::getCMSFields();
99
100
        return $fields;
101
    }
102
103
    /**
104
     * @param Member
105
     *
106
     * @return boolean
107
     */
108 View Code Duplication
    public function canCreate($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...
109
    {
110
        $extended = $this->extendedCan(__FUNCTION__, $member);
111
        if ($extended !== null) {
112
            return $extended;
113
        }
114
        return $this->Parent()->canCreate();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SubmittedForm. Did you maybe mean parentClass()?

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...
115
    }
116
117
    /**
118
     * @param Member
119
     *
120
     * @return boolean
121
     */
122 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...
123
    {
124
        $extended = $this->extendedCan(__FUNCTION__, $member);
125
        if ($extended !== null) {
126
            return $extended;
127
        }
128
        return $this->Parent()->canView();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SubmittedForm. Did you maybe mean parentClass()?

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...
129
    }
130
131
    /**
132
     * @param Member
133
     *
134
     * @return boolean
135
     */
136 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...
137
    {
138
        $extended = $this->extendedCan(__FUNCTION__, $member);
139
        if ($extended !== null) {
140
            return $extended;
141
        }
142
        return $this->Parent()->canEdit();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SubmittedForm. Did you maybe mean parentClass()?

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...
143
    }
144
145
    /**
146
     * @param Member
147
     *
148
     * @return boolean
149
     */
150 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...
151
    {
152
        $extended = $this->extendedCan(__FUNCTION__, $member);
153
        if ($extended !== null) {
154
            return $extended;
155
        }
156
        return $this->Parent()->canDelete();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SubmittedForm. Did you maybe mean parentClass()?

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...
157
    }
158
159
    /**
160
     * Before we delete this form make sure we delete all the
161
     * field values so that we don't leave old data round
162
     *
163
     * @return void
164
     */
165
    protected function onBeforeDelete()
166
    {
167
        if ($this->Values()) {
0 ignored issues
show
Bug introduced by
The method Values() does not exist on 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...
168
            foreach ($this->Values() as $value) {
0 ignored issues
show
Bug introduced by
The method Values() does not exist on 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...
169
                $value->delete();
170
            }
171 1
        }
172
173
        parent::onBeforeDelete();
174
    }
175
}
176