/**
  * @param $title
  * @param $itemUrl
  * @param CMSTableBrand $brand
  */
 private function parseItem($title, $itemUrl, CMSTableBrand $brand)
 {
     $result = array();
     echo "--Parse item " . $title . " [" . $itemUrl . "]. Get sub-items ...\n";
     $subItems = $this->getSubItems($itemUrl);
     echo "---Found " . count($subItems) . " sub-items. Get all variations ...\n";
     $variationsUrls = $this->getAllVariations($subItems);
     echo "----Found " . count($variationsUrls) . " variations. Get data ...\n";
     $variationsData = $this->getVariations($variationsUrls);
     foreach ($variationsData as $variation) {
         $this->countVariation++;
         $size = isset($variation['size']) ? $variation['size'] : 0;
         if ($variation['upc']) {
             $this->countVariationWithUpc++;
         }
         if (!$variation['stock']) {
             echo "\n----------variation {$variation['color']} ({$variation['colorCode']}) not in stock. (not parse!)\n";
             echo "--------------------------------------------\n";
             continue;
         }
         $this->countVariationInStock++;
         echo "\n";
         echo "----------brand         - {$brand->getTitle()}\n";
         echo "----------model_name    - {$title}\n";
         echo "----------external_id   - {$title}\n";
         echo "----------color_title   - {$variation['color']}\n";
         echo "----------color_code    - {$variation['colorCode']}\n";
         echo "----------size 1        - {$size}\n";
         echo "----------size 2        - ~\n";
         echo "----------size 3        - ~\n";
         echo "----------image         - {$variation['img']}\n";
         echo "----------price         - {$variation['price']}\n";
         echo "----------type          - sun\n";
         echo "----------upc           - {$variation['upc']}\n";
         echo "----------stock         - {$variation['stock']}\n\n";
         echo "--------------------------------------------\n";
         // создаем обьект модели и синхронизируем
         $item = new CMSClassGlassesParserItem();
         $item->setBrand($brand);
         $item->setTitle($title);
         $item->setExternalId($title);
         $item->setType(CMSLogicGlassesItemType::getInstance()->getSun());
         $item->setColor($variation['color']);
         $item->setColorCode($variation['colorCode']);
         $item->setStockCount($variation['stock']);
         $item->setPrice($variation['price']);
         $item->setImg($variation['img']);
         $item->setSize($size);
         $item->setIsValid(1);
         if ($variation['upc']) {
             $item->setUpc($variation['upc']);
         }
         $result[] = $item;
     }
     echo "\n=============================================================================================\n";
     $this->syncingResult($result);
     echo "\n=============================================================================================\n";
 }
 /**
  * Парсим модель по ее ссылке
  * @param  string        $href  [ссылка]
  * @param  string        $type  [тип (frames or sun)]
  * @param  CMSTableBrand $brand [бренд]
  */
 private function parsePageItems($href, $type, CMSTableBrand $brand)
 {
     $result = array();
     $http = $this->getHttp();
     $http->doGet($href);
     $content = $http->getContents();
     $dom = str_get_html($content);
     $items_selector = '#carousel-variants .slides li';
     $items = $dom->find($items_selector);
     // Если товары закончились переходим к следующей категории
     // условие важно так как это единственный выход из бесконечного цикла
     if (!count($items)) {
         echo "It no item in carousel ({$href})!!!";
         return;
     }
     foreach ($items as $key => $item) {
         // preg_match("/(\d+).*c\.[ ]*(\d+).*([\d]{2,}\/\d+).*?[ ](.+)/i", $item->title, $matches);
         // из атрибута li[title] строки выделяем основные свойства для модели с помощью регулярки
         // что бы было проще с регуляркой убираем некоторые ненужные фрагменты в строке
         $title = str_replace('w/nosepads', '', $item->title);
         preg_match("/(\\d+).*[c|C]\\.[ ]*(\\d+).*?([A-Za-z].*)/", $title, $matches);
         $item_title_code = trim($matches[1]);
         $color_code = trim($matches[2]);
         // $sizes = trim($matches[3]);
         $color_title = trim($matches[3]);
         $item_title = "Model " . $item_title_code;
         //достаем размеры
         $sizes_selector = "#carousel-detail-3 .carousel-inner div[id={$item->id}] h4 span";
         $cur_item_sizes = $dom->find($sizes_selector);
         if (!count($cur_item_sizes)) {
             echo "No sizes for current item ({$item_title_code}).\n";
             continue;
         }
         $sizes = trim($cur_item_sizes[0]->plaintext);
         $sizes_arr = explode('☐', $sizes);
         $size_1 = $sizes_arr[0];
         $size_2 = $sizes_arr[1];
         $size_3 = 140;
         // достаем ссылку на изображение
         $cur_item_image = $item->find('img.img-responsive');
         if (!count($cur_item_image)) {
             echo "No image for current item ({$item_title_code}).\n";
             continue;
         }
         $item_image = htmlspecialchars_decode(self::URL_BASE . $cur_item_image[0]->src);
         // достаем сток статус
         $stock_selector = ".stockinfo .carousel-inner div[id={$item->id}]";
         $cur_item_stock = $dom->find($stock_selector);
         if (!count($cur_item_stock)) {
             echo "No stock for current item ({$item_title_code}).\n";
             continue;
         }
         $item_stock = trim($cur_item_stock[0]->plaintext);
         // отсекаем те которые out of stock
         if ($item_stock !== "In Stock") {
             continue;
         }
         // определяем тип очков
         if ($type === "frames") {
             $typeItem = CMSLogicGlassesItemType::getInstance()->getEye();
         } else {
             $typeItem = CMSLogicGlassesItemType::getInstance()->getSun();
         }
         // достаем цену
         $price_selector = "#carousel-detail-1 .carousel-inner div[id={$item->id}] .pull-left";
         $cur_item_price = $dom->find($price_selector);
         $item_price = trim(str_replace('$', '', $cur_item_price[0]->plaintext));
         // небольшой лог
         echo "\nurl          - " . $href . "\n";
         echo "item title   - " . $item_title . "\n";
         echo "item ext id  - " . $item_title_code . "\n";
         echo "color code   - " . $color_code . "\n";
         echo "type         - " . $type . "\n";
         echo "item sizes   - " . str_replace("☐", '/', $sizes) . "\n";
         echo "color title  - " . $color_title . "\n";
         echo "item image   - " . $item_image . "\n";
         echo "stock        - " . $item_stock . "\n";
         echo "price        - " . $item_price . "\n";
         echo "==================================================================\n";
         // создаем обьект модели и синхронизируем
         $item = new CMSClassGlassesParserItem();
         $item->setBrand($brand);
         $item->setExternalId($item_title_code);
         $item->setType($typeItem);
         $item->setTitle($item_title);
         $item->setColor($color_title);
         $item->setColorCode($color_code);
         $item->setStockCount(1);
         $item->setPrice($item_price);
         $item->setImg($item_image);
         $item->setSize($size_1);
         $item->setSize2($size_2);
         $item->setSize3($size_3);
         $item->setIsValid(1);
         $result[] = $item;
     }
     $dom->clear();
     foreach ($result as $res) {
         $res->sync();
     }
 }
 /**
  * @param string $typeString
  * @return CMSTableGlassesItemType
  */
 private function getItemType($typeString)
 {
     // определяем тип очков
     if (stripos($typeString, "SUN") !== false) {
         $typeItem = CMSLogicGlassesItemType::getInstance()->getSun();
     } else {
         $typeItem = CMSLogicGlassesItemType::getInstance()->getEye();
     }
     return $typeItem;
 }
 /**
  * Парсинг страницы товара
  * @param CMSTableBrand $brand
  * @param $itemName
  * @param $itemStyle
  */
 private function parseItem(CMSTableBrand $brand, $itemName, $itemStyle)
 {
     echo "--------Sync item {$itemName}, code - {$itemStyle}.\n";
     $result = array();
     $variations = $this->getItem($itemStyle);
     if (!$variations) {
         echo "----------Where are variations?!\n";
         return;
     }
     // счетчики
     $this->countItem++;
     $this->countAllItem++;
     foreach ($variations as $key => $variation) {
         $stock = 0;
         $this->countVariation++;
         $this->countAllVariation++;
         if ($variation['stockStatus'] == "Available") {
             $stock = 1;
         } else {
             echo "\n----------variation {$variation['color']} ({$variation['colorDescription']}) not in stock. (not parse!)\n";
             echo "==================================================================\n";
             continue;
         }
         $this->countVariationInStock++;
         $this->countAllVariationInStock++;
         $itemImg = self::URL_IMG . $variation['colorImage'];
         // определяем тип очков
         if (stripos($variation['marketingGroupDescription'], "SUN") !== false) {
             $typeItem = CMSLogicGlassesItemType::getInstance()->getSun();
         } else {
             $typeItem = CMSLogicGlassesItemType::getInstance()->getEye();
         }
         // убираем из названия цвета код цвета
         $pattern = "/\\(" . $variation['color'] . "\\)/";
         $variation['colorDescription'] = trim(preg_replace($pattern, '', $variation['colorDescription']));
         preg_match("/(\\d{2})(\\d{2})/", $variation['size'], $matches);
         $size1 = isset($matches[1]) ? $matches[1] : 0;
         $size2 = isset($matches[2]) ? $matches[2] : 0;
         // иногда есть размеры формата 0xxx
         if ($size1[0] == "0" && $variation['SSA']) {
             $size1 = $variation['SSA'];
             $size2 = $variation['SSDBL'];
         }
         $upc = trim($variation['upcNumber']);
         // if(strlen($upc) < 13) {
         //     $upc = "0" . $upc;
         // }
         echo "\n";
         echo "----------brand         - {$brand->getTitle()}\n";
         echo "----------model_name    - {$itemName}\n";
         echo "----------external_id   - {$itemStyle}\n";
         echo "----------color_title   - {$variation['colorDescription']}\n";
         echo "----------color_code    - {$variation['color']}\n";
         echo "----------size 1        - {$size1}\n";
         echo "----------size 2        - {$size2}\n";
         echo "----------size 3        - {$variation['templeLength']}\n";
         echo "----------image         - {$itemImg}\n";
         echo "----------price         - {$variation['retail']}\n";
         echo "----------type          - {$variation['marketingGroupDescription']}\n";
         echo "----------stock         - {$stock}\n";
         echo "----------upc           - {$upc}\n";
         echo "--------------------------------------------\n";
         // создаем обьект модели и синхронизируем
         $item = new CMSClassGlassesParserItem();
         $item->setBrand($brand);
         $item->setTitle($itemName);
         $item->setExternalId($itemStyle);
         $item->setType($typeItem);
         $item->setColor($variation['colorDescription']);
         $item->setColorCode(trim($variation['color']));
         $item->setStockCount($stock);
         $item->setPrice(trim($variation['retail']));
         $item->setImg($itemImg);
         $item->setSize($size1);
         $item->setSize2($size2);
         $item->setSize3($variation['templeLength']);
         $item->setIsValid(1);
         $item->setUpc($upc);
         $result[] = $item;
     }
     echo "============================================\n";
     foreach ($result as $res) {
         $res->sync();
     }
 }
 /**
  * Парсинг страницы модели
  * @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";
         }
     }
 }
 /**
  * Достает сток и изображение вариации из корзины, формирует обьект и отправляет на синхронизацию
  * @param $content
  * @param CMSTableBrand $brand
  * @param $type_value
  * @param $item
  */
 private function parsePageItems($content, CMSTableBrand $brand, $type_value, $item)
 {
     $stock = '';
     $image = '';
     $external_id = '';
     $dom = str_get_html($content);
     // достаем сток
     $option_stock_dom = $dom->find('#MainContent_ucCart_rptCart_lblAvailMarker_0');
     if (count($option_stock_dom)) {
         $stock_str = trim($option_stock_dom[0]->plaintext);
         $stock = trim($stock_str) === "IN-STOCK" ? 1 : 0;
     }
     // достаем изображение
     $option_img_dom = $dom->find('#MainContent_ucCart_rptCart_imgFrame_0');
     if (count($option_img_dom)) {
         $image = preg_replace("/&amp;w=(.*)/", '&amp;w=1250', $option_img_dom[0]->src);
         $image = self::URL_ROOT . $image;
     }
     $image = html_entity_decode($image);
     // разбираемся с размерами
     $sizes_arr = explode('-', $item['sizes']);
     $size_1 = isset($sizes_arr[0]) ? $sizes_arr[0] : 0;
     $size_2 = isset($sizes_arr[1]) ? $sizes_arr[1] : 1;
     $size_3 = isset($sizes_arr[2]) ? $sizes_arr[2] : 2;
     // определяем тип очков
     if ($type_value === "Sun") {
         $typeItem = CMSLogicGlassesItemType::getInstance()->getSun();
     } else {
         $typeItem = CMSLogicGlassesItemType::getInstance()->getEye();
     }
     // Формируем переменную которая будет уникальным идентификатором каждой модели
     // Собственно по ней вариации к модели и попадут
     $external_id = $item['name'] . " prov " . $this->getProviderId();
     echo "\n";
     echo "--------brand    - {$brand->getTitle()}\n";
     echo "--------model_name    - {$item['name']}\n";
     echo "--------color_title   - {$item['color_title']}\n";
     echo "--------external_id   - {$external_id}\n";
     echo "--------color_code    - {$item['color_code']}\n";
     echo "--------sizes         - {$item['sizes']}\n";
     echo "--------image         - {$image}\n";
     echo "--------price         - {$item['price']}\n";
     echo "--------type          - {$type_value}\n";
     echo "--------stock         - {$stock}\n";
     echo "--------upc           - {$item['upc']}\n\n";
     // создаем обьект модели и синхронизируем
     $sitem = new CMSClassGlassesParserItem();
     $sitem->setBrand($brand);
     $sitem->setExternalId($external_id);
     $sitem->setType($typeItem);
     $sitem->setTitle($item['name']);
     $sitem->setColor($item['color_title']);
     $sitem->setColorCode($item['color_code']);
     $sitem->setStockCount($stock);
     $sitem->setPrice($item['price']);
     $sitem->setImg($image);
     $sitem->setSize($size_1);
     $sitem->setSize2($size_2);
     $sitem->setSize3($size_3);
     $sitem->setIsValid(1);
     $sitem->setUpc($item['upc']);
     $sitem->sync();
 }