EventPage::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\PageTypes;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\DateField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\TextareaField;
10
use SilverStripe\Forms\TimeField;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
13
class EventPage extends DatedUpdatePage
14
{
15
    private static $description = 'Describes an event occurring on a specific date.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
16
17
    private static $default_parent = EventHolder::class;
0 ignored issues
show
introduced by
The private property $default_parent is not used, and could be removed.
Loading history...
18
19
    private static $can_be_root = false;
0 ignored issues
show
introduced by
The private property $can_be_root is not used, and could be removed.
Loading history...
20
21
    private static $icon_class = 'font-icon-p-event';
0 ignored issues
show
introduced by
The private property $icon_class is not used, and could be removed.
Loading history...
22
23
    private static $singular_name = 'Event Page';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
24
25
    private static $plural_name = 'Event Pages';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
26
27
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        'StartTime' => 'Time',
29
        'EndTime' => 'Time',
30
        'Location' => 'Text',
31
    ];
32
33
    private static $table_name = 'EventPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
34
35
    public function fieldLabels($includerelations = true)
36
    {
37
        $labels = parent::fieldLabels($includerelations);
38
        $labels['StartTime'] = _t('CWP\\CWP\\PageTypes\\DateUpdatePage.StartTimeFieldLabel', 'Start Time');
39
        $labels['EndTime'] = _t('CWP\\CWP\\PageTypes\\DateUpdatePage.EndTimeFieldLabel', 'End Time');
40
        $labels['Location'] = _t('CWP\\CWP\\PageTypes\\DateUpdatePage.LocationFieldLabel', 'Location');
41
42
        return $labels;
43
    }
44
45
    /**
46
     * Add the default for the Date being the current day.
47
     */
48
    public function populateDefaults()
49
    {
50
        if (!isset($this->Date) || $this->Date === null) {
51
            $this->Date = DBDatetime::now()->Format('y-MM-dd');
0 ignored issues
show
Bug Best Practice introduced by
The property Date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
        }
53
54
        if (!isset($this->StartTime) || $this->StartTime === null) {
55
            $this->StartTime = '09:00:00';
0 ignored issues
show
Bug Best Practice introduced by
The property StartTime does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        }
57
58
        if (!isset($this->EndTime) || $this->EndTime === null) {
59
            $this->EndTime = '17:00:00';
0 ignored issues
show
Bug Best Practice introduced by
The property EndTime does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
        }
61
62
        parent::populateDefaults();
63
    }
64
65
    public function getCMSFields()
66
    {
67
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
68
            $fields->removeByName('Date');
69
70
            $dateTimeFields = array();
71
72
            $dateTimeFields[] = $dateField = DateField::create('Date', 'Date');
0 ignored issues
show
Unused Code introduced by
The assignment to $dateField is dead and can be removed.
Loading history...
73
            $dateTimeFields[] = $startTimeField = TimeField::create(
0 ignored issues
show
Unused Code introduced by
The assignment to $startTimeField is dead and can be removed.
Loading history...
74
                'StartTime',
75
                $this->fieldLabel('StartTime')
76
            );
77
            $dateTimeFields[] = $endTimeField = TimeField::create('EndTime', $this->fieldLabel('EndTime'));
0 ignored issues
show
Unused Code introduced by
The assignment to $endTimeField is dead and can be removed.
Loading history...
78
79
            $fields->addFieldsToTab('Root.Main', [
80
                $dateTimeField = FieldGroup::create('Date and time', $dateTimeFields),
0 ignored issues
show
Unused Code introduced by
The assignment to $dateTimeField is dead and can be removed.
Loading history...
81
                $locationField = TextareaField::create('Location', $this->fieldLabel('Location'))
82
            ], 'Abstract');
83
            $locationField->setRows(4);
84
        });
85
        return parent::getCMSFields();
86
    }
87
88
    public function NiceLocation()
89
    {
90
        return nl2br(Convert::raw2xml($this->Location), true);
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Conver...aw2xml($this->Location) can also be of type array and array; however, parameter $string of nl2br() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        return nl2br(/** @scrutinizer ignore-type */ Convert::raw2xml($this->Location), true);
Loading history...
91
    }
92
}
93