]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Фильтрация по активности
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 5 Sep 2025 15:17:13 +0000 (18:17 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 5 Sep 2025 15:17:13 +0000 (18:17 +0300)
erp24/controllers/MatrixTypeController.php
erp24/records/MatrixType.php
erp24/records/MatrixTypeSearch.php [new file with mode: 0644]
erp24/views/matrix-type/index.php

index 389ee1d29b3e71f94d81eb3c5df33a537fea5643..582902af2773244db955b0474f3a1807326586e8 100644 (file)
@@ -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,
         ]);
     }
index 0709aa18ad8688c9d6329ddd4d1cf9a2f539eb0a..2904ff1811229804238cd704ac50701919037f7a 100644 (file)
@@ -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 (file)
index 0000000..d235f8f
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace yii_app\records;
+
+use yii\data\ActiveDataProvider;
+use yii\data\ArrayDataProvider;
+use yii\helpers\ArrayHelper;
+
+
+class MatrixTypeSearch extends MatrixType
+{
+    public function rules()
+    {
+        return [
+            [['active'], 'safe'],
+        ];
+    }
+
+    public function search($params)
+    {
+        $this->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
index 31527777f5bc9e85ec0112eebc0922b52aced84b..cbfd577b5fdc5618ee3eb858187f822764cc72fd 100644 (file)
@@ -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 = 'ТипÑ\8b Ð¼Ð°Ñ\82Ñ\80иÑ\86';
+$this->title = 'УпÑ\80авление ÐºÐ°Ñ\82егоÑ\80иÑ\8fми Ð±Ñ\83кеÑ\82ов';
 $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::
 
     <h1><?= Html::encode($this->title) ?></h1>
 
-    <p>
-        <?= Html::a('Создать новый тип матрицы', ['create'], ['class' => 'btn btn-success']) ?>
-    </p>
+    <div class="d-flex justify-content-between mb-4">
+        <p>
+            <?= Html::a('Добавить группу', ['create'], ['class' => 'btn btn-success']) ?>
+        </p>
+
+
+        <div class="mb-3">
+            <?php $form = ActiveForm::begin([
+                'method' => 'get',
+                'action' => ['index'],
+                'options' => ['class' => 'd-flex align-items-center gap-2'],
+            ]); ?>
+
+            <?= Html::dropDownList(
+                'active',
+                Yii::$app->request->get('active'),
+                [
+                    '' => 'Все',
+                    '1' => 'Активные',
+                    '0' => 'Неактивные',
+                ],
+                ['class' => 'form-select']
+            ) ?>
+
+            <?= Html::submitButton('Применить', ['class' => 'btn btn-primary']) ?>
+
+            <?php ActiveForm::end(); ?>
+        </div>
+    </div>
+
+
 
 <!--    --><?php //= GridView::widget([
 //        'dataProvider' => $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',