[ Index ]
 

Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/includes/functions/ -> functions_categories.php (source)

   1  <?php
   2  /**

   3   * functions_categories.php

   4   *

   5   * @package functions

   6   * @copyright Copyright 2003-2006 Zen Cart Development Team

   7   * @copyright Portions Copyright 2003 osCommerce

   8   * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0

   9   * @version $Id: functions_categories.php 4135 2006-08-14 04:25:02Z drbyte $

  10   */
  11  
  12  ////

  13  // Generate a path to categories

  14    function zen_get_path($current_category_id = '') {
  15      global $cPath_array, $db;
  16  
  17      if (zen_not_null($current_category_id)) {
  18        $cp_size = sizeof($cPath_array);
  19        if ($cp_size == 0) {
  20          $cPath_new = $current_category_id;
  21        } else {
  22          $cPath_new = '';
  23          $last_category_query = "select parent_id
  24                                  from " . TABLE_CATEGORIES . "
  25                                  where categories_id = '" . (int)$cPath_array[($cp_size-1)] . "'";
  26  
  27          $last_category = $db->Execute($last_category_query);
  28  
  29          $current_category_query = "select parent_id
  30                                     from " . TABLE_CATEGORIES . "
  31                                     where categories_id = '" . (int)$current_category_id . "'";
  32  
  33          $current_category = $db->Execute($current_category_query);
  34  
  35          if ($last_category->fields['parent_id'] == $current_category->fields['parent_id']) {
  36            for ($i=0; $i<($cp_size-1); $i++) {
  37              $cPath_new .= '_' . $cPath_array[$i];
  38            }
  39          } else {
  40            for ($i=0; $i<$cp_size; $i++) {
  41              $cPath_new .= '_' . $cPath_array[$i];
  42            }
  43          }
  44          $cPath_new .= '_' . $current_category_id;
  45  
  46          if (substr($cPath_new, 0, 1) == '_') {
  47            $cPath_new = substr($cPath_new, 1);
  48          }
  49        }
  50      } else {
  51        $cPath_new = implode('_', $cPath_array);
  52      }
  53  
  54      return 'cPath=' . $cPath_new;
  55    }
  56  
  57  ////

  58  // Return the number of products in a category

  59  // TABLES: products, products_to_categories, categories

  60    function zen_count_products_in_category($category_id, $include_inactive = false) {
  61      global $db;
  62      $products_count = 0;
  63      if ($include_inactive == true) {
  64        $products_query = "select count(*) as total
  65                           from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  66                           where p.products_id = p2c.products_id
  67                           and p2c.categories_id = '" . (int)$category_id . "'";
  68  
  69      } else {
  70        $products_query = "select count(*) as total
  71                           from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  72                           where p.products_id = p2c.products_id
  73                           and p.products_status = '1'
  74                           and p2c.categories_id = '" . (int)$category_id . "'";
  75  
  76      }
  77      $products = $db->Execute($products_query);
  78      $products_count += $products->fields['total'];
  79  
  80      $child_categories_query = "select categories_id
  81                                 from " . TABLE_CATEGORIES . "
  82                                 where parent_id = '" . (int)$category_id . "'";
  83  
  84      $child_categories = $db->Execute($child_categories_query);
  85  
  86      if ($child_categories->RecordCount() > 0) {
  87        while (!$child_categories->EOF) {
  88          $products_count += zen_count_products_in_category($child_categories->fields['categories_id'], $include_inactive);
  89          $child_categories->MoveNext();
  90        }
  91      }
  92  
  93      return $products_count;
  94    }
  95  
  96  ////

  97  // Return true if the category has subcategories

  98  // TABLES: categories

  99    function zen_has_category_subcategories($category_id) {
 100      global $db;
 101      $child_category_query = "select count(*) as count
 102                               from " . TABLE_CATEGORIES . "
 103                               where parent_id = '" . (int)$category_id . "'";
 104  
 105      $child_category = $db->Execute($child_category_query);
 106  
 107      if ($child_category->fields['count'] > 0) {
 108        return true;
 109      } else {
 110        return false;
 111      }
 112    }
 113  
 114  ////

 115    function zen_get_categories($categories_array = '', $parent_id = '0', $indent = '', $status_setting = '') {
 116      global $db;
 117  
 118      if (!is_array($categories_array)) $categories_array = array();
 119  
 120      // show based on status

 121      if ($status_setting != '') {
 122        $zc_status = " c.categories_status='" . (int)$status_setting . "' and ";
 123      } else {
 124        $zc_status = '';
 125      }
 126  
 127      $categories_query = "select c.categories_id, cd.categories_name, c.categories_status
 128                           from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
 129                           where " . $zc_status . "
 130                           parent_id = '" . (int)$parent_id . "'
 131                           and c.categories_id = cd.categories_id
 132                           and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
 133                           order by sort_order, cd.categories_name";
 134  
 135      $categories = $db->Execute($categories_query);
 136  
 137      while (!$categories->EOF) {
 138        $categories_array[] = array('id' => $categories->fields['categories_id'],
 139                                    'text' => $indent . $categories->fields['categories_name']);
 140  
 141        if ($categories->fields['categories_id'] != $parent_id) {
 142          $categories_array = zen_get_categories($categories_array, $categories->fields['categories_id'], $indent . '&nbsp;&nbsp;', '1');
 143        }
 144        $categories->MoveNext();
 145      }
 146  
 147      return $categories_array;
 148    }
 149  
 150  ////

 151  // Return all subcategory IDs

 152  // TABLES: categories

 153    function zen_get_subcategories(&$subcategories_array, $parent_id = 0) {
 154      global $db;
 155      $subcategories_query = "select categories_id
 156                              from " . TABLE_CATEGORIES . "
 157                              where parent_id = '" . (int)$parent_id . "'";
 158  
 159      $subcategories = $db->Execute($subcategories_query);
 160  
 161      while (!$subcategories->EOF) {
 162        $subcategories_array[sizeof($subcategories_array)] = $subcategories->fields['categories_id'];
 163        if ($subcategories->fields['categories_id'] != $parent_id) {
 164          zen_get_subcategories($subcategories_array, $subcategories->fields['categories_id']);
 165        }
 166        $subcategories->MoveNext();
 167      }
 168    }
 169  
 170  
 171  ////

 172  // Recursively go through the categories and retreive all parent categories IDs

 173  // TABLES: categories

 174    function zen_get_parent_categories(&$categories, $categories_id) {
 175      global $db;
 176      $parent_categories_query = "select parent_id
 177                                  from " . TABLE_CATEGORIES . "
 178                                  where categories_id = '" . (int)$categories_id . "'";
 179  
 180      $parent_categories = $db->Execute($parent_categories_query);
 181  
 182      while (!$parent_categories->EOF) {
 183        if ($parent_categories->fields['parent_id'] == 0) return true;
 184        $categories[sizeof($categories)] = $parent_categories->fields['parent_id'];
 185        if ($parent_categories->fields['parent_id'] != $categories_id) {
 186          zen_get_parent_categories($categories, $parent_categories->fields['parent_id']);
 187        }
 188        $parent_categories->MoveNext();
 189      }
 190    }
 191  
 192  ////

 193  // Construct a category path to the product

 194  // TABLES: products_to_categories

 195    function zen_get_product_path($products_id) {
 196      global $db;
 197      $cPath = '';
 198  
 199      $category_query = "select p2c.categories_id
 200                         from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
 201                         where p.products_id = '" . (int)$products_id . "'
 202                         and p.products_status = '1'
 203                         and p.products_id = p2c.products_id limit 1";
 204  
 205      $category = $db->Execute($category_query);
 206  
 207      if ($category->RecordCount() > 0) {
 208  
 209        $categories = array();
 210        zen_get_parent_categories($categories, $category->fields['categories_id']);
 211  
 212        $categories = array_reverse($categories);
 213  
 214        $cPath = implode('_', $categories);
 215  
 216        if (zen_not_null($cPath)) $cPath .= '_';
 217        $cPath .= $category->fields['categories_id'];
 218      }
 219  
 220      return $cPath;
 221    }
 222  
 223  ////

 224  // Parse and secure the cPath parameter values

 225    function zen_parse_category_path($cPath) {
 226  // make sure the category IDs are integers

 227      $cPath_array = array_map('zen_string_to_int', explode('_', $cPath));
 228  
 229  // make sure no duplicate category IDs exist which could lock the server in a loop

 230      $tmp_array = array();
 231      $n = sizeof($cPath_array);
 232      for ($i=0; $i<$n; $i++) {
 233        if (!in_array($cPath_array[$i], $tmp_array)) {
 234          $tmp_array[] = $cPath_array[$i];
 235        }
 236      }
 237  
 238      return $tmp_array;
 239    }
 240  
 241    function zen_product_in_category($product_id, $cat_id) {
 242      global $db;
 243      $in_cat=false;
 244      $category_query_raw = "select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . "
 245                             where products_id = '" . (int)$product_id . "'";
 246  
 247      $category = $db->Execute($category_query_raw);
 248  
 249      while (!$category->EOF) {
 250        if ($category->fields['categories_id'] == $cat_id) $in_cat = true;
 251        if (!$in_cat) {
 252          $parent_categories_query = "select parent_id from " . TABLE_CATEGORIES . "
 253                                      where categories_id = '" . $category->fields['categories_id'] . "'";
 254  
 255          $parent_categories = $db->Execute($parent_categories_query);
 256  //echo 'cat='.$category->fields['categories_id'].'#'. $cat_id;

 257  
 258          while (!$parent_categories->EOF) {
 259            if (($parent_categories->fields['parent_id'] !=0) ) {
 260              if (!$in_cat) $in_cat = zen_product_in_parent_category($product_id, $cat_id, $parent_categories->fields['parent_id']);
 261            }
 262            $parent_categories->MoveNext();
 263          }
 264        }
 265        $category->MoveNext();
 266      }
 267      return $in_cat;
 268    }
 269  
 270    function zen_product_in_parent_category($product_id, $cat_id, $parent_cat_id) {
 271      global $db;
 272  //echo $cat_id . '#' . $parent_cat_id;

 273      if ($cat_id == $parent_cat_id) {
 274        $in_cat = true;
 275      } else {
 276        $parent_categories_query = "select parent_id from " . TABLE_CATEGORIES . "
 277                                    where categories_id = '" . (int)$parent_cat_id . "'";
 278  
 279        $parent_categories = $db->Execute($parent_categories_query);
 280  
 281        while (!$parent_categories->EOF) {
 282          if ($parent_categories->fields['parent_id'] !=0 && !$incat) {
 283            $in_cat = zen_product_in_parent_category($product_id, $cat_id, $parent_categories->fields['parent_id']);
 284          }
 285          $parent_categories->MoveNext();
 286        }
 287      }
 288      return $in_cat;
 289    }
 290  
 291  
 292  ////

 293  // products with name, model and price pulldown

 294    function zen_draw_products_pull_down($name, $parameters = '', $exclude = '') {
 295      global $currencies, $db;
 296  
 297      if ($exclude == '') {
 298        $exclude = array();
 299      }
 300  
 301      $select_string = '<select name="' . $name . '"';
 302  
 303      if ($parameters) {
 304        $select_string .= ' ' . $parameters;
 305      }
 306  
 307      $select_string .= '>';
 308  
 309      $products = $db->Execute("select p.products_id, pd.products_name, p.products_price
 310                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
 311                                where p.products_id = pd.products_id
 312                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
 313                                order by products_name");
 314  
 315      while (!$products->EOF) {
 316        if (!in_array($products->fields['products_id'], $exclude)) {
 317          $display_price = zen_get_products_base_price($products->fields['products_id']);
 318          $select_string .= '<option value="' . $products->fields['products_id'] . '">' . $products->fields['products_name'] . ' (' . $currencies->format($display_price) . ')</option>';
 319        }
 320        $products->MoveNext();
 321      }
 322  
 323      $select_string .= '</select>';
 324  
 325      return $select_string;
 326    }
 327  
 328  ////

 329  // product pulldown with attributes

 330    function zen_draw_products_pull_down_attributes($name, $parameters = '', $exclude = '') {
 331      global $db, $currencies;
 332  
 333      if ($exclude == '') {
 334        $exclude = array();
 335      }
 336  
 337      $select_string = '<select name="' . $name . '"';
 338  
 339      if ($parameters) {
 340        $select_string .= ' ' . $parameters;
 341      }
 342  
 343      $select_string .= '>';
 344  
 345      $new_fields=', p.products_model';
 346  
 347      $products = $db->Execute("select distinct p.products_id, pd.products_name, p.products_price" . $new_fields ."
 348                                from " . TABLE_PRODUCTS . " p, " .
 349                                         TABLE_PRODUCTS_DESCRIPTION . " pd, " .
 350                                         TABLE_PRODUCTS_ATTRIBUTES . " pa " ."
 351                                where p.products_id= pa.products_id and p.products_id = pd.products_id
 352                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
 353                                order by products_name");
 354  
 355      while (!$products->EOF) {
 356        if (!in_array($products->fields['products_id'], $exclude)) {
 357          $display_price = zen_get_products_base_price($products->fields['products_id']);
 358          $select_string .= '<option value="' . $products->fields['products_id'] . '">' . $products->fields['products_name'] . ' (' . TEXT_MODEL . ' ' . $products->fields['products_model'] . ') (' . $currencies->format($display_price) . ')</option>';
 359        }
 360        $products->MoveNext();
 361      }
 362  
 363      $select_string .= '</select>';
 364  
 365      return $select_string;
 366    }
 367  
 368  
 369  ////

 370  // categories pulldown with products

 371    function zen_draw_products_pull_down_categories($name, $parameters = '', $exclude = '') {
 372      global $db, $currencies;
 373  
 374      if ($exclude == '') {
 375        $exclude = array();
 376      }
 377  
 378      $select_string = '<select name="' . $name . '"';
 379  
 380      if ($parameters) {
 381        $select_string .= ' ' . $parameters;
 382      }
 383  
 384      $select_string .= '>';
 385  
 386      $categories = $db->Execute("select distinct c.categories_id, cd.categories_name " ."
 387                                  from " . TABLE_CATEGORIES . " c, " .
 388                                           TABLE_CATEGORIES_DESCRIPTION . " cd, " .
 389                                           TABLE_PRODUCTS_TO_CATEGORIES . " ptoc " ."
 390                                  where ptoc.categories_id = c.categories_id
 391                                  and c.categories_id = cd.categories_id
 392                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
 393                                  order by categories_name");
 394  
 395      while (!$categories->EOF) {
 396        if (!in_array($categories->fields['categories_id'], $exclude)) {
 397          $select_string .= '<option value="' . $categories->fields['categories_id'] . '">' . $categories->fields['categories_name'] . '</option>';
 398        }
 399        $categories->MoveNext();
 400      }
 401  
 402      $select_string .= '</select>';
 403  
 404      return $select_string;
 405    }
 406  
 407  ////

 408  // categories pulldown with products with attributes

 409    function zen_draw_products_pull_down_categories_attributes($name, $parameters = '', $exclude = '') {
 410      global $db, $currencies;
 411  
 412      if ($exclude == '') {
 413        $exclude = array();
 414      }
 415  
 416      $select_string = '<select name="' . $name . '"';
 417  
 418      if ($parameters) {
 419        $select_string .= ' ' . $parameters;
 420      }
 421  
 422      $select_string .= '>';
 423  
 424      $categories = $db->Execute("select distinct c.categories_id, cd.categories_name " ."
 425                                  from " . TABLE_CATEGORIES . " c, " .
 426                                           TABLE_CATEGORIES_DESCRIPTION . " cd, " .
 427                                           TABLE_PRODUCTS_TO_CATEGORIES . " ptoc, " .
 428                                           TABLE_PRODUCTS_ATTRIBUTES . " pa " ."
 429                                  where pa.products_id= ptoc.products_id
 430                                  and ptoc.categories_id= c.categories_id
 431                                  and c.categories_id = cd.categories_id
 432                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
 433                                  order by categories_name");
 434  
 435      while (!$categories->EOF) {
 436        if (!in_array($categories->fields['categories_id'], $exclude)) {
 437          $select_string .= '<option value="' . $categories->fields['categories_id'] . '">' . $categories->fields['categories_name'] . '</option>';
 438        }
 439        $categories->MoveNext();
 440      }
 441  
 442      $select_string .= '</select>';
 443  
 444      return $select_string;
 445    }
 446  
 447  ////

 448  // look up categories product_type

 449    function zen_get_product_types_to_category($lookup) {
 450      global $db;
 451  
 452      $lookup = str_replace('cPath=','',$lookup);
 453  
 454      $sql = "select product_type_id from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " where category_id='" . (int)$lookup . "'";
 455      $look_up = $db->Execute($sql);
 456  
 457      if ($look_up->RecordCount() > 0) {
 458        return $look_up->fields['product_type_id'];
 459      } else {
 460        return false;
 461      }
 462    }
 463  
 464  //// look up parent categories name

 465    function zen_get_categories_parent_name($categories_id) {
 466      global $db;
 467  
 468      $lookup_query = "select parent_id from " . TABLE_CATEGORIES . " where categories_id='" . (int)$categories_id . "'";
 469      $lookup = $db->Execute($lookup_query);
 470  
 471      $lookup_query = "select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id='" . (int)$lookup->fields['parent_id'] . "'";
 472      $lookup = $db->Execute($lookup_query);
 473  
 474      return $lookup->fields['categories_name'];
 475    }
 476  
 477  ////

 478  // Get all products_id in a Category and its SubCategories

 479  // use as:

 480  // $my_products_id_list = array();

 481  // $my_products_id_list = zen_get_categories_products_list($categories_id)

 482    function zen_get_categories_products_list($categories_id, $include_deactivated = false, $include_child = true) {
 483      global $db;
 484      global $categories_products_id_list;
 485  
 486      if ($include_deactivated) {
 487  
 488        $products = $db->Execute("select p.products_id
 489                                  from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
 490                                  where p.products_id = p2c.products_id
 491                                  and p2c.categories_id = '" . (int)$categories_id . "'");
 492      } else {
 493        $products = $db->Execute("select p.products_id
 494                                  from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
 495                                  where p.products_id = p2c.products_id
 496                                  and p.products_status = '1'
 497                                  and p2c.categories_id = '" . (int)$categories_id . "'");
 498      }
 499  
 500      while (!$products->EOF) {
 501  // categories_products_id_list keeps resetting when category changes ...

 502  //      echo 'Products ID: ' . $products->fields['products_id'] . '<br>';

 503        $categories_products_id_list[] = $products->fields['products_id'];
 504        $products->MoveNext();
 505      }
 506  
 507      if ($include_child) {
 508        $childs = $db->Execute("select categories_id from " . TABLE_CATEGORIES . "
 509                                where parent_id = '" . (int)$categories_id . "'");
 510        if ($childs->RecordCount() > 0 ) {
 511          while (!$childs->EOF) {
 512            zen_get_categories_products_list($childs->fields['categories_id'], $include_deactivated);
 513            $childs->MoveNext();
 514          }
 515        }
 516      }
 517      $products_id_listing = $categories_products_id_list;
 518      return $products_id_listing;
 519    }
 520  
 521  ?>


Généré le : Mon Nov 26 16:45:43 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics