BrandController.php
9.51 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
namespace app\ht\modules\device\controllers;
use Yii;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;
use app\ht\controllers\BaseController;
use common\components\adminlog\AdminLogs;
use common\models\Model as ModelModel;
use common\models\Brand as BrandModel;
use common\helpers\ImageManager;
use domain\device\BrandRepository;
use function date;
use function substr;
use function time;
use function explode;
use function end;
use function array_filter;
use function json_encode;
/**
* 品牌管理控制器
* Class BrandController
* @package app\ht\modules\catalog\controllers
*/
class BrandController extends BaseController
{
/**
* 启用的品牌列表
*/
public function actionIndex()
{
$params = $this->dataList(1);
/**
* 渲染模板
*/
return $this->render('index', $params);
}
/**
* 禁用的品牌列表
*/
public function actionDisabled()
{
$params = $this->dataList(2);
/**
* 渲染模板
*/
return $this->render('disabled', $params);
}
protected function dataList($type = 0)
{
$request = Yii::$app->request;
/**
* 查询过滤处理
*/
$get = [];
/**
* 组织SQL
*/
$where = ['and'];
// 英文名称
$englishName = $request->get('englishName');
$get['englishName'] = $englishName;
if ($englishName) {
$where[] = ['like', 'english_name', $englishName];
}
// 中文名称
$chineseName = $request->get('chineseName');
$get['chineseName'] = $chineseName;
if ($chineseName) {
$where[] = ['like', 'chinese_name', $chineseName];
}
$pageSize = $request->get("pageSize") ? (int) $request->get("pageSize") : 20;
if ($type == 1) {
/**
* 分页处理
*/
$where[] = ['is_available' => 1];
$pages = new Pagination(['totalCount' => BrandRepository::getBrandWithWhereCount($where), 'pageSize' => $pageSize]);
$brands = BrandRepository::getBrandWithWhere($where, $pages, 'english_name ASC');
} else if ($type == 2) {
$where[] = ['is_available' => 0];
$pages = new Pagination(['totalCount' => BrandRepository::getBrandWithWhereCount($where), 'pageSize' => $pageSize]);
$brands = BrandRepository::getBrandWithWhere($where, $pages, 'english_name ASC');
}
/**
* 数据整理
*/
$data = [];
foreach ($brands as $brand) {
if ($brand['img_url']) {
$logo = ImageManager::getUrl($brand['img_url'], ImageManager::$STYLE_90);
} else {
$logo = Yii::$app->request->BaseUrl . '/images/default-item.jpg';
}
$data[] = [
'id' => $brand['id'],
'logo' => $logo,
'chinese_name' => $brand['chinese_name'],
'english_name' => $brand['english_name'],
'status' => ($brand['is_available'] == 1) ? '已启用' : '已禁用',
'created_at' => date('Y-m-d H:i:s', $brand['created_at']),
'updated_at' => date('Y-m-d H:i:s', $brand['updated_at']),
'enable' => ($brand['is_available'] == 1) ? '禁用' : '启用',
];
}
return [
'brands' => $data,
'pages' => $pages,
'get' => $get,
];
}
/**
* 新增界面
*/
public function actionCreate()
{
return $this->render('create');
}
/**
* 新增执行动作
*/
public function actionDoAdd()
{
$post = Yii::$app->request->post();
if (empty($post)) {
return;
}
$model = Yii::createObject(BrandModel::className());
$model->chinese_name = $post['chinese_name'];
$model->english_name = $post['english_name'];
$model->is_available = $post['is_available'];
$model->internal_code = 0;
if (!empty($post['img_path'])) {
$suffix = substr($post['img_path'],(strripos($post['img_path'],".")+1));
$newPath = ImageManager::getBrandLogoPath($suffix);
ImageManager::move($post['img_path'],$newPath);
$model->img_url = $newPath;
}
// logo
/* if (!empty($_FILES['logo']['name'])) {
$image = $_FILES['logo'];
// 图片路径数据整理
$tmp = explode('.', $image['name']);
$suffix = end($tmp);
$savePath = ImageManager::getBrandLogoPath($suffix);
// 上传文件
ImageManager::add($image["tmp_name"], $savePath);
$model->img_url = $savePath;
}*/
$model->save();
Yii::$app->session->setFlash('success', '品牌添加成功');
return $this->redirect(['index']);
}
/**
* 更新界面
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
return $this->render('update', [
'model' => $model
]);
}
/**
* 更新执行动作
*/
public function actionDoUpdate($id)
{
$post = Yii::$app->request->post();
if (empty($post)) {
return;
}
$model = $this->findModel($id);
$model->chinese_name = $post['chinese_name'];
$model->english_name = $post['english_name'];
$model->is_available = $post['is_enable'];
if (!empty($post['img_path'])) {
if (!empty($model->img_url)) {
ImageManager::delete($model->img_url);
}
$suffix = substr($post['img_path'],(strripos($post['img_path'],".")+1));
$newPath = ImageManager::getBrandLogoPath($suffix);
ImageManager::move($post['img_path'],$newPath);
$model->img_url = $newPath;
}
$model->updated_at = time();
// logo
if (!empty($_FILES['logo']['name'])) {
$image = $_FILES['logo'];
// 图片路径数据整理
$tmp = explode('.', $image['name']);
$suffix = end($tmp);
$savePath = ImageManager::getBrandLogoPath($suffix);
if ($model->img_url) {
ImageManager::replace($model->img_url, $savePath, $image["tmp_name"]);
$model->img_url = $savePath;
} else {
ImageManager::add($image["tmp_name"], $savePath);
$model->img_url = $savePath;
}
}
Yii::$app->session->setFlash('success', '品牌更新成功');
$model->save();
return $this->redirect(['index']);
}
/**
* 删除执行动作
*/
public function actionDelete($id)
{
$count = ModelModel::find()->where(['brand_id' => $id])->count();
if ($count == 0) {
BrandModel::findOne(["id"=>$id])->delete();
Yii::$app->session->setFlash('success', '品牌删除完成');
} else {
Yii::$app->session->setFlash('danger', '已经为此品牌选择了设备,不能删除');
}
return $this->redirect(['index']);
}
/**
* 根据主键查找模型
*/
protected function findModel($id)
{
$brand = Yii::createObject(BrandModel::className());
if (($model = $brand::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('访问页面不存在');
}
}
/**
* 启用或禁用
*/
public function actionEnable($id)
{
$model = $this->findModel($id);
$model->is_available = ($model->is_available == 1) ? 0 : 1;
$model->save();
return $this->redirect(['index']);
}
/**
* 批量启用
*/
public function actionBatchEnable($ids)
{
$_ids = explode(',', $ids);
$_ids = array_filter($_ids);
foreach ($_ids as $id) {
$model = $this->findModel($id);
$model->is_available = 1;
$model->save();
}
Yii::$app->getSession()->setFlash('success', '批量启用成功');
return $this->redirect(['index']);
}
/**
* 批量禁用
*/
public function actionBatchDisable($ids)
{
$_ids = explode(',', $ids);
$_ids = array_filter($_ids);
foreach ($_ids as $id) {
$model = $this->findModel($id);
$model->is_available = 0;
$model->save();
}
Yii::$app->getSession()->setFlash('success', '批量禁用成功');
return $this->redirect(['disabled']);
}
/**
* AJAX
* 保存分类图标题
*/
public function actionAjaxUploadImg()
{
if (empty($_FILES['brand_img']['name'])) {
return json_encode([
'success' => false,
]);
}
$image = $_FILES['brand_img'];
// 图片路径数据整理
$tmp = explode('.', $image['name']);
$suffix = end($tmp);
//$savePath = ImageManager::getBrandLogoPath($suffix);
$savePath = ImageManager::getTempImgPath($suffix);
ImageManager::add($image["tmp_name"], $savePath);
/**
* 后台操作日志
*/
AdminLogs::doImport('品牌图片', 1);
return json_encode([
'success' => true,
'url' => ImageManager::getUrl($savePath),
'path' => $savePath
]);
}
}