Completed
Pull Request — master (#647)
by Robbie
02:08
created

SubmittedFormField::canView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\Model\Submission;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
7
8
/**
9
 * Data received from a UserDefinedForm submission
10
 *
11
 * @package userforms
12
 */
13
class SubmittedFormField extends DataObject
14
{
15
    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...
Unused Code introduced by
The property $db 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...
16
        'Name' => 'Varchar',
17
        'Value' => 'Text',
18
        'Title' => 'Varchar(255)'
19
    ];
20
21
    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...
22
        'Parent' => SubmittedForm::class
23
    ];
24
25
    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...
26
        'Title' => 'Title',
27
        'FormattedValue' => 'Value'
28
    ];
29
30
    private static $table_name = 'SubmittedFormField';
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...
31
32
    /**
33
     * @param Member $member
34
     * @param array $context
35
     * @return boolean
36
     */
37
    public function canCreate($member = null, $context = [])
38
    {
39
        return $this->Parent()->canCreate();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...ion\SubmittedFormField>? 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...
40
    }
41
42
    /**
43
     * @param Member $member
44
     *
45
     * @return boolean
46
     */
47
    public function canView($member = null)
48
    {
49
        return $this->Parent()->canView();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...ion\SubmittedFormField>? 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...
50
    }
51
52
    /**
53
     * @param Member $member
54
     *
55
     * @return boolean
56
     */
57
    public function canEdit($member = null)
58
    {
59
        return $this->Parent()->canEdit();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...ion\SubmittedFormField>? 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...
60
    }
61
62
    /**
63
     * @param Member $member
64
     *
65
     * @return boolean
66
     */
67
    public function canDelete($member = null)
68
    {
69
        return $this->Parent()->canDelete();
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...ion\SubmittedFormField>? 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...
70
    }
71
72
    /**
73
     * Generate a formatted value for the reports and email notifications.
74
     * Converts new lines (which are stored in the database text field) as
75
     * <brs> so they will output as newlines in the reports
76
     *
77
     * @return string
78
     */
79
    public function getFormattedValue()
80
    {
81
        return nl2br($this->dbObject('Value')->ATT());
82
    }
83
84
    /**
85
     * Return the value of this submitted form field suitable for inclusion
86
     * into the CSV
87
     *
88
     * @return Text
89
     */
90
    public function getExportValue()
91
    {
92
        return $this->Value;
0 ignored issues
show
Documentation introduced by
The property Value does not exist on object<SilverStripe\User...ion\SubmittedFormField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
    }
94
95
    /**
96
     * Find equivalent editable field for this submission.
97
     *
98
     * Note the field may have been modified or deleted from the original form
99
     * so this may not always return the data you expect. If you need to save
100
     * a particular state of editable form field at time of submission, copy
101
     * that value to the submission.
102
     *
103
     * @return EditableFormField
104
     */
105
    public function getEditableField()
106
    {
107
        return $this->Parent()->Parent()->Fields()->filter([
0 ignored issues
show
Documentation Bug introduced by
The method Parent does not exist on object<SilverStripe\User...ion\SubmittedFormField>? 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...
108
            'Name' => $this->Name
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SilverStripe\User...ion\SubmittedFormField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
        ])->First();
110
    }
111
}
112