Completed
Push — master ( b95ba0...2412b9 )
by Bram
02:35
created

AttendeeExtraField::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 0
1
<?php
2
/**
3
 * AttendeeExtraField.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 24/05/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CheckboxField;
12
use Convert;
13
use DataObject;
14
use DropdownField;
15
use FieldList;
16
use FormField;
17
use GridField;
18
use LiteralField;
19
use OptionsetField;
20
use ReadonlyField;
21
use Tab;
22
use TabSet;
23
use TextField;
24
25
/**
26
 * Class AttendeeExtraField
27
 *
28
 * @property string    Title
29
 * @property string    FieldName
30
 * @property FormField FieldType
31
 * @property string    DefaultValue
32
 * @property string    ExtraClass
33
 * @property boolean   Required
34
 * @property boolean   Editable
35
 *
36
 * @method \CalendarEvent Event()
37
 * @method \HasManyList Options()
38
 */
39
class AttendeeExtraField extends DataObject
40
{
41
    /**
42
     * Field name to be used in the AttendeeField (Composite field)
43
     * @var string
44
     */
45
    protected $fieldName;
46
47
    private static $db = 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 $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...
48
        'Title' => 'Varchar(255)',
49
        'DefaultValue' => 'Varchar(255)',
50
        'ExtraClass' => 'Varchar(255)',
51
        'FieldName' => 'Varchar(255)',
52
        'Required' => 'Boolean',
53
        'Editable' => 'Boolean',
54
        'FieldType' => 'Enum("TextField,EmailField,CheckboxField,OptionsetField","TextField")',
55
        'Sort' => 'Int'
56
    );
57
58
    private static $defaults = 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 $defaults 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...
59
        'Editable' => 1
60
    );
61
62
    private static $default_sort = 'Sort ASC';
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 $default_sort 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...
63
64
    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...
65
        'Event' => 'CalendarEvent'
66
    );
67
68
    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...
69
        'Options' => 'Broarm\EventTickets\AttendeeExtraFieldOption'
70
    );
71
72
    private static $belongs_many_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 $belongs_many_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...
73
        'Attendees' => 'Broarm\EventTickets\Attendee'
74
    );
75
76
    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...
77
        'Title' => 'Title',
78
        'FieldType' => 'FieldType',
79
        'Required.Nice' => 'Required'
80
    );
81
82
    private static $translate = array(
0 ignored issues
show
Unused Code introduced by
The property $translate 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...
83
        'Title'
84
    );
85
86
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
87
    {
88
        $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main')));
89
90
        $fields->addFieldsToTab('Root.Main', array(
91
            $fieldType = DropdownField::create(
92
                'FieldType',
93
                _t('AttendeeExtraField.FieldType', 'Type of field'),
94
                $this->dbObject('FieldType')->enumValues()
95
            ),
96
            TextField::create('Title', _t('AttendeeExtraField.Title', 'Field label or question')),
97
            TextField::create('ExtraClass', _t('AttendeeExtraField.ExtraClass', 'Add an extra class to the field')),
98
            $required = CheckboxField::create('Required', _t('AttendeeExtraField.Required', 'This field is required'))
99
        ));
100
101
        if ($this->exists()) {
102
            if ($this->FieldType !== 'OptionsetField') {
103
                $fields->addFieldToTab('Root.Main', TextField::create(
104
                    'DefaultValue',
105
                    _t('AttendeeExtraField.DefaultValue', 'Set a default value'))
106
                );
107
            } else {
108
                $fields->addFieldToTab('Root.Main', GridField::create(
109
                    'Options',
110
                    _t('AttendeeExtraField.Options', 'Add field options'),
111
                    $this->Options(),
112
                    GridFieldConfig_Fields::create()
113
                ));
114
            }
115
        }
116
117
        if (!$this->Editable) {
118
            $fieldType->setDisabled(true);
119
            $required->setDisabled(true);
120
        }
121
122
        $this->extend('updateCMSFields', $fields);
123
        return $fields;
124
    }
125
126
    /**
127
     * Returns the singular name without the namespaces
128
     *
129
     * @return string
130
     */
131
    public function singular_name()
132
    {
133
        $name = explode('\\', parent::singular_name());
134
        return trim(end($name));
135
    }
136
137
    /**
138
     * Set the Field name based on the title and ID
139
     */
140
    public function onBeforeWrite()
141
    {
142
        if (empty($this->FieldName)) {
143
            $this->FieldName = Convert::raw2url($this->Title);
144
        }
145
146
        parent::onBeforeWrite();
147
    }
148
149
    /**
150
     * Get the value casted based on chosen field type
151
     *
152
     * @return mixed|string
153
     */
154
    public function getValue()
155
    {
156
        switch ($this->FieldType) {
157
            case 'OptionsetField':
158
                return $this->Options()->byID($this->getField('Value'))->Title;
159
            case 'CheckboxField':
160
                return (bool)$this->getField('Value') ? _t('Boolean.YESANSWER', 'Yes') : _t('Boolean.NOANSWER', 'No');
161
            default:
162
                return $this->getField('Value');
163
        }
164
    }
165
166
    /**
167
     * Create a field from given configuration
168
     *
169
     * @param $fieldName
170
     * @param $fieldConfig
171
     *
172
     * @return AttendeeExtraField|DataObject
173
     */
174
    public static function createFromConfig($fieldName, $fieldConfig) {
175
        $field = AttendeeExtraField::create();
176
        $field->Title = _t("AttendeeField.$fieldName", $fieldName);
177
        $field->FieldName = $fieldName;
178
        $field->Required = true;
179
        $field->Editable = false;
180
181
        if (is_array($fieldConfig)) {
182
            foreach ($fieldConfig as $property => $value) {
183
                $field->setField($property, $value);
184
            }
185
        } else {
186
            $field->FieldType = $fieldConfig;
187
        }
188
189
        return $field;
190
    }
191
192
    /**
193
     * Create the configured field
194
     *
195
     * @param $fieldName
196
     * @param $defaultValue
197
     *
198
     * @return \FormField
199
     */
200
    public function createField($fieldName, $defaultValue = null)
201
    {
202
        $fieldType = $this->FieldType;
203
        $field = $fieldType::create($this->fieldName = $fieldName, $this->Title);
204
205
        // Set a default value if set
206
        $field->setValue($defaultValue ? $defaultValue : $this->DefaultValue);
207
208
        // Add any extra classes
209
        $field->addExtraClass($this->ExtraClass);
210
211
        // Check if the field is an option set
212
        if ($field instanceof OptionsetField) {
213
            /** @var OptionsetField $field */
214
            $options = $this->Options()->map('ID', 'Title')->toArray();
215
            $field->setSource($options);
216
217
            // If a field is selected as default set that value
218
            if ($defaultValue = $this->Options()->find('Default', 1)) {
219
                $field->setValue($defaultValue->ID);
220
            }
221
        }
222
223
        return $field;
224
    }
225
226
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
227
    {
228
        return $this->Event()->canView($member);
229
    }
230
231
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
232
    {
233
        return $this->Event()->canEdit($member);
234
    }
235
236
    public function canDelete($member = null)
237
    {
238
        return $this->Editable;
239
    }
240
241
    public function canCreate($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
242
    {
243
        return $this->Event()->canCreate($member);
244
    }
245
}
246