diff --git a/common/models/Project.php b/common/models/Project.php index 672a1d0..0c1f454 100644 --- a/common/models/Project.php +++ b/common/models/Project.php @@ -31,6 +31,7 @@ * @property ProjectSpecialization[] $projectSpecializations * @property Specialization[] $specializations * @property Currency $budgetCurrency + * @property Project $parent */ class Project extends \yii\db\ActiveRecord { diff --git a/frontend/controllers/TenderController.php b/frontend/controllers/TenderController.php index e99d785..47bab2d 100755 --- a/frontend/controllers/TenderController.php +++ b/frontend/controllers/TenderController.php @@ -1,138 +1,233 @@ [ - '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'], - ], - ], - 'access' => [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'actions' => ['change-state'], - 'allow' => true, - 'roles' => [ '@' ], - ], - ], - ], - ]; - } /** - * Displays homepage. - * - * @return mixed + * Site controller */ - public function actionIndex() - { - return $this->redirect(['search/project']); - } - - public function actionView($tender_id) + class TenderController extends Controller { - $model = Project::findOne($tender_id); - return $this->render('view',[ - 'model' => $model - ]); - } + public $enableCsrfValidation = false; - 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 + * @inheritdoc */ - $project = Project::findOne($project_id); - if(empty($project)) { - return ['error' => 'Проект не найден']; - } elseif($project->user_id != $user->id) { - return ['error' => 'Вы можете менять статус только собственных проектов']; + 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' ], + ], + ], + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'actions' => [ 'change-state' ], + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + ]; } + /** - * @var CommentProject $comment + * Displays homepage. + * @return mixed */ - $comment = CommentProject::find()->where(['comment_id' => $comment_id, 'model' => $project->className(), 'model_id' => $project->project_id])->one(); - if(empty($comment)) { - return ['error' => 'Данного предложения не существует']; + public function actionIndex() + { + return $this->redirect([ 'search/project' ]); } - if($comment->user_id == \Yii::$app->user->getId()) { - $comment->scenario = $comment::SCENARIO_OWNER; - } else { - $comment->scenario = $comment::SCENARIO_STATE; - } - if($comment->state == $comment::STATE_TRASH && $comment->scenario == $comment::SCENARIO_STATE) { - return ['error' => 'Исполнитель отменил данное предложение']; + + public function actionView($tender_id) + { + $model = Project::findOne($tender_id); + + return $this->render('view', [ + 'model' => $model, + ]); } - if($comment->state == $comment::STATE_DENY && $comment->scenario == $comment::SCENARIO_OWNER) { - return ['error' => 'Заказчик отменил Ваше предложение']; + + 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->user_id == \Yii::$app->user->getId()) { + $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' => 'Ошибка обновления.' ]; + } } - $comment->state = $state; - if(!$comment->validate()) { - return ['error' => 'Недопустимое значение state']; + 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, + ]), + ]; } - if($comment->changeState()) { - return ['message' => 'Автор оповещен о вашем решении']; - } else { - return ['error' => 'Ошибка обновления.']; + + 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 = "
Здравствуйте, {$user->name}!
Предлагаю Вам принять участие в " . Html::a('Тендере', [ + 'tender/view', + 'tender_id' => $project->project_id, + ]) . "
"; + $message->text = $text; + if(!$message->save()) { + return [ 'error' => 'Не удалось отправить предложение' ]; + } else { + return [ 'message' => 'Предложение успешно отправлено' ]; + } } - } -} + } diff --git a/frontend/views/layouts/company.php b/frontend/views/layouts/company.php index 3e771cb..d7107fe 100755 --- a/frontend/views/layouts/company.php +++ b/frontend/views/layouts/company.php @@ -153,7 +153,7 @@ - написать отзыв + = Html::a('написать отзыв', ['company/review', 'company_id' => $this->params['company']->id], ['class' => 'performance-vacancy-sidebar-write style']) ?>