<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 18.04.16
 * Time: 16:59
 */

namespace common\modules\analyticsSchool\models;


use yii\base\Model;
use yii\data\ActiveDataProvider;

class CoursesSearch extends AnalyticsSchoolCourse {
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['description', 'name', 'image'], 'safe'],
        ];
    }

    /**
     * @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)
    {
        if (count($params)>0){
            $params = array_filter($params);
            $query = AnalyticsSchoolCourse::findByCondition($params);
        }
        else {
            $query = AnalyticsSchoolCourse::find();
        }

        $this->load($params);

        $query->andFilterWhere([
            'id' => $this->id
        ]);

        $query
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'image', $this->image]);
        $query->orderBy(['id' => SORT_DESC]);

        return new ActiveDataProvider([
            'query' => $query,
        ]);
    }
}