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