Quick Search:

Mode

Context

Displaying 3 lines of context. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace:

Diff

1672
 
1675
 
1675
 
category_tree.php
_> 11 <?php
  22 /*
<> 3 -  $Id: category_tree.php 1672 2007-07-26 22:07:13Z hpdl $
   3+  $Id: category_tree.php 1675 2007-08-20 23:02:47Z hpdl $
44 
  55   osCommerce, Open Source E-Commerce Solutions
  66   http://www.oscommerce.com
     
 !
1313 */
  1414 
  1515   class osC_CategoryTree {
<>  16+
   17+/**
   18+ * Flag to control if the total number of products in a category should be calculated
   19+ *
   20+ * @var boolean
   21+ * @access protected
   22+ */
   23+
   24+    protected $_show_total_products = false;
   25+
   26+/**
   27+ * Array containing the category structure relationship data
   28+ *
   29+ * @var array
   30+ * @access protected
   31+ */
   32+
   33+    protected $_data = array();
   34+
1635     var $root_category_id = 0,
  1736         $max_level = 0,
<> 18 -        $data = array(),
1937         $root_start_string = '',
  2038         $root_end_string = '',
  2139         $parent_start_string = '',
     
 !
3250         $cpath_array = array(),
  3351         $cpath_start_string = '',
  3452         $cpath_end_string = '',
<> 35 -        $show_category_product_count = false,
3653         $category_product_count_start_string = '&nbsp;(',
  3754         $category_product_count_end_string = ')';
  3855 
<> 39 -    function __construct($load_from_database = true) {
   56+/**
   57+ * Constructor; load the category structure relationship data from the database
   58+ *
   59+ * @access public
   60+ */
   61+
   62+    public function __construct() {
4063       global $osC_Database, $osC_Cache, $osC_Language;
  4164 
<> 42 -      if (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') {
  43 -        $this->show_category_product_count = true;
   65+      if ( SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1' ) {
   66+        $this->_show_total_products = true;
4467       }
  4568 
<> 46 -      if ($load_from_database === true) {
  47 -        if ($osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720)) {
  48 -          $this->data = $osC_Cache->getCache();
  49 -        } else {
  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);
  53 -          $Qcategories->bindInt(':language_id', $osC_Language->getID());
  54 -          $Qcategories->execute();
   69+      if ( $osC_Cache->read('category_tree-' . $osC_Language->getCode(), 720) ) {
   70+        $this->_data = $osC_Cache->getCache();
   71+      } else {
   72+        $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');
   73+        $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
   74+        $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
   75+        $Qcategories->bindInt(':language_id', $osC_Language->getID());
   76+        $Qcategories->execute();
5577 
<> 56 -          $this->data = array();
  57 -
  58 -          while ($Qcategories->next()) {
  59 -            $this->data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), 'image' => $Qcategories->value('categories_image'), 'count' => 0);
  60 -          }
  61 -
  62 -          $Qcategories->freeResult();
  63 -
  64 -          if ($this->show_category_product_count === true) {
  65 -            $this->calculateCategoryProductCount();
  66 -          }
  67 -
  68 -          $osC_Cache->write($this->data);
   78+        while ( $Qcategories->next() ) {
   79+          $this->_data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'),
   80+                                                                                                             'image' => $Qcategories->value('categories_image'),
   81+                                                                                                             'count' => 0);
6982         }
<> 70 -      }
  71 -    }
7283 
<> 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']);
   84+        if ( $this->_show_total_products === true ) {
   85+          $this->_calculateProductTotals();
7986         }
<>  87+
   88+        $osC_Cache->write($this->_data);
8089       }
  8190     }
  8291 
     
 !
99108       $this->cpath_array = array();
  100109       $this->cpath_start_string = '';
  101110       $this->cpath_end_string = '';
<> 102 -      $this->show_category_product_count = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false;
   111+      $this->_show_total_products = (SERVICES_CATEGORY_PATH_CALCULATE_PRODUCT_COUNT == '1') ? true : false;
103112       $this->category_product_count_start_string = '&nbsp;(';
  104113       $this->category_product_count_end_string = ')';
  105114     }
  106115 
