From: fomichev Date: Thu, 16 Jan 2025 09:02:34 +0000 (+0300) Subject: Добавлено мягкое удаление X-Git-Tag: 1.7~57^2 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=32ea58b22bbe2c2b79eec5d145764e5847c17a7c;p=erp24_rep%2Fyii-erp24%2F.git Добавлено мягкое удаление --- diff --git a/erp24/controllers/StoresTypeListController.php b/erp24/controllers/StoresTypeListController.php index 474cfec6..8d3aaee8 100644 --- a/erp24/controllers/StoresTypeListController.php +++ b/erp24/controllers/StoresTypeListController.php @@ -42,6 +42,11 @@ class StoresTypeListController extends Controller $model = new StoresTypeList(); $editId = $this->request->post('editId'); + $dataProvider = new ActiveDataProvider([ + 'query' => StoresTypeList::find(), + ]); + + if ($model->load($this->request->post())) { if (!empty($editId)) { // Редактирование @@ -54,23 +59,40 @@ class StoresTypeListController extends Controller return $this->redirect(['index']); } else { Yii::$app->session->setFlash('error', 'Ошибка сохранения.'); + return $this->renderAjax('index', [ + 'model' => $model, + 'dataProvider' => $dataProvider, + ]); } } } else { $model->type_alias = strtolower(preg_replace('/[\s-]+/', '_', $model->type_name)); + $existingDeleted = StoresTypeList::find()->where(['type_name' => $model->type_name, 'active' => 0])->one(); + if ($existingDeleted) { + Yii::$app->session->setFlash('error', 'Запись с таким именем была удалена. Вы можете восстановить её.'); + if (Yii::$app->request->isPjax) { + return $this->renderAjax('index', [ + 'model' => $model, + 'dataProvider' => $dataProvider, + ]); + } + return; + } if ($model->save()) { Yii::$app->session->setFlash('success', 'Новая запись успешно создана.'); return $this->redirect(['index']); } else { Yii::$app->session->setFlash('error', 'Ошибка сохранения.'); + if (Yii::$app->request->isPjax) { + return $this->renderAjax('index', [ + 'model' => $model, + 'dataProvider' => $dataProvider, + ]); + } } } } - $dataProvider = new ActiveDataProvider([ - 'query' => StoresTypeList::find(), - ]); - return $this->render('index', [ 'dataProvider' => $dataProvider, 'model' => $model, @@ -150,7 +172,7 @@ class StoresTypeListController extends Controller */ public function actionDelete($id) { - $this->findModel($id)->delete(); + $this->findModel($id)->softDelete(); if (Yii::$app->request->isAjax) { return $this->asJson(['success' => true, 'message' => 'Запись успешно удалена.']); diff --git a/erp24/migrations/m250115_114940_create_stores_type_list_table.php b/erp24/migrations/m250115_114940_create_stores_type_list_table.php index 4ad1b418..443de77e 100644 --- a/erp24/migrations/m250115_114940_create_stores_type_list_table.php +++ b/erp24/migrations/m250115_114940_create_stores_type_list_table.php @@ -21,6 +21,10 @@ class m250115_114940_create_stores_type_list_table extends Migration 'type_name' => $this->string()->notNull()->comment('Наименование типа магазина'), 'type_alias' => $this->string()->notNull()->comment('Алиас типа магазина'), 'type_description' => $this->string()->null()->comment('Описание типа магазина'), + 'active' => $this->tinyInteger()->notNull()->defaultValue(1)->comment('Активность'), + 'deleted_by' => $this->integer()->null()->comment('Пользователь, удаливший запись'), + 'deleted_at' => $this->timestamp() + ->null()->defaultValue(null)->comment('Дата мягкого удаления'), 'created_at' => $this->dateTime() ->notNull() ->defaultExpression('CURRENT_TIMESTAMP') diff --git a/erp24/records/StoresTypeList.php b/erp24/records/StoresTypeList.php index 1955e758..1168e8b4 100644 --- a/erp24/records/StoresTypeList.php +++ b/erp24/records/StoresTypeList.php @@ -3,6 +3,7 @@ namespace yii_app\records; use Yii; +use yii_app\traits\SoftDeleteTrait; /** * This is the model class for table "stores_type_list". @@ -11,10 +12,15 @@ use Yii; * @property string $type_name Наименование типа магазина * @property string $type_alias Алиас типа магазина * @property string|null $type_description Описание типа магазина + * @property Admin $deleted_by удаливший пользователь + * @property int $active активность + * @property string $deleted_at время удаления * @property string $created_at Дата создания записи */ class StoresTypeList extends \yii\db\ActiveRecord { + use SoftDeleteTrait; + /** * {@inheritdoc} */ @@ -30,8 +36,8 @@ class StoresTypeList extends \yii\db\ActiveRecord { return [ [['type_name'], 'required'], - [['created_at'], 'safe'], - [['type_name'], 'unique', 'message' => 'Название "{value}" уже существует.'], + [['created_at', 'active'], 'safe'], + [['type_name'], 'unique', 'targetAttribute' => 'type_name', 'filter' => ['active' => 1], 'message' => 'Название "{value}" уже существует.'], [['type_name', 'type_alias', 'type_description'], 'string', 'max' => 255], ]; } @@ -49,4 +55,18 @@ class StoresTypeList extends \yii\db\ActiveRecord 'created_at' => 'Дата создания записи', ]; } + + public static function hasSoftDelete(): bool + { + return true; + } + + public static function find() + { + $query = parent::find(); + if (static::hasSoftDelete()) { + $query->andWhere(['active' => 1]); + } + return $query; + } } diff --git a/erp24/traits/SoftDeleteTrait.php b/erp24/traits/SoftDeleteTrait.php index 6fd45fed..4b3f48ff 100644 --- a/erp24/traits/SoftDeleteTrait.php +++ b/erp24/traits/SoftDeleteTrait.php @@ -2,7 +2,6 @@ namespace yii_app\traits; use Yii; -use yii\db\ActiveQuery; trait SoftDeleteTrait { diff --git a/erp24/web/js/crud/stores-type-list/index.js b/erp24/web/js/crud/stores-type-list/index.js index 7e2b2972..e7d0675e 100644 --- a/erp24/web/js/crud/stores-type-list/index.js +++ b/erp24/web/js/crud/stores-type-list/index.js @@ -1,4 +1,14 @@ document.addEventListener('DOMContentLoaded', function () { + init(); + $(document).on('pjax:end', function () { + console.log('Pjax обновил содержимое.'); + init(); + }); +}); + + + +function init () { const list = document.getElementById('record-list'); const typeNameField = document.getElementById('type-name-field'); const editIdInput = document.getElementById('edit-id'); @@ -16,7 +26,7 @@ document.addEventListener('DOMContentLoaded', function () { const id = target.dataset.id; fetch('/stores-type-list/get-record?id=' + id) - .then(response => response.json()) + .then(response => response.json()) .then(data => { if (data.error) { alert(data.error); @@ -93,4 +103,4 @@ document.addEventListener('DOMContentLoaded', function () { item.classList.remove('active'); }); }); -}); \ No newline at end of file +} \ No newline at end of file