1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\ad\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use zacksleo\yii2\ad\models\Ad; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
use zacksleo\yii2\ad\models\AdPosition; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* AdController implements the CRUD actions for Ad model. |
15
|
|
|
*/ |
16
|
|
|
class AdController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
|
public function behaviors() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'verbs' => [ |
25
|
|
|
'class' => VerbFilter::className(), |
26
|
|
|
'actions' => [ |
27
|
|
|
'delete' => ['POST'], |
28
|
|
|
], |
29
|
|
|
], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Lists all Ad models. |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function actionIndex() |
38
|
|
|
{ |
39
|
|
|
$slug = Yii::$app->request->get('slug'); |
40
|
|
|
$query = Ad::find(); |
41
|
|
|
$this->view->title = '广告列表: '; |
42
|
|
|
if (!empty($slug) && ($position = AdPosition::findOne(['slug' => $slug])) != null) { |
43
|
|
|
$query = $query->andWhere(['position_id' => $position->id]); |
44
|
|
|
$this->view->title .= $position->name; |
45
|
|
|
} |
46
|
|
|
$dataProvider = new ActiveDataProvider([ |
47
|
|
|
'query' => $query |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
return $this->render('index', [ |
51
|
|
|
'dataProvider' => $dataProvider, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Displays a single Ad model. |
57
|
|
|
* @param integer $id |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function actionView($id) |
61
|
|
|
{ |
62
|
|
|
return $this->render('view', [ |
63
|
|
|
'model' => $this->findModel($id), |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a new Ad model. |
69
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function actionCreate() |
73
|
|
|
{ |
74
|
|
|
$model = new Ad(); |
75
|
|
|
$model->setScenario('insert'); |
76
|
|
|
$slug = Yii::$app->request->get('slug'); |
77
|
|
|
if (!empty($slug) && ($position = AdPosition::findOne(['slug' => $slug])) != null) { |
78
|
|
|
$model->position_id = $position->id; |
79
|
|
|
} |
80
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
81
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
82
|
|
|
} else { |
83
|
|
|
return $this->render('create', [ |
84
|
|
|
'model' => $model, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Updates an existing Ad model. |
91
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
92
|
|
|
* @param integer $id |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function actionUpdate($id) |
96
|
|
|
{ |
97
|
|
|
$model = $this->findModel($id); |
98
|
|
|
$model->scenario = 'update'; |
99
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
100
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
101
|
|
|
} else { |
102
|
|
|
return $this->render('update', [ |
103
|
|
|
'model' => $model, |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Deletes an existing Ad model. |
110
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
111
|
|
|
* @param integer $id |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function actionDelete($id) |
115
|
|
|
{ |
116
|
|
|
$this->findModel($id)->delete(); |
117
|
|
|
|
118
|
|
|
return $this->redirect(['index']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Finds the Ad model based on its primary key value. |
123
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
124
|
|
|
* @param integer $id |
125
|
|
|
* @return Ad the loaded model |
126
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
127
|
|
|
*/ |
128
|
|
|
protected function findModel($id) |
129
|
|
|
{ |
130
|
|
|
if (($model = Ad::findOne($id)) !== null) { |
|
|
|
|
131
|
|
|
return $model; |
132
|
|
|
} else { |
133
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|