Completed
Push — master ( c6b309...12981b )
by Robbie
25:33
created

code/model/submissions/SubmittedForm.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Contents of an UserDefinedForm submission
4
 *
5
 * @package userforms
6
 */
7
8
class SubmittedForm extends DataObject
9
{
10
11
    private static $has_one = array(
12
        "SubmittedBy" => "Member",
13
        "Parent" => "UserDefinedForm",
14
    );
15
16
    private static $has_many = array(
17
        "Values" => "SubmittedFormField"
18
    );
19
20
    private static $summary_fields = array(
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()){
63
                $submitter =  $self->SubmittedBy()->Email;
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 = new GridField(
79
                'Values',
80
                'SubmittedFormField',
81
                $self->Values()->sort('Created', 'ASC')
82
            );
83
84
            $config = new GridFieldConfig();
85
            $config->addComponent(new GridFieldDataColumns());
86
            $config->addComponent(new GridFieldExportButton());
87
            $config->addComponent(new GridFieldPrintButton());
88
            $values->setConfig($config);
89
90
            $fields->addFieldToTab('Root.Main', $values);
91
        });
92
93
        $fields = parent::getCMSFields();
94
95
        return $fields;
96
    }
97
98
    /**
99
     * @param Member
100
     *
101
     * @return boolean
102
     */
103 View Code Duplication
    public function canCreate($member = null)
0 ignored issues
show
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...
104
    {
105
        $extended = $this->extendedCan(__FUNCTION__, $member);
106
        if ($extended !== null) {
107
            return $extended;
108
        }
109
        return $this->Parent()->canCreate();
110
    }
111
112
    /**
113
     * @param Member
114
     *
115
     * @return boolean
116
     */
117 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
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...
118
    {
119
        $extended = $this->extendedCan(__FUNCTION__, $member);
120
        if ($extended !== null) {
121
            return $extended;
122
        }
123
        return $this->Parent()->canView();
124
    }
125
126
    /**
127
     * @param Member
128
     *
129
     * @return boolean
130
     */
131 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
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...
132
    {
133
        $extended = $this->extendedCan(__FUNCTION__, $member);
134
        if ($extended !== null) {
135
            return $extended;
136
        }
137
        return $this->Parent()->canEdit();
138
    }
139
140
    /**
141
     * @param Member
142
     *
143
     * @return boolean
144
     */
145 View Code Duplication
    public function canDelete($member = null)
0 ignored issues
show
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...
146
    {
147
        $extended = $this->extendedCan(__FUNCTION__, $member);
148
        if ($extended !== null) {
149
            return $extended;
150
        }
151
        return $this->Parent()->canDelete();
152
    }
153
154
    /**
155
     * Before we delete this form make sure we delete all the
156
     * field values so that we don't leave old data round
157
     *
158
     * @return void
159
     */
160
    protected function onBeforeDelete()
161
    {
162
        if ($this->Values()) {
163
            foreach ($this->Values() as $value) {
164
                $value->delete();
165
            }
166
        }
167
168
        parent::onBeforeDelete();
169
    }
170
}
171