ArticleCatLogic.php
9.27 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
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: 当燃
* Date: 2015-09-09
*/
namespace app\admin\logic;
use think\Model;
use think\Db;
/**
* 分类逻辑定义
* Class CatsLogic
* @package Admin\Logic
*/
class ArticleCatLogic extends Model
{
/**
* 获得指定分类下的子分类的数组
*
* @access public
* @param int $cat_id 分类的ID
* @param int $selected 当前选中分类的ID
* @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
* @param int $level 限定返回的级数。为0时返回所有级数
* @return mix
*/
public function article_cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0)
{
static $res = NULL;
if ($res === NULL)
{
$data = false;//read_static_cache('art_cat_pid_releate');
if ($data === false)
{
$cat_type = I('cat_type/d');
$where = array();
$where["c.store_id"]=getAdmStoId();
if($cat_type != ""){
$where['c.cat_type'] = $cat_type;
}
$res = DB::name('article_cat')
->field('c.*,count(s.cat_id) as has_children')
->alias('c')
->join('__ARTICLE_CAT__ s','s.parent_id = c.cat_id','LEFT')
->where($where)
->group('c.cat_id')
->order('parent_id,sort_order')
->select();
//write_static_cache('art_cat_pid_releate', $res);
}
else
{
$res = $data;
}
}
if (empty($res) == true)
{
return $re_type ? '' : array();
}
$options = $this->article_cat_options($cat_id, $res); // 获得指定分类下的子分类的数组
/* 截取到指定的缩减级别 */
if ($level > 0)
{
if ($cat_id == 0)
{
$end_level = $level;
}
else
{
$first_item = reset($options); // 获取第一个元素
$end_level = $first_item['level'] + $level;
}
/* 保留level小于end_level的部分 */
foreach ($options AS $key => $val)
{
if ($val['level'] >= $end_level)
{
unset($options[$key]);
}
}
}
$pre_key = 0;
foreach ($options AS $key => $value)
{
$options[$key]['has_children'] = 1;
if ($pre_key > 0)
{
if ($options[$pre_key]['cat_id'] == $options[$key]['parent_id'])
{
$options[$pre_key]['has_children'] = 1;
}
}
$pre_key = $key;
}
if ($re_type == true)
{
$select = '';
foreach ($options AS $var)
{
$select .= '<option value="' . $var['cat_id'] . '" ';
//$select .= ' cat_type="' . $var['cat_type'] . '" ';
$select .= ($selected == $var['cat_id']) ? "selected='ture'" : '';
$select .= '>';
if ($var['level'] > 0)
{
$select .= str_repeat(' ', $var['level'] * 4);
}
$select .= htmlspecialchars(addslashes($var['cat_name'])) . '</option>';
}
return $select;
}
else
{
foreach ($options AS $key => $value)
{
///$options[$key]['url'] = build_uri('article_cat', array('acid' => $value['cat_id']), $value['cat_name']);
}
return $options;
}
}
/**
* 过滤和排序所有文章分类,返回一个带有缩进级别的数组
*
* @access private
* @param int $cat_id 上级分类ID
* @param array $arr 含有所有分类的数组
* @param int $level 级别
* @return void
*/
public function article_cat_options($spec_cat_id, $arr)
{
static $cat_options = array();
if (isset($cat_options[$spec_cat_id]))
{
return $cat_options[$spec_cat_id];
}
if (!isset($cat_options[0]))
{
$level = $last_cat_id = 0;
$options = $cat_id_array = $level_array = array();
while (!empty($arr))
{
foreach ($arr AS $key => $value)
{
$cat_id = $value['cat_id'];
if ($level == 0 && $last_cat_id == 0)
{
if ($value['parent_id'] > 0)
{
break;
}
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id'] = $cat_id;
$options[$cat_id]['name'] = $value['cat_name'];
unset($arr[$key]);
if ($value['has_children'] == 0)
{
continue;
}
$last_cat_id = $cat_id;
$cat_id_array = array($cat_id);
$level_array[$last_cat_id] = ++$level;
continue;
}
if ($value['parent_id'] == $last_cat_id)
{
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id'] = $cat_id;
$options[$cat_id]['name'] = $value['cat_name'];
unset($arr[$key]);
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cat_id;
$cat_id_array[] = $cat_id;
$level_array[$last_cat_id] = ++$level;
}
}
elseif ($value['parent_id'] > $last_cat_id)
{
break;
}
}
$count = count($cat_id_array);
if ($count > 1)
{
$last_cat_id = array_pop($cat_id_array);
}
elseif ($count == 1)
{
if ($last_cat_id != end($cat_id_array))
{
$last_cat_id = end($cat_id_array);
}
else
{
$level = 0;
$last_cat_id = 0;
$cat_id_array = array();
continue;
}
}
if ($last_cat_id && isset($level_array[$last_cat_id]))
{
$level = $level_array[$last_cat_id];
}
else
{
$level = 0;
break;
}
}
$cat_options[0] = $options;
}
else
{
$options = $cat_options[0];
}
if (!$spec_cat_id)
{
return $options;
}
else
{
if (empty($options[$spec_cat_id]))
{
return array();
}
$spec_cat_id_level = $options[$spec_cat_id]['level'];
foreach ($options AS $key => $value)
{
if ($key != $spec_cat_id)
{
unset($options[$key]);
}
else
{
break;
}
}
$spec_cat_id_array = array();
foreach ($options AS $key => $value)
{
if (($spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) ||
($spec_cat_id_level > $value['level']))
{
break;
}
else
{
$spec_cat_id_array[$key] = $value;
}
}
$cat_options[$spec_cat_id] = $spec_cat_id_array;
return $spec_cat_id_array;
}
}
}