Passed
Pull Request — master (#13)
by Robbie
02:31
created

DatedUpdatePage::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\PageTypes;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Forms\DatetimeField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\TextareaField;
9
use SilverStripe\ORM\FieldType\DBDatetime;
10
11
class DatedUpdatePage extends Page
12
{
13
    /**
14
     * Meant as an abstract base class.
15
     *
16
     * {@inheritDoc}
17
     */
18
    private static $hide_ancestor = DatedUpdatePage::class;
0 ignored issues
show
introduced by
The private property $hide_ancestor is not used, and could be removed.
Loading history...
19
20
    private static $singular_name = 'Dated Update Page';
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 = 'Dated Update Pages';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
23
24
    private static $table_name = 'DatedUpdatePage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
27
        'ShowInMenus' => false,
28
    ];
29
30
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'Abstract' => 'Text',
32
        'Date' => 'Datetime',
33
    ];
34
35
    /**
36
     * Add the default for the Date being the current day.
37
     */
38
    public function populateDefaults()
39
    {
40
        parent::populateDefaults();
41
42 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...
43
            $this->Date = DBDatetime::now()->Format('y-MM-dd 09:00:00');
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...
44
        }
45
    }
46
47
    public function fieldLabels($includerelations = true)
48
    {
49
        $labels = parent::fieldLabels($includerelations);
50
        $labels['Date'] = _t('DateUpdatePage.DateLabel', 'Date');
51
        $labels['Abstract'] = _t('DateUpdatePage.AbstractTextFieldLabel', 'Abstract');
52
53
        return $labels;
54
    }
55
56
    public function getCMSFields()
57
    {
58
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
59
            $fields->addFieldToTab(
60
                'Root.Main',
61
                $dateTimeField = DatetimeField::create('Date', $this->fieldLabel('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

61
                $dateTimeField = DatetimeField::create(/** @scrutinizer ignore-type */ 'Date', $this->fieldLabel('Date')),
Loading history...
62
                'Content'
63
            );
64
65
            $fields->addfieldToTab(
66
                'Root.Main',
67
                $abstractField = TextareaField::create('Abstract', $this->fieldLabel('Abstract')),
68
                'Content'
69
            );
70
            $abstractField->setAttribute('maxlength', '160');
71
            $abstractField->setRightTitle(_t(
72
                'DateUpdatePage.AbstractDesc',
73
                'The abstract is used as a summary on the listing pages. It is limited to 160 characters.'
74
            ));
75
            $abstractField->setRows(6);
76
        });
77
        return parent::getCMSFields();
78
    }
79
}
80