Passed
Pull Request — master (#32)
by Robbie
02:44
created

BasePage::onAfterPublish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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