Passed
Push — master ( ccf633...e36de4 )
by Robbie
03:46
created

EditableOption::canPublish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Versioned\Versioned;
9
10
/**
11
 * Base Class for EditableOption Fields such as the ones used in
12
 * dropdown fields and in radio check box groups
13
 *
14
 * @method EditableMultipleOptionField Parent()
15
 * @package userforms
16
 */
17
class EditableOption extends DataObject
18
{
19
    private static $default_sort = 'Sort';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
20
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Name' => 'Varchar(255)',
23
        'Title' => 'Varchar(255)',
24
        'Default' => 'Boolean',
25
        'Sort' => 'Int',
26
        'Value' => 'Varchar(255)',
27
    ];
28
29
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
30
        'Parent' => EditableMultipleOptionField::class,
31
    ];
32
33
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
34
        Versioned::class . "('Stage', 'Live')",
35
    ];
36
37
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
38
        'Title',
39
        'Default',
40
    ];
41
42
    protected static $allow_empty_values = false;
43
44
    private static $table_name = 'EditableOption';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * Returns whether to allow empty values or not.
48
     *
49
     * @return boolean
50
     */
51
    public static function allow_empty_values()
52
    {
53
        return (bool) self::$allow_empty_values;
54
    }
55
56
    /**
57
     * Set whether to allow empty values.
58
     *
59
     * @param boolean $allow
60
     */
61
    public static function set_allow_empty_values($allow)
62
    {
63
        self::$allow_empty_values = (bool) $allow;
64
    }
65
66
    /**
67
     * @deprecated 5.0..6.0 Use "$Title" in templates instead
68
     * @return string
69
     */
70
    public function getEscapedTitle()
71
    {
72
        return Convert::raw2xml($this->Title);
73
    }
74
75
    /**
76
     * Fetches a value for $this->Value. If empty values are not allowed,
77
     * then this will return the title in the case of an empty value.
78
     *
79
     * @return string
80
     */
81
    public function getValue()
82
    {
83
        $value = $this->getField('Value');
84
        if (empty($value) && !self::allow_empty_values()) {
85
            return $this->Title;
86
        }
87
        return $value;
88
    }
89
90
    protected function onBeforeWrite()
91
    {
92
        if (!$this->Sort) {
93
            $this->Sort = EditableOption::get()->max('Sort') + 1;
0 ignored issues
show
Bug Best Practice introduced by
The property Sort does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94
        }
95
96
        parent::onBeforeWrite();
97
    }
98
99
    /**
100
     * @param Member $member
101
     *
102
     * @return boolean
103
     */
104
    public function canEdit($member = null)
105
    {
106
        return $this->Parent()->canEdit($member);
107
    }
108
    /**
109
     * @param Member $member
110
     *
111
     * @return boolean
112
     */
113
    public function canDelete($member = null)
114
    {
115
        return $this->canEdit($member);
116
    }
117
118
    /**
119
     * @param Member $member
120
     * @return bool
121
     */
122
    public function canView($member = null)
123
    {
124
        return $this->Parent()->canView($member);
125
    }
126
127
    /**
128
     * Return whether a user can create an object of this type
129
     *
130
     * @param Member $member
131
     * @param array $context Virtual parameter to allow context to be passed in to check
132
     * @return bool
133
     */
134
    public function canCreate($member = null, $context = [])
135
    {
136
        // Check parent object
137
        $parent = $this->Parent();
138
        if ($parent) {
0 ignored issues
show
introduced by
$parent is of type SilverStripe\UserForms\M...ableMultipleOptionField, thus it always evaluated to true.
Loading history...
139
            return $parent->canCreate($member);
140
        }
141
142
        // Fall back to secure admin permissions
143
        return parent::canCreate($member);
144
    }
145
146
    /**
147
     * @param Member $member
148
     * @return bool
149
     */
150
    public function canPublish($member = null)
151
    {
152
        return $this->canEdit($member);
153
    }
154
    /**
155
     * @param Member $member
156
     * @return bool
157
     */
158
    public function canUnpublish($member = null)
159
    {
160
        return $this->canDelete($member);
161
    }
162
}
163