];
}
- public function beforeValidate()
+ public function beforeSave($insert)
{
- if (parent::beforeValidate()) {
- if ($this->status_id_from !== null && $this->order === null) {
- // Получаем из БД максимальный order для этого status_id_from
- $maxOrder = (int) self::find()
- ->where(['status_id_from' => $this->status_id_from])
- ->max('`order`');
- $this->order = $maxOrder + 1;
- }
- return true;
+ if (!parent::beforeSave($insert)) {
+ return false;
}
- return false;
+
+ if ($this->status_id_from !== null && $this->order === null) {
+ $last = self::find()
+ ->where(['status_id_from' => $this->status_id_from])
+ ->orderBy(['order' => SORT_DESC])
+ ->one();
+
+ $this->order = ($last !== null ? $last->order : 0) + 1;
+ }
+
+ return true;
}
+
}
<?php foreach ($relationModels as $i => $rel):
$initialOrder = $rel->order ?: ($i + 1);
?>
- <div class="relation-item" style="border:1px solid #ddd; padding:10px; margin-bottom:10px; position: relative; background:#f9f9f9;">
- <span class="drag-handle" style="cursor:move; font-size:18px; margin-right:8px;" title="Перетащите, чтобы изменить порядок">☰</span>
+ <div class="relation-item border border-1 border-secondary bg-light p-2 mb-3 position-relative" >
+ <span class="drag-handle cursor-move me-2 fs-5" title="Перетащите, чтобы изменить порядок">☰</span>
<strong>Порядок: <span class="order-label"><?= $initialOrder ?></span></strong>
<?= Html::hiddenInput("Relations[{$i}][order]", $initialOrder, ['class' => 'rel-order-input']) ?>
- <div style="margin-top:8px;">
+ <div class="mt-6">
<?= Html::label('Статус-назначения', null, ['class' => 'control-label']) ?>
<?= Html::dropDownList(
"Relations[{$i}][status_id_to]",
); ?>
</div>
- <div style="margin-top:8px;">
+ <div class="mt-6" >
<?= Html::label('Описание (description)', null, ['class' => 'control-label']) ?>
<?= Html::textInput(
"Relations[{$i}][description]",
); ?>
</div>
- <div style="margin-top:8px;">
+ <div class="mt-6">
<?= Html::label('Текст кнопки (button_text)', null, ['class' => 'control-label']) ?>
<?= Html::textInput(
"Relations[{$i}][button_text]",
); ?>
</div>
- <button type="button" class="btn btn-danger btn-sm remove-relation-btn"
- style="position:absolute; top:10px; right:10px;">
+ <button type="button" class="btn btn-danger btn-sm remove-relation-btn position-absolute top-0 end-0 mt-2 me-2">
Удалить
</button>
</div>
<?php endforeach; ?>
</div>
- <button type="button" id="add-relation-btn" class="btn btn-primary btn-sm" style="margin-bottom:15px;">
+ <button type="button" id="add-relation-btn" class="btn btn-primary btn-sm mb-8" >
+ Добавить ещё одну связь
</button>
<?php ActiveForm::end(); ?>
- <div id="relation-template" style="display:none;">
- <div class="relation-item template" style="border:1px solid #ddd; padding:10px; margin-bottom:10px; position: relative; background:#f9f9f9;">
- <span class="drag-handle" style="cursor:move; font-size:18px; margin-right:8px;" title="Перетащите, чтобы изменить порядок">☰</span>
+ <div id="relation-template d-none" >
+ <div class="relation-item template border border-1 border-secondary bg-light p-2 mb-3 position-relative">
+ <span class="drag-handle cursor-move me-2 fs-5" title="Перетащите, чтобы изменить порядок">☰</span>
<strong>Порядок: <span class="order-label">__order__</span></strong>
<?= Html::hiddenInput("Relations[__index__][order]", "__order__", ['class' => 'rel-order-input']) ?>
- <div style="margin-top:8px;">
+ <div class="mt-6">
<?= Html::label('Статус-назначения', null, ['class' => 'control-label']) ?>
<?= Html::dropDownList(
"Relations[__index__][status_id_to]",
); ?>
</div>
- <div style="margin-top:8px;">
+ <div class="mt-6">
<?= Html::label('Описание (description)', null, ['class' => 'control-label']) ?>
<?= Html::textInput("Relations[__index__][description]", null, ['class' => 'form-control']) ?>
</div>
- <div style="margin-top:8px;">
+ <div class="mt-6">
<?= Html::label('Текст кнопки (button_text)', null, ['class' => 'control-label']) ?>
<?= Html::textInput("Relations[__index__][button_text]", null, ['class' => 'form-control']) ?>
</div>
- <button type="button" class="btn btn-danger btn-sm remove-relation-btn"
- style="position:absolute; top:10px; right:10px;">
+ <button type="button" class="btn btn-danger btn-sm remove-relation-btn position-absolute top-0 end-0 mt-2 me-2">
Удалить
</button>
</div>