|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace cornernote\dashboard\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use cornernote\dashboard\models\Dashboard; |
|
6
|
|
|
use cornernote\dashboard\models\search\DashboardSearch; |
|
7
|
|
|
use cornernote\dashboard\Module; |
|
8
|
|
|
use yii\filters\AccessControl; |
|
9
|
|
|
use yii\web\Controller; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\web\HttpException; |
|
12
|
|
|
use cornernote\dashboard\components\DashboardAccess; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* DashboardController implements the CRUD actions for Dashboard model. |
|
16
|
|
|
*/ |
|
17
|
|
|
class DashboardController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
public function init() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::init(); |
|
23
|
|
|
$this->viewPath = Module::getInstance()->viewPath; |
|
24
|
|
|
|
|
25
|
|
|
// custom initialization code goes here |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function behaviors() |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$updateRoles = Module::getInstance()->updateRoles) { |
|
34
|
|
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
return [ |
|
37
|
|
|
'access' => [ |
|
38
|
|
|
'class' => AccessControl::className(), |
|
39
|
|
|
'rules' => [ |
|
40
|
|
|
[ |
|
41
|
|
|
'allow' => true, |
|
42
|
|
|
'actions' => ['view'], |
|
43
|
|
|
'roles' => ['@'] |
|
44
|
|
|
], |
|
45
|
|
|
[ |
|
46
|
|
|
'allow' => true, |
|
47
|
|
|
'actions' => ['index', 'create', 'update', 'delete'], |
|
48
|
|
|
'roles' => $updateRoles |
|
49
|
|
|
] |
|
50
|
|
|
] |
|
51
|
|
|
] |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Lists all Dashboard models. |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function actionIndex() |
|
60
|
|
|
{ |
|
61
|
|
|
$searchModel = new DashboardSearch; |
|
62
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->get()); |
|
63
|
|
|
|
|
64
|
|
|
return $this->render('index', [ |
|
65
|
|
|
'dataProvider' => $dataProvider, |
|
66
|
|
|
'searchModel' => $searchModel, |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Displays a single Dashboard model. |
|
72
|
|
|
* @param string $id |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function actionView($id) |
|
77
|
|
|
{ |
|
78
|
|
|
if(!$model = $this->findModel($id)){ |
|
79
|
|
|
return $this->render('empty'); |
|
80
|
|
|
} |
|
81
|
|
|
return $this->render('view', compact('model')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Creates a new Dashboard model. |
|
86
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function actionCreate() |
|
90
|
|
|
{ |
|
91
|
|
|
$model = new Dashboard; |
|
92
|
|
|
//$model->scenario = 'create'; |
|
93
|
|
|
|
|
94
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
95
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been created.')); |
|
|
|
|
|
|
96
|
|
|
return $this->redirect(['update', 'id' => $model->id]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!\Yii::$app->request->isPost) { |
|
100
|
|
|
$model->load(Yii::$app->request->get()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->render('create', compact('model')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Updates an existing Dashboard model. |
|
108
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
|
109
|
|
|
* @param string $id |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function actionUpdate($id) |
|
113
|
|
|
{ |
|
114
|
|
|
$model = $this->findModel($id); |
|
115
|
|
|
//$model->scenario = 'update'; |
|
116
|
|
|
|
|
117
|
|
|
$data = Yii::$app->request->post(); |
|
118
|
|
|
if ($data && $model->layout->load($data) && $model->layout->validate()) { |
|
119
|
|
|
$model->options = $model->layout->getOptions(); |
|
120
|
|
|
if ($model->save(false)) { |
|
|
|
|
|
|
121
|
|
|
$model->sortPanels($data['DashboardPanelSort']); |
|
|
|
|
|
|
122
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been updated.')); |
|
|
|
|
|
|
123
|
|
|
return $this->redirect(['dashboard/view', 'id' => $model->id]); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $this->render('update', compact('model')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Deletes an existing Dashboard model. |
|
134
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
|
135
|
|
|
* @param string $id |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function actionDelete($id) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->findModel($id)->delete(); |
|
|
|
|
|
|
141
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been deleted.')); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
return $this->redirect(['index']); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* |
|
148
|
|
|
*/ |
|
149
|
|
|
public function actionSort() |
|
150
|
|
|
{ |
|
151
|
|
|
$sort = Yii::$app->request->post('sort'); |
|
152
|
|
|
if (!empty($sort)) { |
|
153
|
|
|
foreach ($sort as $k => $dashboardId) { |
|
154
|
|
|
$dashboardId = str_replace('dashboard-', '', $dashboardId); |
|
155
|
|
|
$dashboard = Dashboard::findOne($dashboardId); |
|
156
|
|
|
if ($dashboard) { |
|
157
|
|
|
$dashboard->sort = $k; |
|
158
|
|
|
$dashboard->save(false); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Finds the Dashboard model based on its primary key value. |
|
166
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
|
167
|
|
|
* If defined for dashboard allowRoles, check user access |
|
168
|
|
|
* @param string $id |
|
169
|
|
|
* @return Dashboard|false the loaded model |
|
170
|
|
|
* @throws HttpException if the model cannot be found |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function findModel($id) |
|
173
|
|
|
{ |
|
174
|
|
|
if (($model = Dashboard::findOne($id)) !== null) { |
|
175
|
|
|
|
|
176
|
|
|
if (!DashboardAccess::userHasAccess($model->name)) { |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $model; |
|
181
|
|
|
} |
|
182
|
|
|
throw new HttpException(404, 'The requested page does not exist.'); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: