Completed
Pull Request — master (#13)
by Robbie
02:33
created

BaseHomePage::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 71
nc 1
nop 0
dl 0
loc 96
rs 8.3859
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CWP\CWP\PageTypes;
4
5
use CWP\CWP\Model\Quicklink;
6
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...
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\GridField\GridField;
12
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
13
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
14
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
15
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
16
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
17
use SilverStripe\Forms\TextField;
18
use SilverStripe\Forms\ToggleCompositeField;
19
use SilverStripe\Forms\TreeDropdownField;
20
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
0 ignored issues
show
Bug introduced by
The type UndefinedOffset\Sortable...s\GridFieldSortableRows 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...
21
22
/**
23
 * **BaseHomePage** is the basic home page.
24
 * By default it is hidden from the CMS - we rely on developers creating their own
25
 * `HomePage` class in the `mysite/code` which will extend from the **BaseHomePage**.
26
 */
27
class BaseHomePage extends Page
28
{
29
    private static $icon = 'cwp/images/icons/sitetree_images/home.png';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
30
31
    private static $hide_ancestor = BaseHomePage::class;
0 ignored issues
show
introduced by
The private property $hide_ancestor is not used, and could be removed.
Loading history...
32
33
    private static $singular_name = 'Home Page';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
34
35
    private static $plural_name = 'Home Pages';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
36
37
    private static $table_name = 'BaseHomePage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
38
39
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
40
        'FeatureOneTitle' => 'Varchar(255)',
41
        'FeatureOneCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')",
42
        'FeatureOneContent' => 'HTMLText',
43
        'FeatureOneButtonText' => 'Varchar(255)',
44
        'FeatureTwoTitle' => 'Varchar(255)',
45
        'FeatureTwoCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')",
46
        'FeatureTwoContent' => 'HTMLText',
47
        'FeatureTwoButtonText' => 'Varchar(255)'
48
    );
49
50
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
51
        'LearnMorePage' => SiteTree::class,
52
        'FeatureOneLink' => SiteTree::class,
53
        'FeatureTwoLink' => SiteTree::class
54
    );
55
56
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
57
        'Quicklinks' => 'Quicklink.Parent'
58
    );
59
60
    public $pageIcon = 'images/icons/sitetree_images/home.png';
61
62
    public function Quicklinks()
63
    {
64
        return $this->getComponents('Quicklinks')->sort('SortOrder');
65
    }
66
67
    public function getCMSFields()
68
    {
69
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
70
            // Main Content tab
71
            $fields->addFieldToTab(
72
                'Root.Main',
73
                TreeDropdownField::create(
74
                    'LearnMorePageID',
0 ignored issues
show
Bug introduced by
'LearnMorePageID' 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
                    /** @scrutinizer ignore-type */ 'LearnMorePageID',
Loading history...
75
                    _t('BaseHomePage.LearnMoreLink', 'Page to link the "Learn More" button to:'),
76
                    SiteTree::class
77
                ),
78
                'Metadata'
79
            );
80
81
            $gridField = GridField::create(
82
                'Quicklinks',
83
                'Quicklinks',
84
                $this->Quicklinks(),
85
                GridFieldConfig_RelationEditor::create()
86
            );
87
            $gridConfig = $gridField->getConfig();
88
            $gridConfig->getComponentByType(GridFieldAddNewButton::class)->setButtonName(
89
                _t('BaseHomePage.AddNewButton', 'Add new')
90
            );
91
92
            $injector = Injector::inst();
93
94
            $gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
95
            $gridConfig->removeComponentsByType(GridFieldDeleteAction::class);
96
            $gridConfig->addComponent($injector->create(GridFieldDeleteAction::class));
97
            $gridConfig->addComponent($injector->create(GridFieldSortableRows::class, 'SortOrder'));
98
            $gridField->setModelClass(Quicklink::class);
99
100
            $fields->addFieldToTab('Root.Quicklinks', $gridField);
101
102
            $fields->removeByName('Import');
103
104
            $fields->addFieldToTab(
105
                'Root.Features',
106
                ToggleCompositeField::create(
107
                    'FeatureOne',
108
                    _t('SiteTree.FeatureOne', 'Feature One'),
109
                    array(
110
                        TextField::create('FeatureOneTitle', _t('BaseHomePage.Title', 'Title')),
111
                        $dropdownField = DropdownField::create(
112
                            'FeatureOneCategory',
113
                            _t('BaseHomePage.FeatureCategoryDropdown', 'Category icon'),
114
                            singleton(BaseHomePage::class)->dbObject('FeatureOneCategory')->enumValues()
115
                        ),
116
                        HTMLEditorField::create(
117
                            'FeatureOneContent',
118
                            _t('BaseHomePage.FeatureContentFieldLabel', 'Content')
119
                        ),
120
                        TextField::create(
121
                            'FeatureOneButtonText',
122
                            _t('BaseHomePage.FeatureButtonText', 'Button text')
123
                        ),
124
                        TreeDropdownField::create(
125
                            'FeatureOneLinkID',
126
                            _t('BaseHomePage.FeatureLink', 'Page to link to'),
127
                            SiteTree::class
128
                        )->setDescription(_t('BaseHomePage.ButtonTextRequired', 'Button text must be filled in'))
129
                    )
130
                )->setHeadingLevel(3)
131
            );
132
            $dropdownField->setEmptyString('none');
133
134
            $fields->addFieldToTab('Root.Features', ToggleCompositeField::create(
135
                'FeatureTwo',
136
                _t('SiteTree.FeatureTwo', 'Feature Two'),
137
                array(
138
                    TextField::create('FeatureTwoTitle', _t('BaseHomePage.Title', 'Title')),
139
                    $dropdownField = DropdownField::create(
140
                        'FeatureTwoCategory',
141
                        _t('BaseHomePage.FeatureCategoryDropdown', 'Category icon'),
142
                        singleton(BaseHomePage::class)->dbObject('FeatureTwoCategory')->enumValues()
143
                    ),
144
                    HTMLEditorField::create(
145
                        'FeatureTwoContent',
146
                        _t('BaseHomePage.FeatureContentFieldLabel', 'Content')
147
                    ),
148
                    TextField::create(
149
                        'FeatureTwoButtonText',
150
                        _t('BaseHomePage.FeatureButtonText', 'Button text')
151
                    ),
152
                    TreeDropdownField::create(
153
                        'FeatureTwoLinkID',
154
                        _t('BaseHomePage.FeatureLink', 'Page to link to'),
155
                        SiteTree::class
156
                    )->setDescription(_t('BaseHomePage.ButtonTextRequired', 'Button text must be filled in'))
157
                )
158
            )->setHeadingLevel(3));
159
            $dropdownField->setEmptyString('none');
160
        });
161
162
        return parent::getCMSFields();
163
    }
164
}
165