1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use app\models\ContentType; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use yii\web\NotFoundHttpException; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* ContentController implements the CRUD actions for ContentType model. |
14
|
|
|
*/ |
15
|
|
|
class ContentTypeController extends BaseController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'verbs' => [ |
24
|
|
|
'class' => VerbFilter::className(), |
25
|
|
|
'actions' => [ |
26
|
|
|
'delete' => ['POST'], |
27
|
|
|
], |
28
|
|
|
], |
29
|
|
|
'access' => [ |
30
|
|
|
'class' => AccessControl::className(), |
31
|
|
|
'only' => ['index'], |
32
|
|
|
'rules' => [ |
33
|
|
|
['allow' => true, 'actions' => ['index'], 'roles' => ['setContentTypes']], |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Lists all ContentType models. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function actionIndex() |
45
|
|
|
{ |
46
|
|
|
$dataProvider = new ActiveDataProvider([ |
47
|
|
|
'query' => ContentType::find(), |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
return $this->render('index', [ |
51
|
|
|
'dataProvider' => $dataProvider, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Enables or disables a Device. |
57
|
|
|
* |
58
|
|
|
* @param int $id screen id |
59
|
|
|
* |
60
|
|
|
* @return \yii\web\Response |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function actionToggle($id) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$model = $this->findModel($id); |
65
|
|
|
|
66
|
|
|
$model->enabled = !$model->enabled; |
|
|
|
|
67
|
|
|
$model->save(); |
68
|
|
|
|
69
|
|
|
return $this->smartGoBack(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Finds the ContentType model based on its primary key value. |
74
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
75
|
|
|
* |
76
|
|
|
* @param int $id |
77
|
|
|
* |
78
|
|
|
* @return ContentType the loaded model |
79
|
|
|
* |
80
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
protected function findModel($id) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if (($model = ContentType::findOne($id)) !== null) { |
85
|
|
|
return $model; |
86
|
|
|
} else { |
87
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'The requested content does not exist.')); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.