Completed
Push — master ( 5267c5...879542 )
by Bram
02:07
created

UserField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 21 1
A onBeforeWrite() 0 12 2
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
75
        'singular_name' => 'Type of field',
76
        'Title' => 'Title',
77
        'RequiredNice' => 'Required field'
78
    );
79
80
    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...
81
        'Title',
82
        'Default'
83
    );
84
85
    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...
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
     * Show if the field is required in a nice format
122
     *
123
     * BUGFIX Using the shorthand Required.Nice in the summary_fields
124
     * made the fields, that were required (true), be set to (false)
125
     *
126
     * @return mixed
127
     */
128
    public function getRequiredNice()
129
    {
130
        return $this->dbObject('Required')->Nice();
131
    }
132
133
    /**
134
     * Get the value
135
     *
136
     * @return mixed|string
137
     */
138
    public function getValue()
139
    {
140
        return $this->getField('Value');
141
    }
142
143
    /**
144
     * Create a field from given configuration
145
     * These fields are created based on the set default fields @see Attendee::$default_fields
146
     *
147
     * @param $fieldName
148
     * @param $fieldConfig
149
     *
150
     * @return UserField|DataObject
151
     */
152
    public static function createDefaultField($fieldName, $fieldConfig)
153
    {
154
        /** @var UserField $fieldType */
155
        $fieldType = $fieldConfig['FieldType'];
156
157
        $field = $fieldType::create();
158
        $field->Name = $fieldName;
159
        if (is_array($fieldConfig)) {
160
            foreach ($fieldConfig as $property => $value) {
161
                $field->setField($property, $value);
162
            }
163
        }
164
165
        return $field;
166
    }
167
168
    /**
169
     * Create the actual field
170
     * Overwrite this on the field subclass
171
     *
172
     * @param $fieldName string Created by the AttendeeField
173
     * @param $defaultValue string Set a default value
174
     *
175
     * @return FormField
176
     */
177
    public function createField($fieldName, $defaultValue = null)
178
    {
179
        $fieldType = $this->fieldType;
180
        $field = $fieldType::create($fieldName, $this->Title, $defaultValue);
181
        $field->addExtraClass($this->ExtraClass);
182
        $this->extend('updateCreateField', $field);
183
        return $field;
184
    }
185
186
    /**
187
     * Returns the singular name without the namespaces
188
     *
189
     * @return string
190
     */
191
    public function singular_name()
192
    {
193
        $name = explode('\\', parent::singular_name());
194
        return trim(str_replace('User', '', end($name)));
195
    }
196
197
    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...
198
    {
199
        return $this->Event()->canView($member);
200
    }
201
202
    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...
203
    {
204
        return $this->Event()->canEdit($member);
205
    }
206
207
    public function canDelete($member = null)
208
    {
209
        return $this->Editable;
210
    }
211
212
    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...
213
    {
214
        return $this->Event()->canCreate($member);
215
    }
216
}
217