Quick Search:

View

Revision:

Diff

Diff from 1672 to:

Annotations

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

Annotated File View

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