ebay_product.php (31210B)
1 <?php 2 class ModelExtensionOpenBayEbayProduct extends Model { 3 public function getRelistRule($id) { 4 return $this->openbay->ebay->call('item/getAutomationRule', array('id' => $id)); 5 } 6 7 public function importItems($data) { 8 $this->openbay->ebay->log('Starting item import'); 9 $this->load->model('catalog/product'); 10 11 //check for ebay import img table 12 $res = $this->db->query("SHOW TABLES LIKE '" . DB_PREFIX . "ebay_image_import'"); 13 if ($res->num_rows == 0) { 14 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_image_import` (`id` int(11) NOT NULL AUTO_INCREMENT, `image_original` text NOT NULL, `image_new` text NOT NULL, `name` text NOT NULL, `product_id` int(11) NOT NULL, `imgcount` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); 15 } 16 17 if ($this->openbay->addonLoad('openstock')) { 18 $openstock = true; 19 $this->load->model('extension/module/openstock'); 20 } else { 21 $openstock = false; 22 $this->openbay->ebay->log('Openstock module not found'); 23 } 24 25 $categories = array(); 26 $data['data'] = unserialize(gzuncompress(stripslashes(base64_decode(strtr($data['data'], '-_,', '+/='))))); 27 $new_data = base64_decode($data['data']); 28 unset($data['data']); 29 30 $options = json_decode(html_entity_decode($data['options']), 1); 31 $this->openbay->ebay->log('Options data from API'); 32 $this->openbay->ebay->log($data['options']); 33 $this->openbay->ebay->log('Decoded options'); 34 $this->openbay->ebay->log(print_r($options, 1)); 35 $this->openbay->ebay->log('Decoded data'); 36 37 $new_data_1 = unserialize($new_data); 38 unset($new_data); 39 40 $this->openbay->ebay->log('Data unserialized'); 41 42 if ($options['cat'] == 1 || !isset($options['cat'])) { 43 $item_count_loop = 0; 44 foreach ($new_data_1 as $item) { 45 $item_count_loop++; 46 $this->openbay->ebay->log('Processing item: ' . $item_count_loop); 47 48 $parts = explode(':', $item['CategoryName']); 49 50 //skip the first category as they are likely to be selling in that 51 if (isset($parts[1])) { 52 if (!isset($categories[$parts[0]][$parts[1]])) { 53 if (!empty($parts[1])) { 54 $categories[$parts[0]][$parts[1]] = array(); 55 } 56 } 57 } 58 59 if (isset($parts[2])) { 60 if (!isset($categories[$parts[0]][$parts[1]][$parts[2]])) { 61 if (!empty($parts[2])) { 62 $categories[$parts[0]][$parts[1]][$parts[2]] = array(); 63 } 64 } 65 } 66 67 if (isset($parts[3])) { 68 if (!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]])) { 69 if (!empty($parts[3])) { 70 $categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]] = array(); 71 } 72 } 73 } 74 75 if (isset($parts[4])) { 76 if (!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]])) { 77 if (!empty($parts[4])) { 78 $categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]] = array(); 79 } 80 } 81 } 82 83 if (isset($parts[5])) { 84 if (!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]])) { 85 if (!empty($parts[5])) { 86 $categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]] = array(); 87 } 88 } 89 } 90 } 91 92 $cat_link = array(); 93 foreach ($categories as $key1 => $cat1) { 94 foreach ($cat1 as $key2 => $cat2) { 95 //final cat, add to array as node 96 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '0' AND `" . DB_PREFIX . "category_description`.`name` = '" . $this->db->escape($key2) . "' LIMIT 1"); 97 98 if ($qry->num_rows != 0) { 99 $id1 = $qry->row['category_id']; 100 } else { 101 $this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '0', `status` = '1', `top` = '1'"); 102 $id1 = $this->db->getLastId(); 103 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '" . $this->db->escape($key2) . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `category_id` = '" . $this->db->escape($id1) . "'"); 104 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '" . $this->db->escape($id1) . "', `store_id` = '0'"); 105 } 106 107 if (!empty($cat2)) { 108 foreach ($cat2 as $key3 => $cat3) { 109 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '" . $this->db->escape($id1) . "' AND `" . DB_PREFIX . "category_description`.`name` = '" . $this->db->escape($key3) . "' LIMIT 1"); 110 111 if ($qry->num_rows != 0) { 112 $id2 = $qry->row['category_id']; 113 } else { 114 $this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '" . $this->db->escape($id1) . "', `status` = '1', `top` = '1'"); 115 $id2 = $this->db->getLastId(); 116 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '" . $this->db->escape($key3) . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `category_id` = '" . $this->db->escape($id2) . "'"); 117 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '" . $this->db->escape($id2) . "', `store_id` = '0'"); 118 } 119 120 if (!empty($cat3)) { 121 foreach ($cat3 as $key4 => $cat4) { 122 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '" . $this->db->escape($id2) . "' AND `" . DB_PREFIX . "category_description`.`name` = '" . $this->db->escape($key4) . "' LIMIT 1"); 123 124 if ($qry->num_rows != 0) { 125 $id3 = $qry->row['category_id']; 126 } else { 127 $this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '" . $this->db->escape($id2) . "', `status` = '1', `top` = '1'"); 128 $id3 = $this->db->getLastId(); 129 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '" . $this->db->escape($key4) . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `category_id` = '" . $id3 . "'"); 130 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '" . $this->db->escape($id3) . "', `store_id` = '0'"); 131 } 132 133 if (!empty($cat4)) { 134 foreach ($cat4 as $key5 => $cat5) { 135 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '" . $this->db->escape($id3) . "' AND `" . DB_PREFIX . "category_description`.`name` = '" . $this->db->escape($key5) . "' LIMIT 1"); 136 137 if ($qry->num_rows != 0) { 138 $id4 = $qry->row['category_id']; 139 } else { 140 $this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '" . $this->db->escape($id3) . "', `status` = '1', `top` = '1'"); 141 $id4 = $this->db->getLastId(); 142 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '" . $this->db->escape($key5) . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `category_id` = '" . $this->db->escape($id4) . "'"); 143 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '" . $this->db->escape($id4) . "', `store_id` = '0'"); 144 } 145 146 $cat_link[$key1 . ':' . $key2 . ':' . $key3 . ':' . $key4 . ':' . $key5] = $id4; 147 } 148 } else { 149 $cat_link[$key1 . ':' . $key2 . ':' . $key3 . ':' . $key4] = $id3; 150 } 151 } 152 } else { 153 $cat_link[$key1 . ':' . $key2 . ':' . $key3] = $id2; 154 } 155 } 156 } else { 157 $cat_link[$key1 . ':' . $key2] = $id1; 158 } 159 } 160 } 161 162 $this->repairCategories(); 163 164 $this->openbay->ebay->log('Categories done'); 165 } else { 166 $this->openbay->ebay->log('Categories set not to be created'); 167 } 168 169 $current = $this->openbay->ebay->getLiveListingArray(); 170 171 foreach ($new_data_1 as $item) { 172 if (!in_array($item['ItemID'], $current)) { 173 $this->openbay->ebay->log('New item being created: ' . $item['ItemID']); 174 175 //get the manufacturer id 176 $manufacturer_id = 0; 177 if (!empty($item['Brand'])) { 178 $manufacturer_id = $this->manufacturerExists($item['Brand']); 179 } 180 181 //get the length class id 182 $length_class_id = 1; 183 if (isset($item['advanced']['package']['size']['width_unit']) && !empty($item['advanced']['package']['size']['width_unit'])) { 184 $length_class_id = $this->lengthClassExists($item['advanced']['package']['size']['width_unit']); 185 } 186 187 //get the weight class id 188 $weight_class_id = 1; 189 if (isset($item['advanced']['package']['weight']['major_unit']) && !empty($item['advanced']['package']['weight']['major_unit'])) { 190 $weight_class_id = $this->weightClassExists($item['advanced']['package']['weight']['major_unit']); 191 } 192 193 $tax = $this->config->get('ebay_tax'); 194 $net_price = $item['priceGross'] / (($tax / 100) + 1); 195 196 //openstock variant check 197 $openstock_sql = ''; 198 if (!empty($item['variation']) && $openstock == true) { 199 $openstock_sql = "`has_option` = '1',"; 200 } 201 202 //package weight 203 if (isset($item['advanced']['package']['weight']['major'])) { 204 $weight = $item['advanced']['package']['weight']['major'] . '.' . $item['advanced']['package']['weight']['minor']; 205 } else { 206 $weight = 0; 207 } 208 209 //package length 210 if (isset($item['advanced']['package']['size']['length'])) { 211 $length = $item['advanced']['package']['size']['length']; 212 } else { 213 $length = 0; 214 } 215 216 //package width 217 if (isset($item['advanced']['package']['size']['width'])) { 218 $width = $item['advanced']['package']['size']['width']; 219 } else { 220 $width = 0; 221 } 222 223 //package height 224 if (isset($item['advanced']['package']['size']['height'])) { 225 $height = $item['advanced']['package']['size']['height']; 226 } else { 227 $height = 0; 228 } 229 230 $this->db->query(" 231 INSERT INTO `" . DB_PREFIX . "product` SET 232 `quantity` = '" . (int)$item['Quantity'] . "', 233 `manufacturer_id` = '" . (int)$manufacturer_id . "', 234 `stock_status_id` = '6', 235 `price` = '" . (double)$net_price . "', 236 `tax_class_id` = '9', 237 `location` = '" . $this->db->escape(isset($item['note']) ? $item['note'] : '') . "', 238 `mpn` = '" . $this->db->escape(isset($item['advanced']['brand']['mpn']) ? $item['advanced']['brand']['mpn'] : '') . "', 239 `sku` = '" . $this->db->escape(isset($item['SKU']) ? $item['SKU'] : '') . "', 240 `model` = '" . $this->db->escape(isset($item['SKU']) ? $item['SKU'] : '') . "', 241 `isbn` = '" . $this->db->escape(isset($item['advanced']['isbn']) ? $item['advanced']['isbn'] : '') . "', 242 `ean` = '" . $this->db->escape(isset($item['advanced']['ean']) ? $item['advanced']['ean'] : '') . "', 243 `upc` = '" . $this->db->escape(isset($item['advanced']['upc']) ? $item['advanced']['upc'] : '') . "', 244 `weight` = '" . (double)$weight . "', 245 `weight_class_id` = '" . (int)$weight_class_id . "', 246 `length` = '" . (double)$length . "', 247 `width` = '" . (double)$width . "', 248 `height` = '" . (double)$height . "', 249 `length_class_id` = '" . (int)$length_class_id . "', 250 `subtract` = '1', 251 `minimum` = '1', 252 `status` = '1', 253 " . $openstock_sql . " 254 `date_available` = now(), 255 `date_added` = now(), 256 `date_modified` = now() 257 "); 258 259 $product_id = $this->db->getLastId(); 260 261 $this->openbay->ebay->log('Product insert done'); 262 263 //Insert product description 264 $original_description = $item['Description']; 265 266 if (!empty($original_description)) { 267 if (false !== ($item['Description'] = gzuncompress($original_description))) { 268 $item['Description'] = html_entity_decode($item['Description']); 269 } else { 270 $this->openbay->ebay->log('Description could not be decompressed, output below'); 271 $this->openbay->ebay->log($original_description); 272 $item['Description'] = ''; 273 } 274 } 275 276 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_description` SET `product_id` = '" . (int)$product_id . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `name` = '" . $this->db->escape(htmlspecialchars(base64_decode($item['Title']), ENT_COMPAT, 'UTF-8')) . "', `description` = '" . $this->db->escape(htmlspecialchars(utf8_encode($item['Description']), ENT_COMPAT, 'UTF-8')) . "'"); 277 $this->openbay->ebay->log('Product description done'); 278 279 //Insert product store link 280 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_to_store` SET `product_id` = '" . (int)$product_id . "', `store_id` = '0'"); 281 $this->openbay->ebay->log('Store link done'); 282 283 //Create any attributes from eBay for the item 284 if (!empty($item['specs'])) { 285 //check the main group exists, if not create 286 $group_id = $this->attributeGroupExists(base64_decode($item['CategoryNameSingle'])); 287 288 foreach ($item['specs'] as $spec) { 289 //check if the attribute exists in the group, if not create 290 $attribute_id = $this->attributeExists($group_id, base64_decode($spec['name'])); 291 292 //insert the attribute value into the product attribute table 293 $this->attributeAdd($product_id, $attribute_id, base64_decode($spec['value'])); 294 } 295 } 296 297 //Create the product variants for OpenStock 298 $variant = 0; 299 if (!empty($item['variation'])) { 300 $variant = 1; 301 302 if ($openstock == true) { 303 $this->openbay->ebay->log('OpenStock Loaded'); 304 $this->createVariants($product_id, $item); 305 } 306 307 $this->openbay->ebay->log('Variants done'); 308 } 309 310 //insert store to eBay item link 311 $this->openbay->ebay->createLink($product_id, $item['ItemID'], $variant); 312 313 //Insert product/category link 314 if ($options['cat'] == 1 || !isset($options['cat'])) { 315 $this->createCategoryLink($product_id, $cat_link[$item['CategoryName']]); 316 } 317 318 //images 319 $img_count = 0; 320 if (is_array($item['pictures'])) { 321 foreach ($item['pictures'] as $img) { 322 if (!empty($img)) { 323 $name = rand(500000, 1000000000); 324 $this->addImage($img, DIR_IMAGE . 'catalog/' . $name . '.jpg', $name . '.jpg', $product_id, $img_count); 325 $img_count++; 326 } 327 } 328 } 329 330 $this->openbay->ebay->log('Product import completed . '); 331 } else { 332 $this->openbay->ebay->log($item['ItemID'] . ' exists already'); 333 } 334 } 335 336 $this->openbay->ebay->log('Product data import done'); 337 $this->openbay->ebay->getImages(); 338 } 339 340 public function getDisplayProducts() { 341 $data = array(); 342 $data['search_keyword'] = $this->config->get('ebaydisplay_module_keywords'); 343 $data['seller_id'] = $this->config->get('ebaydisplay_module_username'); 344 $data['limit'] = $this->config->get('ebaydisplay_module_limit'); 345 $data['sort'] = $this->config->get('ebaydisplay_module_sort'); 346 $data['search_desc'] = $this->config->get('ebaydisplay_module_description'); 347 348 return $this->openbay->ebay->call('item/searchListingsForDisplay', $data); 349 } 350 351 private function createVariants($product_id, $data) { 352 foreach ($data['variation']['vars'] as $variant_id => $variant) { 353 foreach ($variant['opt'] as $k_opt => $v_opt) { 354 $name = base64_decode($k_opt); 355 $value = $v_opt; 356 357 $option = $this->getOption($name); 358 $option_value = $this->getOptionValue($value, $option['id']); 359 360 $product_option_id = $this->getProductOption($product_id, $option['id']); 361 $product_option_value_id = $this->getProductOptionValue($product_id, $option['id'], $option_value['id'], $product_option_id); 362 $data['variation']['vars'][$variant_id]['product_option_values'][] = $product_option_value_id; 363 } 364 365 $this->db->query("UPDATE `" . DB_PREFIX . "product_option_value` SET subtract = '0', price = '0.000', quantity = '0' WHERE product_id = '" . (int)$product_id . "'"); 366 } 367 368 $all_variants = $this->model_extension_module_openstock->calculateVariants($product_id); 369 foreach ($all_variants as $new_variant) { 370 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_option_variant` SET `product_id` = '" . (int)$product_id . "', `sku` = '', `stock` = '0', `active` = '0', `subtract` = '1', `price` = '0.00', `image` = '', `weight` = '0.00'"); 371 372 $variant_id = $this->db->getLastId(); 373 374 $i = 1; 375 foreach ($new_variant as $new_variant_value) { 376 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_option_variant_value` SET `product_option_variant_id` = '" . (int)$variant_id . "', `product_option_value_id` = '" . (int)$new_variant_value . "', `product_id` = '" . (int)$product_id . "', `sort_order` = '" . (int)$i++ . "'"); 377 } 378 } 379 380 foreach ($data['variation']['vars'] as $variant_id => $variant) { 381 $variant_row = $this->model_extension_module_openstock->getVariantByOptionValues($variant['product_option_values'], $product_id); 382 383 if (!empty($variant_row)) { 384 $this->db->query("UPDATE `" . DB_PREFIX . "product_option_variant` SET `product_id` = '" . (int)$product_id . "', `sku` = '" . $this->db->escape($variant['sku']) . "', `stock` = '" . (int)$variant['qty'] . "', `active` = 1, `price` = '" . (float)$variant['price'] . "' WHERE `product_option_variant_id` = '" . (int)$variant_row['product_option_variant_id'] . "'"); 385 } 386 } 387 } 388 389 private function getOption($name) { 390 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option` `o` LEFT JOIN `" . DB_PREFIX . "option_description` `od` ON (`od`.`option_id` = `o`.`option_id`) WHERE `od`.`name` = '" . $this->db->escape($name) . "'LIMIT 1"); 391 392 if ($qry->num_rows) { 393 return array('id' => (int)$qry->row['option_id'], 'sort' => (int)$qry->row['sort_order']); 394 } else { 395 return $this->createOption($name); 396 } 397 } 398 399 private function createOption($name) { 400 $this->db->query("INSERT INTO `" . DB_PREFIX . "option` SET `type` = 'select', `sort_order` = IFNULL((select `sort` FROM (SELECT (MAX(`sort_order`)+1) AS `sort` FROM `" . DB_PREFIX . "option`) AS `i`),0)"); 401 402 $option_id = $this->db->getLastId(); 403 404 $qry_sort = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option` WHERE `option_id` = '" . (int)$option_id . "' LIMIT 1"); 405 406 $this->db->query("INSERT INTO `" . DB_PREFIX . "option_description` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `name` = '" . $this->db->escape($name) . "', `option_id` = '" . (int)$option_id . "'"); 407 408 return array('id' => (int)$option_id, 'sort' => (int)$qry_sort->row['sort_order']); 409 } 410 411 private function getOptionValue($name, $option_id) { 412 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value` ov LEFT JOIN `" . DB_PREFIX . "option_value_description` `ovd` ON (`ovd`.`option_value_id` = `ov`.`option_value_id`) WHERE `ovd`.`name` = '" . $this->db->escape($name) . "' AND `ovd`.`option_id` = '" . (int)$option_id . "'LIMIT 1"); 413 414 if ($qry->num_rows) { 415 return array('id' => (int)$qry->row['option_value_id'], 'sort' => (int)$qry->row['sort_order']); 416 } else { 417 return $this->createOptionValue($name, $option_id); 418 } 419 } 420 421 private function createOptionValue($name, $option_id) { 422 $this->db->query("INSERT INTO `" . DB_PREFIX . "option_value` SET `option_id` = '" . (int)$option_id . "', `sort_order` = IFNULL((select `sort` FROM (SELECT (MAX(`sort_order`)+1) AS `sort` FROM `" . DB_PREFIX . "option_value`) AS `i`),0)"); 423 424 $id = $this->db->getLastId(); 425 426 $this->db->query("INSERT INTO `" . DB_PREFIX . "option_value_description` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `name` = '" . $this->db->escape($name) . "', `option_id` = '" . (int)$option_id . "', `option_value_id` = '" . (int)$id . "'"); 427 428 return array('id' => (int)$id); 429 } 430 431 private function getProductOption($product_id, $option_id) { 432 $qry = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "' AND option_id = '" . (int)$option_id . "' LIMIT 1"); 433 434 if ($qry->num_rows != 0) { 435 return $qry->row['product_option_id']; 436 } else { 437 $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$option_id . "', required = '1'"); 438 return $this->db->getLastId(); 439 } 440 } 441 442 private function getProductOptionValue($product_id, $option_id, $option_value_id, $product_option_id) { 443 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_option_value` WHERE `product_id` = '" . (int)$product_id . "' AND `option_id` = '" . (int)$option_id . "' AND `product_option_id` = '" . (int)$product_option_id . "' AND `option_value_id` = '" . (int)$option_value_id . "' LIMIT 1"); 444 445 if ($qry->num_rows != 0) { 446 return $qry->row['product_option_value_id']; 447 } else { 448 $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)$option_id . "', option_value_id = '" . (int)$option_value_id . "'"); 449 450 return $this->db->getLastId(); 451 } 452 } 453 454 private function attributeGroupExists($name) { 455 $this->openbay->ebay->log('Checking attribute group: ' . $name); 456 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_group_description` WHERE `name` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "' AND `language_id` = '" . (int)$this->config->get('config_language_id') . "' LIMIT 1"); 457 458 if ($qry->num_rows != 0) { 459 return $qry->row['attribute_group_id']; 460 } else { 461 $this->openbay->ebay->log('New group'); 462 $qry2 = $this->db->query("SELECT `sort_order` FROM `" . DB_PREFIX . "attribute_group` ORDER BY `sort_order` DESC LIMIT 1"); 463 464 if ($qry2->num_rows) { 465 $sort = $qry2->row['sort_order'] + 1; 466 } else { 467 $sort = 0; 468 } 469 470 $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_group` SET `sort_order` = '" . (int)$sort . "'"); 471 472 $id = $this->db->getLastId(); 473 474 $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_group_description` SET `attribute_group_id` = '" . (int)$id . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `name` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "'"); 475 476 return $id; 477 } 478 } 479 480 private function attributeExists($group_id, $name) { 481 $this->openbay->ebay->log('Checking attribute: ' . $name); 482 483 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_description` `ad`, `" . DB_PREFIX . "attribute` `a` WHERE `ad`.`name` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "' AND `ad`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND `a`.`attribute_id` = `ad`.`attribute_id` AND `a`.`attribute_group_id` = '" . (int)$group_id . "' LIMIT 1"); 484 485 if ($qry->num_rows != 0) { 486 return $qry->row['attribute_id']; 487 } else { 488 $this->openbay->ebay->log('New attribute'); 489 $qry2 = $this->db->query("SELECT `sort_order` FROM `" . DB_PREFIX . "attribute` ORDER BY `sort_order` DESC LIMIT 1"); 490 491 if ($qry2->num_rows) { 492 $sort = $qry2->row['sort_order'] + 1; 493 } else { 494 $sort = 0; 495 } 496 497 $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute` SET `sort_order` = '" . (int)$sort . "', `attribute_group_id` = '" . (int)$group_id . "'"); 498 499 $id = $this->db->getLastId(); 500 501 $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_description` SET `attribute_id` = '" . (int)$id . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "', `name` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "'"); 502 503 return (int)$id; 504 } 505 } 506 507 private function attributeAdd($product_id, $attribute_id, $name) { 508 $this->openbay->ebay->log('Adding product attribute'); 509 510 $sql = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_attribute` WHERE `product_id` = '" . (int)$product_id . "' AND `attribute_id` = '" . (int)$attribute_id . "' AND `language_id` = '" . (int)$this->config->get('config_language_id') . "'"); 511 512 if ($sql->num_rows == 0) { 513 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_attribute` SET `product_id` = '" . (int)$product_id . "', `attribute_id` = '" . (int)$attribute_id . "', `text` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "', `language_id` = '" . (int)$this->config->get('config_language_id') . "'"); 514 } 515 } 516 517 private function createCategoryLink($product_id, $category_id) { 518 $this->db->query("INSERT INTO `" . DB_PREFIX . "product_to_category` SET `product_id` = '" . (int)$product_id . "', `category_id` = '" . (int)$category_id . "'"); 519 } 520 521 private function manufacturerExists($name) { 522 $this->openbay->ebay->log('Checking manufacturer: ' . $name); 523 524 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "manufacturer` WHERE LCASE(`name`) = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "' LIMIT 1"); 525 526 if ($qry->num_rows != 0) { 527 return $qry->row['manufacturer_id']; 528 } else { 529 $this->openbay->ebay->log('New manufacturer'); 530 $qry2 = $this->db->query("SELECT `sort_order` FROM `" . DB_PREFIX . "manufacturer` ORDER BY `sort_order` DESC LIMIT 1"); 531 532 if ($qry2->num_rows) { 533 $sort = $qry2->row['sort_order'] + 1; 534 } else { 535 $sort = 0; 536 } 537 538 $this->db->query("INSERT INTO `" . DB_PREFIX . "manufacturer` SET `sort_order` = '" . (int)$sort . "', `name` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "'"); 539 540 $id = $this->db->getLastId(); 541 542 $this->db->query("INSERT INTO `" . DB_PREFIX . "manufacturer_to_store` SET `manufacturer_id` = '" . (int)$id . "', `store_id` = '0'"); 543 544 return $id; 545 } 546 } 547 548 private function weightClassExists($name) { 549 $this->openbay->ebay->log('Checking weight class: ' . $name); 550 551 $qry = $this->db->query("SELECT `weight_class_id` FROM `" . DB_PREFIX . "weight_class_description` WHERE LCASE(`title`) = '" . $this->db->escape(strtolower($name)) . "' LIMIT 1"); 552 553 if ($qry->num_rows != 0) { 554 return $qry->row['weight_class_id']; 555 } else { 556 $this->openbay->ebay->log('New weight class'); 557 558 $this->db->query("INSERT INTO `" . DB_PREFIX . "weight_class` SET `value` = '1'"); 559 560 $id = $this->db->getLastId(); 561 562 $this->db->query("INSERT INTO `" . DB_PREFIX . "weight_class_description` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `weight_class_id` = '" . (int)$id . "', `title` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "', `unit` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "'"); 563 564 return $id; 565 } 566 } 567 568 private function lengthClassExists($name) { 569 $this->openbay->ebay->log('Checking length class: ' . $name); 570 571 $qry = $this->db->query("SELECT `length_class_id` FROM `" . DB_PREFIX . "length_class_description` WHERE LCASE(`title`) = '" . $this->db->escape(strtolower($name)) . "' LIMIT 1"); 572 573 if ($qry->num_rows != 0) { 574 return $qry->row['length_class_id']; 575 } else { 576 $this->openbay->ebay->log('New length class'); 577 578 $this->db->query("INSERT INTO `" . DB_PREFIX . "length_class` SET `value` = '1'"); 579 580 $id = $this->db->getLastId(); 581 582 $this->db->query("INSERT INTO `" . DB_PREFIX . "length_class_description` SET `language_id` = '" . (int)$this->config->get('config_language_id') . "', `length_class_id` = '" . (int)$id . "', `title` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "', `unit` = '" . $this->db->escape(htmlspecialchars($name, ENT_COMPAT)) . "'"); 583 584 return $id; 585 } 586 } 587 588 private function addImage($orig, $new, $name, $product_id, $img_count) { 589 $orig = str_replace(' ', '%20', $orig); 590 591 $this->db->query("INSERT INTO `" . DB_PREFIX . "ebay_image_import` SET `image_original` = '" . $this->db->escape($orig) . "', `image_new` = '" . $this->db->escape($new) . "', `name` = '" . $this->db->escape($name) . "', `product_id` = '" . (int)$product_id . "', `imgcount` = '" . (int)$img_count . "'"); 592 } 593 594 public function resize($filename, $width, $height, $type = "") { 595 if (!file_exists(DIR_IMAGE . 'catalog/' . md5($filename) . '.jpg')) { 596 copy($filename, DIR_IMAGE . 'catalog/' . md5($filename) . '.jpg'); 597 } 598 599 $image_old = DIR_IMAGE . 'catalog/' . md5($filename) . '.jpg'; 600 $image_new = 'cache/ebaydisplay/' . md5($filename) . '-' . $width . 'x' . $height . $type . '.jpg'; 601 602 if (!file_exists(DIR_IMAGE . $image_new)) { 603 $path = ''; 604 605 $directories = explode('/', dirname(str_replace(' . ./', '', $image_new))); 606 607 foreach ($directories as $directory) { 608 $path = $path . '/' . $directory; 609 610 if (!file_exists(DIR_IMAGE . $path)) { 611 @mkdir(DIR_IMAGE . $path, 0777); 612 } 613 } 614 615 list($width_orig, $height_orig) = getimagesize($filename); 616 617 if ($width_orig != $width || $height_orig != $height) { 618 $image = new Image($image_old); 619 $image->resize($width, $height, $type); 620 $image->save(DIR_IMAGE . $image_new); 621 } else { 622 copy($filename, DIR_IMAGE . $image_new); 623 } 624 } 625 626 if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) { 627 return $this->config->get('config_ssl') . 'image/' . $image_new; 628 } else { 629 return $this->config->get('config_url') . 'image/' . $image_new; 630 } 631 } 632 633 private function repairCategories($parent_id = 0) { 634 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$parent_id . "'"); 635 636 foreach ($query->rows as $category) { 637 // Delete the path below the current one 638 $this->db->query("DELETE FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$category['category_id'] . "'"); 639 640 // Fix for records with no paths 641 $level = 0; 642 643 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$parent_id . "' ORDER BY level ASC"); 644 645 foreach ($query->rows as $result) { 646 $this->db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$result['path_id'] . "', level = '" . (int)$level . "'"); 647 648 $level++; 649 } 650 651 $this->db->query("REPLACE INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$category['category_id'] . "', level = '" . (int)$level . "'"); 652 653 $this->repairCategories($category['category_id']); 654 } 655 } 656 }