UserField   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 8
dl 0
loc 194
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 21 1
A onBeforeWrite() 0 12 2
A getFieldType() 0 4 1
A getRequiredNice() 0 4 1
A getValue() 0 4 1
A createDefaultField() 0 15 3
A createField() 0 8 1
A singular_name() 0 5 1
A canView() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A canCreate() 0 4 1
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 FieldList;
15
use FormField;
16
use ReadonlyField;
17
use Tab;
18
use TabSet;
19
use TextField;
20
21
/**
22
 * Class AttendeeExtraField
23
 * todo write migration from AttendeeExtraField -> UserField
24
 *
25
 * @property string  Name
26
 * @property string  Title
27
 * @property string  Default
28
 * @property string  ExtraClass
29
 * @property boolean Required
30
 * @property boolean Editable
31
 *
32
 * @method \CalendarEvent Event()
33
 */
34
class UserField extends DataObject
35
{
36
    /**
37
     * @var FormField
38
     */
39
    protected $fieldType = 'FormField';
40
41
    /**
42
     * Field name to be used in the AttendeeField (Composite field)
43
     *
44
     * @see AttendeeField::__construct()
45
     *
46
     * @var string
47
     */
48
    protected $fieldName;
49
50
    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...
51
        'Name' => 'Varchar(255)', // mostly here for default fields lookup
52
        'Title' => 'Varchar(255)',
53
        'Default' => 'Varchar(255)',
54
        'ExtraClass' => 'Varchar(255)',
55
        'Required' => 'Boolean',
56
        'Editable' => 'Boolean',
57
        'Sort' => 'Int'
58
    );
59
60
    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...
61
        'Editable' => 1
62
    );
63
64
    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...
65
66
    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...
67
        'Event' => 'CalendarEvent'
68
    );
69
70
    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...
71
        'Attendees' => 'Broarm\EventTickets\Attendee'
72
    );
73
74
    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...
75
        'singular_name' => 'Type of field',
76
        'Title' => 'Title',
77
        'RequiredNice' => 'Required field'
78
    );
79
80
    private static $translate = array(
81
        'Title',
82
        'Default'
83
    );
84
85
    public function getCMSFields()
86
    {
87
        $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main')));
88
        $fields->addFieldsToTab('Root.Main', array(
89
            ReadonlyField::create('PreviewFieldType', 'Field type', $this->ClassName),
90
            ReadonlyField::create('Name', _t('AttendeeExtraField.Name', 'Name for this field')),
91
            TextField::create('Title', _t('AttendeeExtraField.Title', 'Field label or question')),
92
            TextField::create('ExtraClass', _t('AttendeeExtraField.ExtraClass', 'Add an extra class to the field')),
93
            TextField::create('Default', _t('AttendeeExtraField.Default', 'Set a default value'))
94
        ));
95
96
        $fields->addFieldsToTab('Root.Validation', array(
97
            CheckboxField::create('Required', _t(
98
                'AttendeeExtraField.Required',
99
                'This field is required'
100
            ))->setDisabled(!(bool)$this->Editable)
101
        ));
102
103
        $this->extend('updateCMSFields', $fields);
104
        return $fields;
105
    }
106
107
    public function onBeforeWrite()
108
    {
109
        // Set the title to be the name when empty
110
        if (empty($this->Name)) {
111
            $str = preg_replace('/[^a-z0-9]+/i', ' ', $this->Title);
112
            $str = trim($str);
113
            $str = ucwords($str);
114
            $this->Name = str_replace(" ", "", $str);
115
        }
116
117
        parent::onBeforeWrite();
118
    }
119
120
    /**
121
     * Get the filed type
122
     *
123
     * @return FormField
124
     */
125
    public function getFieldType()
126
    {
127
        return $this->fieldType;
128
    }
129
130
    /**
131
     * Show if the field is required in a nice format
132
     *
133
     * BUGFIX Using the shorthand Required.Nice in the summary_fields
134
     * made the fields, that were required (true), be set to (false)
135
     *
136
     * @return mixed
137
     */
138
    public function getRequiredNice()
139
    {
140
        return $this->dbObject('Required')->Nice();
141
    }
142
143
    /**
144
     * Get the value
145
     *
146
     * @return mixed|string
147
     */
148
    public function getValue()
149
    {
150
        return $this->getField('Value');
151
    }
152
153
    /**
154
     * Create a field from given configuration
155
     * These fields are created based on the set default fields @see Attendee::$default_fields
156
     *
157
     * @param $fieldName
158
     * @param $fieldConfig
159
     *
160
     * @return UserField|DataObject
161
     */
162
    public static function createDefaultField($fieldName, $fieldConfig)
163
    {
164
        /** @var UserField $fieldType */
165
        $fieldType = $fieldConfig['FieldType'];
166
167
        $field = $fieldType::create();
168
        $field->Name = $fieldName;
169
        if (is_array($fieldConfig)) {
170
            foreach ($fieldConfig as $property => $value) {
171
                $field->setField($property, $value);
172
            }
173
        }
174
175
        return $field;
176
    }
177
178
    /**
179
     * Create the actual field
180
     * Overwrite this on the field subclass
181
     *
182
     * @param $fieldName string Created by the AttendeeField
183
     * @param $defaultValue string Set a default value
184
     * @param $main boolean check if the field is for the main attendee
185
     *
186
     * @return FormField
187
     */
188
    public function createField($fieldName, $defaultValue = null, $main = false)
189
    {
190
        $fieldType = $this->fieldType;
191
        $field = $fieldType::create($fieldName, $this->Title, $defaultValue);
192
        $field->addExtraClass($this->ExtraClass);
193
        $this->extend('updateCreateField', $field);
194
        return $field;
195
    }
196
197
    /**
198
     * Returns the singular name without the namespaces
199
     *
200
     * @return string
201
     */
202
    public function singular_name()
203
    {
204
        $name = explode('\\', parent::singular_name());
205
        return trim(str_replace('User', '', end($name)));
206
    }
207
208
    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...
209
    {
210
        return $this->Event()->canView($member);
211
    }
212
213
    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...
214
    {
215
        return $this->Event()->canEdit($member);
216
    }
217
218
    public function canDelete($member = null)
219
    {
220
        return $this->Editable;
221
    }
222
223
    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...
224
    {
225
        return $this->Event()->canCreate($member);
226
    }
227
}
228