Quick Search:

View

Revision:

Diff

Diff from 686 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 686 2006-08-14 22:33:22Z 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
377
38       global $osC_Database, $osC_Cache, $osC_Language;
hpdl
1
39
hpdl
345
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
377
45         if ($osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720)) {
hpdl
1
46           $this->data = $osC_Cache->getCache();
47         } else {
hpdl
299
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
377
51           $Qcategories->bindInt(':language_id', $osC_Language->getID());
hpdl
1
52           $Qcategories->execute();
53
54           $this->data = array();
55
56           while ($Qcategories->next()) {
hpdl
299
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
299
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 = '';
hpdl
345
100       $this->show_category_product_count = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false;
hpdl
299
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
686
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
686
129             $link_title = $category['name'];
hpdl
1
130           }
131
hpdl
686
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
299
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
238     function getData($id) {
239       foreach ($this->data as $parent => $categories) {
240         foreach ($categories as $category_id => $info) {
241           if ($id == $category_id) {
242             return array('id' => $id,
243                          'name' => $info['name'],
244                          'parent_id' => $parent,
245                          'image' => $info['image'],
246                          'count' => $info['count']
247                         );
248           }
249         }
250       }
251
252       return false;
253     }
254
hpdl
1
255     function calculateCategoryProductCount() {
hpdl
621
256       global $osC_Database;
257
258       $totals = array();
259
260       $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');
261       $Qtotals->bindTable(':table_products', TABLE_PRODUCTS);
262       $Qtotals->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
263       $Qtotals->bindInt(':products_status', 1);
264       $Qtotals->execute();
265
266       while ($Qtotals->next()) {
267         $totals[$Qtotals->valueInt('categories_id')] = $Qtotals->valueInt('total');
268       }
269
270       $Qtotals->freeResult();
271
hpdl
1
272       foreach ($this->data as $parent => $categories) {
273         foreach ($categories as $id => $info) {
hpdl
621
274           if ($totals[$id] > 0) {
275             $this->data[$parent][$id]['count'] = $totals[$id];
hpdl
1
276
hpdl
621
277             $parent_category = $parent;
278             while ($parent_category != $this->root_category_id) {
279               foreach ($this->data as $parent_parent => $parent_categories) {
280                 foreach ($parent_categories as $parent_category_id => $parent_category_info) {
281                   if ($parent_category_id == $parent_category) {
282                     $this->data[$parent_parent][$parent_category_id]['count'] += $this->data[$parent][$id]['count'];
hpdl
1
283
hpdl
621
284                     $parent_category = $parent_parent;
285                     break 2;
286                   }
hpdl
1
287                 }
288               }
289             }
290           }
291         }
292       }
293
hpdl
621
294       unset($totals);
hpdl
1
295     }
296
297     function setRootCategoryID($root_category_id) {
298       $this->root_category_id = $root_category_id;
299     }
300
301     function setMaximumLevel($max_level) {
302       $this->max_level = $max_level;
303     }
304
305     function setRootString($root_start_string, $root_end_string) {
306       $this->root_start_string = $root_start_string;
307       $this->root_end_string = $root_end_string;
308     }
309
310     function setParentString($parent_start_string, $parent_end_string) {
311       $this->parent_start_string = $parent_start_string;
312       $this->parent_end_string = $parent_end_string;
313     }
314
315     function setParentGroupString($parent_group_start_string, $parent_group_end_string) {
316       $this->parent_group_start_string = $parent_group_start_string;
317       $this->parent_group_end_string = $parent_group_end_string;
318     }
319
320     function setChildString($child_start_string, $child_end_string) {
321       $this->child_start_string = $child_start_string;
322       $this->child_end_string = $child_end_string;
323     }
324
325     function setBreadcrumbSeparator($breadcrumb_separator) {
326       $this->breadcrumb_separator = $breadcrumb_separator;
327     }
328
329     function setBreadcrumbUsage($breadcrumb_usage) {
330       if ($breadcrumb_usage === true) {
331         $this->breadcrumb_usage = true;
332       } else {
333         $this->breadcrumb_usage = false;
334       }
335     }
336
337     function setSpacerString($spacer_string, $spacer_multiplier = 2) {
338       $this->spacer_string = $spacer_string;
339       $this->spacer_multiplier = $spacer_multiplier;
340     }
341
342     function setCategoryPath($cpath, $cpath_start_string = '', $cpath_end_string = '') {
343       $this->follow_cpath = true;
344       $this->cpath_array = explode($this->breadcrumb_separator, $cpath);
345       $this->cpath_start_string = $cpath_start_string;
346       $this->cpath_end_string = $cpath_end_string;
347     }
348
349     function setFollowCategoryPath($follow_cpath) {
350       if ($follow_cpath === true) {
351         $this->follow_cpath = true;
352       } else {
353         $this->follow_cpath = false;
354       }
355     }
356
357     function setCategoryPathString($cpath_start_string, $cpath_end_string) {
358       $this->cpath_start_string = $cpath_start_string;
359       $this->cpath_end_string = $cpath_end_string;
360     }
361
362     function setShowCategoryProductCount($show_category_product_count) {
363       if ($show_category_product_count === true) {
364         $this->show_category_product_count = true;
365       } else {
366         $this->show_category_product_count = false;
367       }
368     }
369
370     function setCategoryProductCountString($category_product_count_start_string, $category_product_count_end_string) {
371       $this->category_product_count_start_string = $category_product_count_start_string;
372       $this->category_product_count_end_string = $category_product_count_end_string;
373     }
374   }
375 ?>