Advertisement
Guest User

Untitled

a guest
Aug 6th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 40.48 KB | None | 0 0
  1. <?php
  2. class ModelCatalogProduct extends Model {
  3.     public function addProduct($data) {
  4.         $this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', price_full = '" . (float)$data['price_full'] . "', cost = '" . (float)$data['cost'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
  5.        
  6.         $product_id = $this->db->getLastId();
  7.        
  8.         if (isset($data['image'])) {
  9.             $this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
  10.         }
  11.        
  12.         foreach ($data['product_description'] as $language_id => $value) {
  13.             $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keywords = '" . $this->db->escape($value['meta_keywords']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
  14.         }
  15.        
  16.         if (isset($data['product_store'])) {
  17.             foreach ($data['product_store'] as $store_id) {
  18.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
  19.             }
  20.         }
  21.          
  22.         if (isset($data['product_option'])) {
  23.             foreach ($data['product_option'] as $product_option) {
  24.  
  25. //Put top level extras here
  26. $product_option['extra'] = array();
  27. if(isset($product_option['reqd'])){ $product_option['extra']['reqd'] = $product_option['reqd']; }
  28. if(isset($product_option['allowmultiple'])) { $product_option['extra']['allowmultiple'] = $product_option['allowmultiple']; }
  29. if(isset($product_option['list_height'])) { $product_option['extra']['list_height'] = $product_option['list_height']; }
  30.  
  31.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', sort_order = '" . (int)$product_option['sort_order'] . "', mode = '" . $product_option['mode'] . "', extra = '" . serialize($product_option['extra']) . "'");
  32.                
  33.                 $product_option_id = $this->db->getLastId();
  34.                
  35.                 foreach ($product_option['language'] as $language_id => $language) {
  36.                     $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_description SET product_option_id = '" . (int)$product_option_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  37.                 }              
  38.                
  39.                 if (isset($product_option['product_option_value'])) {
  40.                     foreach ($product_option['product_option_value'] as $product_option_value) {
  41.                         $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" .
  42.                         (float)$product_option_value['price'] . "', price_full = '" . (float)$product_option_value['price_full'] . "',prefix = '" .  $this->db->escape($product_option_value['prefix']) . "', sort_order = '" . (int)$product_option_value['sort_order'] . "'");
  43.                
  44. //Get extra configs for product options
  45. $product_option_value['extras'] = array();
  46. if(isset($product_option_value['txtmaxlength'])){
  47. $product_option_value['extras']['txtmaxlength'] = $product_option_value['txtmaxlength'];
  48. }
  49. if(isset($product_option_value['option_image'])){
  50. $product_option_value['extras']['option_image'] = $product_option_value['option_image'];
  51. }
  52.  
  53.                         $this->db->query("
  54.                        INSERT INTO " . DB_PREFIX . "product_option_value
  55.                        SET product_option_id = '" . (int)$product_option_id . "',
  56.                        product_id = '" . (int)$product_id . "',
  57.                        quantity = '" . (int)$product_option_value['quantity'] . "',
  58.                        subtract = '" . (int)$product_option_value['subtract'] . "',
  59.                        price = '" . (float)$product_option_value['price'] . "',
  60.                        price_full = '" . (float)$product_option_value['price_full'] . "',
  61.                        prefix = '" . $this->db->escape($product_option_value['prefix']) . "',
  62.                        sort_order = '" . (int)$product_option_value['sort_order'] . "',
  63.                        extra = '" . serialize($product_option_value['extras']) . "'"
  64.                         );
  65.  
  66.                         $product_option_value_id = $this->db->getLastId();
  67.                
  68.                         foreach ($product_option_value['language'] as $language_id => $language) {
  69.                             $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value_description SET product_option_value_id = '" . (int)$product_option_value_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  70.                         }                  
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.        
  76. //Text options www.alreadymade.com Joseph De Araujo
  77.         if (isset($data['product_txtoption'])) {
  78.             foreach ($data['product_txtoption'] as $product_txtoption) {
  79.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', sort_order = '" . (int)$product_txtoption['sort_order'] . "', mode = '" . $product_txtoption['mode'] . "'");
  80.  
  81.                 $product_txtoption_id = $this->db->getLastId();
  82.  
  83.                 foreach ($product_txtoption['language'] as $language_id => $language) {
  84.                     $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_description SET product_option_id = '" . (int)$product_txtoption_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  85.                 }
  86.             }
  87.         }
  88. //end text options
  89.         if (isset($data['product_discount'])) {
  90.             foreach ($data['product_discount'] as $value) {
  91.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$value['customer_group_id'] . "', quantity = '" . (int)$value['quantity'] . "', priority = '" . (int)$value['priority'] . "', price = '" . (float)$value['price'] . "', date_start = '" . $this->db->escape($value['date_start']) . "', date_end = '" . $this->db->escape($value['date_end']) . "'");
  92.             }
  93.         }
  94.  
  95.         if (isset($data['product_special'])) {
  96.             foreach ($data['product_special'] as $value) {
  97.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$value['customer_group_id'] . "', priority = '" . (int)$value['priority'] . "', price = '" . (float)$value['price'] . "', date_start = '" . $this->db->escape($value['date_start']) . "', date_end = '" . $this->db->escape($value['date_end']) . "'");
  98.             }
  99.         }
  100.        
  101.         if (isset($data['product_image'])) {
  102.             foreach ($data['product_image'] as $image) {
  103.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($image) . "'");
  104.             }
  105.         }
  106.        
  107.         if (isset($data['product_download'])) {
  108.             foreach ($data['product_download'] as $download_id) {
  109.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
  110.             }
  111.         }
  112.        
  113.         if (isset($data['product_category'])) {
  114.             foreach ($data['product_category'] as $category_id) {
  115.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
  116.             }
  117.         }
  118.        
  119.         if (isset($data['product_related'])) {
  120.             foreach ($data['product_related'] as $related_id) {
  121.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
  122.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
  123.             }
  124.         }
  125.        
  126.         if ($data['keyword']) {
  127.             $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  128.         }
  129.        
  130.         foreach ($data['product_tags'] as $language_id => $value) {
  131.             $tags = explode(',', $value);
  132.             foreach ($tags as $tag) {
  133.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_tags SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
  134.             }
  135.         }
  136.        
  137.        
  138.         $this->cache->delete('product');
  139.     }
  140.    
  141.     public function editProduct($product_id, $data) {
  142.         $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "',price_full = '" . (float)$data['price_full'] . "', cost = '" . (float)$data['cost'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
  143.  
  144.         if (isset($data['image'])) {
  145.             $this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
  146.         }
  147.        
  148.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  149.        
  150.         foreach ($data['product_description'] as $language_id => $value) {
  151.             $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keywords = '" . $this->db->escape($value['meta_keywords']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
  152.         }
  153.  
  154.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  155.  
  156.         if (isset($data['product_store'])) {
  157.             foreach ($data['product_store'] as $store_id) {
  158.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
  159.             }
  160.         }
  161.        
  162.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
  163.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_description WHERE product_id = '" . (int)$product_id . "'");
  164.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
  165.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value_description WHERE product_id = '" . (int)$product_id . "'");
  166.        
  167.         if (isset($data['product_option'])) {
  168.             foreach ($data['product_option'] as $product_option) {
  169.  
  170. $product_option['extra'] = array();
  171. if(isset($product_option['reqd'])){ $product_option['extra']['reqd'] = $product_option['reqd']; }
  172. if(isset($product_option['allowmultiple'])) { $product_option['extra']['allowmultiple'] = $product_option['allowmultiple']; }
  173. if(isset($product_option['list_height'])) { $product_option['extra']['list_height'] = $product_option['list_height']; }
  174.  
  175.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', mode = '" . $product_option['mode'] . "', sort_order = '" . (int)$product_option['sort_order'] . "', extra = '" . serialize($product_option['extra']) . "'");
  176.                
  177.                 $product_option_id = $this->db->getLastId();
  178.                
  179.                 foreach ($product_option['language'] as $language_id => $language) {
  180.                     $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_description SET product_option_id = '" . (int)$product_option_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  181.                 }              
  182.                
  183.                 if (isset($product_option['product_option_value'])) {
  184.                     foreach ($product_option['product_option_value'] as $product_option_value) {
  185.                        
  186. $product_option_value['extras'] = array();
  187. if(isset($product_option_value['txtmaxlength'])){
  188. $product_option_value['extras']['txtmaxlength'] = $product_option_value['txtmaxlength'];
  189. }
  190. if(isset($product_option_value['option_image'])){
  191. $product_option_value['extras']['option_image'] = $product_option_value['option_image'];
  192. }
  193. if(isset($product_option_value['sort_order'])) {
  194.     $sort_order = ", sort_order = '" . (int)$product_option_value['sort_order'] . "'";
  195. } else {
  196.     $sort_order = '';
  197. }
  198.                         $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_full = '" . (float)$product_option_value['price_full'] . "', prefix = '" . $this->db->escape($product_option_value['prefix']) . "' " . $sort_order . ", extra = '" . serialize($product_option_value['extras']) . "'");
  199.                
  200.                         $product_option_value_id = $this->db->getLastId();
  201.                
  202.                         foreach ($product_option_value['language'] as $language_id => $language) {
  203.                             $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value_description SET product_option_value_id = '" . (int)$product_option_value_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  204.                         }                  
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.        
  210. //Textoptions - www.alreadymade.com - Joseph De Araujo
  211.         if (isset($data['product_txtoption'])) {
  212.             foreach ($data['product_txtoption'] as $product_txtoption) {
  213.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', sort_order = '" . (int)$product_txtoption['sort_order'] . "', mode = '" . $product_txtoption['mode'] . "'");
  214.  
  215.                 $product_txtoption_id = $this->db->getLastId();
  216.  
  217.                 foreach ($product_txtoption['language'] as $language_id => $language) {
  218.                     $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_description SET product_option_id = '" . (int)$product_txtoption_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
  219.                 }
  220.             }
  221.         }
  222. //end textoptions
  223.  
  224.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
  225.  
  226.         if (isset($data['product_discount'])) {
  227.             foreach ($data['product_discount'] as $value) {
  228.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$value['customer_group_id'] . "', quantity = '" . (int)$value['quantity'] . "', priority = '" . (int)$value['priority'] . "', price = '" . (float)$value['price'] . "', date_start = '" . $this->db->escape($value['date_start']) . "', date_end = '" . $this->db->escape($value['date_end']) . "'");
  229.             }
  230.         }
  231.        
  232.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
  233.        
  234.         if (isset($data['product_special'])) {
  235.             foreach ($data['product_special'] as $value) {
  236.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$value['customer_group_id'] . "', priority = '" . (int)$value['priority'] . "', price = '" . (float)$value['price'] . "', date_start = '" . $this->db->escape($value['date_start']) . "', date_end = '" . $this->db->escape($value['date_end']) . "'");
  237.             }
  238.         }
  239.        
  240.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  241.        
  242.         if (isset($data['product_image'])) {
  243.             foreach ($data['product_image'] as $image) {
  244.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($image) . "'");
  245.             }
  246.         }
  247.        
  248.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  249.        
  250.         if (isset($data['product_download'])) {
  251.             foreach ($data['product_download'] as $download_id) {
  252.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
  253.             }
  254.         }
  255.        
  256.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  257.        
  258.         if (isset($data['product_category'])) {
  259.             foreach ($data['product_category'] as $category_id) {
  260.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
  261.             }      
  262.         }
  263.  
  264.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_attribute WHERE product_id = '" . (int)$product_id . "'");
  265.  
  266.         if (isset($data['product_attribute'])) {
  267.             foreach ($data['product_attribute'] as $attribute_id) {
  268.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_attribute SET product_id = '" . (int)$product_id . "', attribute_id = '" . (int)$attribute_id . "'");
  269.             }
  270.         }
  271.  
  272.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  273.  
  274.         if (isset($data['product_related'])) {
  275.             foreach ($data['product_related'] as $related_id) {
  276.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
  277.                 $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
  278.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
  279.             }
  280.         }
  281.        
  282.         $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
  283.        
  284.         if ($data['keyword']) {
  285.             $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  286.         }
  287.        
  288.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_tags WHERE product_id = '" . (int)$product_id. "'");
  289.        
  290.         foreach ($data['product_tags'] as $language_id => $value) {
  291.             $tags = explode(',', $value);
  292.             foreach ($tags as $tag) {
  293.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_tags SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
  294.             }
  295.         }
  296.        
  297.         $this->cache->delete('product');
  298.     }
  299.    
  300.     public function copyProduct($product_id) {
  301.         $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  302.        
  303.         if ($query->num_rows) {
  304.             $data = array();
  305.            
  306.             $data = $query->row;
  307.            
  308.             $data = array_merge($data, array('product_description' => $this->getProductDescriptions($product_id)));
  309.             $data = array_merge($data, array('product_option' => $this->getProductOptions($product_id)));
  310.                        
  311.             $data['keyword'] = '';
  312.                        
  313.             $data['status'] = '0';
  314.  
  315.             foreach(array_keys($data['product_description']) as $key) {
  316.                 $data['product_description'][$key]['name'] = $data['product_description'][$key]['name'] . '*';
  317.             }
  318.  
  319.             $data['product_image'] = array();
  320.            
  321.             $results = $this->getProductImages($product_id);
  322.            
  323.             foreach ($results as $result) {
  324.                 $data['product_image'][] = $result['image'];
  325.             }
  326.            
  327.             $data = array_merge($data, array('product_discount' => $this->getProductDiscounts($product_id)));
  328.             $data = array_merge($data, array('product_special' => $this->getProductSpecials($product_id)));
  329.             $data = array_merge($data, array('product_download' => $this->getProductDownloads($product_id)));
  330.             $data = array_merge($data, array('product_category' => $this->getProductCategories($product_id)));
  331.             $data = array_merge($data, array('product_store' => $this->getProductStores($product_id)));
  332.             $data = array_merge($data, array('product_related' => $this->getProductRelated($product_id)));
  333.             $data = array_merge($data, array('product_tags' => $this->getProductTags($product_id)));
  334.            
  335.             $this->addProduct($data);
  336.         }
  337.     }
  338.    
  339.     public function deleteProduct($product_id) {
  340.         $this->db->query("DELETE FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
  341.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  342.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
  343.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_description WHERE product_id = '" . (int)$product_id . "'");
  344.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
  345.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value_description WHERE product_id = '" . (int)$product_id . "'");
  346.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
  347.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  348.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  349.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  350.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  351.         $this->db->query("DELETE FROM " . DB_PREFIX . "review WHERE product_id = '" . (int)$product_id . "'");
  352.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  353.         $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
  354.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_tags WHERE product_id='" . (int)$product_id. "'");
  355.        
  356.         $this->cache->delete('product');
  357.     }
  358.    
  359.     public function getProduct($product_id) {
  360.         $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "') AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  361.  
  362.         return $query->row;
  363.     }
  364.    
  365.     public function getProducts($data = array()) {
  366.         if ($data) {
  367.             $sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  368.        
  369.             if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
  370.                 $sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
  371.             }
  372.  
  373.             if (isset($data['filter_model']) && !is_null($data['filter_model'])) {
  374.                 $sql .= " AND LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($data['filter_model'])) . "%'";
  375.             }
  376.  
  377.             if (isset($data['filter_price']) && !is_null($data['filter_price'])) {
  378.                 $sql .= " AND LCASE(p.price) LIKE '" . $this->db->escape(strtolower($data['filter_price'])) . "%'";
  379.             }
  380. //Added By Chad
  381.             if (isset($data['filter_price_full']) && !is_null($data['filter_price_full'])) {
  382.                 $sql .= " AND LCASE(p.price) LIKE '" . $this->db->escape(strtolower($data['filter_price_full'])) . "%'";
  383.             }
  384. //Added By Chad
  385.             if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  386.                 $sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
  387.             }
  388.            
  389.             if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  390.                 $sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
  391.             }
  392.  
  393.             $sort_data = array(
  394.                 'pd.name',
  395.                 'p.model',
  396.                 'p.price',
  397.                 'p.quantity',
  398.                 'p.status',
  399.                 'p.sort_order'
  400.             );
  401.  
  402.             if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  403.                 $sql .= " ORDER BY " . $data['sort'];
  404.             } else {
  405.                 $sql .= " ORDER BY pd.name";
  406.             }
  407.            
  408.             if (isset($data['order']) && ($data['order'] == 'DESC')) {
  409.                 $sql .= " DESC";
  410.             } else {
  411.                 $sql .= " ASC";
  412.             }
  413.  
  414.             if (isset($data['start']) || isset($data['limit'])) {
  415.                 if ($data['start'] < 0) {
  416.                     $data['start'] = 0;
  417.                 }
  418.  
  419.                 if ($data['limit'] < 1) {
  420.                     $data['limit'] = 20;
  421.                 }
  422.  
  423.                 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  424.             }
  425.  
  426.             $query = $this->db->query($sql);
  427.  
  428.             return $query->rows;
  429.         } else {
  430.             $product_data = $this->cache->get('product.' . $this->config->get('config_language_id'));
  431.  
  432.             if (!$product_data) {
  433.                 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY pd.name ASC");
  434.  
  435.                 $product_data = $query->rows;
  436.  
  437.                 $this->cache->set('product.' . $this->config->get('config_language_id'), $product_data);
  438.             }  
  439.    
  440.             return $product_data;
  441.         }
  442.     }
  443.  
  444.     public function addFeatured($data) {
  445.         $this->db->query("DELETE FROM " . DB_PREFIX . "product_featured");
  446.        
  447.         if (isset($data['product_featured'])) {
  448.             foreach ($data['product_featured'] as $product_id) {
  449.                 $this->db->query("INSERT INTO " . DB_PREFIX . "product_featured SET product_id = '" . (int)$product_id . "'");
  450.             }          
  451.         }
  452.     }
  453.    
  454.     public function getFeaturedProducts() {
  455.         $product_featured_data = array();
  456.         $query = $this->db->query("SELECT product_id FROM " . DB_PREFIX . "product_featured");
  457.  
  458.         foreach ($query->rows as $result) {
  459.             $product_featured_data[] = $result['product_id'];
  460.         }
  461.         return $product_featured_data;
  462.     }
  463.    
  464.     public function getProductsByKeyword($keyword) {
  465.         if ($keyword) {
  466.             $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND (LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($keyword)) . "%' OR LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($keyword)) . "%')");
  467.                                      
  468.             return $query->rows;
  469.         } else {
  470.             return array();
  471.         }
  472.     }
  473.    
  474.     public function getProductsByCategoryId($category_id) {
  475.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2c.category_id = '" . (int)$category_id . "' ORDER BY pd.name ASC");
  476.                                  
  477.         return $query->rows;
  478.     }
  479.  
  480.     public function getProductsByAttributeId($attribute_id) {
  481.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_attribute p2a ON (p.product_id = p2a.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2a.attribute_id = '" . (int)$attribute_id . "' ORDER BY pd.name ASC");
  482.  
  483.         return $query->rows;
  484.     }
  485.    
  486.     public function getProductDescriptions($product_id) {
  487.         $product_description_data = array();
  488.        
  489.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  490.        
  491.         foreach ($query->rows as $result) {
  492.             $product_description_data[$result['language_id']] = array(
  493.                 'name'             => $result['name'],
  494.                 'meta_keywords'    => $result['meta_keywords'],
  495.                 'meta_description' => $result['meta_description'],
  496.                 'description'      => $result['description']
  497.             );
  498.         }
  499.        
  500.         return $product_description_data;
  501.     }
  502.  
  503.     public function getProductOptions($product_id) {
  504.         $product_option_data = array();
  505.        
  506.         $product_option = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order");
  507.        
  508.         foreach ($product_option->rows as $product_option) {
  509.             $product_option_value_data = array();
  510.            
  511.             $product_option_value = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value WHERE product_option_id = '" . (int)$product_option['product_option_id'] . "' ORDER BY sort_order");
  512.            
  513.             foreach ($product_option_value->rows as $product_option_value) {
  514.                 $product_option_value_description_data = array();
  515.                
  516.                 $product_option_value_description = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value_description WHERE product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "'");
  517.  
  518.                 foreach ($product_option_value_description->rows as $result) {
  519.                     $product_option_value_description_data[$result['language_id']] = array('name' => $result['name']);
  520.                 }
  521.            
  522.                 $product_option_value_data[] = array(
  523.                     'product_option_value_id' => $product_option_value['product_option_value_id'],
  524.                     'language'                => $product_option_value_description_data,
  525.                     'quantity'                => $product_option_value['quantity'],
  526.                     'subtract'                => $product_option_value['subtract'],
  527.                     'price'                   => $product_option_value['price'],
  528.                         'price_full'                   => $product_option_value['price_full'],// Added by Chad
  529.                     'prefix'                  => $product_option_value['prefix'],
  530.                     'sort_order'              => $product_option_value['sort_order'],
  531.                     'extras'                  => unserialize($product_option_value['extra'])
  532.                 );
  533.             }
  534.            
  535.             $product_option_description_data = array();
  536.            
  537.             $product_option_description = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_description WHERE product_option_id = '" . (int)$product_option['product_option_id'] . "'");
  538.  
  539.             foreach ($product_option_description->rows as $result) {
  540.                 $product_option_description_data[$result['language_id']] = array('name' => $result['name']);
  541.             }
  542.        
  543.             $product_option_data[] = array(
  544.                 'product_option_id'    => $product_option['product_option_id'],
  545.                 'language'             => $product_option_description_data,
  546.                 'product_option_value' => $product_option_value_data,
  547.                 'sort_order'           => $product_option['sort_order'],
  548.                 'mode'                 => $product_option['mode'],
  549.                 'extra'                => unserialize($product_option['extra'])
  550.             );
  551.         }  
  552.        
  553.         return $product_option_data;
  554.     }
  555.    
  556. //Textoptions www.alreadymade.com Joseph De Araujo
  557.     public function getProductTxtOptions($product_id) {
  558.         $product_txtoption_data = array();
  559.  
  560.         $product_txtoption = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "' AND `mode` = 'text' ORDER BY sort_order");
  561.  
  562.         foreach ($product_txtoption->rows as $product_txtoption) {
  563.             $product_txtoption_value_data = array();
  564.             $product_txtoption_value = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value WHERE product_option_id = '" . (int)$product_txtoption['product_option_id'] . "' ORDER BY sort_order");
  565.  
  566.             $product_txtoption_description_data = array();
  567.             $product_txtoption_description = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_description WHERE product_option_id = '" . (int)$product_txtoption['product_option_id'] . "'");
  568.  
  569.             foreach ($product_txtoption_description->rows as $result) {
  570.                 $product_txtoption_description_data[$result['language_id']] = array('name' => $result['name']);
  571.             }
  572.  
  573.             $product_txtoption_data[] = array(
  574.                 'product_txtoption_id'    => $product_txtoption['product_option_id'],
  575.                 'language'                => $product_txtoption_description_data,
  576.                 'product_txtoption_value' => $product_txtoption_value_data,
  577.                 'sort_order'              => $product_txtoption['sort_order'],
  578.                 'mode'                    => $product_txtoption['mode']
  579.             );
  580.         }
  581.  
  582.         return $product_txtoption_data;
  583.     }
  584. //End Textoption function
  585.  
  586.     public function getProductImages($product_id) {
  587.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  588.        
  589.         return $query->rows;
  590.     }
  591.    
  592.     public function getProductDiscounts($product_id) {
  593.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' ORDER BY quantity, priority, price");
  594.        
  595.         return $query->rows;
  596.     }
  597.    
  598.     public function getProductSpecials($product_id) {
  599.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' ORDER BY priority, price");
  600.        
  601.         return $query->rows;
  602.     }
  603.    
  604.     public function getProductDownloads($product_id) {
  605.         $product_download_data = array();
  606.        
  607.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  608.        
  609.         foreach ($query->rows as $result) {
  610.             $product_download_data[] = $result['download_id'];
  611.         }
  612.        
  613.         return $product_download_data;
  614.     }
  615.  
  616.     public function getProductStores($product_id) {
  617.         $product_store_data = array();
  618.        
  619.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  620.  
  621.         foreach ($query->rows as $result) {
  622.             $product_store_data[] = $result['store_id'];
  623.         }
  624.        
  625.         return $product_store_data;
  626.     }
  627.    
  628.     public function getProductCategories($product_id) {
  629.         $product_category_data = array();
  630.        
  631.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  632.        
  633.         foreach ($query->rows as $result) {
  634.             $product_category_data[] = $result['category_id'];
  635.         }
  636.  
  637.         return $product_category_data;
  638.     }
  639.  
  640.     public function getProductAttributes($product_id) {
  641.         $product_attribute_data = array();
  642.  
  643.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_attribute WHERE product_id = '" . (int)$product_id . "'");
  644.  
  645.         foreach ($query->rows as $result) {
  646.             $product_attribute_data[] = $result['attribute_id'];
  647.         }
  648.  
  649.         return $product_attribute_data;
  650.     }
  651.  
  652.     public function getProductRelated($product_id) {
  653.         $product_related_data = array();
  654.        
  655.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  656.        
  657.         foreach ($query->rows as $result) {
  658.             $product_related_data[] = $result['related_id'];
  659.         }
  660.        
  661.         return $product_related_data;
  662.     }
  663.    
  664.     public function getProductTags($product_id) {
  665.         $product_tag_data = array();
  666.        
  667.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_tags WHERE product_id = '" . (int)$product_id . "'");
  668.        
  669.         $tag_data = array();
  670.        
  671.         foreach ($query->rows as $result) {
  672.             $tag_data[$result['language_id']][] = $result['tag'];
  673.         }
  674.        
  675.         foreach ($tag_data as $language => $tags) {
  676.             $product_tag_data[$language] = implode(',', $tags);
  677.         }
  678.        
  679.         return $product_tag_data;
  680.     }
  681.    
  682.     public function getTotalProducts($data = array()) {
  683.         $sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  684.        
  685.         if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
  686.             $sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
  687.         }
  688.  
  689.         if (isset($data['filter_model']) && !is_null($data['filter_model'])) {
  690.             $sql .= " AND LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($data['filter_model'])) . "%'";
  691.         }
  692.  
  693.         if (isset($data['filter_price']) && !is_null($data['filter_price'])) {
  694.             $sql .= " AND LCASE(p.price) LIKE '" . $this->db->escape(strtolower($data['filter_price'])) . "%'";
  695.         }
  696. //Added By Chad    
  697.         if (isset($data['filter_price_full']) && !is_null($data['filter_price_full'])) {
  698.             $sql .= " AND LCASE(p.price) LIKE '" . $this->db->escape(strtolower($data['filter_price_full'])) . "%'";
  699.         }
  700. //Added By Chad
  701.         if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  702.             $sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
  703.         }
  704.  
  705.         if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  706.             $sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
  707.         }
  708.  
  709.         $query = $this->db->query($sql);
  710.  
  711.         return $query->row['total'];
  712.     }
  713.  
  714.     public function getTotalProductsByStockStatusId($stock_status_id) {
  715.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE stock_status_id = '" . (int)$stock_status_id . "'");
  716.  
  717.         return $query->row['total'];
  718.     }
  719.  
  720.     public function getTotalProductsByImageId($image_id) {
  721.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE image_id = '" . (int)$image_id . "'");
  722.  
  723.         return $query->row['total'];
  724.     }
  725.  
  726.     public function getTotalProductsByTaxClassId($tax_class_id) {
  727.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE tax_class_id = '" . (int)$tax_class_id . "'");
  728.  
  729.         return $query->row['total'];
  730.     }
  731.  
  732.     public function getTotalProductsByWeightClassId($weight_class_id) {
  733.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE weight_class_id = '" . (int)$weight_class_id . "'");
  734.  
  735.         return $query->row['total'];
  736.     }
  737.  
  738.     public function getTotalProductsByLengthClassId($length_class_id) {
  739.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE length_class_id = '" . (int)$length_class_id . "'");
  740.  
  741.         return $query->row['total'];
  742.     }
  743.  
  744.     public function getTotalProductsByOptionId($option_id) {
  745.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_option WHERE option_id = '" . (int)$option_id . "'");
  746.  
  747.         return $query->row['total'];
  748.     }
  749.  
  750.     public function getTotalProductsByDownloadId($download_id) {
  751.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_download WHERE download_id = '" . (int)$download_id . "'");
  752.  
  753.         return $query->row['total'];
  754.     }
  755.  
  756.     public function getTotalProductsByManufacturerId($manufacturer_id) {
  757.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
  758.  
  759.         return $query->row['total'];
  760.     }
  761. }
  762. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement