1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yiimaker/yii2-banner |
4
|
|
|
* @copyright Copyright (c) 2017 Yii Maker |
5
|
|
|
* @license BSD 3-Clause License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ymaker\banner\backend\controllers; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\web\Controller; |
12
|
|
|
use ymaker\banner\backend\services\BannerServiceInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default controller for backend banner module. |
16
|
|
|
* |
17
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class DefaultController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var BannerServiceInterface |
24
|
|
|
*/ |
25
|
|
|
private $_service; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
* @param BannerServiceInterface $service |
31
|
|
|
*/ |
32
|
|
|
public function __construct($id, $module, BannerServiceInterface $service, $config = []) |
33
|
|
|
{ |
34
|
|
|
$this->_service = $service; |
35
|
|
|
parent::__construct($id, $module, $config); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Renders banners list. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function actionIndex() |
44
|
|
|
{ |
45
|
|
|
return $this->render('index', [ |
46
|
|
|
'dataProvider' => $this->_service->getDataProvider(), |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates new banner. |
52
|
|
|
* |
53
|
|
|
* @return string|\yii\web\Response |
54
|
|
|
*/ |
55
|
|
|
public function actionCreate() |
56
|
|
|
{ |
57
|
|
|
return $this->commonAction($this->_service->getModel(), ['index'], 'create'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Updates banner. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return string|\yii\web\Response |
65
|
|
|
*/ |
66
|
|
|
public function actionUpdate($id) |
67
|
|
|
{ |
68
|
|
|
return $this->commonAction( |
69
|
|
|
$this->_service->getModel($id), |
70
|
|
|
['view', 'id' => $id], |
71
|
|
|
'update' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Common code for create and update actions. |
77
|
|
|
* |
78
|
|
|
* @param \yii\db\ActiveRecord $model |
79
|
|
|
* @param array $redirectUrl |
80
|
|
|
* @param string $view |
81
|
|
|
* @return string|\yii\web\Response |
82
|
|
|
*/ |
83
|
|
|
protected function commonAction($model, $redirectUrl, $view) |
84
|
|
|
{ |
85
|
|
|
$request = Yii::$app->getRequest(); |
86
|
|
|
if ($request->getIsPost() && $this->_service->save($request->post())) { |
87
|
|
|
return $this->redirect($redirectUrl); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->render($view, compact('model')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Renders details about banner. |
95
|
|
|
* |
96
|
|
|
* @param int $id Banner model ID. |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function actionView($id) |
100
|
|
|
{ |
101
|
|
|
$model = $this->_service->getModel($id); |
102
|
|
|
return $this->render('view', compact('model')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Delete banner. |
107
|
|
|
* |
108
|
|
|
* @param int $id |
109
|
|
|
* @return \yii\web\Response |
110
|
|
|
*/ |
111
|
|
|
public function actionDelete($id) |
112
|
|
|
{ |
113
|
|
|
$message = 'Error: banner not removed'; |
114
|
|
|
if ($this->_service->delete($id)) { |
115
|
|
|
$message = 'Removed successfully'; |
116
|
|
|
} |
117
|
|
|
Yii::$app->getSession()->setFlash('yii2-banner', $message); |
118
|
|
|
|
119
|
|
|
return $this->redirect(['index']); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|