Completed
Push — master ( bbbea7...f9b5a7 )
by Gino
01:39 queued 11s
created

Plugin.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace GinoPane\BlogTaxonomy;
4
5
use Event;
6
use Backend;
7
use System\Classes\PluginBase;
8
use Backend\Classes\Controller;
9
use GinoPane\BlogTaxonomy\Models\Tag;
10
use GinoPane\BlogTaxonomy\Models\Series;
11
use Backend\Behaviors\RelationController;
12
use RainLab\Blog\Models\Post as PostModel;
13
use GinoPane\BlogTaxonomy\Components\TagList;
14
use GinoPane\BlogTaxonomy\Components\TagPosts;
15
use GinoPane\BlogTaxonomy\Components\SeriesList;
16
use GinoPane\BlogTaxonomy\Components\SeriesPosts;
17
use GinoPane\BlogTaxonomy\Components\RelatedPosts;
18
use GinoPane\BlogTaxonomy\Components\RelatedSeries;
19
use GinoPane\BlogTaxonomy\Console\MigrateFromPlugin;
20
use RainLab\Blog\Controllers\Posts as PostsController;
21
use GinoPane\BlogTaxonomy\Components\SeriesNavigation;
22
use RainLab\Blog\Controllers\Categories as CategoriesController;
23
24
/**
25
 * Class Plugin
26
 *
27
 * @package GinoPane\BlogTaxonomy
28
 */
