GinoPane /
oc-blogtaxonomy-plugin
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 RainLab\Blog\Controllers\Posts as PostsController; |
||
| 19 | use GinoPane\BlogTaxonomy\Components\SeriesNavigation; |
||
| 20 | use RainLab\Blog\Controllers\Categories as CategoriesController; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class Plugin |
||
| 24 | * |
||
| 25 | * @package GinoPane\BlogTaxonomy |
||
| 26 | */ |
||
| 27 | class Plugin extends PluginBase |
||
| 28 | { |
||
| 29 | const LOCALIZATION_KEY = 'ginopane.blogtaxonomy::lang.'; |
||
| 30 | |||
| 31 | const DIRECTORY_KEY = 'ginopane/blogtaxonomy'; |
||
| 32 | |||
| 33 | const REQUIRED_PLUGIN_RAINLAB_BLOG = 'RainLab.Blog'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array Require the RainLab.Blog plugin |
||
| 37 | */ |
||
| 38 | public $require = [self::REQUIRED_PLUGIN_RAINLAB_BLOG]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns information about this plugin |
||
| 42 | * |
||
| 43 | * @return array |
||
|
0 ignored issues
–
show
|
|||
| 44 | */ |
||
| 45 | public function pluginDetails() |
||
| 46 | { |
||
| 47 | return [ |
||
| 48 | 'name' => self::LOCALIZATION_KEY . 'plugin.name', |
||
| 49 | 'description' => self::LOCALIZATION_KEY . 'plugin.description', |
||
| 50 | 'author' => 'Siarhei <Gino Pane> Karavai', |
||
| 51 | 'icon' => 'icon-tags', |
||
| 52 | 'homepage' => 'https://github.com/ginopane/oc-blog-taxonomy' |
||
| 53 | ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Register components |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function registerComponents() |
||
| 62 | { |
||
| 63 | return [ |
||
| 64 | TagList::class => TagList::NAME, |
||
| 65 | TagPosts::class => TagPosts::NAME, |
||
| 66 | RelatedPosts::class => RelatedPosts::NAME, |
||
| 67 | SeriesList::class => SeriesList::NAME, |
||
| 68 | SeriesPosts::class => SeriesPosts::NAME, |
||
| 69 | SeriesNavigation::class => SeriesNavigation::NAME, |
||
| 70 | ]; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Boot method, called right before the request route |
||
| 75 | */ |
||
| 76 | public function boot() |
||
| 77 | { |
||
| 78 | // extend the post model |
||
| 79 | $this->extendModel(); |
||
| 80 | |||
| 81 | // extend posts functionality |
||
| 82 | $this->extendPostsController(); |
||
| 83 | |||
| 84 | // extend categories functionality |
||
| 85 | $this->extendCategoriesController(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Register plugin navigation |
||
| 90 | */ |
||
| 91 | public function registerNavigation() |
||
| 92 | { |
||
| 93 | // Extend the navigation |
||
| 94 | Event::listen('backend.menu.extendItems', function ($manager) { |
||
| 95 | $manager->addSideMenuItems(self::REQUIRED_PLUGIN_RAINLAB_BLOG, 'blog', [ |
||
| 96 | 'series' => [ |
||
| 97 | 'label' => self::LOCALIZATION_KEY . 'navigation.series', |
||
| 98 | 'icon' => 'icon-list-alt', |
||
| 99 | 'code' => 'series', |
||
| 100 | 'owner' => self::REQUIRED_PLUGIN_RAINLAB_BLOG, |
||
| 101 | 'url' => Backend::url(self::DIRECTORY_KEY . '/series') |
||
| 102 | ], |
||
| 103 | |||
| 104 | 'tags' => [ |
||
| 105 | 'label' => self::LOCALIZATION_KEY . 'navigation.tags', |
||
| 106 | 'icon' => 'icon-tags', |
||
| 107 | 'code' => 'tags', |
||
| 108 | 'owner' => self::REQUIRED_PLUGIN_RAINLAB_BLOG, |
||
| 109 | 'url' => Backend::url(self::DIRECTORY_KEY . '/tags') |
||
| 110 | ] |
||
| 111 | ]); |
||
| 112 | }); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Extend RainLab Post model |
||
| 117 | */ |
||
| 118 | private function extendModel() |
||
| 119 | { |
||
| 120 | PostModel::extend(function ($model) { |
||
| 121 | $model->belongsToMany['tags'] = [ |
||
| 122 | Tag::class, |
||
| 123 | 'table' => Tag::CROSS_REFERENCE_TABLE_NAME, |
||
| 124 | 'order' => 'name' |
||
| 125 | ]; |
||
| 126 | |||
| 127 | $model->belongsTo['series'] = [ |
||
| 128 | Series::class, |
||
| 129 | 'key' => Series::TABLE_NAME . "_id" |
||
| 130 | ]; |
||
| 131 | }); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Extends post controller functionality |
||
| 136 | */ |
||
| 137 | private function extendPostsController() |
||
| 138 | { |
||
| 139 | PostsController::extendFormFields(function ($form, $model) { |
||
| 140 | 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 dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | $tab = self::LOCALIZATION_KEY . 'navigation.taxonomy'; |
||
| 145 | |||
| 146 | $categoriesConfig = $form->getField('categories')->config; |
||
| 147 | $categoriesConfig['tab'] = $tab; |
||
| 148 | $categoriesConfig['mode'] = 'relation'; |
||
| 149 | $categoriesConfig['type'] = 'taglist'; |
||
| 150 | $categoriesConfig['label'] = 'rainlab.blog::lang.post.tab_categories'; |
||
| 151 | $categoriesConfig['comment'] = "rainlab.blog::lang.post.categories_comment"; |
||
| 152 | $categoriesConfig['placeholder'] = self::LOCALIZATION_KEY . 'placeholders.categories'; |
||
| 153 | unset($categoriesConfig['commentAbove']); |
||
| 154 | |||
| 155 | $form->removeField('categories'); |
||
| 156 | |||
| 157 | $form->addSecondaryTabFields([ |
||
| 158 | 'categories' => $categoriesConfig, |
||
| 159 | 'tags' => [ |
||
| 160 | 'label' => self::LOCALIZATION_KEY . 'form.tags.label', |
||
| 161 | 'comment' => self::LOCALIZATION_KEY . 'form.tags.comment', |
||
| 162 | 'mode' => 'relation', |
||
| 163 | 'tab' => $tab, |
||
| 164 | 'type' => 'taglist', |
||
| 165 | 'placeholder' => self::LOCALIZATION_KEY . 'placeholders.tags', |
||
| 166 | ], |
||
| 167 | 'series' => [ |
||
| 168 | 'label' => self::LOCALIZATION_KEY . 'form.series.label', |
||
| 169 | 'tab' => $tab, |
||
| 170 | 'type' => 'relation', |
||
| 171 | 'nameFrom' => 'title', |
||
| 172 | 'comment' => self::LOCALIZATION_KEY . 'form.series.comment', |
||
| 173 | 'emptyOption' => self::LOCALIZATION_KEY . 'placeholders.series' |
||
| 174 | ], |
||
| 175 | ]); |
||
| 176 | }); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Extends categories controller functionality |
||
| 181 | */ |
||
| 182 | private function extendCategoriesController() |
||
| 183 | { |
||
| 184 | CategoriesController::extend(function (Controller $controller) { |
||
| 185 | $controller->implement[] = RelationController::class; |
||
| 186 | $relationConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_relation.yaml'; |
||
| 187 | |||
| 188 | if (property_exists($controller, 'relationConfig')) { |
||
| 189 | $controller->relationConfig = $controller->mergeConfig( |
||
| 190 | $controller->relationConfig, |
||
| 191 | $relationConfig |
||
| 192 | ); |
||
| 193 | } else { |
||
| 194 | $controller->addDynamicProperty('relationConfig', $relationConfig); |
||
| 195 | } |
||
| 196 | |||
| 197 | $formConfig = '$/' . self::DIRECTORY_KEY . '/controllers/category/config_form.yaml'; |
||
| 198 | |||
| 199 | if (property_exists($controller, 'formConfig')) { |
||
| 200 | $controller->formConfig = $controller->mergeConfig( |
||
| 201 | $controller->formConfig, |
||
| 202 | $formConfig |
||
| 203 | ); |
||
| 204 | } else { |
||
| 205 | $controller->addDynamicProperty('formConfig', $formConfig); |
||
| 206 | } |
||
| 207 | }); |
||
| 208 | } |
||
| 209 | } |
||
| 210 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.