From e7a09ffe5138d0a1c02e19ad2db5d8d5f1912c77 Mon Sep 17 00:00:00 2001 From: Sergey Zakharevich Date: Thu, 15 Oct 2020 13:02:49 +0300 Subject: [PATCH] 16 #comment Ad additional properties for articles --- Plugin.php | 2 + classes/event/ExtendFieldHandler.php | 60 ++++++++++++++++++ classes/item/ArticleItem.php | 3 +- controllers/Properties.php | 31 ++++++++++ controllers/properties/_list_toolbar.htm | 23 +++++++ controllers/properties/_type.htm | 5 ++ controllers/properties/config_form.yaml | 16 +++++ controllers/properties/config_list.yaml | 16 +++++ controllers/properties/config_reorder.yaml | 3 + controllers/properties/create.htm | 49 +++++++++++++++ controllers/properties/index.htm | 1 + controllers/properties/preview.htm | 22 +++++++ controllers/properties/reorder.htm | 8 +++ controllers/properties/update.htm | 54 ++++++++++++++++ lang/en/lang.php | 10 ++- lang/ru/lang.php | 50 ++++++++------- models/Article.php | 6 +- models/Property.php | 48 +++++++++++++++ models/property/columns.yaml | 43 +++++++++++++ models/property/fields.yaml | 61 +++++++++++++++++++ plugin.yaml | 12 +++- updates/table_create_addition_properties.php | 52 ++++++++++++++++ ...date_table_articles_add_property_field.php | 39 ++++++++++++ updates/version.yaml | 6 +- 24 files changed, 592 insertions(+), 28 deletions(-) create mode 100644 classes/event/ExtendFieldHandler.php create mode 100644 controllers/Properties.php create mode 100644 controllers/properties/_list_toolbar.htm create mode 100644 controllers/properties/_type.htm create mode 100644 controllers/properties/config_form.yaml create mode 100644 controllers/properties/config_list.yaml create mode 100644 controllers/properties/config_reorder.yaml create mode 100644 controllers/properties/create.htm create mode 100644 controllers/properties/index.htm create mode 100644 controllers/properties/preview.htm create mode 100644 controllers/properties/reorder.htm create mode 100644 controllers/properties/update.htm create mode 100644 models/Property.php create mode 100644 models/property/columns.yaml create mode 100644 models/property/fields.yaml create mode 100644 updates/table_create_addition_properties.php create mode 100644 updates/update_table_articles_add_property_field.php diff --git a/Plugin.php b/Plugin.php index ca4a440..53d19a8 100644 --- a/Plugin.php +++ b/Plugin.php @@ -6,6 +6,7 @@ //Event use Lovata\GoodNews\Classes\Event\ArticleModelHandler; use Lovata\GoodNews\Classes\Event\CategoryModelHandler; +use Lovata\GoodNews\Classes\Event\ExtendFieldHandler; /** * Class Plugin @@ -48,5 +49,6 @@ protected function addEventListener() { Event::subscribe(ArticleModelHandler::class); Event::subscribe(CategoryModelHandler::class); + Event::subscribe(ExtendFieldHandler::class); } } diff --git a/classes/event/ExtendFieldHandler.php b/classes/event/ExtendFieldHandler.php new file mode 100644 index 0000000..3a60da3 --- /dev/null +++ b/classes/event/ExtendFieldHandler.php @@ -0,0 +1,60 @@ +listen('backend.form.extendFields', function ($obWidget) { + $this->extendArticleFields($obWidget); + }); + } + + /** + * Extend fields for Article model + * @param \Backend\Widgets\Form $obWidget + */ + public function extendArticleFields($obWidget) + { + if (!$obWidget->getController() instanceof Articles || $obWidget->isNested) { + return; + } + + // Only for the Product model + if (!$obWidget->model instanceof Article || $obWidget->context != 'update') { + return; + } + + $obPropertyList = Property::active()->orderBy('sort_order', 'asc')->get(); + if ($obPropertyList->isEmpty()) { + return; + } + + //Get widget data for properties + $arAdditionPropertyData = []; + /** @var Property $obProperty */ + foreach ($obPropertyList as $obProperty) { + $arPropertyData = $obProperty->getWidgetData(); + if (!empty($arPropertyData)) { + $arAdditionPropertyData[Property::NAME.'['.$obProperty->code.']'] = $arPropertyData; + } + } + + // Add fields + if (!empty($arAdditionPropertyData)) { + $obWidget->addTabFields($arAdditionPropertyData); + } + } +} diff --git a/classes/item/ArticleItem.php b/classes/item/ArticleItem.php index 1534635..b6e783a 100644 --- a/classes/item/ArticleItem.php +++ b/classes/item/ArticleItem.php @@ -19,6 +19,7 @@ * @property string $slug * @property string $preview_text * @property string $content + * @property array $property * @property CategoryItem $category * @property \October\Rain\Argon\Argon $published_start * @property \October\Rain\Argon\Argon $published_stop @@ -95,4 +96,4 @@ protected function setElementObject() { $this->obElement = Article::withTrashed()->find($this->iElementID); } -} \ No newline at end of file +} diff --git a/controllers/Properties.php b/controllers/Properties.php new file mode 100644 index 0000000..035357c --- /dev/null +++ b/controllers/Properties.php @@ -0,0 +1,31 @@ + + + + + + + + + diff --git a/controllers/properties/_type.htm b/controllers/properties/_type.htm new file mode 100644 index 0000000..9963eae --- /dev/null +++ b/controllers/properties/_type.htm @@ -0,0 +1,5 @@ + + + + +fatalError): ?> + + 'layout']) ?> + +
+ formRender() ?> +
+ +
+
+ + + + + +
+
+ + + + +

