Quick Search:

View

Revision:

Diff

Diff from 758 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 758 2006-08-23 12:30:07Z hpdl $
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) {
hpdl
383
38       global $osC_Database, $osC_Cache, $osC_Language;
hpdl
1
39
hpdl
368
40       if (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') {
hpdl
1
41         $this->show_category_product_count = true;
42       }
43
44       if ($load_from_database === true) {
hpdl
383
45         if ($osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720)) {
hpdl
1
46           $this->data = $osC_Cache->getCache();
47         } else {
hpdl
368
48           $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');
49           $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
50           $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
hpdl
383
51           $Qcategories->bindInt(':language_id', $osC_Language->getID());
hpdl
1
52           $Qcategories->execute();
53
54           $this->data = array();
55
56           while ($Qcategories->next()) {
hpdl
368
57             $this->data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), 'image' => $Qcategories->value('categories_image'), 'count' => 0);
hpdl
1
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
hpdl
368
81     function reset() {
82       $this->root_category_id = 0;
83       $this->max_level = 0;
84       $this->root_start_string = '';
85       $this->root_end_string = '';
86       $this->parent_start_string = '';
87       $this->parent_end_string = '';
88       $this->parent_group_start_string = '<ul>';
89       $this->parent_group_end_string = '</ul>';
90       $this->child_start_string = '<li>';
91       $this->child_end_string = '</li>';
92       $this->breadcrumb_separator = '_';
93       $this->breadcrumb_usage = true;
94       $this->spacer_string = '';
95       $this->spacer_multiplier = 1;
96       $this->follow_cpath = false;
97       $this->cpath_array = array();
98       $this->cpath_start_string = '';
99       $this->cpath_end_string = '';
100       $this->show_category_product_count = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false;
101       $this->category_product_count_start_string = '&nbsp;(';
102       $this->category_product_count_end_string = ')';
103     }
104
hpdl
1
105     function buildBranch($parent_id, $level = 0) {
106       $result = $this->parent_group_start_string;
107
108       if (isset($this->data[$parent_id])) {
109         foreach ($this->data[$parent_id] as $category_id => $category) {
110           if ($this->breadcrumb_usage == true) {
111             $category_link = $this->buildBreadcrumb($category_id);
112           } else {
113             $category_link = $category_id;
114           }
115
116           $result .= $this->child_start_string;
117
118           if (isset($this->data[$category_id])) {
119             $result .= $this->parent_start_string;
120           }
121
122           if ($level == 0) {
123             $result .= $this->root_start_string;
124           }
125
hpdl
754
126           if ( ($this->follow_cpath === true) && in_array($category_id, $this->cpath_array) ) {
127             $link_title = $this->cpath_start_string . $category['name'] . $this->cpath_end_string;
hpdl
1
128           } else {
hpdl
754
129             $link_title = $category['name'];
hpdl
1
130           }
131
hpdl
754
132           $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link), $link_title);
133
hpdl
1
134           if ($this->show_category_product_count === true) {
135             $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string;
136           }
137
138           if ($level == 0) {
139             $result .= $this->root_end_string;
140           }
141
142           if (isset($this->data[$category_id])) {
143             $result .= $this->parent_end_string;
144           }
145
146           $result .= $this->child_end_string;
147
148           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
149             if ($this->follow_cpath === true) {
150               if (in_array($category_id, $this->cpath_array)) {
151                 $result .= $this->buildBranch($category_id, $level+1);
152               }
153             } else {
154               $result .= $this->buildBranch($category_id, $level+1);
155             }
156           }
157         }
158       }
159
160       $result .= $this->parent_group_end_string;
161
162       return $result;
163     }
164
165     function buildBranchArray($parent_id, $level = 0, $result = '') {
166       if (empty($result)) {
167         $result = array();
168       }
169
170       if (isset($this->data[$parent_id])) {
171         foreach ($this->data[$parent_id] as $category_id => $category) {
172           if ($this->breadcrumb_usage == true) {
173             $category_link = $this->buildBreadcrumb($category_id);
174           } else {
175             $category_link = $category_id;
176           }
177
178           $result[] = array('id' => $category_link,
179                             'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']);
180
181           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
182             if ($this->follow_cpath === true) {
183               if (in_array($category_id, $this->cpath_array)) {
184                 $result = $this->buildBranchArray($category_id, $level+1, $result);
185               }
186             } else {
187               $result = $this->buildBranchArray($category_id, $level+1, $result);
188             }
189           }
190         }
191       }
192
193       return $result;
194     }
195
196     function buildBreadcrumb($category_id, $level = 0) {
197       $breadcrumb = '';
198
199       foreach ($this->data as $parent => $categories) {
200         foreach ($categories as $id => $info) {
201           if ($id == $category_id) {
202             if ($level < 1) {
203               $breadcrumb = $id;
204             } else {
205               $breadcrumb = $id . $this->breadcrumb_separator . $breadcrumb;
206             }
207
208             if ($parent != $this->root_category_id) {
209               $breadcrumb = $this->buildBreadcrumb($parent, $level+1) . $breadcrumb;
210             }
211           }
212         }
213       }
214
215       return $breadcrumb;
216     }
217
218     function buildTree() {
219       return $this->buildBranch($this->root_category_id);
220     }
221
222     function getTree($parent_id = '') {
223       return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id));
224     }
225
hpdl
368
226     function exists($id) {
227       foreach ($this->data as $parent => $categories) {
228         foreach ($categories as $category_id => $info) {
229           if ($id == $category_id) {
230             return true;
231           }
232         }
233       }
234
235       return false;
236     }
237
hpdl
757
238     function getChildren($category_id, &$array) {
239       foreach ($this->data as $parent => $categories) {
240         if ($parent == $category_id) {
241           foreach ($categories as $id => $info) {
242             $array[] = $id;
243             $this->getChildren($id, $array);
244           }
245         }
246       }
247
248       return $array;
249     }
250
hpdl
368
251     function getData($id) {
252       foreach ($this->data as $parent => $categories) {
253         foreach ($categories as $category_id => $info) {
254           if ($id == $category_id) {
255             return array('id' => $id,
256                          'name' => $info['name'],
257                          'parent_id' => $parent,
258                          'image' => $info['image'],
259                          'count' => $info['count']
260                         );
261           }
262         }
263       }
264
265       return false;
266     }
267
hpdl
1
268     function calculateCategoryProductCount() {
hpdl
638
269       global $osC_Database;
270
271       $totals = array();
272
273       $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');
274       $Qtotals->bindTable(':table_products', TABLE_PRODUCTS);
275       $Qtotals->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
276       $Qtotals->bindInt(':products_status', 1);
277       $Qtotals->execute();
278
279       while ($Qtotals->next()) {
280         $totals[$Qtotals->valueInt('categories_id')] = $Qtotals->valueInt('total');
281       }
282
283       $Qtotals->freeResult();
284
hpdl
1
285       foreach ($this->data as $parent => $categories) {
286         foreach ($categories as $id => $info) {
hpdl
638
287           if ($totals[$id] > 0) {
288             $this->data[$parent][$id]['count'] = $totals[$id];
hpdl
1
289
hpdl
638
290             $parent_category = $parent;
291             while ($parent_category != $this->root_category_id) {
292               foreach ($this->data as $parent_parent => $parent_categories) {
293                 foreach ($parent_categories as $parent_category_id => $parent_category_info) {
294                   if ($parent_category_id == $parent_category) {
295                     $this->data[$parent_parent][$parent_category_id]['count'] += $this->data[$parent][$id]['count'];
hpdl
1
296
hpdl
638
297                     $parent_category = $parent_parent;
298                     break 2;
299                   }
hpdl
1
300                 }
301               }
302             }
303           }
304         }
305       }
306
hpdl
638
307       unset($totals);
hpdl
1
308     }
309
hpdl
758
310     function getNumberOfProducts($id) {
311       foreach ($this->data as $parent => $categories) {
312         foreach ($categories as $category_id => $info) {
313           if ($id == $category_id) {
314             return $info['count'];
315           }
316         }
317       }
318
319       return false;
320     }
321
hpdl
1
322     function setRootCategoryID($root_category_id) {
323       $this->root_category_id = $root_category_id;
324     }
325
326     function setMaximumLevel($max_level) {
327       $this->max_level = $max_level;
328     }
329
330     function setRootString($root_start_string, $root_end_string) {
331       $this->root_start_string = $root_start_string;
332       $this->root_end_string = $root_end_string;
333     }
334
335     function setParentString($parent_start_string, $parent_end_string) {
336       $this->parent_start_string = $parent_start_string;
337       $this->parent_end_string = $parent_end_string;
338     }
339
340     function setParentGroupString($parent_group_start_string, $parent_group_end_string) {
341       $this->parent_group_start_string = $parent_group_start_string;
342       $this->parent_group_end_string = $parent_group_end_string;
343     }
344
345     function setChildString($child_start_string, $child_end_string) {
346       $this->child_start_string = $child_start_string;
347       $this->child_end_string = $child_end_string;
348     }
349
350     function setBreadcrumbSeparator($breadcrumb_separator) {
351       $this->breadcrumb_separator = $breadcrumb_separator;
352     }
353
354     function setBreadcrumbUsage($breadcrumb_usage) {
355       if ($breadcrumb_usage === true) {
356         $this->breadcrumb_usage = true;
357       } else {
358         $this->breadcrumb_usage = false;
359       }
360     }
361
362     function setSpacerString($spacer_string, $spacer_multiplier = 2) {
363       $this->spacer_string = $spacer_string;
364       $this->spacer_multiplier = $spacer_multiplier;
365     }
366
367     function setCategoryPath($cpath, $cpath_start_string = '', $cpath_end_string = '') {
368       $this->follow_cpath = true;
369       $this->cpath_array = explode($this->breadcrumb_separator, $cpath);
370       $this->cpath_start_string = $cpath_start_string;
371       $this->cpath_end_string = $cpath_end_string;
372     }
373
374     function setFollowCategoryPath($follow_cpath) {
375       if ($follow_cpath === true) {
376         $this->follow_cpath = true;
377       } else {
378         $this->follow_cpath = false;
379       }
380     }
381
382     function setCategoryPathString($cpath_start_string, $cpath_end_string) {
383       $this->cpath_start_string = $cpath_start_string;
384       $this->cpath_end_string = $cpath_end_string;
385     }
386
387     function setShowCategoryProductCount($show_category_product_count) {
388       if ($show_category_product_count === true) {
389         $this->show_category_product_count = true;
390       } else {
391         $this->show_category_product_count = false;
392       }
393     }
394
395     function setCategoryProductCountString($category_product_count_start_string, $category_product_count_end_string) {
396       $this->category_product_count_start_string = $category_product_count_start_string;
397       $this->category_product_count_end_string = $category_product_count_end_string;
398     }
399   }
400 ?>