Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HP-2320: add logic for type and move_type input when move #182

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/models/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function ($attribute) {
[['serials'], 'unique', 'on' => ['create', 'copy']],

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

// Trash
[['id', 'dst_id', 'src_id', 'partno', 'serial', 'move_descr'], 'required', 'on' => 'trash'],
Expand Down
3 changes: 2 additions & 1 deletion src/views/part/_move.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use hipanel\modules\stock\models\Part;
use hipanel\modules\stock\widgets\combo\DestinationCombo;
use hipanel\modules\stock\widgets\combo\SourceCombo;
use hipanel\modules\stock\widgets\MoveTypeDropDownList;
use hipanel\widgets\Box;
use hipanel\widgets\ArraySpoiler;
use yii\helpers\Html;
Expand Down Expand Up @@ -52,7 +53,7 @@
]) ?>
</div>
<div class="col-lg-4">
<?= $form->field($model, "[$src_id]type")->dropDownList($types, ['id' => "$src_id-type-" . uniqid()]) ?>
<?= $form->field($model, "[$src_id]type")->widget(MoveTypeDropDownList::class, ['items' => $types, 'id' => "$src_id-type-" . uniqid()]) ?>
</div>
</div>
<div class="row">
Expand Down
12 changes: 10 additions & 2 deletions src/views/part/moveByOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
use hipanel\modules\stock\widgets\combo\RmaDestinationCombo;
use hipanel\modules\stock\widgets\combo\PartnoCombo;
use hipanel\modules\stock\widgets\combo\SourceCombo;
use hipanel\modules\stock\widgets\MoveTypeDropDownList;
use hipanel\widgets\Box;
use hipanel\widgets\DynamicFormWidget;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\StringHelper;

/**
* @var \yii\web\View $this
* @var \hipanel\modules\stock\models\Part[] $models
* @var array $remotehands
* @var array $types
*/

$scenario = $this->context->action->scenario;
$this->title = StringHelper::startsWith($this->context->action->id, 'move-by-one') ? Yii::t('hipanel:stock', 'Move by one') : Yii::t('hipanel:stock', 'RMA');
$this->params['breadcrumbs'][] = ['label' => Yii::t('hipanel:stock', 'Parts'), 'url' => ['index']];
Expand Down Expand Up @@ -94,7 +102,7 @@
</div>
<div class="col-md-6">
<?php $model->dst_id = null ?>
<?php if (strstr($this->context->action->id, 'rma') !== false) : ?>
<?php if (str_contains($this->context->action->id, 'rma')) : ?>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

PHP 8.0 function used but PHP 7.3 installations are supported
The core package hipanel-core supports PHP ^7.3||^8.0, but str_contains() is only available in PHP 8.0+. This could cause runtime errors in PHP 7.3 environments.

Suggestions:

  • Add explicit PHP 8.0+ requirement in this module's composer.json, or
  • Use strpos() !== false as a PHP 7.3 compatible alternative
  • Consider using Symfony's polyfill-php80 package for broader compatibility
🔗 Analysis chain

Conditional for RMA combos is appropriate.
Using str_contains requires at least PHP 8.0. Verify the minimum PHP version in composer.json or project config to avoid runtime errors on older PHP versions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Checking PHP version in composer.json
rg '"php": ".*"'

Length of output: 18


Script:

#!/bin/bash
# Search for composer files and PHP version related configs
fd composer.json
fd composer.lock
fd "^README" -t f
fd Dockerfile
fd "^.php-version$"

Length of output: 135


Script:

#!/bin/bash
# Check content of composer.json and README files
cat composer.json
echo "=== README.md ==="
cat README.md

Length of output: 3712


Script:

#!/bin/bash
# Check hipanel-core package requirements
curl -s https://raw.githubusercontent.com/hiqdev/hipanel-core/master/composer.json | jq .

Length of output: 3882

<?= $form->field($model, "[$i]dst_id")->widget(RmaDestinationCombo::class) ?>
<?php else : ?>
<?= $form->field($model, "[$i]dst_id")->widget(DestinationCombo::class) ?>
Expand All @@ -105,7 +113,7 @@

<div class="row">
<div class="col-md-6">
<?= $form->field($model, "[$i]move_type")->dropDownList($types) ?>
<?= $form->field($model, "[$i]move_type")->widget(MoveTypeDropDownList::class, ['items' => $types]) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, "[$i]remotehands")->dropDownList($remotehands) ?>
Expand Down
65 changes: 65 additions & 0 deletions src/widgets/MoveTypeDropDownList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace hipanel\modules\stock\widgets;

use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\InputWidget;

class MoveTypeDropDownList extends InputWidget
{
public array $items = [];

public function init(): void
{
parent::init();
if (!$this->hasModel()) {
throw new InvalidConfigException("Model is required");
}
$id = ArrayHelper::getValue($this->options, 'id');
$this->view->registerJs(/** @lang JavaScript */ <<<JS
(($) => {
"use strict";
$("#$id").val("");
const dstSelect = $("#$id").parents(".box").find("select[id*=\"-dst_id\"]");
if (dstSelect.length) {
dstSelect.on("change", function () {
const dstType = $(this).find("option:selected").data("type");
if (["chwbox", "stock"].includes(dstType)) {
$("#$id").val("stock");
} else if ([
"ahcloud",
"avdsnode",
"cloudstorage",
"delivery",
"dedicated",
"system",
"nic",
"cdnstat",
"remote",
"reserved",
"suspended",
"termination",
"utilization",
"unused",
"cdnv2",
"vdsmaster",
"cloudservers",
].includes(dstType)) {
$("#$id").val("install");
} else {
$("#$id").val("");
}
});
}
})(jQuery);
JS
);
}

public function run()
{
return Html::activeDropDownList($this->model, $this->attribute, $this->items, ArrayHelper::merge($this->options, ['prompt' => '']));
}
}
21 changes: 20 additions & 1 deletion src/widgets/combo/DestinationCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace hipanel\modules\stock\widgets\combo;

use hiqdev\combo\Combo;
use yii\web\JsExpression;

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

/** {@inheritdoc} */
public $_return = ['id'];
public $_return = ['id', 'type'];

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

public $_primaryFilter = 'name_like';

public function getPluginOptions($options = [])
{
return parent::getPluginOptions([
'select2Options' => [
'templateSelection' => new JsExpression(/** @lang JavaScript */ "function (data, container) {
if ('element' in data) {
$(data.element).attr('data-type', data?.type);
}

return data.text;
}"),
'escapeMarkup' => new JsExpression('function (markup) {
return markup; // Allows HTML
}'),
],
]);
}
}