fatalError)) ?>

+

+ diff --git a/controllers/properties/index.htm b/controllers/properties/index.htm new file mode 100644 index 0000000..ea43a36 --- /dev/null +++ b/controllers/properties/index.htm @@ -0,0 +1 @@ +listRender() ?> diff --git a/controllers/properties/preview.htm b/controllers/properties/preview.htm new file mode 100644 index 0000000..e42d0d8 --- /dev/null +++ b/controllers/properties/preview.htm @@ -0,0 +1,22 @@ + + + + +fatalError): ?> + +
+ formRenderPreview() ?> +
+ + +

fatalError) ?>

+ + +

+ + + +

diff --git a/controllers/properties/reorder.htm b/controllers/properties/reorder.htm new file mode 100644 index 0000000..4ece11b --- /dev/null +++ b/controllers/properties/reorder.htm @@ -0,0 +1,8 @@ + + + + +reorderRender() ?> diff --git a/controllers/properties/update.htm b/controllers/properties/update.htm new file mode 100644 index 0000000..d3c8dae --- /dev/null +++ b/controllers/properties/update.htm @@ -0,0 +1,54 @@ + + + + +fatalError): ?> + + 'layout']) ?> + +
+ formRender() ?> +
+ +
+
+ + + + + + + +
+
+ + + +

fatalError)) ?>

+

