Your IP : 216.73.216.79


Current Path : /var/www/html/project/models/
Upload File :
Current File : /var/www/html/project/models/TasksSearch.php

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Tasks;
use yii\db\Expression;

/**
 * TasksSearch represents the model behind the search form of `app\models\Tasks`.
 */
class TasksSearch extends Tasks {

    public $project_name;
    public $search_startdt;
    public $search_enddt;
    public $user_name;
    public $search_keyword;
    public $search_default;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'project_id', 'parent_id', 'data_id', 'task_no', 'sort', 'assign_to', 'duration', 'deleted_status', 'created_by', 'updated_by'], 'integer'],
            [['task_type', 'task_full_no', 'task_name', 'start_dt', 'end_dt', 'track_type', 'freq', 'task_status',
            'created_dt', 'updated_dt', 'project_name', 'search_startdt', 'search_enddt', 'user_name', 'search_keyword', 'search_default'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios() {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    public function attributeLabels() {
        return [
            'user_name' => 'Assign To',
            'search_keyword' => 'Keyword'
        ];
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params) {
        $query = Tasks::find()->select(['t1.*', 'projects.name AS project_name'])->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id');

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort([
            'defaultOrder' => ['start_dt' => SORT_ASC],
            'attributes' => [
                'project_name',
                'task_name',
                'start_dt',
                'end_dt',
                'task_status'
            ],
        ]);

        $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;
        }

        // grid filtering conditions
        $query->andWhere(['=', 't1.deleted_status', '0']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);
        $query->andWhere(new Expression('FIND_IN_SET(:user_to_find, t1.assign_to)'))->addParams([':user_to_find' => Yii::$app->user->identity->id]);
        $query->andFilterWhere(['IN', 't1.task_status', $this->task_status]);

        if (!empty($this->search_keyword)) {
            $query->andFilterWhere(['OR',
                ['LIKE', 't1.task_name', $this->search_keyword],
                ['LIKE', 't2.task_name', $this->search_keyword],
                ['LIKE', 'projects.name', $this->search_keyword]
            ]);
        }

        if (!empty($this->search_startdt) && !empty($this->search_enddt))
            $query->andFilterWhere(['BETWEEN', 'DATE(t1.end_dt)', $this->search_startdt, $this->search_enddt]);

        if (!empty($this->search_default))
            $query->groupBy(['t1.project_id', 't1.task_type']);

        return $dataProvider;
    }

    public function searchdashboard($params, $type) {
        $query = Tasks::find()->select(['t1.*', 'projects.name AS project_name', 't3.name AS user_name'])->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->leftJoin('tasks t2', 't1.parent_id=t2.id')
                ->innerJoin('user t3', 't1.assign_to=t3.id');
        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort([
            'defaultOrder' => ['end_dt' => SORT_ASC],
            'attributes' => [
                'project_name',
                'task_name',
                'start_dt',
                'end_dt',
                'task_status',
                'user_name'
            ],
        ]);
        $this->load($params);

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

        $query->andWhere(['=', 't1.deleted_status', '0']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        //$query->andWhere(['=', 'projects.status', '1']);
        $query->andWhere(['!=', 't1.task_type', 'phase']);
        $query->andWhere(['OR', ['=', 't1.track_type', 'project'], ['t1.track_type' => NULL]]);
        $query->andFilterWhere(['IN', 't1.task_status', $this->task_status]);

        if (!empty($this->search_keyword)) {
            $query->andFilterWhere(['OR',
                ['LIKE', 't1.task_name', $this->search_keyword],
                ['LIKE', 't2.task_name', $this->search_keyword],
                ['LIKE', 'projects.name', $this->search_keyword],
                    //['LIKE', 't3.name', $this->search_keyword]
            ]);
        }
        $query->andFilterWhere(['=', 't1.assign_to', $this->assign_to]);
        if (!empty($this->search_startdt) && !empty($this->search_enddt))
            $query->andFilterWhere(['BETWEEN', 'DATE(t1.end_dt)', $this->search_startdt, $this->search_enddt]);

        return $dataProvider;
    }

    public function searchadhoc($params) {
        $query = Tasks::find()->select(['t1.*', 'projects.name AS project_name', 't3.name AS user_name'])->from('tasks t1')
                ->leftJoin('projects', 't1.project_id=projects.id')
                ->innerJoin('user t3', 't1.assign_to=t3.id');

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort([
            'defaultOrder' => ['end_dt' => SORT_ASC],
            'attributes' => [
                'project_name',
                'task_name',
                'start_dt',
                'end_dt',
                'task_status',
                'user_name',
                'duration'
            ],
        ]);

        $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;
        }

        // grid filtering conditions
        $query->andWhere(['=', 't1.deleted_status', '0']);
        $query->andWhere(['OR', ['=', 'projects.status', '1'], ['IS', 'projects.status', NULL]]);
        $query->andWhere(['=', 't1.task_type', 'adhoc']);

        if (!empty($this->search_keyword)) {
            $query->andFilterWhere(['OR',
                ['LIKE', 't1.task_name', $this->search_keyword],
                ['LIKE', 'projects.name', $this->search_keyword],
                ['LIKE', 't3.name', $this->search_keyword]
            ]);
        }

        if (!empty($this->search_startdt) && !empty($this->search_enddt))
            $query->andFilterWhere(['BETWEEN', 'DATE(t1.end_dt)', $this->search_startdt, $this->search_enddt]);

        return $dataProvider;
    }

}