From: Vladimir Fomichev Date: Fri, 5 Sep 2025 15:17:13 +0000 (+0300) Subject: Фильтрация по активности X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=9a47d3d3e34ed5ff7b0006807ca9a52fa44f8470;p=erp24_rep%2Fyii-erp24%2F.git Фильтрация по активности --- diff --git a/erp24/controllers/MatrixTypeController.php b/erp24/controllers/MatrixTypeController.php index 389ee1d2..582902af 100644 --- a/erp24/controllers/MatrixTypeController.php +++ b/erp24/controllers/MatrixTypeController.php @@ -8,6 +8,7 @@ use yii\data\ActiveDataProvider; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use yii_app\records\MatrixTypeSearch; /** * MatrixTypeController implements the CRUD actions for MatrixType model. @@ -32,11 +33,11 @@ class MatrixTypeController extends Controller */ public function actionIndex() { - $dataProvider = new ActiveDataProvider([ - 'query' => MatrixType::find(), - ]); + $searchModel = new MatrixTypeSearch(); + $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); return $this->render('index', [ + 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } diff --git a/erp24/records/MatrixType.php b/erp24/records/MatrixType.php index 0709aa18..2904ff18 100644 --- a/erp24/records/MatrixType.php +++ b/erp24/records/MatrixType.php @@ -91,4 +91,20 @@ class MatrixType extends \yii\db\ActiveRecord 'updated_at' => 'Дата обновления', ]; } + + public function getParent() + { + return $this->hasOne(self::class, ['id' => 'parent_id']); + } + + public function collectAncestorIds(array $map): array + { + $ids = []; + $cur = $this; + while ($cur && $cur->parent_id && isset($map[$cur->parent_id])) { + $ids[] = $cur->parent_id; + $cur = $map[$cur->parent_id]; + } + return $ids; + } } diff --git a/erp24/records/MatrixTypeSearch.php b/erp24/records/MatrixTypeSearch.php new file mode 100644 index 00000000..d235f8fb --- /dev/null +++ b/erp24/records/MatrixTypeSearch.php @@ -0,0 +1,57 @@ +load($params, ''); + + $allTypes = MatrixType::find()->orderBy(['parent_id' => SORT_ASC, 'id' => SORT_ASC])->all(); + + if ($this->active === '' || $this->active === null) { + return new ArrayDataProvider(['allModels' => $allTypes, 'pagination' => false, 'sort' => false]); + } + + $map = []; + foreach ($allTypes as $type) { + $map[$type->id] = $type; + } + + if ((string)$this->active === '1') { + $filtered = array_values(array_filter($allTypes, fn($type) => (int)$type->active === 1)); + return new ArrayDataProvider(['allModels' => $filtered, 'pagination' => false, 'sort' => false]); + } + + $include = []; + foreach ($allTypes as $type) { + if ((int)$type->active === 0) { + $include[$type->id] = true; + foreach ($type->collectAncestorIds($map) as $pid) { + $include[$pid] = true; + } + } + } + + $filtered = array_values(array_filter($allTypes, fn($type) => isset($include[$type->id]))); + + return new ArrayDataProvider([ + 'allModels' => $filtered, + 'pagination' => false, + 'sort' => false, + ]); + } +} \ No newline at end of file diff --git a/erp24/views/matrix-type/index.php b/erp24/views/matrix-type/index.php index 31527777..cbfd577b 100644 --- a/erp24/views/matrix-type/index.php +++ b/erp24/views/matrix-type/index.php @@ -4,11 +4,12 @@ use leandrogehlen\treegrid\TreeGrid; use yii\helpers\Html; use yii\grid\GridView; use yii\helpers\Url; +use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ -$this->title = 'Типы матриц'; +$this->title = 'Управление категориями букетов'; $this->params['breadcrumbs'][] = $this->title; $this->registerJsFile('/js/matrix-type/index.js', ['position' => \yii\web\View::POS_END]); ?> @@ -16,9 +17,37 @@ $this->registerJsFile('/js/matrix-type/index.js', ['position' => \yii\web\View::

title) ?>

-

- 'btn btn-success']) ?> -

+
+

+ 'btn btn-success']) ?> +

+ + +
+ 'get', + 'action' => ['index'], + 'options' => ['class' => 'd-flex align-items-center gap-2'], + ]); ?> + + request->get('active'), + [ + '' => 'Все', + '1' => 'Активные', + '0' => 'Неактивные', + ], + ['class' => 'form-select'] + ) ?> + + 'btn btn-primary']) ?> + + +
+
+ + $dataProvider, @@ -41,6 +70,12 @@ $this->registerJsFile('/js/matrix-type/index.js', ['position' => \yii\web\View:: 'keyColumnName' => 'id', 'showOnEmpty' => FALSE, 'parentColumnName' => 'parent_id', + 'rowOptions' => function($model){ + if ((int)$model->active === 0) { + return ['class' => 'text-muted', 'style' => 'background:#ffd1d1;']; + } + return []; + }, 'columns' => [ //'name',