1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* osCommerce Online Merchant |
4
|
|
|
* |
5
|
|
|
* @copyright (c) 2016 osCommerce; https://www.oscommerce.com |
6
|
|
|
* @license MIT; https://www.oscommerce.com/license/mit.txt |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use OSC\OM\OSCOM; |
10
|
|
|
use OSC\OM\Registry; |
11
|
|
|
|
12
|
|
|
class category_tree { |
13
|
|
|
protected $_data = array(); |
14
|
|
|
|
15
|
|
|
var $root_category_id = 0, |
16
|
|
|
$max_level = 0, |
17
|
|
|
$root_start_string = '', |
18
|
|
|
$root_end_string = '', |
19
|
|
|
$parent_start_string = '', |
20
|
|
|
$parent_end_string = '', |
21
|
|
|
$parent_group_start_string = '<ul>', |
22
|
|
|
$parent_group_end_string = '</ul>', |
23
|
|
|
$parent_group_apply_to_root = false, |
24
|
|
|
$child_start_string = '<li>', |
25
|
|
|
$child_end_string = '</li>', |
26
|
|
|
$breadcrumb_separator = '_', |
27
|
|
|
$breadcrumb_usage = true, |
28
|
|
|
$spacer_string = '', |
29
|
|
|
$spacer_multiplier = 1, |
30
|
|
|
$follow_cpath = false, |
31
|
|
|
$cpath_array = array(), |
32
|
|
|
$cpath_start_string = '', |
33
|
|
|
$cpath_end_string = ''; |
34
|
|
|
|
35
|
|
|
public function __construct() { |
36
|
|
|
static $_category_tree_data; |
37
|
|
|
|
38
|
|
|
$OSCOM_Db = Registry::get('Db'); |
39
|
|
|
$OSCOM_Language = Registry::get('Language'); |
40
|
|
|
|
41
|
|
|
if ( isset($_category_tree_data) ) { |
42
|
|
|
$this->_data = $_category_tree_data; |
43
|
|
|
} else { |
44
|
|
|
$Qcategories = $OSCOM_Db->prepare('select c.categories_id, c.parent_id, c.categories_image, cd.categories_name from :table_categories c, :table_categories_description cd where c.categories_id = cd.categories_id and cd.language_id = :language_id order by c.parent_id, c.sort_order, cd.categories_name'); |
45
|
|
|
$Qcategories->bindInt(':language_id', $OSCOM_Language->getId()); |
46
|
|
|
$Qcategories->setCache('categories-lang' . $OSCOM_Language->getId()); |
47
|
|
|
$Qcategories->execute(); |
48
|
|
|
|
49
|
|
|
while ($Qcategories->fetch()) { |
50
|
|
|
$this->_data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), |
51
|
|
|
'image' => $Qcategories->value('categories_image')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$_category_tree_data = $this->_data; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function _buildBranch($parent_id, $level = 0) { |
59
|
|
|
$result = ((($level === 0) && ($this->parent_group_apply_to_root === true)) || ($level > 0)) ? $this->parent_group_start_string : null; |
60
|
|
|
|
61
|
|
|
if ( isset($this->_data[$parent_id]) ) { |
62
|
|
|
foreach ( $this->_data[$parent_id] as $category_id => $category ) { |
63
|
|
|
if ( $this->breadcrumb_usage === true ) { |
64
|
|
|
$category_link = $this->buildBreadcrumb($category_id); |
65
|
|
|
} else { |
66
|
|
|
$category_link = $category_id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$result .= $this->child_start_string; |
70
|
|
|
|
71
|
|
|
if ( isset($this->_data[$category_id]) ) { |
72
|
|
|
$result .= $this->parent_start_string; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( $level === 0 ) { |
76
|
|
|
$result .= $this->root_start_string; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ( ($this->follow_cpath === true) && in_array($category_id, $this->cpath_array) ) { |
80
|
|
|
$link_title = $this->cpath_start_string . $category['name'] . $this->cpath_end_string; |
81
|
|
|
} else { |
82
|
|
|
$link_title = $category['name']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$result .= '<a href="' . OSCOM::link('index.php', 'cPath=' . $category_link) . '">'; |
86
|
|
|
$result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level); |
87
|
|
|
$result .= $link_title . '</a>'; |
88
|
|
|
|
89
|
|
|
if ( $level === 0 ) { |
90
|
|
|
$result .= $this->root_end_string; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( isset($this->_data[$category_id]) ) { |
94
|
|
|
$result .= $this->parent_end_string; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
if ( isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1)) ) { |
100
|
|
|
if ( $this->follow_cpath === true ) { |
101
|
|
|
if ( in_array($category_id, $this->cpath_array) ) { |
102
|
|
|
$result .= $this->_buildBranch($category_id, $level+1); |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
$result .= $this->_buildBranch($category_id, $level+1); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$result .= $this->child_end_string; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$result .= ((($level === 0) && ($this->parent_group_apply_to_root === true)) || ($level > 0)) ? $this->parent_group_end_string : null; |
114
|
|
|
|
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function buildBranchArray($parent_id, $level = 0, $result = '') { |
119
|
|
|
if (empty($result)) { |
120
|
|
|
$result = array(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (isset($this->_data[$parent_id])) { |
124
|
|
|
foreach ($this->_data[$parent_id] as $category_id => $category) { |
125
|
|
|
if ($this->breadcrumb_usage == true) { |
|
|
|
|
126
|
|
|
$category_link = $this->buildBreadcrumb($category_id); |
127
|
|
|
} else { |
128
|
|
|
$category_link = $category_id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$result[] = array('id' => $category_link, |
132
|
|
|
'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']); |
133
|
|
|
|
134
|
|
|
if (isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) { |
135
|
|
|
if ($this->follow_cpath === true) { |
136
|
|
|
if (in_array($category_id, $this->cpath_array)) { |
137
|
|
|
$result = $this->buildBranchArray($category_id, $level+1, $result); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} else { |
140
|
|
|
$result = $this->buildBranchArray($category_id, $level+1, $result); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function buildBreadcrumb($category_id, $level = 0) { |
150
|
|
|
$breadcrumb = ''; |
151
|
|
|
|
152
|
|
|
foreach ($this->_data as $parent => $categories) { |
153
|
|
|
foreach ($categories as $id => $info) { |
154
|
|
|
if ($id == $category_id) { |
155
|
|
|
if ($level < 1) { |
156
|
|
|
$breadcrumb = $id; |
157
|
|
|
} else { |
158
|
|
|
$breadcrumb = $id . $this->breadcrumb_separator . $breadcrumb; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($parent != $this->root_category_id) { |
162
|
|
|
$breadcrumb = $this->buildBreadcrumb($parent, $level+1) . $breadcrumb; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $breadcrumb; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return a formated string representation of the category structure relationship data |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
|
178
|
|
|
public function getTree() { |
179
|
|
|
return $this->_buildBranch($this->root_category_id); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Magic function; return a formated string representation of the category structure relationship data |
184
|
|
|
* |
185
|
|
|
* This is used when echoing the class object, eg: |
186
|
|
|
* |
187
|
|
|
* echo $osC_CategoryTree; |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
|
193
|
|
|
public function __toString() { |
194
|
|
|
return $this->getTree(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
function getArray($parent_id = '') { |
198
|
|
|
return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
function exists($id) { |
202
|
|
|
foreach ($this->_data as $parent => $categories) { |
203
|
|
|
foreach ($categories as $category_id => $info) { |
204
|
|
|
if ($id == $category_id) { |
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
function getChildren($category_id, &$array = array()) { |
214
|
|
|
foreach ($this->_data as $parent => $categories) { |
215
|
|
|
if ($parent == $category_id) { |
216
|
|
|
foreach ($categories as $id => $info) { |
217
|
|
|
$array[] = $id; |
218
|
|
|
$this->getChildren($id, $array); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $array; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Return category information |
228
|
|
|
* |
229
|
|
|
* @param int $id The category ID to return information of |
230
|
|
|
* @param string $key The key information to return (since v3.0.2) |
231
|
|
|
* @return mixed |
232
|
|
|
* @since v3.0.0 |
233
|
|
|
*/ |
234
|
|
|
|
235
|
|
|
public function getData($id, $key = null) { |
236
|
|
|
foreach ( $this->_data as $parent => $categories ) { |
237
|
|
|
foreach ( $categories as $category_id => $info ) { |
238
|
|
|
if ( $id == $category_id ) { |
239
|
|
|
$data = array('id' => $id, |
240
|
|
|
'name' => $info['name'], |
241
|
|
|
'parent_id' => $parent, |
242
|
|
|
'image' => $info['image']); |
243
|
|
|
|
244
|
|
|
return ( isset($key) ? $data[$key] : $data ); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Return the parent ID of a category |
254
|
|
|
* |
255
|
|
|
* @param int $id The category ID to return the parent ID of |
256
|
|
|
* @return int |
257
|
|
|
* @since v3.0.2 |
258
|
|
|
*/ |
259
|
|
|
|
260
|
|
|
public function getParentID($id) { |
261
|
|
|
return $this->getData($id, 'parent_id'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
function setRootCategoryID($root_category_id) { |
265
|
|
|
$this->root_category_id = $root_category_id; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
function setMaximumLevel($max_level) { |
269
|
|
|
$this->max_level = $max_level; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
function setRootString($root_start_string, $root_end_string) { |
273
|
|
|
$this->root_start_string = $root_start_string; |
274
|
|
|
$this->root_end_string = $root_end_string; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
function setParentString($parent_start_string, $parent_end_string) { |
278
|
|
|
$this->parent_start_string = $parent_start_string; |
279
|
|
|
$this->parent_end_string = $parent_end_string; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
function setParentGroupString($parent_group_start_string, $parent_group_end_string, $apply_to_root = false) { |
283
|
|
|
$this->parent_group_start_string = $parent_group_start_string; |
284
|
|
|
$this->parent_group_end_string = $parent_group_end_string; |
285
|
|
|
$this->parent_group_apply_to_root = $apply_to_root; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
function setChildString($child_start_string, $child_end_string) { |
289
|
|
|
$this->child_start_string = $child_start_string; |
290
|
|
|
$this->child_end_string = $child_end_string; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
function setBreadcrumbSeparator($breadcrumb_separator) { |
294
|
|
|
$this->breadcrumb_separator = $breadcrumb_separator; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
function setBreadcrumbUsage($breadcrumb_usage) { |
298
|
|
|
if ($breadcrumb_usage === true) { |
299
|
|
|
$this->breadcrumb_usage = true; |
300
|
|
|
} else { |
301
|
|
|
$this->breadcrumb_usage = false; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
function setSpacerString($spacer_string, $spacer_multiplier = 2) { |
306
|
|
|
$this->spacer_string = $spacer_string; |
307
|
|
|
$this->spacer_multiplier = $spacer_multiplier; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
function setCategoryPath($cpath, $cpath_start_string = '', $cpath_end_string = '') { |
311
|
|
|
$this->follow_cpath = true; |
312
|
|
|
$this->cpath_array = explode($this->breadcrumb_separator, $cpath); |
313
|
|
|
$this->cpath_start_string = $cpath_start_string; |
314
|
|
|
$this->cpath_end_string = $cpath_end_string; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
function setFollowCategoryPath($follow_cpath) { |
318
|
|
|
if ($follow_cpath === true) { |
319
|
|
|
$this->follow_cpath = true; |
320
|
|
|
} else { |
321
|
|
|
$this->follow_cpath = false; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
function setCategoryPathString($cpath_start_string, $cpath_end_string) { |
326
|
|
|
$this->cpath_start_string = $cpath_start_string; |
327
|
|
|
$this->cpath_end_string = $cpath_end_string; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.