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

UserDefinedForm_EmailRecipientCondition.php (2 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
4
/**
5
 * Declares a condition that determines whether an email can be sent to a given recipient
6
 *
7
 * @method UserDefinedForm_EmailRecipient Parent()
8
 *
9
 * @property Enum ConditionOption
10
 * @property Varchar ConditionValue
11
 *
12
 * @method EditableFormField ConditionField
13
 */
14
class UserDefinedForm_EmailRecipientCondition extends DataObject
15
{
16
17
    /**
18
     * List of options
19
     *
20
     * @config
21
     * @var array
22
     */
23
    private static $condition_options = array(
24
        "IsBlank" => "Is blank",
25
        "IsNotBlank" => "Is not blank",
26
        "Equals" => "Equals",
27
        "NotEquals" => "Doesn't equal",
28
        "ValueLessThan" => "Less than",
29
        "ValueLessThanEqual" => "Less than or equal",
30
        "ValueGreaterThan" => "Greater than",
31
        "ValueGreaterThanEqual" => "Greater than or equal"
32
    );
33
34
    private static $db = array(
35
        'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
36
        'ConditionValue' => 'Varchar'
37
    );
38
39
    private static $has_one = array(
40
        'Parent' => 'UserDefinedForm_EmailRecipient',
41
        'ConditionField' => 'EditableFormField'
42
    );
43
44
    /**
45
     *
46
     * Determine if this rule matches the given condition
47
     *
48
     * @param $data
49
     *
50
     * @return bool|null
51
     * @throws LogicException
52
     */
53 2
    public function matches($data)
54
    {
55 2
        $fieldName = $this->ConditionField()->Name;
56 2
        $fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
57 2
        $conditionValue = $this->ConditionValue;
58 2
        $result = null;
59 2
        switch ($this->ConditionOption) {
60 2
            case 'IsBlank':
61 2
                $result = empty($fieldValue);
62 2
                break;
63 2
            case 'IsNotBlank':
64 2
                $result = !empty($fieldValue);
65 2
                break;
66 2
            case 'ValueLessThan':
67 1
                $result = ($fieldValue < $conditionValue);
68 1
                break;
69 2
            case 'ValueLessThanEqual':
70 1
                $result = ($fieldValue <= $conditionValue);
71 1
                break;
72 2
            case 'ValueGreaterThan':
73 1
                $result = ($fieldValue > $conditionValue);
74 1
                break;
75 2
            case 'ValueGreaterThanEqual':
76 1
                $result = ($fieldValue >= $conditionValue);
77 1
                break;
78 2
            case 'NotEquals':
79 2
            case 'Equals':
80 2
                $result = is_array($fieldValue)
81 2
                    ? in_array($conditionValue, $fieldValue)
82 2
                    : $fieldValue == $conditionValue;
83
84 2
                if ($this->ConditionOption == 'NotEquals') {
85 2
                    $result = !($result);
86 2
                }
87 2
                break;
88
            default:
89
                throw new LogicException("Unhandled rule {$this->ConditionOption}");
90
                break;
91 2
        }
92
93 2
        return $result;
94
    }
95
96
        /**
97
     * Return whether a user can create an object of this type
98
     *
99
     * @param Member $member
100
     * @param array $context Virtual parameter to allow context to be passed in to check
101
     * @return bool
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
        // Check parent page
106
        $parent = $this->getCanCreateContext(func_get_args());
107
        if ($parent) {
108
            return $parent->canEdit($member);
109
        }
110
111
        // Fall back to secure admin permissions
112
        return parent::canCreate($member);
113
    }
114
115
    /**
116
     * Helper method to check the parent for this object
117
     *
118
     * @param array $args List of arguments passed to canCreate
119
     * @return SiteTree Parent page instance
120
     */
121 View Code Duplication
    protected function getCanCreateContext($args)
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...
122
    {
123
        // Inspect second parameter to canCreate for a 'Parent' context
124
        if (isset($args[1]['Parent'])) {
125
            return $args[1]['Parent'];
126
        }
127
        // Hack in currently edited page if context is missing
128
        if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
129
            return Controller::curr()->currentPage();
130
        }
131
132
        // No page being edited
133
        return null;
134
    }
135
136
    /**
137
     * @param Member
138
     *
139
     * @return boolean
140
     */
141
    public function canView($member = null)
142
    {
143
        return $this->Parent()->canView($member);
144
    }
145
146
    /**
147
     * @param Member
148
     *
149
     * @return boolean
150
     */
151
    public function canEdit($member = null)
152
    {
153
        return $this->Parent()->canEdit($member);
154
    }
155
156
    /**
157
     * @param Member
158
     *
159
     * @return boolean
160
     */
161
    public function canDelete($member = null)
162
    {
163
        return $this->canEdit($member);
164
    }
165
}
166