eb7e82fb
Administrator
29.02.16
|
1
2
3
|
<?php
namespace frontend\controllers;
|
5077a0ec
Yarik
test
|
4
5
|
use common\models\Bookmark;
use common\models\User;
|
eb7e82fb
Administrator
29.02.16
|
6
|
use yii\filters\AccessControl;
|
5077a0ec
Yarik
test
|
7
|
use yii\filters\VerbFilter;
|
eb7e82fb
Administrator
29.02.16
|
8
|
use yii\web\Controller;
|
5077a0ec
Yarik
test
|
9
|
use yii\web\Response;
|
eb7e82fb
Administrator
29.02.16
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* Site controller
*/
class BookmarksController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
|
eb7e82fb
Administrator
29.02.16
|
24
25
26
27
28
|
'allow' => true,
'roles' => [ '@' ],
],
],
],
|
5077a0ec
Yarik
test
|
29
30
31
32
33
34
35
36
37
38
39
40
41
|
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'add-performer' => [ 'post' ],
'add-customer' => [ 'post' ],
'add-project' => [ 'post' ],
'add-vacancy' => [ 'post' ],
'remove-performer' => [ 'post' ],
'remove-customer' => [ 'post' ],
'remove-project' => [ 'post' ],
'remove-vacancy' => [ 'post' ],
],
],
|
eb7e82fb
Administrator
29.02.16
|
42
43
44
45
46
47
48
49
|
];
}
public function actionIndex()
{
return $this->render('bookmarks');
}
|
5077a0ec
Yarik
test
|
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
|
public function actionAddPerformer()
{
$response = \Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$model_id = \Yii::$app->request->post('id');
if(empty( $model_id )) {
return [ 'error' => 'Ошибка добавления. Отсутствует id исполнителя' ];
}
$user = User::findOne($model_id);
if(empty( $user )) {
return [ 'error' => 'Ошибка добавления. Исполнитель не найден.' ];
}
$model = $user->className();
$user_id = \Yii::$app->user->getId();
$type = Bookmark::TYPE_PERFORMER;
if(Bookmark::findOne([
'model' => $model,
'model_id' => $model_id,
'user_id' => $user_id,
'type' => $type,
])
) {
return [ 'error' => 'Ошибка добавления. Исполнитель уже добавлен.' ];
}
$bookmark = new Bookmark([
'model' => $model,
'model_id' => $model_id,
'user_id' => $user_id,
'type' => $type,
]);
if($bookmark->save()) {
$result = $this->renderAjax('//site/forms-modal-fav', [
'message' => 'Добавлено в закладки.',
'image' => $user->userInfo->image,
'name' => $user->name,
]);
return [ 'result' => $result ];
} else {
return [ 'error' => 'Ошибка добавления.' ];
}
}
public function actionAddCustomer($id)
{
}
public function actionAddProject($id)
{
}
public function actionAddVacancy($id)
{
}
public function actionRemovePerformer()
{
$response = \Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$model_id = \Yii::$app->request->post('id');
if(empty( $model_id )) {
return [ 'error' => 'Ошибка удаления. Отсутствует id исполнителя' ];
}
$user = User::findOne($model_id);
if(empty( $user )) {
return [ 'error' => 'Ошибка добавления. Исполнитель не найден.' ];
}
$model = $user->className();
$user_id = \Yii::$app->user->getId();
$type = Bookmark::TYPE_PERFORMER;
/**
* @var Bookmark $bookmark
*/
$bookmark = Bookmark::findOne([
'model' => $model,
'model_id' => $model_id,
'user_id' => $user_id,
'type' => $type,
]);
if(!empty($bookmark)) {
if($bookmark->delete()) {
return ['message' => 'Закладка успешно удалена'];
} else {
return ['error' => 'Ошибка при удалении'];
}
} else {
return ['error' => 'Закладка не найдена'];
}
}
|
eb7e82fb
Administrator
29.02.16
|
142
|
}
|