SaveController.class.php
2.65 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
<?php
namespace Apicp\Controller\NewsClass;
use Com\PackageValidate;
use Common\Service\ArticleService;
use Common\Service\ClassService;
class SaveController extends \Apicp\Controller\AbstractController
{
/**
* Save
* @author tangxingguo
* @desc 保存分类
* @param Int parent_id 上级分类ID(二级分类保存时必填)
* @param Int class_id 分类ID
* @param String class_name:true 分类标题
* @param String description 分类描述
* @return array
*/
public function Index_post()
{
// 验证规则
$rules = [
'parent_id' => 'integer',
'class_id' => 'integer',
'class_name' => 'require|max:20',
'description' => 'max:120',
'is_open' => 'in:1,2',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 分类信息初始化
$classServ = new ClassService();
$classInfo = [
'class_name' => $postData['class_name'],
'description' => isset($postData['description']) ? $postData['description'] : '',
'is_open' => $postData['is_open']
];
// 唯一分类名检查
if (!$classServ->uniqueName($classInfo['class_name'], $postData['class_id'])) {
E('_ERR_CLASS_NAME_REPEAT');
}
// 二级分类操作
if (isset($postData['parent_id'])) {
if (!isset($postData['parent_id']) || empty($postData['parent_id'])) {
E('_ERR_CLASS_PARENT_ID_EMPTY');
}
$classInfo['parent_id'] = $postData['parent_id'];
// 分类名改变,修改article内分类名称
if (isset($postData['class_id'])) {
$class = $classServ->get($postData['class_id']);
if ($class && $class['class_name'] != $postData['class_name']) {
$articleServ = new ArticleService();
$articleServ->update_by_conds(
[
'class_id' => $postData['class_id']],
[
'class_name' => $postData['class_name']
]
);
}
}
} else {
// 一级分类 默认开启
$classInfo['is_open'] = 2;
}
if (empty($postData['class_id'])) {
// 保存
$postData['class_id'] = $classServ->insert($classInfo);
} else {
// 修改
$classServ->update($postData['class_id'], $classInfo);
}
}
}