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()->orderBy('order'); |
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
|
|
|
$slug = Yii::$app->request->get('slug'); |
76
|
|
|
if (!empty($slug) && ($position = AdPosition::findOne(['slug' => $slug])) != null) { |
77
|
|
|
$model->position_id = $position->id; |
78
|
|
|
} |
79
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
80
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
81
|
|
|
} else { |
82
|
|
|
return $this->render('create', [ |
83
|
|
|
'model' => $model, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Updates an existing Ad model. |
90
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
91
|
|
|
* @param integer $id |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function actionUpdate($id) |
95
|
|
|
{ |
96
|
|
|
$model = $this->findModel($id); |
97
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
98
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
99
|
|
|
} else { |
100
|
|
|
return $this->render('update', [ |
101
|
|
|
'model' => $model, |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Deletes an existing Ad model. |
108
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
109
|
|
|
* @param integer $id |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function actionDelete($id) |
113
|
|
|
{ |
114
|
|
|
$this->findModel($id)->delete(); |
115
|
|
|
|
116
|
|
|
return $this->redirect(['index']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Finds the Ad model based on its primary key value. |
121
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
122
|
|
|
* @param integer $id |
123
|
|
|
* @return Ad the loaded model |
124
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
125
|
|
|
*/ |
126
|
|
|
protected function findModel($id) |
127
|
|
|
{ |
128
|
|
|
if (($model = Ad::findOne($id)) !== null) { |
|
|
|
|
129
|
|
|
$model->available_from = date('Y-m-d H:i', $model->available_from); |
130
|
|
|
$model->available_to = date('Y-m-d H:i', $model->available_to); |
131
|
|
|
return $model; |
132
|
|
|
} else { |
133
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.