<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Quote;

/**
 * QuoteSearch represents the model behind the search form about `backend\models\Quote`.
 */
class QuoteSearch extends Quote
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'quoteIndex', 'chapterId'], 'integer'],
            [['content'], '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)
    {
        $query = Quote::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['quoteIndex'=>SORT_ASC]]
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

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

        $query->andFilterWhere(['like', 'content', $this->content]);
            //->andFilterWhere(['like', 'image', $this->image]);
           // ->andFilterWhere(['like', 'type', $this->type]);

        return $dataProvider;
    }
}
