From 50ed5626f27742c7a9e749a8674d21004bd82c1e Mon Sep 17 00:00:00 2001 From: Yarik Date: Mon, 21 Mar 2016 14:30:52 +0200 Subject: [PATCH] test --- common/models/Project.php | 1 + frontend/controllers/TenderController.php | 331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------- frontend/views/layouts/company.php | 2 +- frontend/views/layouts/performer.php | 12 +++++++++--- frontend/views/tender/forms-modal-offer.php | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/web/js/forms.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------- 6 files changed, 408 insertions(+), 157 deletions(-) create mode 100644 frontend/views/tender/forms-modal-offer.php 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 @@
params[ 'company' ]->comments) ?> отзывов
- написать отзыв + $this->params['company']->id], ['class' => 'performance-vacancy-sidebar-write style']) ?>
params[ 'company' ]->userInfo->image); ?>
diff --git a/frontend/views/layouts/performer.php b/frontend/views/layouts/performer.php index f76c332..068e6bd 100755 --- a/frontend/views/layouts/performer.php +++ b/frontend/views/layouts/performer.php @@ -20,8 +20,11 @@
- Предложить
проект
- 'blog-buttons-offer', + 'data-performer-id' => $this->params[ 'user' ]->id, + ]) ?> + $this->params[ 'user' ]->id, ]), [ 'class' => 'blog-buttons-write' ]) ?> @@ -132,7 +135,10 @@
params[ 'user' ]->comments) ?> отзывов
- написать отзыв + $this->params[ 'user' ]->id, + ], [ 'class' => 'performance-vacancy-sidebar-write style' ]) ?>
params[ 'user' ]->userInfo->image); ?> diff --git a/frontend/views/tender/forms-modal-offer.php b/frontend/views/tender/forms-modal-offer.php new file mode 100644 index 0000000..56306b9 --- /dev/null +++ b/frontend/views/tender/forms-modal-offer.php @@ -0,0 +1,135 @@ + + + + diff --git a/frontend/web/js/forms.js b/frontend/web/js/forms.js index c3a81a1..b59089e 100755 --- a/frontend/web/js/forms.js +++ b/frontend/web/js/forms.js @@ -69,38 +69,6 @@ $(document).ready( ) } - function addToOffer() - { - $.post( - "/site/forms-modal-offer", function(data) - { - $('.forms-modal-hide').append(data) - } - ) - .fail( - function() - { - $('#overlay, .forms-modal-hide').remove(); - alert(txtErrorModal) - } - ) - .done( - function() - { - var newMarg = $(window).scrollTop(); - $('#overlay').fadeIn( - 400, function() - { - $('body').css({overflowY : 'hidden'}) - $('.forms-modal-hide>div') - .css({display : 'block', marginTop : -230 + newMarg}) - .animate({opacity : 1, top : '50%'}, 200); - } - ); - } - ) - } - function addContacts() { $.post( @@ -483,12 +451,58 @@ $(document).ready( //offer + function addToOffer(performer_id) + { + $.post( + "/tender/offer-form", + { + performer_id: performer_id + } , + function(data) + { + if(data.error) { + $('#overlay, .forms-modal-hide').remove(); + alert(data.error); + } else { + $('.forms-modal-hide').append(data.message); + } + } + ) + .fail( + function() + { + $('#overlay, .forms-modal-hide').remove(); + alert(txtErrorModal) + } + ) + .done( + function() + { + var newMarg = $(window).scrollTop(); + $('#overlay').fadeIn( + 400, function() + { + $('body').css({overflowY : 'hidden'}) + $('.forms-modal-hide>div') + .css({display : 'block', marginTop : -230 + newMarg}) + .animate({opacity : 1, top : '50%'}, 200); + } + ); + } + ) + } $('a.get-project, .blog-buttons-offer').click( function(e) { - e.preventDefault() - addRemoveBlocks() - addToOffer() + e.preventDefault(); + var performer_id; + performer_id = $(this).data('performer-id'); + if(!performer_id) { + return false; + } else { + addRemoveBlocks(); + addToOffer(performer_id); + } } ) -- libgit2 0.21.4