public function getImagesDataByBrand() { $query = "SELECT\n `file`.`file_name`,\n `file`.`file_id`,\n `detail`.`detail_id`,\n CONCAT(:img_path, LPAD((FLOOR((`file`.`file_id` / 1000)) * 1000), 6, 0), '/', LPAD(`file`.`file_id`, 6, 0), '/', `file`.`file_name`) AS `img_path`\n FROM `amz_file` AS `file`\n INNER JOIN `amz_glasses_item_detail` AS `detail` ON `detail`.`file_id` = `file`.`file_id`\n LEFT JOIN `amz_glasses_item` AS `item` ON `item`.`item_id` = `detail`.`item_id`\n LEFT JOIN `amz_glasses_brand` AS `brand` ON `brand`.`brand_id` = `item`.`brand_id`\n WHERE `brand`.`brand_id` = :brand_id\n AND `item`.`is_valid` = :is_valid"; $q = CMSPluginDb::getInstance()->getQuery($query); $q->setInt('brand_id', $this->brandId); $q->setInt('is_valid', 1); $q->setText('img_path', DOCROOT . self::IMG_PATH); $data = $q->execute(); $imgData = $data->getData(); if (empty($imgData)) { die("No one image for brand!"); } return $imgData; }
/** * @param string $colorCode * @return bool * @throws CMSException */ private function getUpcAndSize($colorCode) { $query = "SELECT * FROM `amz_upc` WHERE `provider_id` = :provider_id AND `model` = :colorCode"; $q = CMSPluginDb::getInstance()->getQuery($query); $q->setInt('provider_id', $this->getProviderId()); $q->setText('colorCode', $colorCode); $data = $q->execute(); $upcData = $data->getData(); if (empty($upcData)) { return false; } $upc = current($upcData)['upc']; $size = current($upcData)['size']; if (strlen($upc) < self::UPC_MIN_LENGTH) { $upc = "0" . $upc; } return array('upc' => $upc, 'size' => $size); }
/** * @param string $itemTitle * @return string */ private function getUpcCode($itemTitle, $colorCode, $sizeOne) { if ($itemTitle[0] == self::CUT_SYMBOL) { $itemTitle = mb_substr($itemTitle, 1); } $itemTitleVariantOne = preg_replace('/ +/', ' ', $itemTitle); $itemTitleVariantTwo = str_replace(' ', '', $itemTitleVariantOne); $itemTitleVariantThree = str_replace(' ', '0', $itemTitleVariantOne); $itemTitleVariantFour = str_replace(' ', ' 0', $itemTitleVariantOne); $query = "SELECT * FROM amz_upc\r\n WHERE provider_id = :provider_id\r\n AND (`model` LIKE :item_title_variant_one\r\n OR `model` LIKE :item_title_variant_two\r\n OR `model` LIKE :item_title_variant_three\r\n OR `model` LIKE :item_title_variant_four)\r\n AND `color` LIKE :color_code\r\n AND `size` = :size_one"; $q = CMSPluginDb::getInstance()->getQuery($query); $q->setInt('provider_id', $this->getProviderId()); $q->setText('item_title_variant_one', '%' . $itemTitleVariantOne . '%'); $q->setText('item_title_variant_two', '%' . $itemTitleVariantTwo . '%'); $q->setText('item_title_variant_three', '%' . $itemTitleVariantThree . '%'); $q->setText('item_title_variant_four', '%' . $itemTitleVariantFour . '%'); $q->setText('color_code', '%' . $colorCode . '%'); $q->setText('size_one', $sizeOne); $data = $q->execute(); $upcData = $data->getData(); if (empty($upcData)) { return false; } return current($upcData)['upc']; }
/** * Парсинг страницы модели * @param array $product_link_arr массив с названием модели, ссылкой и типом * @param CMSTableBrand $brand */ private function parseItem($product_link_arr, CMSTableBrand $brand) { $variation_links_arr = array(); $variations = array(); $result = array(); $item_name = $product_link_arr['name']; $item_url = $product_link_arr['href']; $item_type = $product_link_arr['type']; $variation_links_arr = $this->getVariationLinks($item_url); // не нашлось вариаций if (!$variation_links_arr) { return; } // определяем тип очков if ($item_type === "eye") { $type = CMSLogicGlassesItemType::getInstance()->getEye(); } else { $type = CMSLogicGlassesItemType::getInstance()->getSun(); } $variations = $this->getVariations($variation_links_arr); // формируем массив обьектов foreach ($variations as $key => $variation) { $external_id = $variation['item_code'] . " prov " . $this->getProviderId(); $final_item_name = $item_name . " " . $variation['item_code']; // небольшой лог echo "\n--------url - " . $item_url . "\n"; echo "--------item title - " . $final_item_name . "\n"; echo "--------item ext id - " . $external_id . "\n"; echo "--------item code - " . $variation['item_code'] . "\n"; echo "--------color code - " . $variation['color_code'] . "\n"; echo "--------type - " . $item_type . "\n"; echo "--------item sizes - " . $variation['size'] . "/0/0\n"; echo "--------color title - " . $variation['color'] . "\n"; echo "--------item image - " . $variation['image'] . "\n"; echo "--------stock - " . $variation['stock'] . "\n"; echo "--------price - " . $variation['price'] . "\n\n"; if (!$variation['stock']) { echo "--------Variation {$variation['color']} ({$variation['color_code']}) not in stock. (not parse!)\n"; echo "==================================================================\n"; continue; } if (!$variation['image']) { echo "--------Variation {$variation['color']} ({$variation['color_code']}) hasnt image.\n"; // костыль // смотим есть ли эта деталь уже в базе и есть ли у нее изображение // если да то предполагаем что на сайте пропала картинка и синхронизируем $query = "SELECT `d`.`file_id`\n FROM `amz_glasses_item` AS `i`\n LEFT JOIN `amz_glasses_item_detail` AS `d` ON `d`.`item_id` = `i`.`item_id`\n WHERE `i`.`item_ext_id` = :item_ext_id\n AND `d`.`detail_color_code` = :detail_color_code"; $q = CMSPluginDb::getInstance()->getQuery($query); $q->setText('item_ext_id', $external_id); $q->setText('detail_color_code', $variation['color_code']); $row = $q->execute()->getFirstRecord(); if (!$row) { echo "----------Variation hasnt image in base too.(not parse!)\n"; echo "==================================================================\n"; continue; } } $this->count_variations++; // это несколько иной метод получения картинки, который предпочтительнее для sftp $imgFile = CMSLogicGlassesFileCache::getInstance()->getOneMarchon($variation['image'], true, clone $this->getHttp()); echo "==================================================================\n"; $item = new CMSClassGlassesParserItem(); $item->setTitle($final_item_name); $item->setBrand($brand); $item->setExternalId($external_id); $item->setImg($variation['image']); if ($imgFile) { $item->setImgFile($imgFile->getFile()); } $item->setType($type); $item->setColor($variation['color']); $item->setColorCode($variation['color_code']); $item->setStockCount(1); $item->setPrice($variation['price']); $item->setSize($variation['size']); $item->setSize2(0); $item->setSize3(0); $item->setIsValid(1); $result[] = $item; } if (empty($result)) { echo "\n--------No one variation to parse.\n\n"; return; } $this->count_items++; foreach ($result as $item) { if ($item instanceof CMSClassGlassesParserItem) { $item->sync(); echo "ok\n"; } } }