Explorar o código

feat: policy search filter backend

jiangzixin hai 1 ano
pai
achega
135d1e3c0f

+ 13 - 4
server/backend/controllers/PolicyController.php

@@ -2,6 +2,7 @@
 
 namespace backend\controllers;
 
+use backend\models\search\PolicySearch;
 use common\components\Controller;
 use common\enums\PolicyEnum;
 use common\models\Policy;
@@ -9,6 +10,7 @@ use Yii;
 use yii\base\Exception;
 use yii\data\ActiveDataProvider;
 use yii\filters\VerbFilter;
+use yii\helpers\ArrayHelper;
 use yii\helpers\Url;
 use yii\web\NotFoundHttpException;
 use common\modules\config\models\Config;
@@ -37,12 +39,19 @@ class PolicyController extends Controller
     public function actionIndex()
     {
         Url::remember();
-        $dataProvider = new ActiveDataProvider([
-            'query' => Policy::find(),
-        ]);
-
+        $searchModel = new PolicySearch();
+        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
+//        $dataProvider = new ActiveDataProvider([
+//            'query' => Policy::find(),
+//        ]);
+
+        $cengciList = Config::find()->select(['extra'])->where(['group' => 'policy', 'name' => 'cengci'])->one();
+        $cengciList = explode("\r\n",$cengciList['extra']);
+        $cengciList = array_combine($cengciList, $cengciList);
         return $this->render('index', [
             'dataProvider' => $dataProvider,
+            'searchModel' => $searchModel,
+            'cengciList' => $cengciList,
         ]);
     }
 

+ 72 - 0
server/backend/models/search/PolicySearch.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace backend\models\search;
+
+use common\models\Policy;
+use yii\base\Model;
+use yii\data\ActiveDataProvider;
+
+/**
+ * PolicySearch represents the model behind the search form about `common\models\Policy`.
+ */
+class PolicySearch extends Policy
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'integer'],
+            [['title', 'type', 'cengci', 'estate'], 'string'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function scenarios()
+    {
+        // bypass scenarios() implementation in the parent class
+        return Model::scenarios();
+    }
+
+    /**
+     * Creates data provider instance with search query applied.
+     *
+     * @param array $params
+     *
+     * @return ActiveDataProvider
+     */
+    public function search($params)
+    {
+        $query = Policy::find();
+
+        $dataProvider = new ActiveDataProvider([
+            'query' => $query,
+//            'sort' => [
+//                'defaultOrder' => [
+//                    'id' => SORT_ASC
+//                ]
+//            ]
+        ]);
+
+        $this->load($params);
+
+        if (!$this->validate()) {
+            // uncomment the following line if you do not want to return any records when validation fails
+            // $query->where('0=1');
+            return $dataProvider;
+        }
+        $query->andFilterWhere([
+            'id' => $this->id,
+            'estate' => $this->estate,
+        ]);
+
+        $query->andFilterWhere(['like', 'title', $this->title]);
+        $query->andFilterWhere(['like', 'type', $this->type]);
+        $query->andFilterWhere(['like', 'cengci', $this->cengci]);
+
+        return $dataProvider;
+    }
+}

+ 45 - 0
server/backend/views/policy/_search.php

