Completed
Push — master ( 72993e...eb9a96 )
by Gino
04:42 queued 03:19
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->belongsToMany['tags'] = [
135
                Tag::class,
136
                'table' => Tag::CROSS_REFERENCE_TABLE_NAME,
137
                'order' => 'name'
138
            ];
139
140
            $model->belongsTo['series'] = [
141
                Series::class,
142
                'key' => Series::TABLE_NAME . "_id"
143
            ];
144
        });
145
    }
146
147
    /**
148
     * Extends post controller functionality
149
     */
150
    private function extendPostsController()
151
    {
152
        PostsController::extendFormFields(function ($form, $model) {
153
            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...
154
                return;
155
            }
156
157
            /*
158
             * When extending the form, you should check to see if $formWidget->isNested === false
159
             * as the Repeater FormWidget includes nested Form widgets which can cause your changes
160
             * to be made in unexpected places.
161
             *
162
             * @link https://octobercms.com/docs/plugin/extending#extending-backend-form
163
             */
164
            if (!empty($form->isNested)) {
165
                return;
166
            }
167
            
168
            $tab = self::LOCALIZATION_KEY . 'navigation.taxonomy';
169
170
            $categoriesConfig = $form->getField('categories')->config;
171
            $categoriesConfig['tab'] = $tab;
172
            $categoriesConfig['mode'] = 'relation';
173
            $categoriesConfig['type'] = 'taglist';
174
            $categoriesConfig['label'] = 'rainlab.blog::lang.post.tab_categories';
175
            $categoriesConfig['comment'] = "rainlab.blog::lang.post.categories_comment";
176
            $categoriesConfig['placeholder'] = self::LOCALIZATION_KEY . 'placeholders.categories';
177
            unset($categoriesConfig['commentAbove']);
178
179
            $form->removeField('categories');
180
181
            $form->addSecondaryTabFields([
182
                'categories' => $categoriesConfig,
183
                'tags' => [
184
                    'label' => self::LOCALIZATION_KEY . 'form.tags.label',
185
                    'comment' => self::LOCALIZATION_KEY . 'form.tags.comment',
186
                    'mode' => 'relation',
187
                    'tab' => $tab,
188
                    'type' => 'taglist',
189
                    'placeholder' => self::LOCALIZATION_KEY . 'placeholders.tags',
190
                ],
191
                'series' => [
192
                    'label' => self::LOCALIZATION_KEY . 'form.series.label',
193
                    'tab' => $tab,
194
                    'type' => 'relation',
195
                    'nameFrom' => 'title',
196
                    'comment' => self::LOCALIZATION_KEY . 'form.series.comment',
197
                    // October CMS has a bug with displaying of placeholders without an explicit empty option
198
                    // https://github.com/octobercms/october/pull/4060
199
                    'placeholder' => self::LOCALIZATION_KEY . 'placeholders.series',
200
                    'emptyOption' => self::LOCALIZATION_KEY . 'placeholders.series'
201
                ],
202
            ]);
203
        });
204
    }
205
206
    /**
207
     * Extends categories controller functionality
208
     */
209
    private function extendCategoriesController()
210
    {
211
        CategoriesController::extend(function (Controller $controller) {
212
            $controller->implement[] = RelationController::class;
213
            $relationConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_relation.yaml';
214
215
            if (property_exists($controller, 'relationConfig')) {
216
                $controller->relationConfig = $controller->mergeConfig(
217
                    $controller->relationConfig,
218
                    $relationConfig
219
                );
220
            } else {
221
                $controller->addDynamicProperty('relationConfig', $relationConfig);
222
            }
223
224
            $formConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_form.yaml';
225
226
            if (property_exists($controller, 'formConfig')) {
227
                $controller->formConfig = $controller->mergeConfig(
228
                    $controller->formConfig,
229
                    $formConfig
230
                );
231
            } else {
232
                $controller->addDynamicProperty('formConfig', $formConfig);
233
            }
234
        });
235
    }
236
}
237