  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: category_tree.php 1672 2007-07-26 22:07:13Z hpdl $ |
| |
| 3 | + | $Id: category_tree.php 1675 2007-08-20 23:02:47Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
|
|
 |
… |
|
13 | 13 | | */ |
| |
14 | 14 | | |
| |
15 | 15 | | class osC_CategoryTree { |
  |
| 16 | + | |
| |
| 17 | + | /** |
| |
| 18 | + | * Flag to control if the total number of products in a category should be calculated |
| |
| 19 | + | * |
| |
| 20 | + | * @var boolean |
| |
| 21 | + | * @access protected |
| |
| 22 | + | */ |
| |
| 23 | + | |
| |
| 24 | + | protected $_show_total_products = false; |
| |
| 25 | + | |
| |
| 26 | + | /** |
| |
| 27 | + | * Array containing the category structure relationship data |
| |
| 28 | + | * |
| |
| 29 | + | * @var array |
| |
| 30 | + | * @access protected |
| |
| 31 | + | */ |
| |
| 32 | + | |
| |
| 33 | + | protected $_data = array(); |
| |
| 34 | + | |
|
16 | 35 | | var $root_category_id = 0, |
| |
17 | 36 | | $max_level = 0, |
  |
18 | | - | $data = array(), |
|
19 | 37 | | $root_start_string = '', |
| |
20 | 38 | | $root_end_string = '', |
| |
21 | 39 | | $parent_start_string = '', |
| |
|
|
 |
… |
|
32 | 50 | | $cpath_array = array(), |
| |
33 | 51 | | $cpath_start_string = '', |
| |
34 | 52 | | $cpath_end_string = '', |
  |
35 | | - | $show_category_product_count = false, |
|
36 | 53 | | $category_product_count_start_string = ' (', |
| |
37 | 54 | | $category_product_count_end_string = ')'; |
| |
38 | 55 | | |
  |
39 | | - | function __construct($load_from_database = true) { |
| |
| 56 | + | /** |
| |
| 57 | + | * Constructor; load the category structure relationship data from the database |
| |
| 58 | + | * |
| |
| 59 | + | * @access public |
| |
| 60 | + | */ |
| |
| 61 | + | |
| |
| 62 | + | public function __construct() { |
|
40 | 63 | | global $osC_Database, $osC_Cache, $osC_Language; |
| |
41 | 64 | | |
  |
42 | | - | if (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') { |
| |
43 | | - | $this->show_category_product_count = true; |
| |
| 65 | + | if ( SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1' ) { |
| |
| 66 | + | $this->_show_total_products = true; |
|
44 | 67 | | } |
| |
45 | 68 | | |
  |
46 | | - | if ($load_from_database === true) { |
| |
47 | | - | if ($osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720)) { |
| |
48 | | - | $this->data = $osC_Cache->getCache(); |
| |
49 | | - | } else { |
| |
50 | | - | $Qcategories = $osC_Database->query('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'); |
| |
51 | | - | $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES); |
| |
52 | | - | $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION); |
| |
53 | | - | $Qcategories->bindInt(':language_id', $osC_Language->getID()); |
| |
54 | | - | $Qcategories->execute(); |
| |
| 69 | + | if ( $osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720) ) { |
| |
| 70 | + | $this->_data = $osC_Cache->getCache(); |
| |
| 71 | + | } else { |
| |
| 72 | + | $Qcategories = $osC_Database->query('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'); |
| |
| 73 | + | $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES); |
| |
| 74 | + | $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION); |
| |
| 75 | + | $Qcategories->bindInt(':language_id', $osC_Language->getID()); |
| |
| 76 | + | $Qcategories->execute(); |
|
55 | 77 | | |
  |
56 | | - | $this->data = array(); |
| |
57 | | - | |
| |
58 | | - | while ($Qcategories->next()) { |
| |
59 | | - | $this->data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), 'image' => $Qcategories->value('categories_image'), 'count' => 0); |
| |
60 | | - | } |
| |
61 | | - | |
| |
62 | | - | $Qcategories->freeResult(); |
| |
63 | | - | |
| |
64 | | - | if ($this->show_category_product_count === true) { |
| |
65 | | - | $this->calculateCategoryProductCount(); |
| |
66 | | - | } |
| |
67 | | - | |
| |
68 | | - | $osC_Cache->write($this->data); |
| |
| 78 | + | while ( $Qcategories->next() ) { |
| |
| 79 | + | $this->_data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), |
| |
| 80 | + | 'image' => $Qcategories->value('categories_image'), |
| |
| 81 | + | 'count' => 0); |
|
69 | 82 | | } |
  |
