1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace toir427\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use toir427\admin\models\Assignment; |
7
|
|
|
use toir427\admin\models\searchs\Assignment as AssignmentSearch; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* AssignmentController implements the CRUD actions for Assignment model. |
14
|
|
|
* |
15
|
|
|
* @author Misbahul D Munir <[email protected]> |
16
|
|
|
* @since 1.0 |
17
|
|
|
*/ |
18
|
|
|
class AssignmentController extends Controller |
19
|
|
|
{ |
20
|
|
|
public $userClassName; |
21
|
|
|
public $idField = 'id'; |
22
|
|
|
public $usernameField = 'username'; |
23
|
|
|
public $fullnameField; |
24
|
|
|
public $searchClass; |
25
|
|
|
public $extraColumns = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function init() |
31
|
|
|
{ |
32
|
|
|
parent::init(); |
33
|
|
|
if ($this->userClassName === null) { |
34
|
|
|
$this->userClassName = Yii::$app->getUser()->identityClass; |
35
|
|
|
$this->userClassName = $this->userClassName ? : 'toir427\admin\models\User'; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function behaviors() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
'verbs' => [ |
46
|
|
|
'class' => VerbFilter::className(), |
47
|
|
|
'actions' => [ |
48
|
|
|
'assign' => ['post'], |
49
|
|
|
'assign' => ['post'], |
50
|
|
|
'revoke' => ['post'], |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Lists all Assignment models. |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function actionIndex() |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
if ($this->searchClass === null) { |
64
|
|
|
$searchModel = new AssignmentSearch; |
65
|
|
|
$dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams(), $this->userClassName, $this->usernameField); |
66
|
|
|
} else { |
67
|
|
|
$class = $this->searchClass; |
68
|
|
|
$searchModel = new $class; |
69
|
|
|
$dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('index', [ |
73
|
|
|
'dataProvider' => $dataProvider, |
74
|
|
|
'searchModel' => $searchModel, |
75
|
|
|
'idField' => $this->idField, |
76
|
|
|
'usernameField' => $this->usernameField, |
77
|
|
|
'extraColumns' => $this->extraColumns, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Displays a single Assignment model. |
83
|
|
|
* @param integer $id |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function actionView($id) |
87
|
|
|
{ |
88
|
|
|
$model = $this->findModel($id); |
89
|
|
|
|
90
|
|
|
return $this->render('view', [ |
91
|
|
|
'model' => $model, |
92
|
|
|
'idField' => $this->idField, |
93
|
|
|
'usernameField' => $this->usernameField, |
94
|
|
|
'fullnameField' => $this->fullnameField, |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Assign items |
100
|
|
|
* @param string $id |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function actionAssign($id) |
104
|
|
|
{ |
105
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
106
|
|
|
$model = new Assignment($id); |
107
|
|
|
$success = $model->assign($items); |
108
|
|
|
Yii::$app->getResponse()->format = 'json'; |
109
|
|
|
return array_merge($model->getItems(), ['success' => $success]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Assign items |
114
|
|
|
* @param string $id |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function actionRevoke($id) |
118
|
|
|
{ |
119
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
120
|
|
|
$model = new Assignment($id); |
121
|
|
|
$success = $model->revoke($items); |
122
|
|
|
Yii::$app->getResponse()->format = 'json'; |
123
|
|
|
return array_merge($model->getItems(), ['success' => $success]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Finds the Assignment model based on its primary key value. |
128
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
129
|
|
|
* @param integer $id |
130
|
|
|
* @return Assignment the loaded model |
131
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
132
|
|
|
*/ |
133
|
|
|
protected function findModel($id) |
134
|
|
|
{ |
135
|
|
|
$class = $this->userClassName; |
136
|
|
|
if (($user = $class::findIdentity($id)) !== null) { |
137
|
|
|
return new Assignment($id, $user); |
138
|
|
|
} else { |
139
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|