Skip to content

Commit c733b7f

Browse files
authored
HP-2320: add logic for type and move_type input when move (#182)
1 parent 949bf73 commit c733b7f

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

src/models/Part.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function ($attribute) {
139139
[['serials'], 'unique', 'on' => ['create', 'copy']],
140140

141141
// Move by one
142-
[['id', 'dst_id', 'src_id', 'partno', 'serial'], 'required', 'on' => 'move-by-one'],
142+
[['id', 'dst_id', 'src_id', 'partno', 'serial', 'move_type'], 'required', 'on' => 'move-by-one'],
143143

144144
// Trash
145145
[['id', 'dst_id', 'src_id', 'partno', 'serial', 'move_descr'], 'required', 'on' => 'trash'],

src/views/part/_move.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use hipanel\modules\stock\models\Part;
88
use hipanel\modules\stock\widgets\combo\DestinationCombo;
99
use hipanel\modules\stock\widgets\combo\SourceCombo;
10+
use hipanel\modules\stock\widgets\MoveTypeDropDownList;
1011
use hipanel\widgets\Box;
1112
use hipanel\widgets\ArraySpoiler;
1213
use yii\helpers\Html;
@@ -52,7 +53,7 @@
5253
]) ?>
5354
</div>
5455
<div class="col-lg-4">
55-
<?= $form->field($model, "[$src_id]type")->dropDownList($types, ['id' => "$src_id-type-" . uniqid()]) ?>
56+
<?= $form->field($model, "[$src_id]type")->widget(MoveTypeDropDownList::class, ['items' => $types, 'id' => "$src_id-type-" . uniqid()]) ?>
5657
</div>
5758
</div>
5859
<div class="row">

src/views/part/moveByOne.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
use hipanel\modules\stock\widgets\combo\RmaDestinationCombo;
66
use hipanel\modules\stock\widgets\combo\PartnoCombo;
77
use hipanel\modules\stock\widgets\combo\SourceCombo;
8+
use hipanel\modules\stock\widgets\MoveTypeDropDownList;
89
use hipanel\widgets\Box;
910
use hipanel\widgets\DynamicFormWidget;
1011
use yii\bootstrap\ActiveForm;
1112
use yii\helpers\Html;
1213
use yii\helpers\StringHelper;
1314

15+
/**
16+
* @var \yii\web\View $this
17+
* @var \hipanel\modules\stock\models\Part[] $models
18+
* @var array $remotehands
19+
* @var array $types
20+
*/
21+
1422
$scenario = $this->context->action->scenario;
1523
$this->title = StringHelper::startsWith($this->context->action->id, 'move-by-one') ? Yii::t('hipanel:stock', 'Move by one') : Yii::t('hipanel:stock', 'RMA');
1624
$this->params['breadcrumbs'][] = ['label' => Yii::t('hipanel:stock', 'Parts'), 'url' => ['index']];
@@ -94,7 +102,7 @@
94102
</div>
95103
<div class="col-md-6">
96104
<?php $model->dst_id = null ?>
97-
<?php if (strstr($this->context->action->id, 'rma') !== false) : ?>
105+
<?php if (str_contains($this->context->action->id, 'rma')) : ?>
98106
<?= $form->field($model, "[$i]dst_id")->widget(RmaDestinationCombo::class) ?>
99107
<?php else : ?>
100108
<?= $form->field($model, "[$i]dst_id")->widget(DestinationCombo::class) ?>
@@ -105,7 +113,7 @@
105113

106114
<div class="row">
107115
<div class="col-md-6">
108-
<?= $form->field($model, "[$i]move_type")->dropDownList($types) ?>
116+
<?= $form->field($model, "[$i]move_type")->widget(MoveTypeDropDownList::class, ['items' => $types]) ?>
109117
</div>
110118
<div class="col-md-6">
111119
<?= $form->field($model, "[$i]remotehands")->dropDownList($remotehands) ?>

src/widgets/MoveTypeDropDownList.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hipanel\modules\stock\widgets;
4+
5+
use yii\base\InvalidConfigException;
6+
use yii\helpers\ArrayHelper;
7+
use yii\helpers\Html;
8+
use yii\widgets\InputWidget;
9+
10+
class MoveTypeDropDownList extends InputWidget
11+
{
12+
public array $items = [];
13+
14+
public function init(): void
15+
{
16+
parent::init();
17+
if (!$this->hasModel()) {
18+
throw new InvalidConfigException("Model is required");
19+
}
20+
$id = ArrayHelper::getValue($this->options, 'id');
21+
$this->view->registerJs(/** @lang JavaScript */ <<<JS
22+
(($) => {
23+
"use strict";
24+
$("#$id").val("");
25+
const dstSelect = $("#$id").parents(".box").find("select[id*=\"-dst_id\"]");
26+
if (dstSelect.length) {
27+
dstSelect.on("change", function () {
28+
const dstType = $(this).find("option:selected").data("type");
29+
if (["chwbox", "stock"].includes(dstType)) {
30+
$("#$id").val("stock");
31+
} else if ([
32+
"ahcloud",
33+
"avdsnode",
34+
"cloudstorage",
35+
"delivery",
36+
"dedicated",
37+
"system",
38+
"nic",
39+
"cdnstat",
40+
"remote",
41+
"reserved",
42+
"suspended",
43+
"termination",
44+
"utilization",
45+
"unused",
46+
"cdnv2",
47+
"vdsmaster",
48+
"cloudservers",
49+
].includes(dstType)) {
50+
$("#$id").val("install");
51+
} else {
52+
$("#$id").val("");
53+
}
54+
});
55+
}
56+
})(jQuery);
57+
JS
58+
);
59+
}
60+
61+
public function run()
62+
{
63+
return Html::activeDropDownList($this->model, $this->attribute, $this->items, ArrayHelper::merge($this->options, ['prompt' => '']));
64+
}
65+
}

src/widgets/combo/DestinationCombo.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace hipanel\modules\stock\widgets\combo;
1313

1414
use hiqdev\combo\Combo;
15+
use yii\web\JsExpression;
1516

1617
class DestinationCombo extends Combo
1718
{
@@ -25,10 +26,28 @@ class DestinationCombo extends Combo
2526
public $url = '/stock/move/directions-list';
2627

2728
/** {@inheritdoc} */
28-
public $_return = ['id'];
29+
public $_return = ['id', 'type'];
2930

3031
/** {@inheritdoc} */
3132
public $_rename = ['text' => 'name'];
3233

3334
public $_primaryFilter = 'name_like';
35+
36+
public function getPluginOptions($options = [])
37+
{
38+
return parent::getPluginOptions([
39+
'select2Options' => [
40+
'templateSelection' => new JsExpression(/** @lang JavaScript */ "function (data, container) {
41+
if ('element' in data) {
42+
$(data.element).attr('data-type', data?.type);
43+
}
44+
45+
return data.text;
46+
}"),
47+
'escapeMarkup' => new JsExpression('function (markup) {
48+
return markup; // Allows HTML
49+
}'),
50+
],
51+
]);
52+
}
3453
}

0 commit comments

Comments
 (0)