Tsimi

catalog/includes/classes/wishlist.php

Feb 11th, 2015
33
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.   $Id$ Wish List class revision 3
  4.  
  5.   osCommerce, Open Source E-Commerce Solutions
  6.   http://www.oscommerce.com
  7.  
  8.   Copyright (c) 2012 osCommerce
  9.  
  10.   Released under the GNU General Public License
  11. */
  12.  
  13.   class wishlist {
  14.     var $contents, $total, $weight, $wlistID, $content_type;
  15.  
  16.     function wishlist() {
  17.       $this->reset();
  18.     }
  19.  
  20.     function restore_wishlist() {
  21.       global $customer_id;
  22.  
  23.       if (!tep_session_is_registered('customer_id')) return false;
  24.  
  25. // insert current list contents in database
  26.       if (is_array($this->contents)) {
  27.         reset($this->contents);
  28.         while (list($products_id, ) = each($this->contents)) {
  29.           $qty = $this->contents[$products_id]['qty'];
  30.           $product_query = tep_db_query("select products_id from " . TABLE_WISHLIST . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  31.           if (!tep_db_num_rows($product_query)) {
  32.             tep_db_query("insert into " . TABLE_WISHLIST . " (customers_id, products_id, customers_wishlist_quantity, customers_wishlist_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
  33.             if (isset($this->contents[$products_id]['attributes'])) {
  34.               reset($this->contents[$products_id]['attributes']);
  35.               while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
  36.                 tep_db_query("insert into " . TABLE_WISHLIST_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
  37.               }
  38.             }
  39.           } else {
  40.             tep_db_query("update " . TABLE_WISHLIST . " set customers_wishlist_quantity = '" . tep_db_input($qty) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  41.           }
  42.         }
  43.       }
  44.  
  45. // reset per-session list contents, but not the database contents
  46.       $this->reset(false);
  47.  
  48.       $products_query = tep_db_query("select products_id, customers_wishlist_quantity from " . TABLE_WISHLIST . " where customers_id = '" . (int)$customer_id . "'");
  49.       while ($products = tep_db_fetch_array($products_query)) {
  50.         $this->contents[$products['products_id']] = array('qty' => $products['customers_wishlist_quantity']);
  51. // attributes
  52.         $attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_WISHLIST_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'");
  53.         while ($attributes = tep_db_fetch_array($attributes_query)) {
  54.           $this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
  55.         }
  56.       }
  57.  
  58.       $this->cleanup();
  59.  
  60. // assign a temporary unique ID to the list contents to prevent hack attempts
  61.       $this->wlistID = $this->generate_wlist_id();
  62.     }
  63.  
  64.     function clear() {
  65.       global $customer_id;
  66.  
  67.         // Remove all from database
  68.     if (tep_session_is_registered('customer_id')) {
  69.         $wishlist_products_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
  70.         while($wishlist_products = tep_db_fetch_array($wishlist_products_query)) {
  71.                 tep_db_query("delete from " . TABLE_WISHLIST . " where products_id = '" . tep_db_prepare_input($wishlist_products['products_id']) . "' and customers_id = '" . (int)$customer_id . "'");
  72.                 tep_db_query("delete from " . TABLE_WISHLIST_ATTRIBUTES . " where products_id = '" . tep_db_prepare_input($wishlist_products['products_id']) . "' and customers_id = '" . (int)$customer_id . "'");
  73.         }
  74.         }
  75.     }
  76.  
  77.     function reset($reset_database = false) {
  78.       global $customer_id;
  79.  
  80.       $this->contents = array();
  81.       $this->total = 0;
  82.       $this->weight = 0;
  83.       $this->content_type = false;
  84.  
  85.       if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
  86.         tep_db_query("delete from " . TABLE_WISHLIST . " where customers_id = '" . (int)$customer_id . "'");
  87.         tep_db_query("delete from " . TABLE_WISHLIST_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
  88.       }
  89.  
  90.       unset($this->wlistID);
  91.       if (tep_session_is_registered('wlistID')) tep_session_unregister('wlistID');
  92.     }
  93.  
  94.     function add_wishlist($products_id, $qty = '1', $attributes = '', $notify = true) {
  95.       global $new_products_id_in_wlist, $customer_id;
  96.  
  97.       $products_id_string = tep_get_uprid($products_id, $attributes);
  98.       $products_id = tep_get_prid($products_id_string);
  99.  
  100.       if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
  101.         $qty = MAX_QTY_IN_CART;
  102.       }
  103.  
  104.       $attributes_pass_check = true;
  105.  
  106.       if (is_array($attributes) && !empty($attributes)) {
  107.         reset($attributes);
  108.         while (list($option, $value) = each($attributes)) {
  109.           if (!is_numeric($option) || !is_numeric($value)) {
  110.             $attributes_pass_check = false;
  111.             break;
  112.           } else {
  113.             $check_query = tep_db_query("select products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "' limit 1");
  114.             if (tep_db_num_rows($check_query) < 1) {
  115.               $attributes_pass_check = false;
  116.               break;
  117.             }
  118.           }
  119.         }
  120.       } elseif (tep_has_product_attributes($products_id)) {
  121.         $attributes_pass_check = false;
  122.       }
  123.  
  124.       if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) {
  125.         $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
  126.         $check_product = tep_db_fetch_array($check_product_query);
  127.  
  128.         if (($check_product !== false) && ($check_product['products_status'] == '1')) {
  129.           if ($notify == true) {
  130.             $new_products_id_in_wlist = $products_id;
  131.             tep_session_register('new_products_id_in_wlist');
  132.           }
  133.  
  134.           if ($this->in_wlist($products_id_string)) {
  135.             $this->update_quantity($products_id_string, $qty, $attributes);
  136.           } else {
  137.             $this->contents[$products_id_string] = array('qty' => (int)$qty);
  138. // insert into database
  139.             if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_WISHLIST . " (customers_id, products_id, customers_wishlist_quantity, customers_wishlist_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "')");
  140.  
  141.             if (is_array($attributes)) {
  142.               reset($attributes);
  143.               while (list($option, $value) = each($attributes)) {
  144.                 $this->contents[$products_id_string]['attributes'][$option] = $value;
  145. // insert into database
  146.                 if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_WISHLIST_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "')");
  147.               }
  148.             }
  149.           }
  150.  
  151.           $this->cleanup();
  152.  
  153. // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
  154.           $this->wlistID = $this->generate_wlist_id();
  155.         }
  156.       }
  157.     }
  158.  
  159.     function update_quantity($products_id, $quantity = '', $attributes = '') {
  160.       global $customer_id;
  161.  
  162.       $products_id_string = tep_get_uprid($products_id, $attributes);
  163.       $products_id = tep_get_prid($products_id_string);
  164.  
  165.       if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$quantity > MAX_QTY_IN_CART)) {
  166.         $quantity = MAX_QTY_IN_CART;
  167.       }
  168.  
  169.       $attributes_pass_check = true;
  170.  
  171.       if (is_array($attributes)) {
  172.         reset($attributes);
  173.         while (list($option, $value) = each($attributes)) {
  174.           if (!is_numeric($option) || !is_numeric($value)) {
  175.             $attributes_pass_check = false;
  176.             break;
  177.           }
  178.         }
  179.       }
  180.  
  181.       if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity) && ($attributes_pass_check == true)) {
  182.         $this->contents[$products_id_string] = array('qty' => (int)$quantity);
  183. // update database
  184.         if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_WISHLIST . " set customers_wishlist_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'");
  185.  
  186.         if (is_array($attributes)) {
  187.           reset($attributes);
  188.           while (list($option, $value) = each($attributes)) {
  189.             $this->contents[$products_id_string]['attributes'][$option] = $value;
  190. // update database
  191.             if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_WISHLIST_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "'");
  192.           }
  193.         }
  194.  
  195. // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
  196.         $this->wlistID = $this->generate_wlist_id();
  197.       }
  198.     }
  199.  
  200.     function cleanup() {
  201.       global $customer_id;
  202.  
  203.       reset($this->contents);
  204.       while (list($key,) = each($this->contents)) {
  205.         if ($this->contents[$key]['qty'] < 1) {
  206.           unset($this->contents[$key]);
  207. // remove from database
  208.           if (tep_session_is_registered('customer_id')) {
  209.             tep_db_query("delete from " . TABLE_WISHLIST . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
  210.             tep_db_query("delete from " . TABLE_WISHLIST_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
  211.           }
  212.         }
  213.       }
  214.     }
  215.  
  216.     function count_contents() {  // get total number of items in list
  217.       $total_items = 0;
  218.       if (is_array($this->contents)) {
  219.         reset($this->contents);
  220.         while (list($products_id, ) = each($this->contents)) {
  221.           $total_items += $this->get_quantity($products_id);
  222.         }
  223.       }
  224.  
  225.       return $total_items;
  226.     }
  227.  
  228.     function get_quantity($products_id) {
  229.       if (isset($this->contents[$products_id])) {
  230.         return $this->contents[$products_id]['qty'];
  231.       } else {
  232.         return 0;
  233.       }
  234.     }
  235.  
  236.     function in_wlist($products_id) {
  237.       if (isset($this->contents[$products_id])) {
  238.         return true;
  239.       } else {
  240.         return false;
  241.       }
  242.     }
  243.  
  244.     function remove($products_id) {
  245.       global $customer_id;
  246.  
  247.       unset($this->contents[$products_id]);
  248. // remove from database
  249.       if (tep_session_is_registered('customer_id')) {
  250.         tep_db_query("delete from " . TABLE_WISHLIST . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  251.         tep_db_query("delete from " . TABLE_WISHLIST_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
  252.       }
  253.  
  254. // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
  255.       $this->wlistID = $this->generate_wlist_id();
  256.     }
  257.  
  258.     function remove_all() {
  259.       $this->reset();
  260.     }
  261.  
  262.     function get_product_id_list() {
  263.       $product_id_list = '';
  264.       if (is_array($this->contents)) {
  265.         reset($this->contents);
  266.         while (list($products_id, ) = each($this->contents)) {
  267.           $product_id_list .= ', ' . $products_id;
  268.         }
  269.       }
  270.  
  271.       return substr($product_id_list, 2);
  272.     }
  273.  
  274.     function calculate() {
  275.       global $currencies;
  276.  
  277.       $this->total = 0;
  278.       $this->weight = 0;
  279.       if (!is_array($this->contents)) return 0;
  280.  
  281.       reset($this->contents);
  282.       while (list($products_id, ) = each($this->contents)) {
  283.         $qty = $this->contents[$products_id]['qty'];
  284.  
  285. // products price
  286.         $product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
  287.         if ($product = tep_db_fetch_array($product_query)) {
  288.           $prid = $product['products_id'];
  289.           $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
  290.           $products_price = $product['products_price'];
  291.           $products_weight = $product['products_weight'];
  292.  
  293.           $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1' order by specials_new_products_price limit 1");
  294.           if (tep_db_num_rows ($specials_query)) {
  295.             $specials = tep_db_fetch_array($specials_query);
  296.             $products_price = $specials['specials_new_products_price'];
  297.           }
  298.  
  299.           $this->total += $currencies->calculate_price($products_price, $products_tax, $qty);
  300.           $this->weight += ($qty * $products_weight);
  301.         }
  302.  
  303. // attributes price
  304.         if (isset($this->contents[$products_id]['attributes'])) {
  305.           reset($this->contents[$products_id]['attributes']);
  306.           while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
  307.             $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
  308.             $attribute_price = tep_db_fetch_array($attribute_price_query);
  309.             if ($attribute_price['price_prefix'] == '+') {
  310.               $this->total += $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
  311.             } else {
  312.               $this->total -= $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
  313.             }
  314.           }
  315.         }
  316.       }
  317.     }
  318.  
  319.     function attributes_price($products_id) {
  320.       $attributes_price = 0;
  321.  
  322.       if (isset($this->contents[$products_id]['attributes'])) {
  323.         reset($this->contents[$products_id]['attributes']);
  324.         while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
  325.           $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
  326.           $attribute_price = tep_db_fetch_array($attribute_price_query);
  327.           if ($attribute_price['price_prefix'] == '+') {
  328.             $attributes_price += $attribute_price['options_values_price'];
  329.           } else {
  330.             $attributes_price -= $attribute_price['options_values_price'];
  331.           }
  332.         }
  333.       }
  334.  
  335.       return $attributes_price;
  336.     }
  337.  
  338. // WORKAROUND FOR MINI CART ISSUE
  339.     function get_wishlist_products() {
  340.       global $languages_id;
  341.  
  342.       if (!is_array($this->contents)) return false;
  343.  
  344.       $wish_products_array = array();
  345.       reset($this->contents);
  346.       while (list($products_id, ) = each($this->contents)) {
  347.         $wish_products_query = tep_db_query("select * from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
  348.         if ($wish_products = tep_db_fetch_array($wish_products_query)) {
  349.           $temp = array('id' => $products_id,
  350.                         'name' => $wish_products['products_name'],
  351.                         'products_status' => $wish_products['products_status'],
  352.                         'model' => $wish_products['products_model'],
  353.                         'image' => $wish_products['products_image'],
  354.                         'price' => $wish_products['products_price'],
  355.                         'quantity' => $this->contents[$products_id]['qty'],
  356.                         'weight' => $wish_products['products_weight'],
  357.                         'final_price' => ($wish_products['products_price'] + $this->attributes_price($products_id)),
  358.                         'tax_class_id' => $wish_products['products_tax_class_id'],
  359.                         'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
  360.                     if (isset($wish_products['image_folder'])) $temp['image_folder'] = $wish_products['image_folder'];
  361.                     if (isset($wish_products['image_display'])) $temp['image_display'] = $wish_products['image_display'];
  362.           $prid = $wish_products['products_id'];
  363.           $wish_specials_query = tep_db_query("select specials_new_products_price, expires_date from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1' order by specials_new_products_price, expires_date limit 1");
  364.           if (tep_db_num_rows($wish_specials_query)) {
  365.             $wish_specials = tep_db_fetch_array($wish_specials_query);
  366.             $temp['sale_price'] = $wish_specials['specials_new_products_price'];
  367.                         $temp['sale_expires'] = tep_date_short($wish_specials['expires_date']);
  368.                         $temp['final_price'] = ($wish_specials['specials_new_products_price'] + $this->attributes_price($products_id));
  369.           }
  370.                     $wish_products_array[] = $temp;
  371.         }
  372.       }
  373.  
  374.       return $wish_products_array;
  375.     }
  376. // EOF WORKAROUND FOR MINI CART ISSUE
  377.  
  378.     function show_total() {
  379.       $this->calculate();
  380.  
  381.       return $this->total;
  382.     }
  383.  
  384.     function show_weight() {
  385.       $this->calculate();
  386.  
  387.       return $this->weight;
  388.     }
  389.  
  390.     function generate_wlist_id($length = 5) {
  391.       return tep_create_random_value($length, 'digits');
  392.     }
  393.  
  394.     function get_content_type() {
  395.       $this->content_type = false;
  396.  
  397.       if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {
  398.         reset($this->contents);
  399.         while (list($products_id, ) = each($this->contents)) {
  400.           if (isset($this->contents[$products_id]['attributes'])) {
  401.             reset($this->contents[$products_id]['attributes']);
  402.             while (list(, $value) = each($this->contents[$products_id]['attributes'])) {
  403.               $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = '" . (int)$value . "' and pa.products_attributes_id = pad.products_attributes_id");
  404.               $virtual_check = tep_db_fetch_array($virtual_check_query);
  405.  
  406.               if ($virtual_check['total'] > 0) {
  407.                 switch ($this->content_type) {
  408.                   case 'physical':
  409.                     $this->content_type = 'mixed';
  410.  
  411.                     return $this->content_type;
  412.                     break;
  413.                   default:
  414.                     $this->content_type = 'virtual';
  415.                     break;
  416.                 }
  417.               } else {
  418.                 switch ($this->content_type) {
  419.                   case 'virtual':
  420.                     $this->content_type = 'mixed';
  421.  
  422.                     return $this->content_type;
  423.                     break;
  424.                   default:
  425.                     $this->content_type = 'physical';
  426.                     break;
  427.                 }
  428.               }
  429.             }
  430.           } else {
  431.             switch ($this->content_type) {
  432.               case 'virtual':
  433.                 $this->content_type = 'mixed';
  434.  
  435.                 return $this->content_type;
  436.                 break;
  437.               default:
  438.                 $this->content_type = 'physical';
  439.                 break;
  440.             }
  441.           }
  442.         }
  443.       } else {
  444.         $this->content_type = 'physical';
  445.       }
  446.  
  447.       return $this->content_type;
  448.     }
  449.  
  450.     function unserialize($broken) {
  451.       for(reset($broken);$kv=each($broken);) {
  452.         $key=$kv['key'];
  453.         if (gettype($this->$key)!="user function")
  454.         $this->$key=$kv['value'];
  455.       }
  456.     }
  457.  
  458.   }
  459. ?>
RAW Paste Data