1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace cornernote\dashboard\controllers; |
4
|
|
|
|
5
|
|
|
use cornernote\dashboard\models\DashboardPanel; |
6
|
|
|
use cornernote\dashboard\models\search\DashboardPanelSearch; |
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\DashboardPanelAccess; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* DashboardPanelController implements the CRUD actions for DashboardPanel model. |
16
|
|
|
*/ |
17
|
|
|
class DashboardPanelController extends Controller |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public function behaviors() |
24
|
|
|
{ |
25
|
|
|
if (!$updateRoles = Module::getInstance()->updateRoles) { |
26
|
|
|
return []; |
27
|
|
|
} |
28
|
|
|
return [ |
29
|
|
|
'access' => [ |
30
|
|
|
'class' => AccessControl::className(), |
31
|
|
|
'rules' => [ |
32
|
|
|
[ |
33
|
|
|
'allow' => true, |
34
|
|
|
'actions' => ['view'], |
35
|
|
|
'roles' => ['@'] |
36
|
|
|
], |
37
|
|
|
[ |
38
|
|
|
'allow' => true, |
39
|
|
|
'actions' => ['index', 'create', 'update', 'delete'], |
40
|
|
|
'roles' => $updateRoles |
41
|
|
|
] |
42
|
|
|
] |
43
|
|
|
] |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates a new DashboardPanel model. |
49
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function actionCreate() |
53
|
|
|
{ |
54
|
|
|
$model = new DashboardPanel; |
55
|
|
|
//$model->scenario = 'create'; |
56
|
|
|
|
57
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
58
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel has been created.')); |
|
|
|
|
59
|
|
|
return $this->redirect(['update', 'id' => $model->id]); |
60
|
|
|
} elseif (!\Yii::$app->request->isPost) { |
61
|
|
|
$model->load(Yii::$app->request->get()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->render('create', compact('model')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Updates an existing DashboardPanel model. |
69
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
70
|
|
|
* @param string $id |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function actionUpdate($id) |
74
|
|
|
{ |
75
|
|
|
$model = $this->findModel($id); |
76
|
|
|
//$model->scenario = 'update'; |
77
|
|
|
|
78
|
|
|
$data = Yii::$app->request->post(); |
79
|
|
|
if ($data && $model->panel->load($data) && $model->panel->validate()) { |
80
|
|
|
$model->options = $model->panel->getOptions(); |
81
|
|
|
if ($model->save(false)) { |
|
|
|
|
82
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel has been updated.')); |
|
|
|
|
83
|
|
|
return $this->redirect(['dashboard/view', 'id' => $model->dashboard->id]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->render('update', compact('model')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Deletes an existing DashboardPanel model. |
93
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
94
|
|
|
* @param string $id |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function actionDelete($id) |
98
|
|
|
{ |
99
|
|
|
$model = $this->findModel($id); |
100
|
|
|
$model->delete(); |
|
|
|
|
101
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel has been deleted.')); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return $this->redirect(['dashboard/view', 'id' => $model->dashboard_id]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Finds the DashboardPanel model based on its primary key value. |
108
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
109
|
|
|
* If defined for panel allowRoles, check user access |
110
|
|
|
* @param string $id |
111
|
|
|
* @return DashboardPanel the loaded model |
112
|
|
|
* @throws HttpException if the model cannot be found |
113
|
|
|
*/ |
114
|
|
|
protected function findModel($id) |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
if (($model = DashboardPanel::findOne($id)) !== null) { |
118
|
|
|
|
119
|
|
|
if (!DashboardPanelAccess::userHasAccess($model->name)) { |
120
|
|
|
throw new HttpException(401, 'You are not allowed to access this page.'); |
121
|
|
|
} |
122
|
|
|
return $model; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
throw new HttpException(404, 'The requested page does not exist.'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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: