UpdateWithLang.php
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace thread\actions;
use Yii;
use yii\base\Exception;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
use yii\log\Logger;
//
use thread\app\base\models\ActiveRecord;
use thread\modules\seo\modules\modellink\components\Crud;
/**
* Class UpdateWithLang
*
* @package thread\actions
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c) 2016, VipDesign
* @usage
* public function actions() {
* return [
* ...
* 'create' => [
* 'class' => CreateWithLang::class,
* 'modelClass' => Model::class,
* 'modelClassLang' => ModelLang::class
* ],
* ...
* ];
* }
*/
class UpdateWithLang extends ActionCRUD
{
/**
* @var \Closure|null
*/
public $afterSaveCallback = null;
/**
* Init action
*
* @throws Exception
*/
public function init()
{
if ($this->modelClass === null) {
throw new Exception(__CLASS__ . '::$modelClass must be set.');
}
if ($this->modelClassLang === null) {
throw new Exception(__CLASS__ . '::modelClassLang must be set.');
}
$this->model = new $this->modelClass;
$this->modelLang = new $this->modelClassLang;
/** @var ActiveRecord $this ->model */
if ($this->model === null) {
throw new Exception($this->modelClass . 'must be exists.');
}
if ($this->modelLang === null) {
throw new Exception($this->modelClassLang . 'must be exists.');
}
if (!$this->model->isScenario($this->scenario)) {
throw new Exception($this->modelClass . '::' . $this->scenario . " scenario doesn't exist");
}
if (!$this->modelLang->isScenario($this->scenario)) {
throw new Exception($this->modelClassLang . '::' . $this->scenario . " scenario doesn't exist");
}
}
/**
* Run action
*
* @param $id
* @return string|\yii\web\Response
* @throws NotFoundHttpException
*/
public function run($id)
{
$this->model = $this->findModel($id);
$this->modelLang = $this->findModelLang($id);
if ($this->model == null) {
throw new NotFoundHttpException;
}
if (Yii::$app->getRequest()->isAjax) {
return $this->controller->renderPartial($this->view, [
'model' => $this->model,
'modelLang' => $this->modelLang,
]);
} else {
if ($this->saveModel()) {
return $this->controller->redirect($this->getRedirect());
} else {
$this->controller->layout = $this->layout;
return $this->controller->render($this->view, [
'model' => $this->model,
'modelLang' => $this->modelLang,
]);
}
}
}
/**
* Save data to DB
* Scenario 'backend' should be set for model
*
* @return bool
*/
public function saveModel()
{
$save = false;
$this->model->setScenario($this->scenario);
$this->modelLang->setScenario($this->scenario);
if ($this->model->load(Yii::$app->getRequest()->post())) {
$model = $this->model;
$transaction = $model::getDb()->beginTransaction();
try {
$save = $this->model->save();
if ($save && $this->modelLang->load(Yii::$app->getRequest()->post())) {
$this->modelLang->rid = $this->model->id;
$this->modelLang->lang = Yii::$app->language;
//
$save = $this->modelLang->save();
}
$save ? $transaction->commit() : $transaction->rollBack();
if ($save) {
//LOG
if ($this->useLog && $save) {
$this->model = $model;
$this->sendToLog();
}
$this->saveSeoModel();
$this->afterSaveModel();
}
} catch (Exception $e) {
Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
$transaction->rollBack();
}
}
return $save;
}
/**
* @return string
*/
public function getLogInfo()
{
$m = $this->logMessage;
if ($m instanceof \Closure) {
$mess = $m();
} elseif (!empty($m['message'])) {
$mess = $m;
} else {
//
$module = (Yii::$app->controller->module->module->id == "app-backend") ?
Yii::$app->controller->module->id : Yii::$app->controller->module->module->id . '/' . Yii::$app->controller->module->id;
$controller = Yii::$app->controller->id;
//
$model = $this->getModel();
$title = (isset($model['lang']['title'])) ? $model['lang']['title'] : ((isset($model['title'])) ? $model['title'] : $model->id);
$mess['message'] = Yii::t('app', 'Update') . ":" . $title;
$mess['url'] = Url::toRoute(['/' . $module . '/' . $controller . '/update', 'id' => $this->getModel()->id]);
}
return $mess;
}
/**
* Run Callback function if model saved correctly
*/
protected function afterSaveModel()
{
$model = $this->model;
$defaultLang = Yii::$app->languages->getDefault();
if (isset($model['default_title']) && isset($model['lang']['title'])) {
if ($defaultLang['local'] == $model['lang']['lang'] && $model['default_title'] != $model['lang']['title']) {
$model['default_title'] = $model['lang']['title'];
$transaction = $model::getDb()->beginTransaction();
try {
$save = $model->save();
$save ? $transaction->commit() : $transaction->rollBack();
} catch (Exception $e) {
Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
$transaction->rollBack();
}
}
}
//
if ($this->afterSaveCallback instanceof \Closure) {
$f = $this->afterSaveCallback;
$f($this);
}
}
/**
*
*/
public function saveSeoModel()
{
$model = $this->model;
//
$seoCrud = new Crud();
$seoCrud->getByModel($model)->getPostModel()->saveModel();
}
}