TenderController.php 3.68 KB
<?php
namespace frontend\controllers;

use common\models\Project;
use common\modules\comment\models\CommentProject;
use Yii;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use frontend\models\Options;
use frontend\models\OptionValues;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use frontend\models\OptionsToValues;
use yii\validators\EmailValidator;
use common\models\User;
use yii\helpers\VarDumper;
use common\models\Page; 
use frontend\models\Option;
use common\models\Social;


/**
 * Site controller
 */
class TenderController extends Controller
{

    public $enableCsrfValidation = false;
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'change-state' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->redirect(['search/project']);
    }

    public function actionView($tender_id)
    {
        $model = Project::findOne($tender_id);

        return $this->render('view',[
            'model' => $model
        ]);
    }

    public function actionChangeState()
    {
        /**
         * @var User $user
         */
        $response = \Yii::$app->response;
        $response->format = $response::FORMAT_JSON;
        $user = \Yii::$app->user->identity;
        $project_id = \Yii::$app->request->post('project_id');
        $comment_id = \Yii::$app->request->post('comment_id');
        $state = \Yii::$app->request->post('state');
        if(empty($project_id) || empty($comment_id) || empty($state)) {
            return ['error' => 'project_id, comment_id, state должны быть отправлены в запросе'];
        }
        /**
         * @var Project $project
         */
        $project = Project::findOne($project_id);
        if(empty($project)) {
            return ['error' => 'Проект не найден'];
        } elseif($project->user_id != $user->id) {
            return ['error' => 'Вы можете менять статус только собственных проектов'];
        }
        /**
         * @var CommentProject $comment
         */
        $comment = CommentProject::find()->where(['comment_id' => $comment_id, 'model' => $project->className(), 'model_id' => $project->project_id])->one();
        if(empty($comment)) {
            return ['error' => 'Данного предложения не существует'];
        }
        if($comment->state == $comment::STATE_TRASH) {
            return ['error' => 'Исполнитель отменил данное предложение'];
        }
        $comment->scenario = $comment::SCENARIO_STATE;
        $comment->state = $state;
        if(!$comment->validate()) {
            return ['error' => 'Недопустимое значение state'];
        }
        if($comment->changeState()) {
            return ['message' => 'Автор оповещен о вашем решении'];
        } else {
            return ['error' => 'Ошибка обновления.'];
        }
    }

}