Completed
Push — master ( 58895a...0930e8 )
by Robbie
15s
created

EventPage::populateDefaults()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 15
Code Lines 7

Duplication

Lines 3
Ratio 20 %

Importance

Changes 0
Metric Value
cc 7
eloc 7
nc 8
nop 0
dl 3
loc 15
rs 8.2222
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 = 'cwp/images/icons/sitetree_images/event_page.png';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
22
23
    public $pageIcon =  'images/icons/sitetree_images/event_page.png';
24
25
    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...
26
27
    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...
28
29
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
30
        'StartTime' => 'Time',
31
        'EndTime' => 'Time',
32
        'Location' => 'Text',
33
    ];
34
35
    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...
36
37
    public function fieldLabels($includerelations = true)
38
    {
39
        $labels = parent::fieldLabels($includerelations);
40
        $labels['StartTime'] = _t('DateUpdatePage.StartTimeFieldLabel', 'Start Time');
41
        $labels['EndTime'] = _t('DateUpdatePage.EndTimeFieldLabel', 'End Time');
42
        $labels['Location'] = _t('DateUpdatePage.LocationFieldLabel', 'Location');
43
44
        return $labels;
45
    }
46
47
    /**
48
     * Add the default for the Date being the current day.
49
     */
50
    public function populateDefaults()
51
    {
52 View Code Duplication
        if (!isset($this->Date) || $this->Date === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $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...
54
        }
55
56
        if (!isset($this->StartTime) || $this->StartTime === null) {
57
            $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...
58
        }
59
60
        if (!isset($this->EndTime) || $this->EndTime === null) {
61
            $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...
62
        }
63
64
        parent::populateDefaults();
65
    }
66
67
    public function getCMSFields()
68
    {
69
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
70
            $fields->removeByName('Date');
71
72
            $dateTimeFields = array();
73
74
            $dateTimeFields[] = $dateField = DateField::create('Date', 'Date');
0 ignored issues
show
Bug introduced by
'Date' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

74
            $dateTimeFields[] = $dateField = DateField::create(/** @scrutinizer ignore-type */ 'Date', 'Date');
Loading history...
Unused Code introduced by
The assignment to $dateField is dead and can be removed.
Loading history...
75
            $dateTimeFields[] = $startTimeField = TimeField::create(
0 ignored issues
show
Unused Code introduced by
The assignment to $startTimeField is dead and can be removed.
Loading history...
76
                'StartTime',
77
                '&nbsp;&nbsp;' . $this->fieldLabel('StartTime')
78
            );
79
            $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...
80
81
            $fields->addFieldsToTab('Root.Main', [
82
                $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...
83
                $locationField = TextareaField::create('Location', $this->fieldLabel('Location'))
84
            ], 'Abstract');
85
            $locationField->setRows(4);
86
        });
87
        return parent::getCMSFields();
88
    }
89
90
    public function NiceLocation()
91
    {
92
        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; 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

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