From 924e2ea17506090e47dd64dd0c6157869a8ad1c4 Mon Sep 17 00:00:00 2001 From: Semenov Date: Sat, 7 Mar 2020 19:11:48 +0300 Subject: [PATCH] measure update --- src/components/MeasureComponent.php | 89 +------------- src/controllers/AdminMeasureController.php | 110 +++++++++++++----- ..._090602_append_data_table__cms_measure.php | 4 +- 3 files changed, 89 insertions(+), 114 deletions(-) diff --git a/src/components/MeasureComponent.php b/src/components/MeasureComponent.php index 5468112..6ffcc2f 100644 --- a/src/components/MeasureComponent.php +++ b/src/components/MeasureComponent.php @@ -34,26 +34,6 @@ static public function descriptorConfig() public $default_measure_code = "796"; //шт. - public $active_measure_codes = [ - "796", - - "112", - "163", - "166", - - "003", - "006", - ]; - - /*public function behaviors() - { - return ArrayHelper::merge(parent::behaviors(), [ - - ]); - }*/ - - - public function getConfigFormFields() { return [ @@ -64,13 +44,13 @@ public function getConfigFormFields() return \Yii::$app->measureClassifier->getDataForSelect(); } ], - 'active_measure_codes' => [ + /*'active_measure_codes' => [ 'class' => SelectField::class, 'multiple' => true, 'items' => function() { return \Yii::$app->measureClassifier->getDataForSelect(); } - ], + ],*/ ]; } @@ -79,80 +59,21 @@ public function rules() { return ArrayHelper::merge(parent::rules(), [ [['default_measure_code'], 'string'], - [['active_measure_codes'], 'safe'], ]); } + public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'default_measure_code' => 'Единица по умолчанию', - 'active_measure_codes' => 'Валюты по умолчанию', ]); } - + + public function attributeHints() { return ArrayHelper::merge(parent::attributeHints(), [ - 'active_measure_codes' => 'Валюты которые показываются первыми при выборе в списке', ]); } - - - - /** - * @return Measure[] - */ - public function getActiveMeasures() - { - $result = []; - - foreach ((array) $this->active_measure_codes as $key => $code) - { - if ($model = \Yii::$app->measureClassifier->getMeasureByCode($code)) { - $result[$code] = $model; - } - } - - return $result; - } - - public function getDataForSelect() - { - $result = []; - - $first = []; - foreach ((array) $this->active_measure_codes as $key => $code) - { - if ($model = \Yii::$app->measureClassifier->getMeasureByCode($code)) { - $first[$code] = $model->name . " (" . $model->symbol . ") " . "[" . $model->code . "]"; - } - } - - $result["По умолчанию"] = $first; - - foreach (\Yii::$app->measureClassifier->data as $key => $data) - { - $title1 = ArrayHelper::getValue($data, 'title'); - ArrayHelper::remove($data, 'title'); - - foreach ($data as $subKey => $subData) - { - $title = ArrayHelper::getValue($subData, 'title') . " - " . $title1; - ArrayHelper::remove($subData, 'title'); - $tmpArr = []; - - foreach ((array) $subData as $measureKey => $measure) - { - if (!in_array(ArrayHelper::getValue($measure, 'code'), $this->active_measure_codes)) { - $tmpArr[ArrayHelper::getValue($measure, 'code')] = ArrayHelper::getValue($measure, 'name') . " (" . ArrayHelper::getValue($measure, 'symbol') . ") " . "[" . ArrayHelper::getValue($measure, 'code') . "]"; - } - } - - $result[$title] = $tmpArr; - } - } - - return $result; - } } \ No newline at end of file diff --git a/src/controllers/AdminMeasureController.php b/src/controllers/AdminMeasureController.php index e26fef3..5477957 100644 --- a/src/controllers/AdminMeasureController.php +++ b/src/controllers/AdminMeasureController.php @@ -11,10 +11,14 @@ use skeeks\cms\backend\controllers\BackendModelStandartController; use skeeks\cms\backend\grid\DefaultActionColumn; use skeeks\cms\backend\ViewBackendAction; +use skeeks\cms\base\DynamicModel; +use skeeks\cms\helpers\RequestResponse; use skeeks\cms\kladr\models\KladrLocation; use skeeks\cms\measure\models\CmsMeasure; use skeeks\yii2\form\fields\FieldSet; use skeeks\yii2\form\fields\NumberField; +use skeeks\yii2\form\fields\SelectField; +use yii\base\Event; use yii\helpers\ArrayHelper; /** @@ -42,8 +46,8 @@ public function actions() return ArrayHelper::merge(parent::actions(), [ 'classifier' => [ 'class' => ViewBackendAction::class, - 'name' => 'Классификатор', - 'icon' => 'fa fa-list' + 'name' => 'Классификатор', + 'icon' => 'fa fa-list', ], 'index' => [ 'filters' => [ @@ -52,10 +56,10 @@ public function actions() 'name', ], ], - 'grid' => [ - 'defaultOrder' => [ + 'grid' => [ + 'defaultOrder' => [ //'def' => SORT_DESC, - 'priority' => SORT_ASC + 'priority' => SORT_ASC, ], "visibleColumns" => [ 'checkbox', @@ -65,19 +69,28 @@ public function actions() 'priority', ], - 'columns' => [ + 'columns' => [ 'customName' => [ - 'attribute' => 'name', - 'class' => DefaultActionColumn::class, - 'viewAttribute' => 'asText' - ] - ] + 'attribute' => 'name', + 'class' => DefaultActionColumn::class, + 'viewAttribute' => 'asText', + ], + ], ], ], "create" => [ - 'fields' => [$this, 'updateFields'], + 'fields' => [$this, 'updateFields'], + 'on initFormModels' => function (Event $e) { + $model = $e->sender->model; + + $dm = new DynamicModel(['classifier']); + $dm->addRule(['classifier'], 'string'); + + $e->sender->formModels['dm'] = $dm; + + }, ], "update" => [ @@ -87,31 +100,72 @@ public function actions() } - public function updateFields() + public function updateFields($action) { - return [ - 'main' => [ - 'class' => FieldSet::class, - 'name' => 'Основные данные', - 'fields' => [ - 'code', - 'name', - 'symbol', - ] + $mainFields = [ + 'code', + 'name', + 'symbol', + ]; + + + /** + * @var CmsMeasure $model + */ + $model = $action->model; + if ($model->isNewRecord) { + + $dm = $action->formModels['dm']; + if ($dm->classifier) { + if ($measure = \Yii::$app->measureClassifier->getMeasureByCode($dm->classifier)) { + $model->name = $measure->name; + $model->symbol = $measure->symbol; + $model->symbol_intl = $measure->symbol_intl; + $model->symbol_letter_intl = $measure->symbol_letter_intl; + $model->code = $measure->code; + } + } + + + $mainFields = [ + + 'dm.classifier' => [ + 'class' => SelectField::class, + 'items' => \Yii::$app->measureClassifier->getDataForSelect(), + 'label' => 'Базовый классификатор', + 'hint' => 'Выберите интересующую единицу измерения и все данные будут заполнены в форме автоматически', + 'elementOptions' => [ + RequestResponse::DYNAMIC_RELOAD_FIELD_ELEMENT => 'true', + ], + ], + + 'code', + 'name', + 'symbol', + ]; + } + + $result = [ + 'main' => [ + 'class' => FieldSet::class, + 'name' => 'Основные данные', + 'fields' => $mainFields, ], 'second' => [ - 'class' => FieldSet::class, + 'class' => FieldSet::class, 'elementOptions' => ['isOpen' => false], - 'name' => 'Дополнительно', - 'fields' => [ + 'name' => 'Дополнительно', + 'fields' => [ 'symbol_intl', 'symbol_letter_intl', 'priority' => [ - 'class' => NumberField::class - ] - ] + 'class' => NumberField::class, + ], + ], ], ]; + + return $result; } } diff --git a/src/migrations/m200307_090602_append_data_table__cms_measure.php b/src/migrations/m200307_090602_append_data_table__cms_measure.php index 0142ad8..47d5911 100644 --- a/src/migrations/m200307_090602_append_data_table__cms_measure.php +++ b/src/migrations/m200307_090602_append_data_table__cms_measure.php @@ -88,11 +88,11 @@ public function safeUp() 'priority' => 350, ]); - $this->insert('{{%cms_measure}}', [ + /*$this->insert('{{%cms_measure}}', [ 'code' => '383', 'name' => 'Рубль', 'symbol' => 'руб', - ]); + ]);*/ }