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\data\ActiveDataProvider; |
12
|
|
|
use yii\web\Controller; |
13
|
|
|
use yii\web\NotFoundHttpException; |
14
|
|
|
use ymaker\email\templates\services\ServiceInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* CRUD controller for backend. |
18
|
|
|
* |
19
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class DefaultController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Email templates service instance. |
26
|
|
|
* Instance will be gotten from DI container. |
27
|
|
|
* |
28
|
|
|
* @var ServiceInterface |
29
|
|
|
*/ |
30
|
|
|
protected $_service; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function __construct($id, $module, ServiceInterface $service, $config = []) |
37
|
|
|
{ |
38
|
|
|
$this->_service = $service; |
39
|
|
|
parent::__construct($id, $module, $config); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Find email template model by ID. |
44
|
|
|
* |
45
|
|
|
* @param integer $id Model ID. |
46
|
|
|
* @return \ymaker\email\templates\models\entities\EmailTemplate |
47
|
|
|
* @throws NotFoundHttpException |
48
|
|
|
*/ |
49
|
|
|
protected function findModel($id) |
50
|
|
|
{ |
51
|
|
|
if ($model = $this->_service->getModel($id)) { |
52
|
|
|
return $model; |
53
|
|
|
} |
54
|
|
|
throw new NotFoundHttpException('Email template not found!'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Renders data provider with all template models. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function actionIndex() |
63
|
|
|
{ |
64
|
|
|
$dataProvider = $this->_service->getDataProvider(); |
65
|
|
|
return $this->render('index', compact('dataProvider')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create email template model. |
70
|
|
|
* |
71
|
|
|
* @param null|string $lang Model language. |
72
|
|
|
* @return string|\yii\web\Response |
73
|
|
|
*/ |
74
|
|
|
public function actionCreate($lang = null) |
75
|
|
|
{ |
76
|
|
|
$request = Yii::$app->getRequest(); |
77
|
|
View Code Duplication |
if ($request->getIsPost()) { |
|
|
|
|
78
|
|
|
if ($this->_service->create($request->post())) { |
79
|
|
|
return $this->redirect(['index']); |
80
|
|
|
} |
81
|
|
|
$errors = $this->_service->getErrors(); |
82
|
|
|
} |
83
|
|
|
$template = $this->_service->getModel(); |
84
|
|
|
$translation = $this->_service->getTranslationModel(null, $lang); |
85
|
|
|
|
86
|
|
|
return $this->render('create', compact([ |
87
|
|
|
'errors', |
88
|
|
|
'template', |
89
|
|
|
'translation', |
90
|
|
|
])); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* View email template models details. |
95
|
|
|
* |
96
|
|
|
* @param integer $id Model ID. |
97
|
|
|
* @param null|string $lang Model language. |
98
|
|
|
* @return string |
99
|
|
|
* @throws NotFoundHttpException |
100
|
|
|
*/ |
101
|
|
|
public function actionView($id, $lang = null) |
102
|
|
|
{ |
103
|
|
|
$lang = $lang ?: Yii::$app->language; |
104
|
|
|
$template = $this->findModel($id); |
105
|
|
|
$translation = $this->_service->getTranslationModel($id, $lang); |
106
|
|
|
|
107
|
|
|
return $this->render('view', compact([ |
108
|
|
|
'template', |
109
|
|
|
'translation', |
110
|
|
|
])); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Update email template model. |
115
|
|
|
* |
116
|
|
|
* @param integer $id Model ID. |
117
|
|
|
* @param null|string $lang Model language. |
118
|
|
|
* @return string|\yii\web\Response |
119
|
|
|
* @throws NotFoundHttpException |
120
|
|
|
*/ |
121
|
|
|
public function actionUpdate($id, $lang = null) |
122
|
|
|
{ |
123
|
|
|
$lang = $lang ?: Yii::$app->language; |
124
|
|
|
$translation = $this->_service->getTranslationModel($id, $lang); |
125
|
|
|
|
126
|
|
|
$request = Yii::$app->getRequest(); |
127
|
|
View Code Duplication |
if ($request->getIsPost()) { |
|
|
|
|
128
|
|
|
if ($this->_service->update($translation, $request->post())) { |
129
|
|
|
return $this->redirect(['view', 'id' => $id]); |
130
|
|
|
} |
131
|
|
|
$errors = $this->_service->getErrors(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$template = $this->findModel($id); |
135
|
|
|
$defaultTranslation = $this->_service->getDefaultTranslationModel($id); |
136
|
|
|
|
137
|
|
|
return $this->render('update', compact([ |
138
|
|
|
'errors', |
139
|
|
|
'template', |
140
|
|
|
'translation', |
141
|
|
|
'defaultTranslation', |
142
|
|
|
])); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Delete email template model. |
147
|
|
|
* |
148
|
|
|
* @param integer $id Model ID. |
149
|
|
|
* @return \yii\web\Response |
150
|
|
|
* @throws NotFoundHttpException |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
|
|
public function actionDelete($id) |
154
|
|
|
{ |
155
|
|
|
$this->findModel($id)->delete(); |
156
|
|
|
return $this->redirect(['index']); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.