Quick Search:

Mode

Context

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

Other Diffs

Ignore

Blank Lines Whitespace:

Diff

1497
 
1674
 
1674
 
product.php
_> 11 <?php
  22 /*
<> 3 -  $Id: account.php 207 2005-09-26 01:29:31 +0200 (Mo, 26 Sep 2005) hpdl $
   3+  $Id: $
44 
  55   osCommerce, Open Source E-Commerce Solutions
  66   http://www.oscommerce.com
  77 
<> 8 -  Copyright (c) 2006 osCommerce
   8+  Copyright (c) 2007 osCommerce
99 
  1010   This program is free software; you can redistribute it and/or modify
  1111   it under the terms of the GNU General Public License v2 (1991)
     
 !
1515   class osC_Product {
  1616     var $_data = array();
  1717 
<> 18 -    function osC_Product($id) {
   18+    function __construct($id) {
1919       global $osC_Database, $osC_Services, $osC_Language, $osC_Image;
  2020 
<> 21 -      if (!empty($id)) {
  22 -        $Qproduct = $osC_Database->query('select p.products_id as id, p.products_quantity as quantity, p.products_price as price, p.products_tax_class_id as tax_class_id, p.products_date_added as date_added, p.products_date_available as date_available, p.manufacturers_id, pd.products_name as name, pd.products_description as description, pd.products_model as model, pd.products_keyword as keyword, pd.products_tags as tags, pd.products_url as url from :table_products p, :table_products_description pd where');
  23 -        $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
  24 -        $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
   21+      if ( !empty($id) ) {
   22+        if ( is_numeric($id) ) {
   23+          $Qproduct = $osC_Database->query('select products_id as id, parent_id, products_quantity as quantity, products_price as price, products_model as model, products_tax_class_id as tax_class_id, products_date_added as date_added, products_date_available as date_available, manufacturers_id, has_children from :table_products where products_id = :products_id and products_status = :products_status');
   24+          $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
   25+          $Qproduct->bindInt(':products_id', $id);
   26+          $Qproduct->bindInt(':products_status', 1);
   27+          $Qproduct->execute();
2528 
<> 26 -        if (ereg('^[0-9]+(#?([0-9]+:?[0-9]+)+(;?([0-9]+:?[0-9]+)+)*)*$', $id)) {
  27 -          $Qproduct->appendQuery('p.products_id = :products_id');
  28 -          $Qproduct->bindInt(':products_id', osc_get_product_id($id));
   29+          if ( $Qproduct->numberOfRows() === 1 ) {
   30+            $this->_data = $Qproduct->toArray();
   31+            $this->_data['master_id'] = $Qproduct->valueInt('id');
   32+            $this->_data['has_children'] = $Qproduct->valueInt('has_children');
   33+
   34+            if ( $Qproduct->valueInt('parent_id') > 0 ) {
   35+              $Qmaster = $osC_Database->query('select products_id, has_children from :table_products where products_id = :products_id and products_status = :products_status');
   36+              $Qmaster->bindTable(':table_products', TABLE_PRODUCTS);
   37+              $Qmaster->bindInt(':products_id', $Qproduct->valueInt('parent_id'));
   38+              $Qmaster->bindInt(':products_status', 1);
   39+              $Qmaster->execute();
   40+
   41+              if ( $Qmaster->numberOfRows() === 1 ) {
   42+                $this->_data['master_id'] = $Qmaster->valueInt('products_id');
   43+                $this->_data['has_children'] = $Qmaster->valueInt('has_children');
   44+              } else { // master product is disabled so invalidate the product variant
   45+                $this->_data = array();
   46+              }
   47+            }
   48+
   49+            if ( !empty($this->_data) ) {
   50+              $Qdesc = $osC_Database->query('select products_name as name, products_description as description, products_keyword as keyword, products_tags as tags, products_url as url from :table_products_description where products_id = :products_id and language_id = :language_id');
   51+              $Qdesc->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
   52+              $Qdesc->bindInt(':products_id', $this->_data['master_id']);
   53+              $Qdesc->bindInt(':language_id', $osC_Language->getID());
   54+              $Qdesc->execute();
   55+
   56+              $this->_data = array_merge($this->_data, $Qdesc->toArray());
   57+            }
   58+          }
2959         } else {
<> 30 -          $Qproduct->appendQuery('pd.products_keyword = :products_keyword');
   60+          $Qproduct = $osC_Database->query('select p.products_id as id, p.parent_id, p.products_quantity as quantity, p.products_price as price, p.products_model as model, p.products_tax_class_id as tax_class_id, p.products_date_added as date_added, p.products_date_available as date_available, p.manufacturers_id, p.has_children, pd.products_name as name, pd.products_description as description, pd.products_keyword as keyword, pd.products_tags as tags, pd.products_url as url from :table_products p, :table_products_description pd where pd.products_keyword = :products_keyword and pd.language_id = :language_id and pd.products_id = p.products_id and p.products_status = :products_status');
   61+          $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
   62+          $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
3163           $Qproduct->bindValue(':products_keyword', $id);
<> 32 -        }
   64+          $Qproduct->bindInt(':language_id', $osC_Language->getID());
   65+          $Qproduct->bindInt(':products_status', 1);
   66+          $Qproduct->execute();
3367 
<> 34 -        $Qproduct->appendQuery('and p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = :language_id');
  35 -        $Qproduct->bindInt(':language_id', $osC_Language->getID());
  36 -        $Qproduct->execute();
   68+          if ($Qproduct->numberOfRows() === 1) {
   69+            $this->_data = $Qproduct->toArray();
3770 
<> 38 -        if ($Qproduct->numberOfRows() === 1) {
  39 -          $this->_data = $Qproduct->toArray();
   71+            $this->_data['master_id'] = $Qproduct->valueInt('id');
   72+            $this->_data['has_children'] = $Qproduct->valueInt('has_children');
   73+          }
   74+        }
4075 
<>  76+        if ( !empty($this->_data) ) {
4177           $this->_data['images'] = array();
  4278 
  4379           $Qimages = $osC_Database->query('select id, image, default_flag from :table_products_images where products_id = :products_id order by sort_order');
  4480           $Qimages->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
<> 45 -          $Qimages->bindInt(':products_id', $this->_data['id']);
   81+          $Qimages->bindInt(':products_id', $this->_data['master_id']);
4682           $Qimages->execute();
  4783 
  4884           while ($Qimages->next()) {
     
 !
5187 
  5288           $Qcategory = $osC_Database->query('select categories_id from :table_products_to_categories where products_id = :products_id limit 1');
  5389           $Qcategory->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
<> 54 -          $Qcategory->bindInt(':products_id', $this->_data['id']);
   90+          $Qcategory->bindInt(':products_id', $this->_data['master_id']);
5591           $Qcategory->execute();
  5692 
  5793           $this->_data['category_id'] = $Qcategory->valueInt('categories_id');
  5894 
<> 59 -          $Qcheck = $osC_Database->query('select products_attributes_id from :table_products_attributes patrib where products_id = :products_id limit 1');
  60 -          $Qcheck->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
  61 -          $Qcheck->bindInt(':products_id', $this->_data['id']);
  62 -          $Qcheck->execute();
   95+          if ( $this->_data['has_children'] === 1 ) {
   96+            $this->_data['variants'] = array();
6397 
<> 64 -          if ($Qcheck->numberOfRows() === 1) {
  65 -            $this->_data['attributes'] = array();
   98+            $Qsubproducts = $osC_Database->query('select * from :table_products where parent_id = :parent_id and products_status = :products_status');
   99+            $Qsubproducts->bindTable(':table_products', TABLE_PRODUCTS);
   100+            $Qsubproducts->bindInt(':parent_id', $this->_data['master_id']);
   101+            $Qsubproducts->bindInt(':products_status', 1);
   102+            $Qsubproducts->execute();
66103 
<> 67 -            $Qattributes = $osC_Database->query('select pa.*, po.*, pov.* from :table_products_attributes pa, :table_products_options po, :table_products_options_values pov where pa.products_id = :products_id and pa.options_id = po.products_options_id and po.language_id = :language_id and pa.options_values_id = pov.products_options_values_id and pov.language_id = :language_id order by po.products_options_name, pov.products_options_values_name');
  68 -            $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
  69 -            $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
  70 -            $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
  71 -            $Qattributes->bindInt(':products_id', $this->_data['id']);
  72 -            $Qattributes->bindInt(':language_id', $osC_Language->getID());
  73 -            $Qattributes->bindInt(':language_id', $osC_Language->getID());
  74 -            $Qattributes->execute();
   104+            while ( $Qsubproducts->next() ) {
   105+              $this->_data['variants'][$Qsubproducts->valueInt('products_id')]['data'] = array('price' => $Qsubproducts->value('products_price'),
   106+                                                                                               'tax_class_id' => $Qsubproducts->valueInt('products_tax_class_id'),
   107+                                                                                               'model' => $Qsubproducts->value('products_model'),
   108+                                                                                               'quantity' => $Qsubproducts->value('products_quantity'),
   109+                                                                                               'weight' => $Qsubproducts->value('products_weight'),
   110+                                                                                               'weight_class_id' => $Qsubproducts->valueInt('products_weight_class'));
75111 
<> 76 -            while ($Qattributes->next()) {
  77 -              $this->_data['attributes'][] = array('options_id' => $Qattributes->valueInt('options_id'),
  78 -                                                   'options_name' => $Qattributes->value('products_options_name'),
  79 -                                                   'options_values_id' => $Qattributes->valueInt('options_values_id'),
  80 -                                                   'options_values_name' => $Qattributes->value('products_options_values_name'),
  81 -                                                   'options_values_price' => $Qattributes->value('options_values_price'),
  82 -                                                   'price_prefix' => $Qattributes->value('price_prefix'));
   112+              $Qvariants = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title, pvv.sort_order as value_sort_order from :table_products_variants pv, :table_products_variants_groups pvg, :table_products_variants_values pvv where pv.products_id = :products_id and pv.variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id order by pvg.sort_order, pvg.title');
   113+              $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
   114+              $Qvariants->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
   115+              $Qvariants->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
   116+              $Qvariants->bindInt(':products_id', $Qsubproducts->valueInt('products_id'));
   117+              $Qvariants->bindInt(':languages_id', $osC_Language->getID());
   118+              $Qvariants->bindInt(':languages_id', $osC_Language->getID());
   119+              $Qvariants->execute();
   120+
   121+              while ( $Qvariants->next() ) {
   122+                $this->_data['variants'][$Qsubproducts->valueInt('products_id')]['values'][$Qvariants->valueInt('group_id')] = array('value_id' => $Qvariants->valueInt('value_id'),
   123+                                                                                                                                     'group_title' => $Qvariants->value('group_title'),
   124+                                                                                                                                     'value_title' => $Qvariants->value('value_title'),
   125+                                                                                                                                     'sort_order' => $Qvariants->value('value_sort_order'),
   126+                                                                                                                                     'module' => $Qvariants->value('module'));
   127+              }
83128             }
  84129           }
  85130 
<> 86 -          if ($osC_Services->isStarted('reviews')) {
   131+          if ( $osC_Services->isStarted('reviews') ) {
87132             $Qavg = $osC_Database->query('select avg(reviews_rating) as rating from :table_reviews where products_id = :products_id and languages_id = :languages_id and reviews_status = 1');
  88133             $Qavg->bindTable(':table_reviews', TABLE_REVIEWS);
<> 89 -            $Qavg->bindInt(':products_id', $this->_data['id']);
   134+            $Qavg->bindInt(':products_id', $this->_data['master_id']);
90135             $Qavg->bindInt(':languages_id', $osC_Language->getID());
  91136             $Qavg->execute();
  92137 
     
 !
97142     }
  98143 
  99144     function isValid() {
<> 100 -      if (empty($this->_data)) {
  101 -        return false;
  102 -      }
  103 -
  104 -      return true;
   145+      return !empty($this->_data);
105146     }
  106147 
<> 107 -    function getData($key) {
  108 -      if (isset($this->_data[$key])) {
   148+    function getData($key = null) {
   149+      if ( isset($this->_data[$key]) ) {
109150         return $this->_data[$key];
  110151       }
  111152 
<> 112 -      return false;
   153+      return $this->_data;
113154     }
  114155 
  115156     function getID() {
  116157       return $this->_data['id'];
  117158     }
  118159 
<>  160+    function getMasterID() {
   161+      return $this->_data['master_id'];
   162+    }
   163+
119164     function getTitle() {
  120165       return $this->_data['name'];
  121166     }
     
 !
157202       if (($with_special === true) && $osC_Services->isStarted('specials') && ($new_price = $osC_Specials->getPrice($this->_data['id']))) {
  158203         $price = '<s>' . $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']) . '</s> <span class="productSpecialPrice">' . $osC_Currencies->displayPrice($new_price, $this->_data['tax_class_id']) . '</span>';
  159204       } else {
<> 160 -        $price = $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']);
   205+        if ( $this->hasVariants() ) {
   206+          $price = 'from&nbsp;' . $osC_Currencies->displayPrice($this->getVariantMinPrice(), $this->_data['tax_class_id']);
   207+        } else {
   208+          $price = $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']);
   209+        }
161210       }
  162211 
  163212       return $price;
  164213     }
  165214 
<>  215+    function getVariantMinPrice() {
   216+      $price = null;
   217+
   218+      foreach ( $this->_data['variants'] as $variant ) {
   219+        if ( ($price === null) || ($variant['data']['price'] < $price) ) {
   220+          $price = $variant['data']['price'];
   221+        }
   222+      }
   223+
   224+      return ( $price !== null ) ? $price : 0;
   225+    }
   226+
   227+    function getVariantMaxPrice() {
   228+      $price = 0;
   229+
   230+      foreach ( $this->_data['variants'] as $variant ) {
   231+        if ( $variant['data']['price'] > $price ) {
   232+          $price = $variant['data']['price'];
   233+        }
   234+      }
   235+
   236+      return $price;
   237+    }
   238+
166239     function getCategoryID() {
  167240       return $this->_data['category_id'];
  168241     }
     
 !
203276       return $this->_data['date_added'];
  204277     }
  205278 
<> 206 -    function hasAttributes() {
  207 -      return (isset($this->_data['attributes']) && !empty($this->_data['attributes']));
   279+    function hasVariants() {
   280+      return (isset($this->_data['variants']) && !empty($this->_data['variants']));
208281     }
  209282 
<> 210 -    function &getAttributes() {
  211 -      global $osC_Currencies;
   283+    function getVariants($filter_duplicates = true) {
   284+      if ( $filter_duplicates === true ) {
   285+        $values_array = array();
212286 
<> 213 -      $array = array();
   287+        foreach ( $this->_data['variants'] as $product_id => $variants ) {
   288+          foreach ( $variants['values'] as $group_id => $values ) {
   289+            if ( !isset($values_array[$group_id]) ) {
   290+              $values_array[$group_id]['group_id'] = $group_id;
   291+              $values_array[$group_id]['title'] = $values['group_title'];
   292+              $values_array[$group_id]['module'] = $values['module'];
   293+            }
214294 
<> 215 -      foreach ($this->_data['attributes'] as $attribute) {
  216 -        if (!isset($array[$attribute['options_id']])) {
  217 -          $array[$attribute['options_id']] = array('options_name' => $attribute['options_name'],
  218 -                                                   'values' => array(),
  219 -                                                   'data' => array());
   295+            $value_exists = false;
   296+
   297+            if ( isset($values_array[$group_id]['data']) ) {
   298+              foreach ( $values_array[$group_id]['data'] as $value ) {
   299+                if ( $value['id'] == $values['value_id'] ) {
   300+                  $value_exists = true;
   301+
   302+                  break;
   303+                }
   304+              }
   305+            }
   306+
   307+            if ( $value_exists === false ) {
   308+              $values_array[$group_id]['data'][] = array('id' => $values['value_id'],
   309+                                                         'text' => $values['value_title']);
   310+            }
   311+          }
220312         }
  221313 
<> 222 -        $array[$attribute['options_id']]['values'][] = array('options_values_id' => $attribute['options_values_id'],
  223 -                                                             'options_values_name' => $attribute['options_values_name'],
  224 -                                                             'options_values_price' => $attribute['options_values_price'],
  225 -                                                             'price_prefix' => $attribute['price_prefix']);
   314+        return $values_array;
   315+      }
226316 
<> 227 -        $array[$attribute['options_id']]['data'][] = array('id' => $attribute['options_values_id'],
  228 -                                                           'text' => $attribute['options_values_name'] . ($attribute['options_values_price'] != '0' ? ' (' . $attribute['price_prefix'] . $osC_Currencies->displayPrice($attribute['options_values_price'], $this->_data['tax_class_id']) . ')' : ''));
   317+      return $this->_data['variants'];
   318+    }
   319+
   320+    function variantExists($variant) {
   321+      return is_numeric($this->getProductVariantID($variant));
   322+    }
   323+
   324+    function getProductVariantID($variant) {
   325+      $_product_id = false;
   326+
   327+      $_size = sizeof($variant);
   328+
   329+      foreach ( $this->_data['variants'] as $product_id => $variants ) {
   330+        if ( sizeof($variants['values']) === $_size ) {
   331+          $_array = array();
   332+
   333+          foreach ( $variants['values'] as $group_id => $value ) {
   334+            $_array[$group_id] = $value['value_id'];
   335+          }
   336+
   337+          if ( sizeof(array_diff_assoc($_array, $variant)) === 0 ) {
   338+            $_product_id = $product_id;
   339+
   340+            break;
   341+          }
   342+        }
229343       }
  230344 
<> 231 -      return $array;
   345+      return $_product_id;
   346+
   347+/*HPDL; Useful for static version
   348+
   349+        $Qcheck = $osC_Database->query('select products_id from :table_products where parent_id = :parent_id limit 1');
   350+        $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
   351+        $Qcheck->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
   352+        $Qcheck->execute();
   353+
   354+        if ( $Qcheck->numberOfRows() < 1 ) {
   355+          return true;
   356+        } else {
   357+          $Qvariants = $osC_Database->query('select p.products_id from :table_products p, :table_products_variants pv where p.parent_id = :parent_id and p.products_id = pv.products_id and pv.variants_values_id in (":variants_values_id") group by pv.products_id');
   358+          $Qvariants->bindTable(':table_products', TABLE_PRODUCTS);
   359+          $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
   360+          $Qvariants->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
   361+          $Qvariants->bindRaw(':variants_values_id', implode('", "', $variants));
   362+          $Qvariants->execute();
   363+
   364+          while ( $Qvariants->next() ) {
   365+            $Qvcheck = $osC_Database->query('select count(*) as total from :table_products_variants where products_id = :products_id');
   366+            $Qvcheck->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
   367+            $Qvcheck->bindInt(':products_id', $Qvariants->valueInt('products_id'));
   368+            $Qvcheck->execute();
   369+
   370+            if ( $Qvcheck->valueInt('total') === sizeof($variants) ) {
   371+              return true;
   372+            }
   373+          }
   374+        }
   375+*/
   376+
