<?php

namespace common\modules\support\models\redmine;

use Yii;

use common\models\Settings;

/**
 * This is the ActiveQuery class for [[Issue]].
 *
 * @see Issue
 */
class IssueQuery extends \yii\db\ActiveQuery
{
    public $orderBy = ['created_on' => SORT_DESC];

    public function search($s)
    {
        if($s && trim($s) != '')
        {
            return $this->andWhere(['like', 'subject', $s]);
        }

        return $this;
    }

    public function me()
    {
        return $this->andWhere(['author_id' => Yii::$app->support->identity->support_id]);
    }

    public function work()
    {
        $status = Settings::getValue('support-status-work');
        return $this->andWhere(['status_id' => $status]);
    }

    public function rating()
    {
        $status = Settings::getValue('support-status-new');
        return $this->andWhere(['status_id' => $status]);
    }

    public function test()
    {
        $status = Settings::getValue('support-status-test');
        return $this->andWhere(['status_id' => $status]);
    }

    public function approve()
    {
        $status = Settings::getValue('support-status-approve');
        return $this->andWhere(['status_id' => $status]);
    }

    public function accepted()
    {
        $status = Settings::getValue('support-status-accepted');
        return $this->andWhere(['status_id' => $status]);
    }

    /**
     * @inheritdoc
     * @return Issue[]|array
     */
    public function all($db = null)
    {
        return parent::all($db);
    }

    /**
     * @inheritdoc
     * @return Issue|array|null
     */
    public function one($db = null)
    {
        return parent::one($db);
    }
}
