Passed
Push — 2.1 ( 0725f2...9942fb )
by Robbie
03:01
created

BasePage::getCMSFields()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 62
rs 8.829
c 0
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 SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
10
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
11
use SilverStripe\Forms\GridField\GridFieldDataColumns;
12
use SilverStripe\Forms\GridField\GridFieldEditButton;
13
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
14
use SilverStripe\Forms\TreeMultiselectField;
15
use SilverStripe\Taxonomy\TaxonomyTerm;
16
use SilverStripe\View\ArrayData;
17
use TractorCow\Fluent\Model\Locale;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Model\Locale 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...
18
use TractorCow\Fluent\State\FluentState;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\State\FluentState 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...
19
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
20
use SilverStripe\ORM\FieldType\DBInt;
21
22
/**
23
 * `BasePage` is a foundation page class which can be used for constructing your own page types. By default it is
24
 * hidden from the CMS - we rely on developers creating their own `Page` class in `mysite/code` which will extend
25
 * from `BasePage`.
26
 *
27
 * Please note: you should generally always extend `Page` with your custom page types. Refrain from subclassing
28
 * `BasePage` directly. Doing so will omit `Page` from your custom class's hierarchy, and this can have unintended
29
 * side effects where modules rely on modifying things on the `Page` class, as well as with templates and themes.
30
 */
31
32
class BasePage extends SiteTree
33
{
34
    private static $icon = 'cwp/cwp:images/icons/sitetree_images/page.png';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
35
36
    /**
37
     * Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide"
38
     *
39
     * {@inheritDoc}
40
     */
41
    private static $hide_ancestor = BasePage::class;
0 ignored issues
show
introduced by
The private property $hide_ancestor is not used, and could be removed.
Loading history...
42
43
    private static $api_access = [
0 ignored issues
show
introduced by
The private property $api_access is not used, and could be removed.
Loading history...
44
        'view' => [
45
            'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
46
            'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
47
        ],
48
        'edit' => [
49
            'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
50
            'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
51
        ],
52
    ];
53
54
    private static $table_name = 'BasePage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
55
56
    /**
57
     * @config
58
     * @var string
59
     */
60
    private static $related_pages_title = 'Related pages';
0 ignored issues
show
introduced by
The private property $related_pages_title is not used, and could be removed.
Loading history...
61
62
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
63
        'Terms' => TaxonomyTerm::class,
64
        'RelatedPages' => BasePage::class,
65
    ];
66
67
    /**
68
     * @var array
69
     * @config
70
     */
71
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
72
        'RelatedPages' => [
73
            'SortOrder' => DBInt::class,
74
        ],
75
    ];
76
77
    private static $plural_name = 'Base Pages';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
78
79
    /**
80
     * Get the footer holder.
81
     */
82
    public function getFooter()
83
    {
84
        return FooterHolder::get_one(FooterHolder::class);
85
    }
86
87
    public function RelatedPages()
88
    {
89
        return $this->getManyManyComponents('RelatedPages')->sort('SortOrder');
90
    }
91
92
    public function RelatedPagesTitle()
93
    {
94
        return $this->config()->get('related_pages_title');
95
    }
96
97
    public function getCMSFields()
98
    {
99
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
100
            // Related Pages
101
            $components = GridFieldConfig_RelationEditor::create();
102
            $components->removeComponentsByType(GridFieldAddNewButton::class);
103
            $components->removeComponentsByType(GridFieldEditButton::class);
104
            $components->removeComponentsByType(GridFieldFilterHeader::class);
105
            $components->addComponent(new GridFieldOrderableRows('SortOrder'));
106
107
            /** @var GridFieldDataColumns $dataColumns */
108
            $dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
109
            $dataColumns->setDisplayFields([
110
                'Title' => _t(__CLASS__ . '.ColumnTitle', 'Title'),
111
                'ClassName' => _t(__CLASS__ . '.ColumnPageType', 'Page Type')
112
            ]);
113
114
            $fields->findOrMakeTab(
115
                'Root.RelatedPages',
116
                _t(__CLASS__ . '.RelatedPages', 'Related pages')
117
            );
118
            $fields->addFieldToTab(
119
                'Root.RelatedPages',
120
                GridField::create(
121
                    'RelatedPages',
122
                    _t(__CLASS__ . '.RelatedPages', 'Related pages'),
123
                    $this->RelatedPages(),
124
                    $components
125
                )
126
            );
127
128
            // Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc)
129
            $hasMany = $this->hasMany();
130
            $manyMany = $this->manyMany();
131
            if (!isset($hasMany['Tags']) && !isset($manyMany['Tags'])) {
132
                $components = GridFieldConfig_RelationEditor::create();
133
                $components->removeComponentsByType(GridFieldAddNewButton::class);
134
                $components->removeComponentsByType(GridFieldEditButton::class);
135
136
                /** @var GridFieldAddExistingAutocompleter $autoCompleter */
137
                $autoCompleter = $components->getComponentByType(GridFieldAddExistingAutocompleter::class);
138
                $autoCompleter->setResultsFormat('$Name ($TaxonomyName)');
139
140
                /** @var GridFieldDataColumns $dataColumns */
141
                $dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
142
                $dataColumns->setDisplayFields([
143
                    'Name' => _t(__CLASS__ . '.Term', 'Term'),
144
                    'TaxonomyName' => _t(__CLASS__ . '.Taxonomy', 'Taxonomy')
145
                ]);
146
147
                $fields->findOrMakeTab('Root.Tags', _t(__CLASS__ . '.TagsTabTitle', 'Tags'));
148
                $fields->addFieldToTab(
149
                    'Root.Tags',
150
                    TreeMultiselectField::create(
151
                        'Terms',
152
                        _t(__CLASS__ . '.Terms', 'Terms'),
153
                        TaxonomyTerm::class
154
                    )->setDescription(_t(__CLASS__ . '.TermsDescription', 'Click to search for additional terms'))
155
                );
156
            }
157
        });
158
        return parent::getCMSFields();
159
    }
160
161
    /**
162
     * Returns the native language name for the selected locale/language, empty string if Fluent is not available
163
     *
164
     * @return string
165
     */
166
    public function getSelectedLanguage()
167
    {
168
        if (!class_exists(Locale::class) || !$this->hasMethod('Locales')) {
169
            return '';
170
        }
171
172
        /** @var ArrayData $information */
173
        $information = $this->LocaleInformation(FluentState::singleton()->getLocale());
0 ignored issues
show
Bug introduced by
The method LocaleInformation() does not exist on CWP\CWP\PageTypes\BasePage. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

173
        /** @scrutinizer ignore-call */ 
174
        $information = $this->LocaleInformation(FluentState::singleton()->getLocale());
Loading history...
174
175
        return $information->LanguageNative;
176
    }
177
}
178