@@ -0,0 +1,45 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/* @var $this yii\web\View */
+/* @var $model backend\models\search\PolicySearch */
+/* @var $cengciList [] */
+/* @var $form yii\widgets\ActiveForm */
+?>
+
+<div class="box box-success">
+    <div class="box-header" style="display:none;">
+        <h2 class="box-title">goods搜索</h2>
+        <div class="box-tools">
+            <button class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" data-original-title=""
+                    title=""><i class="fa fa-minus"></i></button>
+        </div>
+    </div>
+    <div class="box-body">
+
+        <?php $form = ActiveForm::begin([
+            'action' => ['index'],
+            'method' => 'get', 'options' => ['class' => 'form-inline'],
+        ]); ?>
+
+        <?= $form->field($model, 'id') ?>
+
+        <?= $form->field($model, 'title') ?>
+        <?= $form->field($model, 'type')->dropDownList(\common\models\Policy::getTypeList(), ['prompt' => '全部']) ?>
+        <?= $form->field($model, 'estate')->dropDownList(\common\enums\PolicyEnum::$list, ['prompt' => '全部']) ?>
+        <?= $form->field($model, 'cengci')->dropDownList($cengciList, ['prompt' => '全部']) ?>
+
+
+
+
+        <div class="form-group">
+            <?= Html::submitButton(Yii::t('common', 'Search'), ['class' => 'btn btn-success']) ?>
+            <?= Html::resetButton(Yii::t('common', 'Reset'), ['class' => 'btn btn-default']) ?>
+        </div>
+
+        <?php ActiveForm::end(); ?>
+
+    </div>
+</div>

+ 22 - 0
server/backend/views/policy/index.php

@@ -3,8 +3,10 @@
 use yii\grid\GridView;
 use yii\helpers\Html;
 
+/* @var $searchModel backend\models\search\PolicySearch */
 /* @var $this yii\web\View */
 /* @var $dataProvider yii\data\ActiveDataProvider */
+/* @var $cengciList [] */
 
 $this->title = '政策列表';
 $this->params['breadcrumbs'][] = $this->title;
@@ -12,6 +14,7 @@ $this->params['breadcrumbs'][] = $this->title;
 <?php $this->beginBlock('content-header'); ?>
 <?= $this->title . ' ' . Html::a(Yii::t('common', '新增政策'), ['create'], ['class' => 'btn btn-success']) ?>
 <?php $this->endBlock(); ?>
+<?php echo $this->render('_search', ['model' => $searchModel, 'cengciList' => $cengciList]); ?>
 <div class="box box-success">
     <div class="box-body">
     <?= GridView::widget([
@@ -19,6 +22,25 @@ $this->params['breadcrumbs'][] = $this->title;
         'columns' => [
             'id',
             'title',
+            [
+                'attribute' => 'type',
+                'value' => function ($model) {
+                    /* @var  $model \common\models\Policy */
+                    return $model->getTypeText();
+                },
+            ],
+            [
+                'attribute' => 'estate',
+                'value' => function ($model) {
+                    /* @var  $model \common\models\Policy */
+                    if ($model->estate) {
+                        return \common\enums\PolicyEnum::$list[$model->estate];
+                    } else {
+                        return '';
+                    }
+                },
+            ],
+            'cengci',
             'author',
             'created_at:datetime',
             ['class' => 'common\helpers\DiyActionColumn'],

+ 16 - 2
server/common/models/Policy.php

@@ -29,8 +29,8 @@ class Policy extends \yii\db\ActiveRecord
     public static function getTypeList()
     {
         return [
-            self::TYPE_SURVEY => '匹配(仅显示在匹配列表中)',
-            self::TYPE_SEARCH => '查询(仅供政策搜索查询)',
+            self::TYPE_SURVEY => '匹配(人才匹配)',
+            self::TYPE_SEARCH => '查询(政策查询)',
         ];
     }
 
@@ -96,6 +96,7 @@ class Policy extends \yii\db\ActiveRecord
             'title' => '标题',
             'author'=>'作者',
             'type'=>'类型',//类型:0 匹配,1 查询
+            'cengci'=>'层次',
             'estate'=>'产业',
             'created_at'=>'发布时间'
         ];
@@ -131,4 +132,17 @@ class Policy extends \yii\db\ActiveRecord
 
         return [$title, $author, $description, $model->keywords];
     }
+
+    public function getTypeText()
+    {
+        if (!empty($this->type)) {
+            $keys = explode(',', $this->type);
+            $list = self::getTypeList();
+            if (count($keys) == 2) {
+                return $list[$keys[0]] . ',' . $list[$keys[1]];
+            }
+            return $list[$keys[0]];
+        }
+        return '';
+    }
 }