Quick Search:

View

Revision:

Diff

Diff from 1862 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/trunk/oscommerce/includes/classes/category_tree.php

Annotated File View

hpdl
1
1 <?php
2 /*
mattice
151
3   $Id: category_tree.php 1862 2009-03-06 23:34:07Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1860
8   Copyright (c) 2007 osCommerce
hpdl
1
9
hpdl
1498
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
15   class osC_CategoryTree {
hpdl
1862
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
hpdl
1
35     var $root_category_id = 0,
36         $max_level = 0,
37         $root_start_string = '',
38         $root_end_string = '',
39         $parent_start_string = '',
40         $parent_end_string = '',
41         $parent_group_start_string = '<ul>',
42         $parent_group_end_string = '</ul>',
43         $child_start_string = '<li>',
44         $child_end_string = '</li>',
45         $breadcrumb_separator = '_',
46         $breadcrumb_usage = true,
47         $spacer_string = '',
48         $spacer_multiplier = 1,
49         $follow_cpath = false,
50         $cpath_array = array(),
51         $cpath_start_string = '',
52         $cpath_end_string = '',
53         $category_product_count_start_string = '&nbsp;(',
54         $category_product_count_end_string = ')';
55
hpdl
1862
56 /**
57  * Constructor; load the category structure relationship data from the database
58  *
59  * @access public
60  */
61
62     public function __construct() {
hpdl
383
63       global $osC_Database, $osC_Cache, $osC_Language;
hpdl
1
64
hpdl
1862
65       if ( SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1' ) {
66         $this->_show_total_products = true;
hpdl
1
67       }
68
hpdl
1862
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();
hpdl
1
77
hpdl
1862
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);
82         }
hpdl
1
83
hpdl
1862
84         if ( $this->_show_total_products === true ) {
85           $this->_calculateProductTotals();
hpdl
1
86         }
87
hpdl
1862
88         $osC_Cache->write($this->_data);
hpdl
1
89       }
90     }
91
hpdl
368
92     function reset() {
93       $this->root_category_id = 0;
94       $this->max_level = 0;
95       $this->root_start_string = '';
96       $this->root_end_string = '';
97       $this->parent_start_string = '';
98       $this->parent_end_string = '';
99       $this->parent_group_start_string = '<ul>';
100       $this->parent_group_end_string = '</ul>';
101       $this->child_start_string = '<li>';
102       $this->child_end_string = '</li>';
103       $this->breadcrumb_separator = '_';
104       $this->breadcrumb_usage = true;
105       $this->spacer_string = '';
106       $this->spacer_multiplier = 1;
107       $this->follow_cpath = false;
108       $this->cpath_array = array();
109       $this->cpath_start_string = '';
110       $this->cpath_end_string = '';
hpdl
1862
111       $this->_show_total_products = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false;
hpdl
368
112       $this->category_product_count_start_string = '&nbsp;(';
113       $this->category_product_count_end_string = ')';
114     }
115
hpdl
1862
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) {
hpdl
1
126       $result = $this->parent_group_start_string;
127
hpdl
1862
128       if ( isset($this->_data[$parent_id]) ) {
129         foreach ( $this->_data[$parent_id] as $category_id => $category ) {
130           if ( $this->breadcrumb_usage === true ) {
hpdl
1
131             $category_link = $this->buildBreadcrumb($category_id);
132           } else {
133             $category_link = $category_id;
134           }
135
136           $result .= $this->child_start_string;
137
hpdl
1862
138           if ( isset($this->_data[$category_id]) ) {
hpdl
1
139             $result .= $this->parent_start_string;
140           }
141
hpdl
1862
142           if ( $level === 0 ) {
hpdl
1
143             $result .= $this->root_start_string;
144           }
145
hpdl
754
146           if ( ($this->follow_cpath === true) && in_array($category_id, $this->cpath_array) ) {
147             $link_title = $this->cpath_start_string . $category['name'] . $this->cpath_end_string;
hpdl
1
148           } else {
hpdl
754
149             $link_title = $category['name'];
hpdl
1
150           }
151
hpdl
754
152           $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link), $link_title);
153
hpdl
1862
154           if ( $this->_show_total_products === true ) {
hpdl
1
155             $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string;
156           }
157
hpdl
1862
158           if ( $level === 0 ) {
hpdl
1
159             $result .= $this->root_end_string;
160           }
161
hpdl
1862
162           if ( isset($this->_data[$category_id]) ) {
hpdl
1
163             $result .= $this->parent_end_string;
164           }
165
166           $result .= $this->child_end_string;
167
hpdl
1862
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);
hpdl
1
172               }
173             } else {
hpdl
1862
174               $result .= $this->_buildBranch($category_id, $level+1);
hpdl
1
175             }
176           }
177         }
178       }
179
180       $result .= $this->parent_group_end_string;
181
182       return $result;
183     }
184
185     function buildBranchArray($parent_id, $level = 0, $result = '') {
186       if (empty($result)) {
187         $result = array();
188       }
189
hpdl
1862
190       if (isset($this->_data[$parent_id])) {
191         foreach ($this->_data[$parent_id] as $category_id => $category) {
hpdl
1
192           if ($this->breadcrumb_usage == true) {
193             $category_link = $this->buildBreadcrumb($category_id);
194           } else {
195             $category_link = $category_id;
196           }
197
198           $result[] = array('id' => $category_link,
199                             'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']);
200
hpdl
1862
201           if (isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
hpdl
1
202             if ($this->follow_cpath === true) {
203               if (in_array($category_id, $this->cpath_array)) {
204                 $result = $this->buildBranchArray($category_id, $level+1, $result);
205               }
206             } else {
207               $result = $this->buildBranchArray($category_id, $level+1, $result);
208             }
209           }
210         }
211       }
212
213       return $result;
214     }
215
216     function buildBreadcrumb($category_id, $level = 0) {
217       $breadcrumb = '';
218
hpdl
1862
219       foreach ($this->_data as $parent => $categories) {
hpdl
1
220         foreach ($categories as $id => $info) {
221           if ($id == $category_id) {
222             if ($level < 1) {
223               $breadcrumb = $id;
224             } else {
225               $breadcrumb = $id . $this->breadcrumb_separator . $breadcrumb;
226             }
227
228             if ($parent != $this->root_category_id) {
229               $breadcrumb = $this->buildBreadcrumb($parent, $level+1) . $breadcrumb;
230             }
231           }
232         }
233       }
234
235       return $breadcrumb;
236     }
237
hpdl
1862
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);
hpdl
1
247     }
248
hpdl
1862
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 = '') {
hpdl
1
265       return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id));
266     }
267
hpdl
368
268     function exists($id) {
hpdl
1862
269       foreach ($this->_data as $parent => $categories) {
hpdl
368
270         foreach ($categories as $category_id => $info) {
271           if ($id == $category_id) {
272             return true;
273           }
274         }
275       }
276
277       return false;
278     }
279
hpdl
757
280     function getChildren($category_id, &$array) {
hpdl
1862
281       foreach ($this->_data as $parent => $categories) {
hpdl
757
282         if ($parent == $category_id) {
283           foreach ($categories as $id => $info) {
284             $array[] = $id;
285             $this->getChildren($id, $array);
286           }
287         }
288       }
289
290       return $array;
291     }
292
hpdl
368
293     function getData($id) {
hpdl
1862
294       foreach ($this->_data as $parent => $categories) {
hpdl
368
295         foreach ($categories as $category_id => $info) {
296           if ($id == $category_id) {
297             return array('id' => $id,
298                          'name' => $info['name'],
299                          'parent_id' => $parent,
300                          'image' => $info['image'],
301                          'count' => $info['count']
302                         );
303           }
304         }
305       }
306
307       return false;
308     }
309
hpdl
1862
310 /**
311  * Calculate the number of products in each category
312  *
313  * @access protected
314  */
315
316     protected function _calculateProductTotals($filter_active = true) {
hpdl
638
317       global $osC_Database;
318
319       $totals = array();
320
hpdl
1862
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');
hpdl
638
329       $Qtotals->bindTable(':table_products', TABLE_PRODUCTS);
330       $Qtotals->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
331       $Qtotals->execute();
332
hpdl
1862
333       while ( $Qtotals->next() ) {
hpdl
638
334         $totals[$Qtotals->valueInt('categories_id')] = $Qtotals->valueInt('total');
335       }
336
hpdl
1862
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];
hpdl
638
341
342             $parent_category = $parent;
hpdl
1
343
hpdl
1862
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
hpdl
638
350                     $parent_category = $parent_parent;
hpdl
1862
351
hpdl
638
352                     break 2;
353                   }
hpdl
1
354                 }
355               }
356             }
357           }
358         }
359       }
360     }
361
hpdl
758
362     function getNumberOfProducts($id) {
hpdl
1862
363       foreach ($this->_data as $parent => $categories) {
hpdl
758
364         foreach ($categories as $category_id => $info) {
365           if ($id == $category_id) {
366             return $info['count'];
367           }
368         }
369       }
370
371       return false;
372     }
373
hpdl
1
374     function setRootCategoryID($root_category_id) {
375       $this->root_category_id = $root_category_id;
376     }
377
378     function setMaximumLevel($max_level) {
379       $this->max_level = $max_level;
380     }
381
382     function setRootString($root_start_string, $root_end_string) {
383       $this->root_start_string = $root_start_string;
384       $this->root_end_string = $root_end_string;
385     }
386
387     function setParentString($parent_start_string, $parent_end_string) {
388       $this->parent_start_string = $parent_start_string;
389       $this->parent_end_string = $parent_end_string;
390     }
391
392     function setParentGroupString($parent_group_start_string, $parent_group_end_string) {
393       $this->parent_group_start_string = $parent_group_start_string;
394       $this->parent_group_end_string = $parent_group_end_string;
395     }
396
397     function setChildString($child_start_string, $child_end_string) {
398       $this->child_start_string = $child_start_string;
399       $this->child_end_string = $child_end_string;
400     }
401
402     function setBreadcrumbSeparator($breadcrumb_separator) {
403       $this->breadcrumb_separator = $breadcrumb_separator;
404     }
405
406     function setBreadcrumbUsage($breadcrumb_usage) {
407       if ($breadcrumb_usage === true) {
408         $this->breadcrumb_usage = true;
409       } else {
410         $this->breadcrumb_usage = false;
411       }
412     }
413
414     function setSpacerString($spacer_string, $spacer_multiplier = 2) {
415       $this->spacer_string = $spacer_string;
416       $this->spacer_multiplier = $spacer_multiplier;
417     }
418
419     function setCategoryPath($cpath, $cpath_start_string = '', $cpath_end_string = '') {
420       $this->follow_cpath = true;
421       $this->cpath_array = explode($this->breadcrumb_separator, $cpath);
422       $this->cpath_start_string = $cpath_start_string;
423       $this->cpath_end_string = $cpath_end_string;
424     }
425
426     function setFollowCategoryPath($follow_cpath) {
427       if ($follow_cpath === true) {
428         $this->follow_cpath = true;
429       } else {
430         $this->follow_cpath = false;
431       }
432     }
433
434     function setCategoryPathString($cpath_start_string, $cpath_end_string) {
435       $this->cpath_start_string = $cpath_start_string;
436       $this->cpath_end_string = $cpath_end_string;
437     }
438
439     function setShowCategoryProductCount($show_category_product_count) {
440       if ($show_category_product_count === true) {
hpdl
1862
441         $this->_show_total_products = true;
hpdl
1
442       } else {
hpdl
1862
443         $this->_show_total_products = false;
hpdl
1
444       }
445     }
446
447     function setCategoryProductCountString($category_product_count_start_string, $category_product_count_end_string) {
448       $this->category_product_count_start_string = $category_product_count_start_string;
449       $this->category_product_count_end_string = $category_product_count_end_string;
450     }
451   }
452 ?>