70 | | - | } |
| |
71 | | - | } |
|
72 | 83 | | |
  |
73 | | - | function setData(&$data_array) { |
| |
74 | | - | if (is_array($data_array)) { |
| |
75 | | - | $this->data = array(); |
| |
76 | | - | |
| |
77 | | - | for ($i=0, $n=sizeof($data_array); $i<$n; $i++) { |
| |
78 | | - | $this->data[$data_array[$i]['parent_id']][$data_array[$i]['categories_id']] = array('name' => $data_array[$i]['categories_name'], 'count' => $data_array[$i]['categories_count']); |
| |
| 84 | + | if ( $this->_show_total_products === true ) { |
| |
| 85 | + | $this->_calculateProductTotals(); |
|
79 | 86 | | } |
  |
| 87 | + | |
| |
| 88 | + | $osC_Cache->write($this->_data); |
|
80 | 89 | | } |
| |
81 | 90 | | } |
| |
82 | 91 | | |
| |
|
|
 |
… |
|
99 | 108 | | $this->cpath_array = array(); |
| |
100 | 109 | | $this->cpath_start_string = ''; |
| |
101 | 110 | | $this->cpath_end_string = ''; |
  |
102 | | - | $this->show_category_product_count = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false; |
| |
| 111 | + | $this->_show_total_products = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false; |
|
103 | 112 | | $this->category_product_count_start_string = ' ('; |
| |
104 | 113 | | $this->category_product_count_end_string = ')'; |
| |
105 | 114 | | } |
| |
106 | 115 | | |
  |
