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.
*/
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,
]);
}
'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;
+ }
}
--- /dev/null
+<?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
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]);
?>
<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,
'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',