+ diff --git a/lang/en/lang.php b/lang/en/lang.php index b9b2bdc..6179949 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -23,7 +23,7 @@ 'category_data_desc' => '', 'category_list' => 'Category list', 'category_list_desc' => '', - + 'sorting_publish_asc' => 'By date of publication (asc)', 'sorting_publish_desc' => 'By date of publication (desc)', 'sorting_view_count_acs' => 'By view count (asc)', @@ -32,6 +32,7 @@ 'menu' => [ 'article' => 'Articles', 'category' => 'Categories', + 'property' => 'Addition properties', ], 'tab' => [ 'permissions' => 'Manage article', @@ -44,6 +45,10 @@ 'name' => 'category', 'list_title' => 'Category list', ], + 'property' => [ + 'name' => 'property', + 'list_title' => 'Property list', + ], 'status' => [ 1 => 'New', 2 => 'In progress', @@ -53,5 +58,6 @@ 'permission' => [ 'article' => 'Manage article', 'category' => 'Manage category', + 'property' => 'Manage addition properties', ], -]; \ No newline at end of file +]; diff --git a/lang/ru/lang.php b/lang/ru/lang.php index 68b3c40..92a0399 100644 --- a/lang/ru/lang.php +++ b/lang/ru/lang.php @@ -1,50 +1,55 @@ [ + 'plugin' => [ 'name' => 'GoodNews', 'description' => 'Новостной плагин', ], - 'field' => [ + 'field' => [ 'content' => 'Контент новости', 'published_start' => 'Дата начала публикации', 'published_stop' => 'Дата окончания публикации', 'status' => 'Статус', ], - 'component' => [ - 'article_page' => 'Страница новости', - 'article_page_desc' => '', - 'article_data' => 'Данные новости', - 'article_data_desc' => '', - 'article_list' => 'Список новостей', - 'article_list_desc' => '', + 'component' => [ + 'article_page' => 'Страница новости', + 'article_page_desc' => '', + 'article_data' => 'Данные новости', + 'article_data_desc' => '', + 'article_list' => 'Список новостей', + 'article_list_desc' => '', + + 'category_page' => 'Страница категории', + 'category_page_desc' => '', + 'category_data' => 'Данные категории', + 'category_data_desc' => '', + 'category_list' => 'Список категорий', + 'category_list_desc' => '', - 'category_page' => 'Страница категории', - 'category_page_desc' => '', - 'category_data' => 'Данные категории', - 'category_data_desc' => '', - 'category_list' => 'Список категорий', - 'category_list_desc' => '', - 'sorting_publish_asc' => 'По дате публикации (asc)', 'sorting_publish_desc' => 'По дате публикации (desc)', 'sorting_view_count_acs' => 'По количеству просмотров (asc)', 'sorting_view_count_desc' => 'По количеству просмотров (desc)', ], - 'menu' => [ + 'menu' => [ 'article' => 'Новости', 'category' => 'Категории', + 'property' => 'Дополнительные свойства', ], - 'tab' => [ + 'tab' => [ 'permissions' => 'Управление блонком новостей', ], - 'article' => [ + 'article' => [ 'name' => 'новости', 'list_title' => 'Список новостей', ], - 'category' => [ + 'category' => [ 'name' => 'категории', 'list_title' => 'Список категорий', ], - 'status' => [ + 'property' => [ + 'name' => 'свойства', + 'list_title' => 'Список свойств', + ], + 'status' => [ 1 => 'Новая', 2 => 'Готовится к выпуску', 3 => 'Проверяется', @@ -53,5 +58,6 @@ 'permission' => [ 'article' => 'Управление новостями', 'category' => 'Управление категориями', + 'property' => 'Управление дополнительными свойствами', ], -]; \ No newline at end of file +]; diff --git a/models/Article.php b/models/Article.php index 77f3208..5e84e4a 100644 --- a/models/Article.php +++ b/models/Article.php @@ -30,6 +30,7 @@ * @property integer $view_count * @property string $preview_text * @property string $content + * @property array $property * @property \October\Rain\Argon\Argon $published_start * @property \October\Rain\Argon\Argon $published_stop * @property \October\Rain\Argon\Argon $created_at @@ -86,6 +87,7 @@ class Article extends Model Category::class, ], ]; + public $jsonable = ['property']; public $attachOne = [ 'preview_image' => 'System\Models\File' @@ -105,6 +107,7 @@ class Article extends Model 'published_start', 'published_stop', 'view_count', + 'property', ]; public $cached = [ @@ -120,6 +123,7 @@ class Article extends Model 'view_count', 'preview_image', 'images', + 'property', ]; /** @@ -180,4 +184,4 @@ public function getStatusIdOptions() self::STATUS_PUBLISHED => Lang::get('lovata.goodnews::lang.status.' . self::STATUS_PUBLISHED), ]; } -} \ No newline at end of file +} diff --git a/models/Property.php b/models/Property.php new file mode 100644 index 0000000..fe0a977 --- /dev/null +++ b/models/Property.php @@ -0,0 +1,48 @@ + 'required', + 'code' => 'required|unique:lovata_good_news_addition_properties', + ]; + + public $attributeNames = [ + 'name' => 'lovata.toolbox::lang.field.name', + 'code' => 'lovata.toolbox::lang.field.code', + ]; + + protected $fillable = [ + 'active', + 'name', + 'code', + 'description', + 'type', + 'settings', + 'sort_order', + ]; + + /** + * Before save method + */ + public function beforeSave() + { + $this->slug = $this->setSluggedValue('slug', 'name'); + } +} diff --git a/models/property/columns.yaml b/models/property/columns.yaml new file mode 100644 index 0000000..9e0883a --- /dev/null +++ b/models/property/columns.yaml @@ -0,0 +1,43 @@ +columns: + name: + label: 'lovata.toolbox::lang.field.name' + type: text + searchable: true + sortable: true + active: + label: 'lovata.toolbox::lang.field.active' + type: switch + searchable: false + sortable: true + code: + label: 'lovata.toolbox::lang.field.code' + type: text + searchable: true + sortable: true + type: + label: 'lovata.toolbox::lang.field.type' + type: text + searchable: false + sortable: true + id: + label: 'lovata.toolbox::lang.field.id' + type: number + sortable: true + searchable: true + width: 100px + sort_order: + label: 'lovata.toolbox::lang.field.sort_order' + type: number + searchable: false + sortable: true + width: 150px + created_at: + label: 'lovata.toolbox::lang.field.created_at' + type: timetense + invisible: true + sortable: true + updated_at: + label: 'lovata.toolbox::lang.field.updated_at' + type: timetense + invisible: true + sortable: true \ No newline at end of file diff --git a/models/property/fields.yaml b/models/property/fields.yaml new file mode 100644 index 0000000..26c223b --- /dev/null +++ b/models/property/fields.yaml @@ -0,0 +1,61 @@ +fields: + active: + label: 'lovata.toolbox::lang.field.active' + span: left + default: 1 + type: switch + name: + label: 'lovata.toolbox::lang.field.name' + span: left + required: 1 + type: text + type: + label: 'lovata.toolbox::lang.field.type' + span: right + type: dropdown + code: + label: 'lovata.toolbox::lang.field.code' + span: left + type: text + settings[tab]: + label: 'lovata.toolbox::lang.field.property_tab' + span: right + type: text + 'settings[list]': + label: 'lovata.toolbox::lang.field.property_list_value' + prompt: 'lovata.toolbox::lang.button.add_property_value' + span: right + type: repeater + trigger: + action: show + field: type + condition: 'value[select] || value[checkbox] || value[radio] || value[balloon_selector] || value[tag_list]' + form: + fields: + value: + label: 'lovata.toolbox::lang.field.value' + span: full + type: text + 'settings[datepicker]': + label: 'lovata.toolbox::lang.field.mode' + span: right + options: + date: 'lovata.toolbox::lang.field.date' + datetime: 'lovata.toolbox::lang.field.datetime' + time: 'lovata.toolbox::lang.field.time' + trigger: + action: show + field: type + condition: 'value[date]' + type: dropdown + 'settings[mediafinder]': + label: 'lovata.toolbox::lang.field.mode' + span: right + options: + file: 'lovata.toolbox::lang.field.file' + image: 'lovata.toolbox::lang.field.image' + trigger: + action: show + field: type + condition: 'value[mediafinder]' + type: dropdown diff --git a/plugin.yaml b/plugin.yaml index 9400be7..efdf2ec 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -24,6 +24,12 @@ navigation: icon: icon-sitemap permissions: - 'good-news-menu-category' + side-menu-goodnews-property: + label: 'lovata.goodnews::lang.menu.property' + url: lovata/goodnews/properties + icon: icon-sitemap + permissions: + - 'good-news-menu-property' permissions: good-news-menu-article: tab: 'lovata.goodnews::lang.tab.permissions' @@ -32,4 +38,8 @@ permissions: good-news-menu-category: tab: 'lovata.goodnews::lang.tab.permissions' label: 'lovata.goodnews::lang.permission.category' - order: 100 \ No newline at end of file + order: 100 + good-news-menu-property: + tab: 'lovata.goodnews::lang.tab.permissions' + label: 'lovata.goodnews::lang.permission.property' + order: 100 diff --git a/updates/table_create_addition_properties.php b/updates/table_create_addition_properties.php new file mode 100644 index 0000000..a14961f --- /dev/null +++ b/updates/table_create_addition_properties.php @@ -0,0 +1,52 @@ +engine = 'InnoDB'; + $obTable->increments('id'); + $obTable->boolean('active')->default(1); + $obTable->string('name'); + $obTable->string('slug'); + $obTable->string('code')->nullable(); + $obTable->string('description')->nullable(); + $obTable->string('type')->default('input'); + $obTable->text('settings')->nullable(); + $obTable->integer('sort_order')->nullable(); + $obTable->timestamps(); + + $obTable->index('name'); + $obTable->index('slug'); + $obTable->index('code'); + $obTable->index('sort_order'); + }); + } + + /** + * Rollback migration + */ + public function down() + { + Schema::dropIfExists(self::TABLE); + } +} diff --git a/updates/update_table_articles_add_property_field.php b/updates/update_table_articles_add_property_field.php new file mode 100644 index 0000000..16058b4 --- /dev/null +++ b/updates/update_table_articles_add_property_field.php @@ -0,0 +1,39 @@ +mediumText('property')->nullable(); + }); + } + } + + /** + * Rollback migration + */ + public function down() + { + if (Schema::hasTable(self::TABLE) && Schema::hasColumn(self::TABLE, 'property')) { + Schema::table(self::TABLE, function (Blueprint $obTable) { + $obTable->dropColumn(['property']); + }); + } + } +} diff --git a/updates/version.yaml b/updates/version.yaml index e18dc77..3fd3321 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -10,4 +10,8 @@ 1.3.0: - 'Added article_count attribute to CategoryItem class. Thanks for contribution Dinwid.' 1.3.1: - - 'Fixed logic in CategoryItem::getPageUrl() method. Thanks for contribution Dinwid.' \ No newline at end of file + - 'Fixed logic in CategoryItem::getPageUrl() method. Thanks for contribution Dinwid.' +1.3.2: + - 'Add additional properties to article model' + - update_table_articles_add_property_field.php + - table_create_addition_properties.php