EditableDateField::getFormField()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableDateField\FormField;
10
11
/**
12
 * EditableDateField
13
 *
14
 * Allows a user to add a date field.
15
 *
16
 * @package userforms
17
 */
18
class EditableDateField extends EditableFormField
19
{
20
    private static $singular_name = 'Date Field';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
21
22
    private static $plural_name = 'Date Fields';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
23
24
    private static $has_placeholder = true;
0 ignored issues
show
introduced by
The private property $has_placeholder is not used, and could be removed.
Loading history...
25
26
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
27
        'DefaultToToday' => 'Boolean' // From customsettings
28
    ];
29
30
    private static $table_name = 'EditableDateField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @return FieldList
34
     */
35
    public function getCMSFields()
36
    {
37
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
38
            $fields->addFieldToTab(
39
                'Root.Main',
40
                CheckboxField::create(
41
                    'DefaultToToday',
42
                    _t('SilverStripe\\UserForms\\Model\\EditableFormField.DEFAULTTOTODAY', 'Default to Today?')
43
                ),
44
                'RightTitle'
45
            );
46
        });
47
48
        return parent::getCMSFields();
49
    }
50
51
    /**
52
     * Return the form field
53
     *
54
     */
55
    public function getFormField()
56
    {
57
        $defaultValue = $this->DefaultToToday
0 ignored issues
show
Bug Best Practice introduced by
The property DefaultToToday does not exist on SilverStripe\UserForms\M...Field\EditableDateField. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
            ? DBDatetime::now()->Format('yyyy-MM-dd')
59
            : $this->Default;
60
61
        $field = FormField::create($this->Name, $this->Title ?: false, $defaultValue)
62
            ->setFieldHolderTemplate(EditableFormField::class . '_holder')
63
            ->setTemplate(EditableFormField::class);
64
65
        $this->doUpdateFormField($field);
66
67
        return $field;
68
    }
69
}
70