Completed
Push — master ( 22f31a...c89319 )
by Bram
02:59
created

AttendeeExtraField::getCMSFields()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 26
nc 6
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
     * Create the configured field
151
     *
152
     * @param $fieldName
153
     * @param $defaultValue
154
     *
155
     * @return \FormField
156
     */
157
    public function createField($fieldName, $defaultValue = null)
158
    {
159
        $fieldType = $this->FieldType;
160
        $field = $fieldType::create($this->fieldName = $fieldName, $this->Title);
161
162
        // Set a default value if set
163
        $field->setValue($defaultValue ? $defaultValue : $this->DefaultValue);
164
165
        // Add any extra classes
166
        $field->addExtraClass($this->ExtraClass);
167
168
        // Check if the field is an option set
169
        if ($field instanceof OptionsetField) {
170
            /** @var OptionsetField $field */
171
            $options = $this->Options()->map('ID', 'Title')->toArray();
172
            $field->setSource($options);
173
174
            // If a field is selected as default set that value
175
            if ($defaultValue = $this->Options()->find('Default', 1)) {
176
                $field->setValue($defaultValue->ID);
177
            }
178
        }
179
180
        return $field;
181
    }
182
183
    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...
184
    {
185
        return $this->Event()->canView($member);
186
    }
187
188
    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...
189
    {
190
        return $this->Event()->canEdit($member);
191
    }
192
193
    public function canDelete($member = null)
194
    {
195
        return true;//$this->Editable;
196
    }
197
198
    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...
199
    {
200
        return $this->Event()->canCreate($member);
201
    }
202
}
203