Quick Search:

View

Revision:

Diff

Diff from 151 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 151 2005-08-02 14:33:25Z mattice $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2004 osCommerce
9
10   Released under the GNU General Public License
11 */
12
13   class osC_CategoryTree {
14     var $root_category_id = 0,
15         $max_level = 0,
16         $data = array(),
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         $child_start_string = '<li>',
24         $child_end_string = '</li>',
25         $breadcrumb_separator = '_',
26         $breadcrumb_usage = true,
27         $spacer_string = '',
28         $spacer_multiplier = 1,
29         $follow_cpath = false,
30         $cpath_array = array(),
31         $cpath_start_string = '',
32         $cpath_end_string = '',
33         $show_category_product_count = false,
34         $category_product_count_start_string = '&nbsp;(',
35         $category_product_count_end_string = ')';
36
37     function osC_CategoryTree($load_from_database = true) {
38       global $osC_Database, $osC_Session, $osC_Cache;
39
40       if (SHOW_COUNTS == 'true') {
41         $this->show_category_product_count = true;
42       }
43
44       if ($load_from_database === true) {
45         if ($osC_Cache->read('category_tree-' . $osC_Session->value('language'), 720)) {
46           $this->data = $osC_Cache->getCache();
47         } else {
48           $Qcategories = $osC_Database->query('select c.categories_id, cd.categories_name, c.parent_id 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');
49           $Qcategories->bindRaw(':table_categories', TABLE_CATEGORIES);
50           $Qcategories->bindRaw(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
51           $Qcategories->bindInt(':language_id', $osC_Session->value('languages_id'));
52           $Qcategories->execute();
53
54           $this->data = array();
55
56           while ($Qcategories->next()) {
57             $this->data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), 'count' => 0);
58           }
59
60           $Qcategories->freeResult();
61
62           if ($this->show_category_product_count === true) {
63             $this->calculateCategoryProductCount();
64           }
65
66           $osC_Cache->writeBuffer($this->data);
67         }
68       }
69     }
70
71     function setData(&$data_array) {
72       if (is_array($data_array)) {
73         $this->data = array();
74
75         for ($i=0, $n=sizeof($data_array); $i<$n; $i++) {
76           $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']);
77         }
78       }
79     }
80
81     function buildBranch($parent_id, $level = 0) {
82       $result = $this->parent_group_start_string;
83
84       if (isset($this->data[$parent_id])) {
85         foreach ($this->data[$parent_id] as $category_id => $category) {
86           if ($this->breadcrumb_usage == true) {
87             $category_link = $this->buildBreadcrumb($category_id);
88           } else {
89             $category_link = $category_id;
90           }
91
92           $result .= $this->child_start_string;
93
94           if (isset($this->data[$category_id])) {
95             $result .= $this->parent_start_string;
96           }
97
98           if ($level == 0) {
99             $result .= $this->root_start_string;
100           }
101
102           $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link) . '">';
103           if ($this->follow_cpath === true) {
104             if (in_array($category_id, $this->cpath_array)) {
105               $result .= $this->cpath_start_string . $category['name'] . $this->cpath_end_string;
106             } else {
107               $result .= $category['name'];
108             }
109           } else {
110             $result .= $category['name'];
111           }
112           $result .= '</a>';
113
114           if ($this->show_category_product_count === true) {
115             $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string;
116           }
117
118           if ($level == 0) {
119             $result .= $this->root_end_string;
120           }
121
122           if (isset($this->data[$category_id])) {
123             $result .= $this->parent_end_string;
124           }
125
126           $result .= $this->child_end_string;
127
128           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
129             if ($this->follow_cpath === true) {
130               if (in_array($category_id, $this->cpath_array)) {
131                 $result .= $this->buildBranch($category_id, $level+1);
132               }
133             } else {
134               $result .= $this->buildBranch($category_id, $level+1);
135             }
136           }
137         }
138       }
139
140       $result .= $this->parent_group_end_string;
141
142       return $result;
143     }
144
145     function buildBranchArray($parent_id, $level = 0, $result = '') {
146       if (empty($result)) {
147         $result = array();
148       }
149
150       if (isset($this->data[$parent_id])) {
151         foreach ($this->data[$parent_id] as $category_id => $category) {
152           if ($this->breadcrumb_usage == true) {
153             $category_link = $this->buildBreadcrumb($category_id);
154           } else {
155             $category_link = $category_id;
156           }
157
158           $result[] = array('id' => $category_link,
159                             'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']);
160
161           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
162             if ($this->follow_cpath === true) {
163               if (in_array($category_id, $this->cpath_array)) {
164                 $result = $this->buildBranchArray($category_id, $level+1, $result);
165               }
166             } else {
167               $result = $this->buildBranchArray($category_id, $level+1, $result);
168             }
169           }
170         }
171       }
172
173       return $result;
174     }
175
176     function buildBreadcrumb($category_id, $level = 0) {
177       $breadcrumb = '';
178
179       foreach ($this->data as $parent => $categories) {
180         foreach ($categories as $id => $info) {
181           if ($id == $category_id) {
182             if ($level < 1) {
183               $breadcrumb = $id;
184             } else {
185               $breadcrumb = $id . $this->breadcrumb_separator . $breadcrumb;
186             }
187
188             if ($parent != $this->root_category_id) {
189               $breadcrumb = $this->buildBreadcrumb($parent, $level+1) . $breadcrumb;
190             }
191           }
192         }
193       }
194
195       return $breadcrumb;
196     }
197
198     function buildTree() {
199       return $this->buildBranch($this->root_category_id);
200     }
201
202     function getTree($parent_id = '') {
203       return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id));
204     }
205
206     function calculateCategoryProductCount() {
207       foreach ($this->data as $parent => $categories) {
208         foreach ($categories as $id => $info) {
209           $this->data[$parent][$id]['count'] = $this->countCategoryProducts($id);
210
211           $parent_category = $parent;
212           while ($parent_category != $this->root_category_id) {
213             foreach ($this->data as $parent_parent => $parent_categories) {
214               foreach ($parent_categories as $parent_category_id => $parent_category_info) {
215                 if ($parent_category_id == $parent_category) {
216                   $this->data[$parent_parent][$parent_category_id]['count'] += $this->data[$parent][$id]['count'];
217
218                   $parent_category = $parent_parent;
219                   break 2;
220                 }
221               }
222             }
223           }
224         }
225       }
226     }
227
228     function countCategoryProducts($category_id) {
229       global $osC_Database;
230
231       $Qcategories = $osC_Database->query('select count(*) as total from :table_products p, :table_products_to_categories p2c where p2c.categories_id = :categories_id and p2c.products_id = p.products_id and p.products_status = 1');
232       $Qcategories->bindRaw(':table_products', TABLE_PRODUCTS);
233       $Qcategories->bindRaw(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
234       $Qcategories->bindInt(':categories_id', $category_id);
235       $Qcategories->execute();
236
237       $count = $Qcategories->valueInt('total');
238
239       $Qcategories->freeResult();
240
241       return $count;
242     }
243
244     function setRootCategoryID($root_category_id) {
245       $this->root_category_id = $root_category_id;
246     }
247
248     function setMaximumLevel($max_level) {
249       $this->max_level = $max_level;
250     }
251
252     function setRootString($root_start_string, $root_end_string) {
253       $this->root_start_string = $root_start_string;
254       $this->root_end_string = $root_end_string;
255     }
256
257     function setParentString($parent_start_string, $parent_end_string) {
258       $this->parent_start_string = $parent_start_string;
259       $this->parent_end_string = $parent_end_string;
260     }
261
262     function setParentGroupString($parent_group_start_string, $parent_group_end_string) {
263       $this->parent_group_start_string = $parent_group_start_string;
264       $this->parent_group_end_string = $parent_group_end_string;
265     }
266
267     function setChildString($child_start_string, $child_end_string) {
268       $this->child_start_string = $child_start_string;
269       $this->child_end_string = $child_end_string;
270     }
271
272     function setBreadcrumbSeparator($breadcrumb_separator) {
273       $this->breadcrumb_separator = $breadcrumb_separator;
274     }
275
276     function setBreadcrumbUsage($breadcrumb_usage) {
277       if ($breadcrumb_usage === true) {
278         $this->breadcrumb_usage = true;
279       } else {
280         $this->breadcrumb_usage = false;
281       }
282     }
283
284     function setSpacerString($spacer_string, $spacer_multiplier = 2) {
285       $this->spacer_string = $spacer_string;
286       $this->spacer_multiplier = $spacer_multiplier;
287     }
288
289     function setCategoryPath($cpath, $cpath_start_string = '', $cpath_end_string = '') {
290       $this->follow_cpath = true;
291       $this->cpath_array = explode($this->breadcrumb_separator, $cpath);
292       $this->cpath_start_string = $cpath_start_string;
293       $this->cpath_end_string = $cpath_end_string;
294     }
295
296     function setFollowCategoryPath($follow_cpath) {
297       if ($follow_cpath === true) {
298         $this->follow_cpath = true;
299       } else {
300         $this->follow_cpath = false;
301       }
302     }
303
304     function setCategoryPathString($cpath_start_string, $cpath_end_string) {
305       $this->cpath_start_string = $cpath_start_string;
306       $this->cpath_end_string = $cpath_end_string;
307     }
308
309     function setShowCategoryProductCount($show_category_product_count) {
310       if ($show_category_product_count === true) {
311         $this->show_category_product_count = true;
312       } else {
313         $this->show_category_product_count = false;
314       }
315     }
316
317     function setCategoryProductCountString($category_product_count_start_string, $category_product_count_end_string) {
318       $this->category_product_count_start_string = $category_product_count_start_string;
319       $this->category_product_count_end_string = $category_product_count_end_string;
320     }
321   }
322 ?>