[ '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' ], 'offer-form' => [ 'post' ], 'offer' => [ 'post' ], ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => [ 'change-state' ], 'allow' => true, 'roles' => [ '@' ], ], ], ], ]; } /** * Redirect to search page. * * @return string */ public function actionIndex() { return $this->redirect([ 'search/project' ]); } public function actionView($tender_id) { /** * @var Project $model */ $model = Project::findOne($tender_id); if(!empty($model)) { $model->updateCounters(['view_count' => 1]); } else { throw new NotFoundHttpException(Yii::t('app', 'project_not_found')); } 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($project->user_id != $user->id && $comment->user_id != $user->id) { return ['error' => 'Вы можете менять статус только своих предложений, либо предложений по своим заказам']; } if($comment->user_id == $user->id) { $comment->scenario = $comment::SCENARIO_OWNER; } else { $comment->scenario = $comment::SCENARIO_STATE; } if($comment->state == $comment::STATE_TRASH && $comment->scenario == $comment::SCENARIO_STATE) { return [ 'error' => 'Исполнитель отменил данное предложение' ]; } if($comment->state == $comment::STATE_DENY && $comment->scenario == $comment::SCENARIO_OWNER) { return [ 'error' => 'Заказчик отменил Ваше предложение' ]; } $comment->state = $state; if(!$comment->validate()) { return [ 'error' => 'Недопустимое значение state' ]; } if($comment->changeState()) { return [ 'message' => 'Автор оповещен о вашем решении' ]; } else { return [ 'error' => 'Ошибка обновления.' ]; } } public function actionOfferForm() { /** * @var User $user */ $response = \Yii::$app->response; $request = \Yii::$app->request; $response->format = $response::FORMAT_JSON; $user = \Yii::$app->user->identity; $performer_id = $request->post('performer_id'); if(empty( $performer_id )) { return [ 'error' => 'performer_id должен быть задан' ]; } $projects = $user->getProjects() ->with('parent') ->all(); if(empty( $projects )) { return [ 'error' => 'У Вас еще нету заказов' ]; } return [ 'message' => $this->renderAjax('forms-modal-offer', [ 'performer_id' => $performer_id, 'projects' => $projects, ]), ]; } public function actionOffer() { /** * @var User $user * @var Project $project * @var User $performer * @var Chat $chat */ $response = \Yii::$app->response; $request = \Yii::$app->request; $response->format = $response::FORMAT_JSON; $user = \Yii::$app->user->identity; $project_id = $request->post('project_id'); $performer_id = $request->post('performer_id'); if(empty( $project_id ) || empty( $performer_id )) { return [ 'error' => 'project_id и performer_id должны быть заданы' ]; } $performer = User::find() ->where([ 'id' => $performer_id ]) ->one(); if(empty( $performer )) { return [ 'error' => 'Исполнитель не найден' ]; } $project = Project::find() ->where([ 'project_id' => $project_id ]) ->one(); if(empty( $project )) { return [ 'error' => 'Заказ не найден' ]; } elseif($project->user_id != $user->id) { return [ 'error' => 'Вы можете предлагать только собственные заказы' ]; } $chat = $user->getChat($performer->id) ->one(); if(empty( $chat )) { $chat = new Chat([ 'form_user' => $user->id, 'to_user' => $performer->id, ]); if(!$chat->save()) { return [ 'error' => 'Не удалось создать чат' ]; } } $message = new Message([ 'chat_id' => $chat->chat_id, 'user_id' => $user->id, 'status' => 1, ]); $text = "
Здравствуйте, {$performer->name}!
Предлагаю Вам принять участие в " . Html::a('заказе', [ 'tender/view', 'tender_id' => $project->project_id, ]) . "
"; $message->text = $text; if(!$message->save()) { return [ 'error' => 'Не удалось отправить предложение' ]; } else { return [ 'message' => 'Предложение успешно отправлено' ]; } } }