<> 107 -    function buildBranch($parent_id, $level = 0) {
   116+/**
   117+ * Return a formated string representation of a category and its subcategories
   118+ *
   119+ * @param int $parent_id The parent ID of the category to build from
   120+ * @param int $level Internal flag to note the depth of the category structure
   121+ * @access protected
   122+ * @return string
   123+ */
   124+
   125+    protected function _buildBranch($parent_id, $level = 0) {
108126       $result = $this->parent_group_start_string;
  109127 
<> 110 -      if (isset($this->data[$parent_id])) {
  111 -        foreach ($this->data[$parent_id] as $category_id => $category) {
  112 -          if ($this->breadcrumb_usage == true) {
   128+      if ( isset($this->_data[$parent_id]) ) {
   129+        foreach ( $this->_data[$parent_id] as $category_id => $category ) {
   130+          if ( $this->breadcrumb_usage === true ) {
113131             $category_link = $this->buildBreadcrumb($category_id);
  114132           } else {
  115133             $category_link = $category_id;
  116134           }
  117135 
  118136           $result .= $this->child_start_string;
  119137 
<> 120 -          if (isset($this->data[$category_id])) {
   138+          if ( isset($this->_data[$category_id]) ) {
121139             $result .= $this->parent_start_string;
  122140           }
  123141 
<> 124 -          if ($level == 0) {
   142+          if ( $level === 0 ) {
125143             $result .= $this->root_start_string;
  126144           }
  127145 
     
 !
133151 
  134152           $result .= str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link), $link_title);
  135153 
<> 136 -          if ($this->show_category_product_count === true) {
   154+          if ( $this->_show_total_products === true ) {
137155             $result .= $this->category_product_count_start_string . $category['count'] . $this->category_product_count_end_string;
  138156           }
  139157 
<> 140 -          if ($level == 0) {
   158+          if ( $level === 0 ) {
141159             $result .= $this->root_end_string;
  142160           }
  143161 
<> 144 -          if (isset($this->data[$category_id])) {
   162+          if ( isset($this->_data[$category_id]) ) {
145163             $result .= $this->parent_end_string;
  146164           }
  147165 
  148166           $result .= $this->child_end_string;
  149167 
<> 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);
   168+          if ( isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1)) ) {
   169+            if ( $this->follow_cpath === true ) {
   170+              if ( in_array($category_id, $this->cpath_array) ) {
   171+                $result .= $this->_buildBranch($category_id, $level+1);
154172               }
  155173             } else {
<> 156 -              $result .= $this->buildBranch($category_id, $level+1);
   174+              $result .= $this->_buildBranch($category_id, $level+1);
157175             }
  158176           }
  159177         }
     
 !
169187         $result = array();
  170188       }
  171189 
<> 172 -      if (isset($this->data[$parent_id])) {
  173 -        foreach ($this->data[$parent_id] as $category_id => $category) {
   190+      if (isset($this->_data[$parent_id])) {
   191+        foreach ($this->_data[$parent_id] as $category_id => $category) {
174192           if ($this->breadcrumb_usage == true) {
  175193             $category_link = $this->buildBreadcrumb($category_id);
  176194           } else {
     
 !
180198           $result[] = array('id' => $category_link,
  181199                             'title' => str_repeat($this->spacer_string, $this->spacer_multiplier * $level) . $category['name']);
  182200 
<> 183 -          if (isset($this->data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
   201+          if (isset($this->_data[$category_id]) && (($this->max_level == '0') || ($this->max_level > $level+1))) {
184202             if ($this->follow_cpath === true) {
  185203               if (in_array($category_id, $this->cpath_array)) {
  186204                 $result = $this->buildBranchArray($category_id, $level+1, $result);
     
 !
198216     function buildBreadcrumb($category_id, $level = 0) {
  199217       $breadcrumb = '';
  200218 
<> 201 -      foreach ($this->data as $parent => $categories) {
   219+      foreach ($this->_data as $parent => $categories) {
202220         foreach ($categories as $id => $info) {
  203221           if ($id == $category_id) {
  204222             if ($level < 1) {
     
 !
217235       return $breadcrumb;
  218236     }
  219237 
<> 220 -    function buildTree() {
  221 -      return $this->buildBranch($this->root_category_id);
   238+/**
   239+ * Return a formated string representation of the category structure relationship data
   240+ *
   241+ * @access public
   242+ * @return string
   243+ */
   244+
   245+    public function getTree() {
   246+      return $this->_buildBranch($this->root_category_id);
222247     }
  223248 
<> 224 -    function getTree($parent_id = '') {
   249+/**
   250+ * Magic function; return a formated string representation of the category structure relationship data
   251+ *
   252+ * This is used when echoing the class object, eg:
   253+ *
   254+ * echo $osC_CategoryTree;
   255+ *
   256+ * @access public
   257+ * @return string
   258+ */
   259+
   260+    public function __toString() {
   261+      return $this->getTree();
   262+    }
   263+
   264+    function getArray($parent_id = '') {
225265       return $this->buildBranchArray((empty($parent_id) ? $this->root_category_id : $parent_id));
  226266     }
  227267 
  228268     function exists($id) {
<> 229 -      foreach ($this->data as $parent => $categories) {
   269+      foreach ($this->_data as $parent => $categories) {
230270         foreach ($categories as $category_id => $info) {
  231271           if ($id == $category_id) {
  232272             return true;
     
 !
238278     }
  239279 
  240280     function getChildren($category_id, &$array) {
<> 241 -      foreach ($this->data as $parent => $categories) {
   281+      foreach ($this->_data as $parent => $categories) {
242282         if ($parent == $category_id) {
  243283           foreach ($categories as $id => $info) {
  244284             $array[] = $id;
     
 !
251291     }
  252292 
  253293     function getData($id) {
<> 254 -      foreach ($this->data as $parent => $categories) {
   294+      foreach ($this->_data as $parent => $categories) {
255295         foreach ($categories as $category_id => $info) {
  256296           if ($id == $category_id) {
  257297             return array('id' => $id,
     
 !
267307       return false;
  268308     }
  269309 
<> 270 -    function calculateCategoryProductCount() {
   310+/**
   311+ * Calculate the number of products in each category
   312+ *
   313+ * @access protected
   314+ */
   315+
   316+    protected function _calculateProductTotals($filter_active = true) {
271317       global $osC_Database;
  272318 
  273319       $totals = array();
  274320 
<> 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');
   321+      $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');
   322+
   323+      if ( $filter_active === true ) {
   324+        $Qtotals->appendQuery('and p.products_status = :products_status');
   325+        $Qtotals->bindInt(':products_status', 1);
   326+      }
   327+
   328+      $Qtotals->appendQuery('group by p2c.categories_id');
276329       $Qtotals->bindTable(':table_products', TABLE_PRODUCTS);
  277330       $Qtotals->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
<> 278 -      $Qtotals->bindInt(':products_status', 1);
279331       $Qtotals->execute();
  280332 
<> 281 -      while ($Qtotals->next()) {
   333+      while ( $Qtotals->next() ) {
282334         $totals[$Qtotals->valueInt('categories_id')] = $Qtotals->valueInt('total');
  283335       }
  284336 
<> 285 -      $Qtotals->freeResult();
   337+      foreach ( $this->_data as $parent => $categories ) {
   338+        foreach ( $categories as $id => $info ) {
   339+          if ( isset($totals[$id]) && ($totals[$id] > 0) ) {
   340+            $this->_data[$parent][$id]['count'] = $totals[$id];
286341 
<> 287 -      foreach ($this->data as $parent => $categories) {
  288 -        foreach ($categories as $id => $info) {
  289 -          if (isset($totals[$id]) && ($totals[$id] > 0)) {
  290 -            $this->data[$parent][$id]['count'] = $totals[$id];
  291 -
292342             $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'];
298343 
<>  344+            while ( $parent_category != $this->root_category_id ) {
   345+              foreach ( $this->_data as $parent_parent => $parent_categories ) {
   346+                foreach ( $parent_categories as $parent_category_id => $parent_category_info ) {
   347+                  if ( $parent_category_id == $parent_category ) {
   348+                    $this->_data[$parent_parent][$parent_category_id]['count'] += $this->_data[$parent][$id]['count'];
   349+
299350                     $parent_category = $parent_parent;
<>  351+
300352                     break 2;
  301353                   }
  302354                 }
     
 !
305357           }
  306358         }
  307359       }
<> 308 -
  309 -      unset($totals);
310360     }
  311361 
  312362     function getNumberOfProducts($id) {
<> 313 -      foreach ($this->data as $parent => $categories) {
   363+      foreach ($this->_data as $parent => $categories) {
314364         foreach ($categories as $category_id => $info) {
  315365           if ($id == $category_id) {
  316366             return $info['count'];
     
 !
388438 
  389439     function setShowCategoryProductCount($show_category_product_count) {
  390440       if ($show_category_product_count === true) {
<> 391 -        $this->show_category_product_count = true;
   441+        $this->_show_total_products = true;
392442       } else {
<> 393 -        $this->show_category_product_count = false;
   443+        $this->_show_total_products = false;
<_ 394444       }
  395445     }
  396446