TenderController.php
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?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'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['change-state'],
'allow' => true,
'roles' => [ '@' ],
],
],
],
];
}
/**
* 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->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' => 'Ошибка обновления.'];
}
}
}