232377     }
  233378 
  234379     function checkEntry($id) {
  235380       global $osC_Database;
  236381 
<> 237 -      $Qcheck = $osC_Database->query('select p.products_id from :table_products p');
  238 -      $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
   382+      $Qproduct = $osC_Database->query('select p.products_id from :table_products p');
   383+      $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
239384 
<> 240 -      if (ereg('^[0-9]+(#?([0-9]+:?[0-9]+)+(;?([0-9]+:?[0-9]+)+)*)*$', $id)) {
  241 -        $Qcheck->appendQuery('where p.products_id = :products_id');
  242 -        $Qcheck->bindInt(':products_id', osc_get_product_id($id));
   385+      if ( is_numeric($id) ) {
   386+        $Qproduct->appendQuery('where p.products_id = :products_id');
   387+        $Qproduct->bindInt(':products_id', $id);
243388       } else {
<> 244 -        $Qcheck->appendQuery(', :table_products_description pd where pd.products_keyword = :products_keyword and pd.products_id = p.products_id');
  245 -        $Qcheck->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
  246 -        $Qcheck->bindValue(':products_keyword', $id);
   389+        $Qproduct->appendQuery(', :table_products_description pd where pd.products_keyword = :products_keyword and pd.products_id = p.products_id');
   390+        $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
   391+        $Qproduct->bindValue(':products_keyword', $id);
247392       }
  248393 
<> 249 -      $Qcheck->appendQuery('and p.products_status = 1 limit 1');
  250 -      $Qcheck->execute();
   394+      $Qproduct->appendQuery('and p.products_status = 1 limit 1');
   395+      $Qproduct->execute();
251396 
<> 252 -      if ($Qcheck->numberOfRows() === 1) {
  253 -        return true;
  254 -      }
  255 -
  256 -      return false;
   397+      return ( $Qproduct->numberOfRows() === 1 );
<_ 257398     }
  258399 
  259400     function incrementCounter() {