29
class Plugin extends PluginBase
30
{
31
    const LOCALIZATION_KEY = 'ginopane.blogtaxonomy::lang.';
32
33
    const DIRECTORY_KEY = 'ginopane/blogtaxonomy';
34
35
    const REQUIRED_PLUGIN_RAINLAB_BLOG = 'RainLab.Blog';
36
37
    /**
38
     * @var array   Require the RainLab.Blog plugin
39
     */
40
    public $require = [
41
        'RainLab.Blog'
42
    ];
43
44
    /**
45
     * Returns information about this plugin
46
     *
47
     * @return  array
48
     */
49
    public function pluginDetails(): array
50
    {
51
        return [
52
            'name'        => self::LOCALIZATION_KEY . 'plugin.name',
53
            'description' => self::LOCALIZATION_KEY . 'plugin.description',
54
            'author'      => 'Siarhei <Gino Pane> Karavai',
55
            'icon'        => 'icon-tags',
56
            'homepage'    => 'https://github.com/GinoPane/oc-blogtaxonomy-plugin'
57
        ];
58
    }
59
60
    /**
61
     * Register components
62
     *
63
     * @return  array
64
     */
65
    public function registerComponents(): array
66
    {
67
        return [
68
            TagList::class          => TagList::NAME,
69
            TagPosts::class         => TagPosts::NAME,
70
            RelatedPosts::class     => RelatedPosts::NAME,
71
            SeriesList::class       => SeriesList::NAME,
72
            SeriesPosts::class      => SeriesPosts::NAME,
73
            SeriesNavigation::class => SeriesNavigation::NAME,
74
            RelatedSeries::class    => RelatedSeries::NAME
75
        ];
76
    }
77
78
    /**
79
     *
80
     */
81
    public function register()
82
    {
83
        $this->registerConsoleCommand(MigrateFromPlugin::NAME, MigrateFromPlugin::class);
84
    }
85
86
    /**
87
     * Boot method, called right before the request route
88
     */
89
    public function boot()
90
    {
91
        // extend the post model
92
        $this->extendModel();
93
94
        // extend posts functionality
95
        $this->extendPostsController();
96
97
        // extend categories functionality
98
        $this->extendCategoriesController();
99
    }
100
101
    /**
102
     * Register plugin navigation
103
     */
104
    public function registerNavigation()
105
    {
106
        // Extend the navigation
107
        Event::listen('backend.menu.extendItems', function ($manager) {
108
            $manager->addSideMenuItems(self::REQUIRED_PLUGIN_RAINLAB_BLOG, 'blog', [
109
                'series' => [
110
                    'label' => self::LOCALIZATION_KEY . 'navigation.series',
111
                    'icon' => 'icon-list-alt',
112
                    'code' => 'series',
113
                    'owner' => self::REQUIRED_PLUGIN_RAINLAB_BLOG,
114
                    'url' => Backend::url(self::DIRECTORY_KEY . '/series')
115
                ],
116
117
                'tags' => [
118
                    'label' => self::LOCALIZATION_KEY . 'navigation.tags',
119
                    'icon'  => 'icon-tags',
120
                    'code'  => 'tags',
121
                    'owner' => self::REQUIRED_PLUGIN_RAINLAB_BLOG,
122
                    'url'   => Backend::url(self::DIRECTORY_KEY . '/tags')
123
                ]
124
            ]);
125
        });
126
    }
127
128
    /**
129
     * Extend RainLab Post model
130
     */
131
    private function extendModel()
132
    {
133
        PostModel::extend(function ($model) {
134
            $model->morphToMany = [
135
                'tags' => [Tag::class, 'name' => Tag::PIVOT_COLUMN]
136
            ];
137
138
            $model->belongsTo['series'] = [
139
                Series::class,
140
                'key' => Series::TABLE_NAME . "_id"
141
            ];
142
        });
143
    }
144
145
    /**
146
     * Extends post controller functionality
147
     */
148
    private function extendPostsController()
149
    {
150
        PostsController::extendFormFields(function ($form, $model) {
151
            if (!$model instanceof PostModel) {
0 ignored issues
show
The class RainLab\Blog\Models\Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
152
                return;
153
            }
154
155
            /*
156
             * When extending the form, you should check to see if $formWidget->isNested === false
157
             * as the Repeater FormWidget includes nested Form widgets which can cause your changes
158
             * to be made in unexpected places.
159
             *
160
             * @link https://octobercms.com/docs/plugin/extending#extending-backend-form
161
             */
162
            if (!empty($form->isNested)) {
163
                return;
164
            }
165
            
166
            $tab = self::LOCALIZATION_KEY . 'navigation.taxonomy';
167
168
            $categoriesConfig = $form->getField('categories')->config;
169
            $categoriesConfig['tab'] = $tab;
170
            $categoriesConfig['mode'] = 'relation';
171
            $categoriesConfig['type'] = 'taglist';
172
            $categoriesConfig['label'] = 'rainlab.blog::lang.post.tab_categories';
173
            $categoriesConfig['comment'] = "rainlab.blog::lang.post.categories_comment";
174
            $categoriesConfig['placeholder'] = self::LOCALIZATION_KEY . 'placeholders.categories';
175
            unset($categoriesConfig['commentAbove']);
176
177
            $form->removeField('categories');
178
179
            $form->addSecondaryTabFields([
180
                'categories' => $categoriesConfig,
181
                'tags' => [
182
                    'label' => self::LOCALIZATION_KEY . 'form.tags.label',
183
                    'comment' => self::LOCALIZATION_KEY . 'form.tags.comment_post',
184
                    'mode' => 'relation',
185
                    'tab' => $tab,
186
                    'type' => 'taglist',
187
                    'placeholder' => self::LOCALIZATION_KEY . 'placeholders.tags',
188
                ],
189
                'series' => [
190
                    'label' => self::LOCALIZATION_KEY . 'form.series.label',
191
                    'tab' => $tab,
192
                    'type' => 'relation',
193
                    'nameFrom' => 'title',
194
                    'comment' => self::LOCALIZATION_KEY . 'form.series.comment',
195
                    // October CMS has a bug with displaying of placeholders without an explicit empty option
196
                    // https://github.com/octobercms/october/pull/4060
197
                    'placeholder' => self::LOCALIZATION_KEY . 'placeholders.series',
198
                    'emptyOption' => self::LOCALIZATION_KEY . 'placeholders.series'
199
                ],
200
            ]);
201
        });
202
    }
203
204
    /**
205
     * Extends categories controller functionality
206
     */
207
    private function extendCategoriesController()
208
    {
209
        CategoriesController::extend(function (Controller $controller) {
210
            $controller->implement[] = RelationController::class;
211
            $relationConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_relation.yaml';
212
213
            if (property_exists($controller, 'relationConfig')) {
214
                $controller->relationConfig = $controller->mergeConfig(
215
                    $controller->relationConfig,
216
                    $relationConfig
217
                );
218
            } else {
219
                $controller->addDynamicProperty('relationConfig', $relationConfig);
220
            }
221
222
            $formConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_form.yaml';
223
224
            if (property_exists($controller, 'formConfig')) {
225
                $controller->formConfig = $controller->mergeConfig(
226
                    $controller->formConfig,
227
                    $formConfig
228
                );
229
            } else {
230
                $controller->addDynamicProperty('formConfig', $formConfig);
231
            }
232
        });
233
    }
234
}
235