Repository navigation
Expand file tree
/
Copy pathBackController.php
More file actions
218 lines (185 loc) · 6.45 KB
/
Copy pathBackController.php
File metadata and controls
218 lines (185 loc) · 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
211
212
213
214
215
216
217
218
<?php
/**
* Базовый класс для всех контроллеров панели управления
*
* @category YupeComponents
* @package yupe.modules.yupe.components.controllers
* @author YupeTeam <support@yupe.ru>
* @license BSD https://raw.github.com/yupe/yupe/master/LICENSE
* @version 0.6
* @link https://yupe.ru
*
*/
namespace yupe\components\controllers;
use Yii;
use yupe\components\WebModule;
use yupe\events\YupeBackendControllerInitEvent;
use yupe\events\YupeControllerInitEvent;
use yupe\events\YupeEvents;
use yupe\widgets\YFlashMessages;
use application\components\Controller;
use CHttpException;
use CActiveRecord;
use Exception;
use CLogger;
/**
* Class BackController
* @package yupe\components\controllers
*/
abstract class BackController extends Controller
{
/**
*
*/
const BULK_DELETE = 'delete';
// Прятать sidebar или нет:
/**
* @var bool
*/
public $hideSidebar = false;
/**
* @return array
*/
public function filters()
{
return Yii::app()->getModule('yupe')->getBackendFilters();
}
/**
* По умолчанию для роли admin разрешены все действия.
* @return array
*/
public function accessRules()
{
return [
['allow', 'roles' => ['admin']],
['deny']
];
}
/**
*
*/
public function init()
{
parent::init();
$this->yupe->getComponent('bootstrap');
$this->layout = $this->yupe->getBackendLayoutAlias();
$backendTheme = $this->yupe->backendTheme;
$this->pageTitle = Yii::t('YupeModule.yupe', 'Yupe control panel!');
if ($this->yupe->hidePanelUrls == WebModule::CHOICE_NO) {
Yii::app()->getErrorHandler()->errorAction = '/yupe/backend/error';
}
Yii::app()->eventManager->fire(
YupeEvents::BACKEND_CONTROLLER_INIT,
new YupeControllerInitEvent($this, Yii::app()->getUser())
);
if ($backendTheme) {
Yii::app()->theme = "backend_" . $backendTheme;
} else {
Yii::app()->theme = $this->yupe->theme;
}
}
/**
* @param \CAction $action
* @return bool
*/
protected function beforeAction($action)
{
/**
* $this->module->getId() !== 'install' избавляет от ошибок на этапе установки
* $this->id !== 'backend' || ($this->id == 'backend' && $action->id != 'modupdate') устраняем проблемы с зацикливанием
*/
if (($this->id !== 'backend' || ($this->id == 'backend' && $action->id != 'modupdate'))
&& ($updates = Yii::app()->migrator->checkForUpdates(
[$this->module->getId() => $this->module]
)) !== null
&& count($updates) > 0
) {
Yii::app()->getUser()->setFlash(
YFlashMessages::WARNING_MESSAGE,
Yii::t('YupeModule.yupe', 'You must install all migration before start working with module.')
);
$this->redirect(['/yupe/backend/modupdate', 'name' => $this->module->getId()]);
}
return parent::beforeAction($action);
}
/**
* @throws \CHttpException
*/
public function actionMultiaction()
{
if (!Yii::app()->getRequest()->getIsAjaxRequest() || !Yii::app()->getRequest()->getIsPostRequest()) {
throw new CHttpException(404);
}
$modelClass = Yii::app()->getRequest()->getPost('model');
$action = Yii::app()->getRequest()->getPost('do');
if (!isset($modelClass, $action)) {
throw new CHttpException(404);
}
$items = Yii::app()->getRequest()->getPost('items');
if (!is_array($items) || empty($items)) {
Yii::app()->ajax->success();
}
$transaction = Yii::app()->getDb()->beginTransaction();
try {
switch ($action) {
case self::BULK_DELETE:
$models = CActiveRecord::model($modelClass)->findAllByPk($items);
$count = 0;
foreach ($models as $model) {
$count += (int)$model->delete();
}
$transaction->commit();
Yii::app()->ajax->success(
Yii::t(
'YupeModule.yupe',
'Removed {count} records!',
[
'{count}' => $count
]
)
);
break;
default:
throw new CHttpException(404);
break;
}
} catch (Exception $e) {
$transaction->rollback();
Yii::log($e->__toString(), CLogger::LEVEL_ERROR);
Yii::app()->ajax->failure($e->getMessage());
}
}
/**
* @throws \CHttpException
*/
public function actionSort()
{
$id = (int)Yii::app()->getRequest()->getQuery('id');
$direction = Yii::app()->getRequest()->getQuery('direction');
$modelClass = Yii::app()->getRequest()->getQuery('model');
$sortField = Yii::app()->getRequest()->getQuery('sortField');
if (!isset($direction, $id, $modelClass, $sortField)) {
throw new CHttpException(404, Yii::t('YupeModule.yupe', 'Page was not found!'));
}
$model = new $modelClass();
$model_depends = new $modelClass();
$model = $model->resetScope()->findByPk($id);
if (!$model) {
throw new CHttpException(404, Yii::t('YupeModule.yupe', 'Page was not found!'));
}
if ($direction === 'up') {
$model_depends = $model_depends->findByAttributes([$sortField => ($model->$sortField - 1)]);
$model_depends->$sortField++;
$model->$sortField--; // example menu_order column in sql
} else {
$model_depends = $model_depends->findByAttributes([$sortField => ($model->$sortField + 1)]);
$model_depends->$sortField--;
$model->$sortField++;
}
$model->update([$sortField]);
$model_depends->update([$sortField]);
if (!Yii::app()->getRequest()->getIsAjaxRequest()) {
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : ['index']);
}
}
}