Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model = $this->findModel($id);
+
if ($model) {
+
+ $guid = $model->guid;
+
+
+ $remainingRecords = Product1cReplacement::find()
+ ->where(['guid' => $guid])
+ ->andWhere(['deleted_at' => null])
+ ->count();
+
$stateBefore = $model->guid_replacement;
$this->logReplacementAction(
$model->id,
$stateBefore,
'Запись удалена'
);
- if ($model->softDelete()) {
- return ['success' => true];
+ if ($model->softDelete()) {
+ return [
+ 'success' => true,
+ 'message' => 'Запись успешно удалена',
+ 'redirect' => $remainingRecords <= 1,
+ ];
}
}
return ['success' => false, 'message' => 'Ошибка при удалении записи.'];
}
+
+
/**
* Finds the Product1CReplacement model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
function initReplacementHandlers() {
let counter = $('.guid-replacement-select').length;
- $('#add-guid-replacement').on('click', function () {
+ $('#add-guid-replacement').off('click').on('click', function () {
counter++;
let newField = `
<div class="guid-replacement-group my-4" id="guid-replacement-group-${counter}" style="position: relative;">
});
});
- $(document).on('click', '.remove-guid-replacement', function () {
+ $(document).off('click', '.remove-guid-replacement').on('click', '.remove-guid-replacement', function () {
let target = $(this).data('target');
$(target).remove();
});
- $(document).on('click', '.delete-guid-replacement', function (e) {
+ $(document).off('click', '.delete-guid-replacement').on('click', '.delete-guid-replacement', function (e) {
e.preventDefault();
if (!confirm('Вы уверены, что хотите удалить эту запись?')) {
return;
success: function(data) {
console.log(data);
if (data.success) {
-
- $.pjax.reload({container: '#pjax-container', async: false});
+ alert(data.message);
+ if (data.redirect) {
+ window.location.href = '/crud/product1c-replacement/index';
+ } else {
+ $.pjax.reload({container: '#pjax-container', async: false});
+ }
} else {
alert(data.message || 'Ошибка при удалении записи.');
}