PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/model/catalog/product.php

https://bitbucket.org/emetakom/opencart_serbian
PHP | 755 lines | 566 code | 189 blank | 0 comment | 98 complexity | 8cf4ccd5898c5a44898da0515de925e6 MD5 | raw file
  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']) . "', upc = '" . $this->db->escape($data['upc']) . "', 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'] . "', points = '" . (int)$data['points'] . "', 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 = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
  5. $product_id = $this->db->getLastId();
  6. if (isset($data['image'])) {
  7. $this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
  8. }
  9. foreach ($data['product_description'] as $language_id => $value) {
  10. $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_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
  11. }
  12. if (isset($data['product_store'])) {
  13. foreach ($data['product_store'] as $store_id) {
  14. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
  15. }
  16. }
  17. if (isset($data['product_attribute'])) {
  18. foreach ($data['product_attribute'] as $product_attribute) {
  19. if ($product_attribute['attribute_id']) {
  20. $this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id = '" . (int)$product_attribute['attribute_id'] . "'");
  21. foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {
  22. $this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . "', attribute_id = '" . (int)$product_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" . $this->db->escape($product_attribute_description['text']) . "'");
  23. }
  24. }
  25. }
  26. }
  27. if (isset($data['product_option'])) {
  28. foreach ($data['product_option'] as $product_option) {
  29. if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
  30. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");
  31. $product_option_id = $this->db->getLastId();
  32. if (isset($product_option['product_option_value'])) {
  33. foreach ($product_option['product_option_value'] as $product_option_value) {
  34. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . $this->db->escape($product_option_value['option_value_id']) . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");
  35. }
  36. }
  37. } else {
  38. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value = '" . $this->db->escape($product_option['option_value']) . "', required = '" . (int)$product_option['required'] . "'");
  39. }
  40. }
  41. }
  42. if (isset($data['product_discount'])) {
  43. foreach ($data['product_discount'] as $product_discount) {
  44. $this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_discount['customer_group_id'] . "', quantity = '" . (int)$product_discount['quantity'] . "', priority = '" . (int)$product_discount['priority'] . "', price = '" . (float)$product_discount['price'] . "', date_start = '" . $this->db->escape($product_discount['date_start']) . "', date_end = '" . $this->db->escape($product_discount['date_end']) . "'");
  45. }
  46. }
  47. if (isset($data['product_special'])) {
  48. foreach ($data['product_special'] as $product_special) {
  49. $this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_special['customer_group_id'] . "', priority = '" . (int)$product_special['priority'] . "', price = '" . (float)$product_special['price'] . "', date_start = '" . $this->db->escape($product_special['date_start']) . "', date_end = '" . $this->db->escape($product_special['date_end']) . "'");
  50. }
  51. }
  52. if (isset($data['product_image'])) {
  53. foreach ($data['product_image'] as $product_image) {
  54. $this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($product_image['image']) . "', sort_order = '" . (int)$product_image['sort_order'] . "'");
  55. }
  56. }
  57. if (isset($data['product_download'])) {
  58. foreach ($data['product_download'] as $download_id) {
  59. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
  60. }
  61. }
  62. if (isset($data['product_category'])) {
  63. foreach ($data['product_category'] as $category_id) {
  64. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
  65. }
  66. }
  67. if (isset($data['product_related'])) {
  68. foreach ($data['product_related'] as $related_id) {
  69. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
  70. $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
  71. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
  72. $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
  73. }
  74. }
  75. if (isset($data['product_reward'])) {
  76. foreach ($data['product_reward'] as $customer_group_id => $product_reward) {
  77. $this->db->query("INSERT INTO " . DB_PREFIX . "product_reward SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$customer_group_id . "', points = '" . (int)$product_reward['points'] . "'");
  78. }
  79. }
  80. if (isset($data['product_layout'])) {
  81. foreach ($data['product_layout'] as $store_id => $layout) {
  82. if ($layout['layout_id']) {
  83. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_layout SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
  84. }
  85. }
  86. }
  87. foreach ($data['product_tag'] as $language_id => $value) {
  88. if ($value) {
  89. $tags = explode(',', $value);
  90. foreach ($tags as $tag) {
  91. $this->db->query("INSERT INTO " . DB_PREFIX . "product_tag SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
  92. }
  93. }
  94. }
  95. if ($data['keyword']) {
  96. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  97. }
  98. $this->cache->delete('product');
  99. }
  100. public function editProduct($product_id, $data) {
  101. $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', 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'] . "', points = '" . (int)$data['points'] . "', 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 = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
  102. if (isset($data['image'])) {
  103. $this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
  104. }
  105. $this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  106. foreach ($data['product_description'] as $language_id => $value) {
  107. $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_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
  108. }
  109. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  110. if (isset($data['product_store'])) {
  111. foreach ($data['product_store'] as $store_id) {
  112. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
  113. }
  114. }
  115. $this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "'");
  116. if (!empty($data['product_attribute'])) {
  117. foreach ($data['product_attribute'] as $product_attribute) {
  118. if ($product_attribute['attribute_id']) {
  119. $this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id = '" . (int)$product_attribute['attribute_id'] . "'");
  120. foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {
  121. $this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . "', attribute_id = '" . (int)$product_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" . $this->db->escape($product_attribute_description['text']) . "'");
  122. }
  123. }
  124. }
  125. }
  126. $this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
  127. $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
  128. if (isset($data['product_option'])) {
  129. foreach ($data['product_option'] as $product_option) {
  130. if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
  131. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");
  132. $product_option_id = $this->db->getLastId();
  133. if (isset($product_option['product_option_value'])) {
  134. foreach ($product_option['product_option_value'] as $product_option_value) {
  135. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "', product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . $this->db->escape($product_option_value['option_value_id']) . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");
  136. }
  137. }
  138. } else {
  139. $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value = '" . $this->db->escape($product_option['option_value']) . "', required = '" . (int)$product_option['required'] . "'");
  140. }
  141. }
  142. }
  143. $this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
  144. if (isset($data['product_discount'])) {
  145. foreach ($data['product_discount'] as $product_discount) {
  146. $this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_discount['customer_group_id'] . "', quantity = '" . (int)$product_discount['quantity'] . "', priority = '" . (int)$product_discount['priority'] . "', price = '" . (float)$product_discount['price'] . "', date_start = '" . $this->db->escape($product_discount['date_start']) . "', date_end = '" . $this->db->escape($product_discount['date_end']) . "'");
  147. }
  148. }
  149. $this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
  150. if (isset($data['product_special'])) {
  151. foreach ($data['product_special'] as $product_special) {
  152. $this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_special['customer_group_id'] . "', priority = '" . (int)$product_special['priority'] . "', price = '" . (float)$product_special['price'] . "', date_start = '" . $this->db->escape($product_special['date_start']) . "', date_end = '" . $this->db->escape($product_special['date_end']) . "'");
  153. }
  154. }
  155. $this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  156. if (isset($data['product_image'])) {
  157. foreach ($data['product_image'] as $product_image) {
  158. $this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($product_image['image']) . "', sort_order = '" . (int)$product_image['sort_order'] . "'");
  159. }
  160. }
  161. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  162. if (isset($data['product_download'])) {
  163. foreach ($data['product_download'] as $download_id) {
  164. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
  165. }
  166. }
  167. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  168. if (isset($data['product_category'])) {
  169. foreach ($data['product_category'] as $category_id) {
  170. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
  171. }
  172. }
  173. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  174. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");
  175. if (isset($data['product_related'])) {
  176. foreach ($data['product_related'] as $related_id) {
  177. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
  178. $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
  179. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
  180. $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
  181. }
  182. }
  183. $this->db->query("DELETE FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "'");
  184. if (isset($data['product_reward'])) {
  185. foreach ($data['product_reward'] as $customer_group_id => $value) {
  186. $this->db->query("INSERT INTO " . DB_PREFIX . "product_reward SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$customer_group_id . "', points = '" . (int)$value['points'] . "'");
  187. }
  188. }
  189. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "'");
  190. if (isset($data['product_layout'])) {
  191. foreach ($data['product_layout'] as $store_id => $layout) {
  192. if ($layout['layout_id']) {
  193. $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_layout SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
  194. }
  195. }
  196. }
  197. $this->db->query("DELETE FROM " . DB_PREFIX . "product_tag WHERE product_id = '" . (int)$product_id. "'");
  198. foreach ($data['product_tag'] as $language_id => $value) {
  199. if ($value) {
  200. $tags = explode(',', $value);
  201. foreach ($tags as $tag) {
  202. $this->db->query("INSERT INTO " . DB_PREFIX . "product_tag SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
  203. }
  204. }
  205. }
  206. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
  207. if ($data['keyword']) {
  208. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  209. }
  210. $this->cache->delete('product');
  211. }
  212. public function copyProduct($product_id) {
  213. $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') . "'");
  214. if ($query->num_rows) {
  215. $data = array();
  216. $data = $query->row;
  217. $data['keyword'] = '';
  218. $data['status'] = '0';
  219. $data = array_merge($data, array('product_attribute' => $this->getProductAttributes($product_id)));
  220. $data = array_merge($data, array('product_description' => $this->getProductDescriptions($product_id)));
  221. $data = array_merge($data, array('product_discount' => $this->getProductDiscounts($product_id)));
  222. $data = array_merge($data, array('product_image' => $this->getProductImages($product_id)));
  223. $data['product_image'] = array();
  224. $results = $this->getProductImages($product_id);
  225. foreach ($results as $result) {
  226. $data['product_image'][] = $result['image'];
  227. }
  228. $data = array_merge($data, array('product_option' => $this->getProductOptions($product_id)));
  229. $data = array_merge($data, array('product_related' => $this->getProductRelated($product_id)));
  230. $data = array_merge($data, array('product_reward' => $this->getProductRewards($product_id)));
  231. $data = array_merge($data, array('product_special' => $this->getProductSpecials($product_id)));
  232. $data = array_merge($data, array('product_tag' => $this->getProductTags($product_id)));
  233. $data = array_merge($data, array('product_category' => $this->getProductCategories($product_id)));
  234. $data = array_merge($data, array('product_download' => $this->getProductDownloads($product_id)));
  235. $data = array_merge($data, array('product_layout' => $this->getProductLayouts($product_id)));
  236. $data = array_merge($data, array('product_store' => $this->getProductStores($product_id)));
  237. $this->addProduct($data);
  238. }
  239. }
  240. public function deleteProduct($product_id) {
  241. $this->db->query("DELETE FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
  242. $this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "'");
  243. $this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  244. $this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
  245. $this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  246. $this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
  247. $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
  248. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  249. $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");
  250. $this->db->query("DELETE FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "'");
  251. $this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
  252. $this->db->query("DELETE FROM " . DB_PREFIX . "product_tag WHERE product_id='" . (int)$product_id. "'");
  253. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  254. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  255. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "'");
  256. $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  257. $this->db->query("DELETE FROM " . DB_PREFIX . "review WHERE product_id = '" . (int)$product_id . "'");
  258. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
  259. $this->cache->delete('product');
  260. }
  261. public function getProduct($product_id) {
  262. $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') . "'");
  263. return $query->row;
  264. }
  265. public function getProducts($data = array()) {
  266. if ($data) {
  267. $sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id)";
  268. if (!empty($data['filter_category_id'])) {
  269. $sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";
  270. }
  271. $sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  272. if (!empty($data['filter_name'])) {
  273. $sql .= " AND LCASE(pd.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
  274. }
  275. if (!empty($data['filter_model'])) {
  276. $sql .= " AND LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_model'])) . "%'";
  277. }
  278. if (!empty($data['filter_price'])) {
  279. $sql .= " AND p.price LIKE '" . $this->db->escape($data['filter_price']) . "%'";
  280. }
  281. if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  282. $sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
  283. }
  284. if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  285. $sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
  286. }
  287. if (!empty($data['filter_category_id'])) {
  288. if (!empty($data['filter_sub_category'])) {
  289. $implode_data = array();
  290. $implode_data[] = "category_id = '" . (int)$data['filter_category_id'] . "'";
  291. $this->load->model('catalog/category');
  292. $categories = $this->model_catalog_category->getCategories($data['filter_category_id']);
  293. foreach ($categories as $category) {
  294. $implode_data[] = "p2c.category_id = '" . (int)$category['category_id'] . "'";
  295. }
  296. $sql .= " AND (" . implode(' OR ', $implode_data) . ")";
  297. } else {
  298. $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  299. }
  300. }
  301. $sql .= " GROUP BY p.product_id";
  302. $sort_data = array(
  303. 'pd.name',
  304. 'p.model',
  305. 'p.price',
  306. 'p.quantity',
  307. 'p.status',
  308. 'p.sort_order'
  309. );
  310. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  311. $sql .= " ORDER BY " . $data['sort'];
  312. } else {
  313. $sql .= " ORDER BY pd.name";
  314. }
  315. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  316. $sql .= " DESC";
  317. } else {
  318. $sql .= " ASC";
  319. }
  320. if (isset($data['start']) || isset($data['limit'])) {
  321. if ($data['start'] < 0) {
  322. $data['start'] = 0;
  323. }
  324. if ($data['limit'] < 1) {
  325. $data['limit'] = 20;
  326. }
  327. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  328. }
  329. $query = $this->db->query($sql);
  330. return $query->rows;
  331. } else {
  332. $product_data = $this->cache->get('product.' . (int)$this->config->get('config_language_id'));
  333. if (!$product_data) {
  334. $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");
  335. $product_data = $query->rows;
  336. $this->cache->set('product.' . (int)$this->config->get('config_language_id'), $product_data);
  337. }
  338. return $product_data;
  339. }
  340. }
  341. public function getProductsByCategoryId($category_id) {
  342. $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");
  343. return $query->rows;
  344. }
  345. public function getProductDescriptions($product_id) {
  346. $product_description_data = array();
  347. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
  348. foreach ($query->rows as $result) {
  349. $product_description_data[$result['language_id']] = array(
  350. 'name' => $result['name'],
  351. 'description' => $result['description'],
  352. 'meta_keyword' => $result['meta_keyword'],
  353. 'meta_description' => $result['meta_description']
  354. );
  355. }
  356. return $product_description_data;
  357. }
  358. public function getProductAttributes($product_id) {
  359. $product_attribute_data = array();
  360. $product_attribute_query = $this->db->query("SELECT pa.attribute_id, ad.name FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_id . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY pa.attribute_id");
  361. foreach ($product_attribute_query->rows as $product_attribute) {
  362. $product_attribute_description_data = array();
  363. $product_attribute_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id = '" . (int)$product_attribute['attribute_id'] . "'");
  364. foreach ($product_attribute_description_query->rows as $product_attribute_description) {
  365. $product_attribute_description_data[$product_attribute_description['language_id']] = array('text' => $product_attribute_description['text']);
  366. }
  367. $product_attribute_data[] = array(
  368. 'attribute_id' => $product_attribute['attribute_id'],
  369. 'name' => $product_attribute['name'],
  370. 'product_attribute_description' => $product_attribute_description_data
  371. );
  372. }
  373. return $product_attribute_data;
  374. }
  375. public function getProductOptions($product_id) {
  376. $product_option_data = array();
  377. $product_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.product_id = '" . (int)$product_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  378. foreach ($product_option_query->rows as $product_option) {
  379. if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
  380. $product_option_value_data = array();
  381. $product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
  382. foreach ($product_option_value_query->rows as $product_option_value) {
  383. $product_option_value_data[] = array(
  384. 'product_option_value_id' => $product_option_value['product_option_value_id'],
  385. 'option_value_id' => $product_option_value['option_value_id'],
  386. 'name' => $product_option_value['name'],
  387. 'image' => $product_option_value['image'],
  388. 'quantity' => $product_option_value['quantity'],
  389. 'subtract' => $product_option_value['subtract'],
  390. 'price' => $product_option_value['price'],
  391. 'price_prefix' => $product_option_value['price_prefix'],
  392. 'points' => $product_option_value['points'],
  393. 'points_prefix' => $product_option_value['points_prefix'],
  394. 'weight' => $product_option_value['weight'],
  395. 'weight_prefix' => $product_option_value['weight_prefix']
  396. );
  397. }
  398. $product_option_data[] = array(
  399. 'product_option_id' => $product_option['product_option_id'],
  400. 'option_id' => $product_option['option_id'],
  401. 'name' => $product_option['name'],
  402. 'type' => $product_option['type'],
  403. 'product_option_value' => $product_option_value_data,
  404. 'required' => $product_option['required']
  405. );
  406. } else {
  407. $product_option_data[] = array(
  408. 'product_option_id' => $product_option['product_option_id'],
  409. 'option_id' => $product_option['option_id'],
  410. 'name' => $product_option['name'],
  411. 'type' => $product_option['type'],
  412. 'option_value' => $product_option['option_value'],
  413. 'required' => $product_option['required']
  414. );
  415. }
  416. }
  417. return $product_option_data;
  418. }
  419. public function getProductImages($product_id) {
  420. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
  421. return $query->rows;
  422. }
  423. public function getProductDiscounts($product_id) {
  424. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' ORDER BY quantity, priority, price");
  425. return $query->rows;
  426. }
  427. public function getProductSpecials($product_id) {
  428. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' ORDER BY priority, price");
  429. return $query->rows;
  430. }
  431. public function getProductRewards($product_id) {
  432. $product_reward_data = array();
  433. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "'");
  434. foreach ($query->rows as $result) {
  435. $product_reward_data[$result['customer_group_id']] = array('points' => $result['points']);
  436. }
  437. return $product_reward_data;
  438. }
  439. public function getProductDownloads($product_id) {
  440. $product_download_data = array();
  441. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
  442. foreach ($query->rows as $result) {
  443. $product_download_data[] = $result['download_id'];
  444. }
  445. return $product_download_data;
  446. }
  447. public function getProductStores($product_id) {
  448. $product_store_data = array();
  449. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
  450. foreach ($query->rows as $result) {
  451. $product_store_data[] = $result['store_id'];
  452. }
  453. return $product_store_data;
  454. }
  455. public function getProductLayouts($product_id) {
  456. $product_layout_data = array();
  457. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "'");
  458. foreach ($query->rows as $result) {
  459. $product_layout_data[$result['store_id']] = $result['layout_id'];
  460. }
  461. return $product_layout_data;
  462. }
  463. public function getProductCategories($product_id) {
  464. $product_category_data = array();
  465. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
  466. foreach ($query->rows as $result) {
  467. $product_category_data[] = $result['category_id'];
  468. }
  469. return $product_category_data;
  470. }
  471. public function getProductRelated($product_id) {
  472. $product_related_data = array();
  473. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
  474. foreach ($query->rows as $result) {
  475. $product_related_data[] = $result['related_id'];
  476. }
  477. return $product_related_data;
  478. }
  479. public function getProductTags($product_id) {
  480. $product_tag_data = array();
  481. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_tag WHERE product_id = '" . (int)$product_id . "'");
  482. $tag_data = array();
  483. foreach ($query->rows as $result) {
  484. $tag_data[$result['language_id']][] = $result['tag'];
  485. }
  486. foreach ($tag_data as $language => $tags) {
  487. $product_tag_data[$language] = implode(',', $tags);
  488. }
  489. return $product_tag_data;
  490. }
  491. public function getTotalProducts($data = array()) {
  492. $sql = "SELECT COUNT(DISTINCT p.product_id) AS total FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id)";
  493. if (!empty($data['filter_category_id'])) {
  494. $sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";
  495. }
  496. $sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  497. if (!empty($data['filter_name'])) {
  498. $sql .= " AND LCASE(pd.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
  499. }
  500. if (!empty($data['filter_model'])) {
  501. $sql .= " AND LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_model'])) . "%'";
  502. }
  503. if (!empty($data['filter_price'])) {
  504. $sql .= " AND p.price LIKE '" . $this->db->escape($data['filter_price']) . "%'";
  505. }
  506. if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
  507. $sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
  508. }
  509. if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  510. $sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
  511. }
  512. if (!empty($data['filter_category_id'])) {
  513. if (!empty($data['filter_sub_category'])) {
  514. $implode_data = array();
  515. $implode_data[] = "p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  516. $this->load->model('catalog/category');
  517. $categories = $this->model_catalog_category->getCategories($data['filter_category_id']);
  518. foreach ($categories as $category) {
  519. $implode_data[] = "p2c.category_id = '" . (int)$category['category_id'] . "'";
  520. }
  521. $sql .= " AND (" . implode(' OR ', $implode_data) . ")";
  522. } else {
  523. $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  524. }
  525. }
  526. $query = $this->db->query($sql);
  527. return $query->row['total'];
  528. }
  529. public function getTotalProductsByTaxClassId($tax_class_id) {
  530. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE tax_class_id = '" . (int)$tax_class_id . "'");
  531. return $query->row['total'];
  532. }
  533. public function getTotalProductsByStockStatusId($stock_status_id) {
  534. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE stock_status_id = '" . (int)$stock_status_id . "'");
  535. return $query->row['total'];
  536. }
  537. public function getTotalProductsByWeightClassId($weight_class_id) {
  538. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE weight_class_id = '" . (int)$weight_class_id . "'");
  539. return $query->row['total'];
  540. }
  541. public function getTotalProductsByLengthClassId($length_class_id) {
  542. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE length_class_id = '" . (int)$length_class_id . "'");
  543. return $query->row['total'];
  544. }
  545. public function getTotalProductsByDownloadId($download_id) {
  546. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_download WHERE download_id = '" . (int)$download_id . "'");
  547. return $query->row['total'];
  548. }
  549. public function getTotalProductsByManufacturerId($manufacturer_id) {
  550. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
  551. return $query->row['total'];
  552. }
  553. public function getTotalProductsByAttributeId($attribute_id) {
  554. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
  555. return $query->row['total'];
  556. }
  557. public function getTotalProductsByOptionId($option_id) {
  558. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_option WHERE option_id = '" . (int)$option_id . "'");
  559. return $query->row['total'];
  560. }
  561. public function getTotalProductsByLayoutId($layout_id) {
  562. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_layout WHERE layout_id = '" . (int)$layout_id . "'");
  563. return $query->row['total'];
  564. }
  565. }
  566. ?>