AttrService.class.php
8.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
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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 16/9/18
* Time: 14:37
*/
namespace Common\Service;
use VcySDK\Service;
use VcySDK\Member;
use Common\Model\AttrModel;
class AttrService extends AbstractService
{
/**
* 是否存在自定义信息:是
*/
const ATTR_CUSTOM_IS_TRUE = 1;
/**
* 是否存在自定义信息:否
*/
const ATTR_CUSTOM_IS_FALSE = 0;
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new AttrModel();
}
/**
* 交换两个属性的顺序
* @author liyifei
* @param int $attrId1 第一个属性ID
* @param int $attrId2 第二个属性ID
* @return bool
*/
public function exchangeOrder($attrId1, $attrId2)
{
$attrData1 = $this->_d->get_by_conds(['attr_id' => $attrId1]);
$attrData2 = $this->_d->get_by_conds(['attr_id' => $attrId2]);
if (empty($attrData1) || empty($attrData2)) {
E('_ERR_ATTR_UNDEFINED');
}
// 交换属性顺序
$result1 = $this->_d->update_by_conds(['attr_id' => $attrId1], ['`order`' => $attrData2['order']]);
if (!$result1) {
E('_ERR_OPERATE_FIELD');
}
$result2 = $this->_d->update_by_conds(['attr_id' => $attrId2], ['`order`' => $attrData1['order']]);
if (!$result2) {
E('_ERR_OPERATE_FIELD');
}
return true;
}
/**
* 修改属性
* @author liyifei
* @param int $attrId 属性ID
* @param array $params 属性参数
* + string option 属性选项值
* + string attr_name 属性名称
* + int is_open 是否有效
* + int is_show 是否前端显示
* @return int|bool
*/
public function updateAttrById($attrId, $params)
{
// 该属性是否存在
$data = $this->get($attrId);
if (empty($data)) {
E('_ERR_ATTR_UNDEFINED');
}
// 属性类型不允许编辑
// unset($params['type']);
// 系统属性的属性名、显示顺序、是否开启,三个属性值不可编辑
/**if ($data['is_system'] == AttrModel::IS_SYSTEM_TRUE) {
unset($params['order'], $params['is_open'], $params['attr_name']);
}*/
if (0 == $data['is_open_edit']) {
unset($params['is_open']);
}
if (0 == $data['is_open_cp_edit']) {
unset($params['is_open_cp']);
}
if (0 == $data['is_required_edit']) {
unset($params['is_required']);
}
if (0 == $data['is_required_cp_edit']) {
unset($params['is_required_cp']);
}
if (0 == $data['is_show_edit']) {
unset($params['is_show']);
}
$return = $this->update_by_conds(['attr_id' => $attrId], $params);
return $return;
}
/**
* 创建属性
* @author liyifei
* @param array $params 属性参数
* + string option 属性选项值
* + string attr_name 属性名称
* + int type 字段类型
* + int is_open 是否有效
* + int is_show 是否前端显示
* @return int|bool|mixed
*/
public function addAttr($params)
{
// 类型为空或属性名称为空
if (empty($params['type']) || empty($params['attr_name'])) {
E('_ERR_PARAM_FORMAT');
}
// 获取所有属性类型
$types = $this->getAllAttrType([AttrModel::ATTR_TYPE_SPECIAL, AttrModel::ATTR_TYPE_LEADER]);
// 属性类型不存在
if (isset($params['type']) && !in_array($params['type'], $types)) {
E('_ERR_ATTR_TYPE_UNDEFINED');
}
// 属性类型为单选、下拉框单选、多选时,选项option必须有值
$selectType = [
AttrModel::ATTR_TYPE_RADIO,
AttrModel::ATTR_TYPE_CHECKBOX
];
if (isset($params['type']) && in_array($params['type'], $selectType) && !isset($params['option'])) {
E('_ERR_ATTR_VALUE_IS_EMPTY');
}
// 属性名是否重复
$data = $this->get_by_conds(['attr_name' => $params['attr_name']]);
if ($data) {
E('_ERR_ATTR_NAME_IS_REPEAT');
}
// 显示顺序字段自动递增
$data = $this->_d->getMaxOrder();
if (isset($data['max_order'])) {
$params['order'] = $data['max_order'] + 1;
} else {
$params['order'] = 1;
}
// 获取架构可用预留字段
$allowFields = [
'custom1',
'custom2',
'custom3',
'custom4',
'custom5',
'custom6',
'custom7',
'custom8',
'custom9',
'custom10'
];
$result = $this->_d->getUsedField($allowFields);
$usedFields = array_column($result, 'field_name');
$fields = array_diff($allowFields, $usedFields);
if (empty($fields)) {
E('_ERR_FIELD_IS_USED');
}
$params['field_name'] = current($fields);
// 自定义属性均可编辑
$params['is_open_edit'] = AttrModel::ATTR_IS_EDIT_TRUE;
$params['is_required_edit'] = AttrModel::ATTR_IS_EDIT_TRUE;
$params['is_show_edit'] = AttrModel::ATTR_IS_EDIT_TRUE;
// 自定义属性均显示于身份信息区域
$params['postion'] = AttrModel::AREA_CUSTOM;
$return = $this->insert($params);
return $return;
}
/**
* 删除自定义属性
* @author liyifei
* @param int $fieldName 自定义属性字段名
* @return mixed
*/
public function deleteAttr($fieldName)
{
// 清空所有用户该属性值(自定义属性)
$memServ = new Member(Service::instance());
$memServ->delAllExt(['extKey' => $fieldName]);
// 执行本地数据删除操作
$result = $this->delete_by_conds(['field_name' => $fieldName]);
if (!$result) {
E('_ERR_OPERATE_FIELD');
}
return true;
}
/**
* 验证日期时间类型
* @author liyifei
* @param string $dateTime 日期时间(标准格式:2016-09-09 12:09:59或2016/09/09 12:09:59)
* @return bool
*/
public function checkDateTime($dateTime)
{
if (!preg_match('/^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/', $dateTime) && !preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $dateTime)) {
return false;
}
$arr = explode(' ', $dateTime);
if (count($arr) != 2) {
return false;
}
$resData = $this->checkDate($arr[0]);
$resTime = $this->checkTime($arr[1]);
if (!$resData || !$resTime) {
return false;
}
return true;
}
/**
* 验证日期类型
* @author liyifei
* @param string $date 日期(标准格式:2016-09-09或2016/09/09或2016-9-9或2016/9/9或20160909)
* @return bool
*/
public function checkDate($date)
{
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) && !preg_match('/^\d{4}\/\d{2}\/\d{2}$/', $date) && !preg_match('/^\d{4}-\d{1}-\d{1}$/', $date) && !preg_match('/^\d{4}\/\d{1}\/\d{1}$/', $date)) {
// 验证20160909类型日期
$year = substr($date, 0, 4);
$month = substr($date, 4, 2);
$day = substr($date, 6, 2);
if (checkdate($month, $day, $year)) {
return true;
} else {
return false;
}
}
if (strpos($date, '/') !== false) {
$flag = '/';
} elseif (strpos($date, '-') !== false) {
$flag = '-';
} else {
return false;
}
$arr = explode($flag, $date);
if (count($arr) != 3) {
return false;
}
if (!checkdate($arr[1], $arr[2], $arr[0])) {
return false;
}
return true;
}
/**
* 验证时间类型
* @author liyifei
* @param string $time 时间(标准格式:12:09:59)
* @return bool
*/
public function checkTime($time)
{
if (!preg_match('/^\d{2}:\d{2}:\d{2}$/', $time)) {
return false;
}
$arr = explode(':', $time);
if (count($arr) != 3) {
return false;
}
if (intval($arr[0]) > 24 || intval($arr[1]) > 59 || intval($arr[1]) < 0 || intval($arr[2]) > 59 || intval($arr[2]) < 0) {
return false;
}
return true;
}
}