Skip to content

Commit

Permalink
measure update
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeks-semenov committed Mar 7, 2020
1 parent d36ef88 commit 924e2ea
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 114 deletions.
89 changes: 5 additions & 84 deletions src/components/MeasureComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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();
}
],
],*/
];
}

Expand All @@ -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;
}
}
110 changes: 82 additions & 28 deletions src/controllers/AdminMeasureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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' => [
Expand All @@ -52,10 +56,10 @@ public function actions()
'name',
],
],
'grid' => [
'defaultOrder' => [
'grid' => [
'defaultOrder' => [
//'def' => SORT_DESC,
'priority' => SORT_ASC
'priority' => SORT_ASC,
],
"visibleColumns" => [
'checkbox',
Expand All @@ -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" => [
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public function safeUp()
'priority' => 350,
]);

$this->insert('{{%cms_measure}}', [
/*$this->insert('{{%cms_measure}}', [
'code' => '383',
'name' => 'Рубль',
'symbol' => 'руб',
]);
]);*/

}

Expand Down

0 comments on commit 924e2ea

Please sign in to comment.