Completed
Push — master ( e38edc...acf857 )
by Bram
02:11
created

UserField   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 3
cbo 7
dl 0
loc 161
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 21 1
A onBeforeWrite() 0 12 2
A getValue() 0 4 1
A createDefaultField() 0 15 3
A createField() 0 4 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
     * Field name to be used in the AttendeeField (Composite field)
38
     *
39
     * @see AttendeeField::__construct()
40
     *
41
     * @var string
42
     */
43
    protected $fieldName;
44
45
    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...
46
        'Name' => 'Varchar(255)', // mostly here for default fields lookup
47
        'Title' => 'Varchar(255)',
48
        'Default' => 'Varchar(255)',
49
        'ExtraClass' => 'Varchar(255)',
50
        'Required' => 'Boolean',
51
        'Editable' => 'Boolean',
52
        'Sort' => 'Int'
53
    );
54
55
    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...
56
        'Editable' => 1
57
    );
58
59
    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...
60
61
    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...
62
        'Event' => 'CalendarEvent'
63
    );
64
65
    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...
66
        'Attendees' => 'Broarm\EventTickets\Attendee'
67
    );
68
69
    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...
70
        'ClassName' => 'FieldType',
71
        'Title' => 'Title',
72
        'Required.Nice' => 'Required'
73
    );
74
75
    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...
76
        'Title',
77
        'Default'
78
    );
79
80
    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...
81
    {
82
        $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main')));
83
        $fields->addFieldsToTab('Root.Main', array(
84
            ReadonlyField::create('FieldType', 'Field type', $this->ClassName),
85
            ReadonlyField::create('Name', _t('AttendeeExtraField.Name', 'Name for this field')),
86
            TextField::create('Title', _t('AttendeeExtraField.Title', 'Field label or question')),
87
            TextField::create('ExtraClass', _t('AttendeeExtraField.ExtraClass', 'Add an extra class to the field')),
88
            TextField::create('Default', _t('AttendeeExtraField.Default', 'Set a default value'))
89
        ));
90
91
        $fields->addFieldsToTab('Root.Validation', array(
92
            CheckboxField::create('Required', _t(
93
                'AttendeeExtraField.Required',
94
                'This field is required'
95
            ))->setDisabled(!(bool)$this->Editable)
96
        ));
97
98
        $this->extend('updateCMSFields', $fields);
99
        return $fields;
100
    }
101
102
    public function onBeforeWrite()
103
    {
104
        // Set the title to be the name when empty
105
        if (empty($this->Name)) {
106
            $str = preg_replace('/[^a-z0-9]+/i', ' ', $this->Title);
107
            $str = trim($str);
108
            $str = ucwords($str);
109
            $this->Name = str_replace(" ", "", $str);
110
        }
111
112
        parent::onBeforeWrite();
113
    }
114
115
    /**
116
     * Get the value
117
     *
118
     * @return mixed|string
119
     */
120
    public function getValue()
121
    {
122
        return $this->getField('Value');
123
    }
124
125
    /**
126
     * Create a field from given configuration
127
     * These fields are created based on the set default fields @see Attendee::$default_fields
128
     *
129
     * @param $fieldName
130
     * @param $fieldConfig
131
     *
132
     * @return UserField|DataObject
133
     */
134
    public static function createDefaultField($fieldName, $fieldConfig)
135
    {
136
        /** @var UserField $fieldType */
137
        $fieldType = $fieldConfig['FieldType'];
138
139
        $field = $fieldType::create();
140
        $field->Name = $fieldName;
141
        if (is_array($fieldConfig)) {
142
            foreach ($fieldConfig as $property => $value) {
143
                $field->setField($property, $value);
144
            }
145
        }
146
147
        return $field;
148
    }
149
150
    /**
151
     * Create the actual field
152
     * Overwrite this on the field subclass
153
     *
154
     * @param $fieldName string Created by the AttendeeField
155
     * @param $defaultValue string Set a default value
156
     *
157
     * @return FormField
0 ignored issues
show
Documentation introduced by
Should the return type not be FormField|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
     */
159
    public function createField($fieldName, $defaultValue = null)
160
    {
161
        return null;
162
    }
163
164
    /**
165
     * Returns the singular name without the namespaces
166
     *
167
     * @return string
168
     */
169
    public function singular_name()
170
    {
171
        $name = explode('\\', parent::singular_name());
172
        return trim(end($name));
173
    }
174
175
    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...
176
    {
177
        return $this->Event()->canView($member);
178
    }
179
180
    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...
181
    {
182
        return $this->Event()->canEdit($member);
183
    }
184
185
    public function canDelete($member = null)
186
    {
187
        return $this->Editable;
188
    }
189
190
    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...
191
    {
192
        return $this->Event()->canCreate($member);
193
    }
194
}
195