AjaxController.php
5.4 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
139
140
141
142
143
144
145
146
147
148
<?php
namespace frontend\controllers;
use common\models\Portfolio;
use common\models\PortfolioUser;
use common\models\User;
use common\models\UserSearch;
use yii\db\ActiveRecord;
use yii\web\Controller;
/**
* Site controller
*/
class AjaxController extends Controller
{
public $enableCsrfValidation = false;
public function behaviors()
{
return [
];
}
public function actionProjectUser()
{
$ids = json_decode(\Yii::$app->request->get('ids'));
$model = new UserSearch();
$dataProvider = $model->search(\Yii::$app->request->queryParams);
$dataProvider->query->andFilterWhere([
'not in',
'id',
$ids,
]);
$dataProvider->query->andWhere([
'user_info.is_freelancer' => 1,
'type' => 1,
]);
$dataProvider->query->with('specializations');
return $this->renderAjax('users', [
'model' => $model,
'dataProvider' => $dataProvider,
]);
}
public function actionUserInput()
{
/**
* @var ActiveRecord $model
*/
$request = \Yii::$app->request;
$response = \Yii::$app->response;
$response->format = $response::FORMAT_JSON;
$user_id = $request->get('id');
$model = $request->get('model');
$component = $request->get('component');
if(empty( $user_id )) {
return [ 'error' => 'User ID must be set' ];
} elseif(empty( $model )) {
return [ 'error' => 'Model must be set' ];
}
$user = User::find()
->where([
'id' => $user_id,
'type' => 1,
'user_info.is_freelancer' => 1,
])
->joinWith('userInfo')
->one();
if(empty( $user )) {
return [ 'error' => 'User not found' ];
}
if(!class_exists($model)) {
return [ 'error' => 'Class not found' ];
}
$model = new $model();
if(!$model instanceof ActiveRecord) {
return [ 'error' => 'Class must extend ActiveRecord' ];
}
if(!$model->hasAttribute('user_id')) {
return [ 'error' => 'Class must have user_id attribute' ];
}
$html = $this->renderPartial('project_user', [
'model' => $model,
'user' => $user,
]);
return [
'result' => [
'html' => $html,
'id' => $user->id,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'component' => $component,
],
];
}
public function actionPortfolioUserRemove()
{
$request = \Yii::$app->request;
$response = \Yii::$app->response;
$response->format = $response::FORMAT_JSON;
$post = $request->post();
if(empty( $post[ 'user_id' ] ) || empty( $post[ 'portfolio_id' ] )) {
return [ 'error' => 'user_id и portfolio_id должны быть заполнены.' ];
}
$user_id = $post[ 'user_id' ];
$portfolio_id = $post[ 'portfolio_id' ];
$portfolio = Portfolio::find()
->where([
'portfolio_id' => $portfolio_id,
'user_id' => \Yii::$app->user->id,
])
->one();
if(empty( $portfolio )) {
return [ 'error' => 'Запись не найдена' ];
}
$user = User::find()
->where([ 'id' => $user_id ])
->one();
if(empty( $user )) {
return [ 'error' => 'Пользователь не найден' ];
}
$portfolio_user = PortfolioUser::find()
->where([
'portfolio_id' => $portfolio->portfolio_id,
'user_id' => $user->id,
'status' => 1,
])
->one();
if(empty( $portfolio_user )) {
return [ 'error' => 'Пользователь не относится к данной записи' ];
}
if($portfolio_user->delete()) {
return [
'result' => [
'message' => 'Пользователь удален',
'user_id' => $user->id,
'portfolio_id' => $portfolio->portfolio_id,
],
];
} else {
return [ 'error' => 'Ошибка удаления' ];
}
}
}