107 | | - | function buildBranch($parent_id, $level = 0) { |
| |
| 116 | + | /** |
| |
| 117 | + | * Return a formated string representation of a category and its subcategories |
| |
| 118 | + | * |
| |
| 119 | + | * @param int $parent_id The parent ID of the category to build from |
| |
| 120 | + | * @param int $level Internal flag to note the depth of the category structure |
| |
| 121 | + | * @access protected |
| |
| 122 | + | * @return string |
| |
| 123 | + | */ |
| |
| 124 | + | |
| |
| 125 | + | protected function _buildBranch($parent_id, $level = 0) { |
|
108 | 126 | | $result = $this->parent_group_start_string; |
| |
109 | 127 | | |
  |
110 | | - | if (isset($this->data[$parent_id])) { |
| |
111 | | - | foreach ($this->data[$parent_id] as $category_id => $category) { |
| |
112 | | - | if ($this->breadcrumb_usage == true) { |
| |
| 128 | + | if ( isset($this->_data[$parent_id]) ) { |
| |
| 129 | + | foreach ( $this->_data[$parent_id] as $category_id => $category ) { |
| |
| 130 | + | if ( $this->breadcrumb_usage === true ) { |
|
113 | 131 | | $category_link = $this->buildBreadcrumb($category_id); |
| |
114 | 132 | | } else { |
| |
115 | 133 | | $category_link = $category_id; |
| |
116 | 134 | | } |
| |
117 | 135 | | |
| |
118 | 136 | | $result .= $this->child_start_string; |
| |
119 | 137 | | |
  |
120 | | - | if (isset($this->data[$category_id])) { |
| |
| 138 | + | if ( isset($this->_data[$category_id]) ) { |
|
121 | 139 | | $result .= $this->parent_start_string; |
| |
122 | 140 | | } |
| |
123 | 141 | | |
  |
124 | | - | if ($level == 0) { |
| |
| 142 | + | if ( $level === 0 ) { |
|
125 | 143 | | $result .= $this->root_start_string; |
| |
126 | 144 | | } |
| |
127 | 145 | | |
| |
|
|
 |
… |
|
133 | 151 | | |
| |
134 | 152 | | $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link), $link_title); |
| |
135 | 153 | | |
  |
136 | | - | if ($this->show_category_product_count === true) { |
| |
| 154 | + | if ( $this->_show_total_products === true ) { |
|
137 | 155 | | $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string; |
| |
138 | 156 | | } |
| |
139 | 157 | | |
  |
140 | | - | if ($level == 0) { |
| |
| 158 | + | if ( $level === 0 ) { |
|
141 | 159 | | $result .= $this->root_end_string; |
| |
142 | 160 | | } |
| |
143 | 161 | | |
  |
144 | | - | if (isset($this->data[$category_id])) { |
| |
| 162 | + | if ( isset($this->_data[$category_id]) ) { |
|
145 | 163 | | $result .= $this->parent_end_string; |
| |
146 | 164 | | } |
| |
147 | 165 | | |
| |
148 | 166 | | $result .= $this->child_end_string; |
| |
149 | 167 | | |
  |
150 | | - | if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) { |
| |
151 | | - | if ($this->follow_cpath === true) { |
| |
152 | | - | if (in_array($category_id, $this->cpath_array)) { |
| |
153 | | - | $result .= $this->buildBranch($category_id, $level+1); |
| |
| 168 | + | if ( isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1)) ) { |
| |
| 169 | + | if ( $this->follow_cpath === true ) { |
| |
| 170 | + | if ( in_array($category_id, $this->cpath_array) ) { |
| |
| 171 | + | $result .= $this->_buildBranch($category_id, $level+1); |
|
154 | 172 | | } |
| |
155 | 173 | | } else { |
  |
156 | | - | $result .= $this->buildBranch($category_id, $level+1); |
| |
| 174 | + | $result .= $this->_buildBranch($category_id, $level+1); |
|
157 | 175 | | } |
| |
158 | 176 | | } |
| |
159 | 177 | | } |
| |
|
|
 |
… |
|
169 | 187 | | $result = array(); |
| |
170 | 188 | | } |
| |
171 | 189 | | |
  |
172 | | - | if (isset($this->data[$parent_id])) { |
| |
173 | | - | foreach ($this->data[$parent_id] as $category_id => $category) { |
| |
| 190 | + | if (isset($this->_data[$parent_id])) { |
| |
| 191 | + | foreach ($this->_data[$parent_id] as $category_id => $category) { |
|
174 | 192 | | if ($this->breadcrumb_usage == true) { |
| |
175 | 193 | | $category_link = $this->buildBreadcrumb($category_id); |
| |
176 | 194 | | } else { |
| |
|
|
 |
… |
|
180 | 198 | | $result[] = array('id' => $category_link, |
| |
181 | 199 | | 'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']); |
| |
182 | 200 | | |
  |
183 | | - | if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) { |
| |
| 201 | + | if (isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) { |
|
184 | 202 | | if ($this->follow_cpath === true) { |
| |
185 | 203 | | if (in_array($category_id, $this->cpath_array)) { |
| |
186 | 204 | | $result = $this->buildBranchArray($category_id, $level+1, $result); |
| |
|
|
 |
… |
|
198 | 216 | | function buildBreadcrumb($category_id, $level = 0) { |
| |
199 | 217 | | $breadcrumb = ''; |
| |
200 | 218 | | |
  |
201 | | - | foreach ($this->data as $parent => $categories) { |
| |
| 219 | + | foreach ($this->_data as $parent => $categories) { |
|
202 | 220 | | foreach ($categories as $id => $info) { |
| |
203 | 221 | | if ($id == $category_id) { |
| |
204 | 222 | | if ($level < 1) { |
| |
|
|
 |
… |
|
217 | 235 | | return $breadcrumb; |
| |
218 | 236 | | } |
| |
219 | 237 | | |
  |
220 | | - | function buildTree() { |
| |
221 | | - | return $this->buildBranch($this->root_category_id); |
| |
| 238 | + | /** |
| |
| 239 | + | * Return a formated string representation of the category structure relationship data |
| |
| 240 | + | * |
| |
| 241 | + | * @access public |
| |
| 242 | + | * @return string |
| |
| 243 | + | */ |
| |
| 244 | + | |
| |
| 245 | + | public function getTree() { |
| |
| 246 | + | return $this->_buildBranch($this->root_category_id); |
|
222 | 247 | | } |
| |
223 | 248 | | |
  |
224 | | - | function getTree($parent_id = '') { |
| |
| 249 | + | /** |
| |
| 250 | + | * Magic function; return a formated string representation of the category structure relationship data |
| |
| 251 | + | * |
| |
| 252 | + | * This is used when echoing the class object, eg: |
| |
| 253 | + | * |
| |
| 254 | + | * echo $osC_CategoryTree; |
| |
| 255 | + | * |
| |
| 256 | + | * @access public |
| |
| 257 | + | * @return string |
| |
| 258 | + | */ |
| |
| 259 | + | |
| |
| 260 | + | public function __toString() { |
| |
| 261 | + | return $this->getTree(); |
| |
| 262 | + | } |
| |
| 263 | + | |
| |
| 264 | + | function getArray($parent_id = '') { |
|
225 | 265 | | return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id)); |
| |
226 | 266 | | } |
| |
227 | 267 | | |
| |
228 | 268 | | function exists($id) { |
  |
229 | | - | foreach ($this->data as $parent => $categories) { |
| |
| 269 | + | foreach ($this->_data as $parent => $categories) { |
|
230 | 270 | | foreach ($categories as $category_id => $info) { |
| |
231 | 271 | | if ($id == $category_id) { |
| |
232 | 272 | | return true; |
| |
|
|
 |
… |
|
238 | 278 | | } |
| |
239 | 279 | | |
| |
240 | 280 | | function getChildren($category_id, &$array) { |
  |
241 | | - | foreach ($this->data as $parent => $categories) { |
| |
| 281 | + | foreach ($this->_data as $parent => $categories) { |
|
242 | 282 | | if ($parent == $category_id) { |
| |
243 | 283 | | foreach ($categories as $id => $info) { |
| |
244 | 284 | | $array[] = $id; |
| |
|
|
 |
… |
|
251 | 291 | | } |
| |
252 | 292 | | |
| |
253 | 293 | | function getData($id) { |
  |
254 | | - | foreach ($this->data as $parent => $categories) { |
| |
| 294 | + | foreach ($this->_data as $parent => $categories) { |
|
255 | 295 | | foreach ($categories as $category_id => $info) { |
| |
256 | 296 | | if ($id == $category_id) { |
| |
257 | 297 | | return array('id' => $id, |
| |
|
|
 |
… |
|
267 | 307 | | return false; |
| |
268 | 308 | | } |
| |
269 | 309 | | |
  |
270 | | - | function calculateCategoryProductCount() { |
| |
| 310 | + | /** |
| |
| 311 | + | * Calculate the number of products in each category |
| |
| 312 | + | * |
| |
| 313 | + | * @access protected |
| |
| 314 | + | */ |
| |
| 315 | + | |
| |
| 316 | + | protected function _calculateProductTotals($filter_active = true) { |
|
271 | 317 | | global $osC_Database; |
| |
272 | 318 | | |
| |
273 | 319 | | $totals = array(); |
| |
274 | 320 | | |
  |
275 | | - | $Qtotals = $osC_Database->query('select p2c.categories_id, count(*) as total from :table_products p, :table_products_to_categories p2c where p2c.products_id = p.products_id and p.products_status = :products_status group by p2c.categories_id'); |
| |
| 321 | + | $Qtotals = $osC_Database->query('select p2c.categories_id, count(*) as total from :table_products p, :table_products_to_categories p2c where p2c.products_id = p.products_id'); |
| |
| 322 | + | |
| |
| 323 | + | if ( $filter_active === true ) { |
| |
| 324 | + | $Qtotals->appendQuery('and p.products_status = :products_status'); |
| |
| 325 | + | $Qtotals->bindInt(':products_status', 1); |
| |
| 326 | + | } |
| |
| 327 | + | |
| |
| 328 | + | $Qtotals->appendQuery('group by p2c.categories_id'); |
|
276 | 329 | | $Qtotals->bindTable(':table_products', TABLE_PRODUCTS); |
| |
277 | 330 | | $Qtotals->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES); |
  |
278 | | - | $Qtotals->bindInt(':products_status', 1); |
|
279 | 331 | | $Qtotals->execute(); |
| |
280 | 332 | | |
  |
281 | | - | while ($Qtotals->next()) { |
| |
| 333 | + | while ( $Qtotals->next() ) { |
|
282 | 334 | | $totals[$Qtotals->valueInt('categories_id')] = $Qtotals->valueInt('total'); |
| |
283 | 335 | | } |
| |
284 | 336 | | |
  |
285 | | - | $Qtotals->freeResult(); |
| |
| 337 | + | foreach ( $this->_data as $parent => $categories ) { |
| |
| 338 | + | foreach ( $categories as $id => $info ) { |
| |
| 339 | + | if ( isset($totals[$id]) && ($totals[$id] > 0) ) { |
| |
| 340 | + | $this->_data[$parent][$id]['count'] = $totals[$id]; |
|
286 | 341 | | |
  |
287 | | - | foreach ($this->data as $parent => $categories) { |
| |
288 | | - | foreach ($categories as $id => $info) { |
| |
289 | | - | if (isset($totals[$id]) && ($totals[$id] > 0)) { |
| |
290 | | - | $this->data[$parent][$id]['count'] = $totals[$id]; |
| |
291 | | - | |
|
292 | 342 | | $parent_category = $parent; |
  |
293 | | - | while ($parent_category != $this->root_category_id) { |
| |
294 | | - | foreach ($this->data as $parent_parent => $parent_categories) { |
| |
295 | | - | foreach ($parent_categories as $parent_category_id => $parent_category_info) { |
| |
296 | | - | if ($parent_category_id == $parent_category) { |
| |
297 | | - | $this->data[$parent_parent][$parent_category_id]['count'] += $this->data[$parent][$id]['count']; |
|
298 | 343 | | |
  |
| 344 | + | while ( $parent_category != $this->root_category_id ) { |
| |
| 345 | + | foreach ( $this->_data as $parent_parent => $parent_categories ) { |
| |
| 346 | + | foreach ( $parent_categories as $parent_category_id => $parent_category_info ) { |
| |
| 347 | + | if ( $parent_category_id == $parent_category ) { |
| |
| 348 | + | $this->_data[$parent_parent][$parent_category_id]['count'] += $this->_data[$parent][$id]['count']; |
| |
| 349 | + | |
|
299 | 350 | | $parent_category = $parent_parent; |
  |
| 351 | + | |
|
300 | 352 | | break 2; |
| |
301 | 353 | | } |
| |
302 | 354 | | } |
| |
|
|
 |
… |
|
305 | 357 | | } |
| |
306 | 358 | | } |
| |
307 | 359 | | } |
  |
308 | | - | |
| |
309 | | - | unset($totals); |
|
310 | 360 | | } |
| |
311 | 361 | | |
| |
312 | 362 | | function getNumberOfProducts($id) { |
  |
313 | | - | foreach ($this->data as $parent => $categories) { |
| |
| 363 | + | foreach ($this->_data as $parent => $categories) { |
|
314 | 364 | | foreach ($categories as $category_id => $info) { |
| |
315 | 365 | | if ($id == $category_id) { |
| |
316 | 366 | | return $info['count']; |
| |
|
|
 |
… |
|
388 | 438 | | |
| |
389 | 439 | | function setShowCategoryProductCount($show_category_product_count) { |
| |
390 | 440 | | if ($show_category_product_count === true) { |
  |
391 | | - | $this->show_category_product_count = true; |
| |
| 441 | + | $this->_show_total_products = true; |
|
392 | 442 | | } else { |
  |
393 | | - | $this->show_category_product_count = false; |
| |
| 443 | + | $this->_show_total_products = false; |
  |
394 | 444 | | } |
| |
395 | 445 | | } |
| |
396 | 446 | | |