PageRenderTime 4ms CodeModel.GetById 5ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 1ms

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