shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

product.php (41127B)


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