Quick Search:

View

Revision:

Diff

Diff from 383 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 383 2006-01-09 16:35:46Z 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
126           $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link) . '">';
127           if ($this->follow_cpath === true) {
128             if (in_array($category_id, $this->cpath_array)) {
129               $result .= $this->cpath_start_string . $category['name'] . $this->cpath_end_string;
130             } else {
131               $result .= $category['name'];
132             }
133           } else {
134             $result .= $category['name'];
135           }
136           $result .= '</a>';
137
138           if ($this->show_category_product_count === true) {
139             $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string;
140           }
141
142           if ($level == 0) {
143             $result .= $this->root_end_string;
144           }
145
146           if (isset($this->data[$category_id])) {
147             $result .= $this->parent_end_string;
148           }
149
150           $result .= $this->child_end_string;
151
152           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
153             if ($this->follow_cpath === true) {
154               if (in_array($category_id, $this->cpath_array)) {
155                 $result .= $this->buildBranch($category_id, $level+1);
156               }
157             } else {
158               $result .= $this->buildBranch($category_id, $level+1);
159             }
160           }
161         }
162       }
163
164       $result .= $this->parent_group_end_string;
165
166       return $result;
167     }
168
169     function buildBranchArray($parent_id, $level = 0, $result = '') {
170       if (empty($result)) {
171         $result = array();
172       }
173
174       if (isset($this->data[$parent_id])) {
175         foreach ($this->data[$parent_id] as $category_id => $category) {
176           if ($this->breadcrumb_usage == true) {
177             $category_link = $this->buildBreadcrumb($category_id);
178           } else {
179             $category_link = $category_id;
180           }
181
182           $result[] = array('id' => $category_link,
183                             'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']);
184
185           if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
186             if ($this->follow_cpath === true) {
187               if (in_array($category_id, $this->cpath_array)) {
188                 $result = $this->buildBranchArray($category_id, $level+1, $result);
189               }
190             } else {
191               $result = $this->buildBranchArray($category_id, $level+1, $result);
192             }
193           }
194         }
195       }
196
197       return $result;
198     }
199
200     function buildBreadcrumb($category_id, $level = 0) {
201       $breadcrumb = '';
202
203       foreach ($this->data as $parent => $categories) {
204         foreach ($categories as $id => $info) {
205           if ($id == $category_id) {
206             if ($level < 1) {
207               $breadcrumb = $id;
208             } else {
209               $breadcrumb = $id . $this->breadcrumb_separator . $breadcrumb;
210             }
211
212             if ($parent != $this->root_category_id) {
213               $breadcrumb = $this->buildBreadcrumb($parent, $level+1) . $breadcrumb;
214             }
215           }
216         }
217       }
218
219       return $breadcrumb;
220     }
221
222     function buildTree() {
223       return $this->buildBranch($this->root_category_id);
224     }
225
226     function getTree($parent_id = '') {
227       return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id));
228     }
229
hpdl
368
230     function exists($id) {
231       foreach ($this->data as $parent => $categories) {
232         foreach ($categories as $category_id => $info) {
233           if ($id == $category_id) {
234             return true;
235           }
236         }
237       }
238
239       return false;
240     }
241
242     function getData($id) {
243       foreach ($this->data as $parent => $categories) {
244         foreach ($categories as $category_id => $info) {
245           if ($id == $category_id) {
246             return array('id' => $id,
247                          'name' => $info['name'],
248                          'parent_id' => $parent,
249                          'image' => $info['image'],
250                          'count' => $info['count']
251                         );
252           }
253         }
254       }
255
256       return false;
257     }
258
hpdl
1
259     function calculateCategoryProductCount() {
260       foreach ($this->data as $parent => $categories) {
261         foreach ($categories as $id => $info) {
262           $this->data[$parent][$id]['count'] = $this->countCategoryProducts($id);
263
264           $parent_category = $parent;
265           while ($parent_category != $this->root_category_id) {
266             foreach ($this->data as $parent_parent => $parent_categories) {
267               foreach ($parent_categories as $parent_category_id => $parent_category_info) {
268                 if ($parent_category_id == $parent_category) {
269                   $this->data[$parent_parent][$parent_category_id]['count'] += $this->data[$parent][$id]['count'];
270
271                   $parent_category = $parent_parent;
272                   break 2;
273                 }
274               }
275             }
276           }
277         }
278       }
279     }
280
281     function countCategoryProducts($category_id) {
282       global $osC_Database;
283
284       $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');
285       $Qcategories->bindRaw(':table_products', TABLE_PRODUCTS);
286       $Qcategories->bindRaw(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
287       $Qcategories->bindInt(':categories_id', $category_id);
288       $Qcategories->execute();
289
290       $count = $Qcategories->valueInt('total');
291
292       $Qcategories->freeResult();
293
294       return $count;
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 ?>