private function addGood($item, $code) { $group = ShGroups::findOne(['code' => strval($item->{'Группы'}->{'Ид'})]); $good = ShGoods::findOne(['code' => $code]); $link = $good ? Links::findOne($good->links_id) : new Links(); $link->categories_id = self::GOOD_CATEGORIES_ID; $link->parent = $group->links_id; $link->anchor = strval(isset($item->{'НаименованиеНаСайте'}) && $item->{'НаименованиеНаСайте'} ? $item->{'НаименованиеНаСайте'} : $item->{'Наименование'}); $link->link_name = isset($item->{'URI'}) && $item->{'URI'} ? strval($item->{'URI'}) : $link->anchor2translit($link->anchor); $link_name = $link->link_name; $num = 1; if (isset($link->id)) { while (Links::findByUrlForLink($link->link_name, $link->id, $link->parent)) { $num += 1; $link->link_name = $link_name . '-' . $num; } } else { while (Links::findByUrl($link->link_name, $link->parent)) { $num += 1; $link->link_name = $link_name . '-' . $num; } } $link->level = $group->link->level + 1; $link->child_exist = 0; $link->url = $group->link->url . '/' . $link->link_name; $link->seq = isset($link->id) ? $link->seq : Links::findLastSequence(self::GOOD_CATEGORIES_ID, $link->parent) + 1; $link->title = isset($link->id) ? $link->title : $link->anchor; $link->created_at = isset($link->id) ? $link->created_at : time(); $link->updated_at = time(); $link->state = $item->{'НеПубликуетсяНаСайте'} == 'истина' ? 0 : 1; $link->layouts_id = self::GOOD_LAYOUT; $link->views_id = self::GOOD_VIEW; $link->content_nums = 1; $link->save(); $content = Contents::findOne(['links_id' => $link->id]); if (!$content) { $content = new Contents(); $content->links_id = $link->id; $content->seq = 1; } $content->content = strval($item->{'Описание'}); $content->save(); if (!$good) { $good = new ShGoods(); $good->links_id = $link->id; $good->groups_id = $group->id; $good->code = $code; } $good->good = strval($item->{'Наименование'}); $good->save(); if ($item->{'Картинки'} && $item->{'Картинки'}->{'Картинка'}) { $this->addImage($link->id, $item->{'Картинки'}->{'Картинка'}); } return ['id' => $good->id, 'links_id' => $link->id]; }
private function parserOffers($offers_sxe) { /* Поиск и удаление цен для товара и его характеристик */ foreach ($offers_sxe as $item_sxe) { if (preg_match('/(.+)#(.+)/', $item_sxe->{'Ид'}, $matches)) { $good_code = strval($matches[1]); $item_code = strval($matches[2]); $prices_item = ShPriceItem::find()->innerJoinWith('item')->where(['mod_sh_items.code' => $item_code])->all(); if ($prices_item) { ShPriceItem::deleteAll(['id' => ArrayHelper::getColumn($prices_item, 'id')]); } } else { $good_code = strval($item_sxe->{'Ид'}); } $price_good = ShPriceGood::find()->innerJoinWith('good')->where(['mod_sh_goods.code' => $good_code])->all(); if ($price_good) { ShPriceGood::deleteAll(['id' => ArrayHelper::getColumn($price_good, 'id')]); } } $price_types = ArrayHelper::map(ShPriceTypes::find()->all(), 'code', 'id'); $good_min_price = array(); foreach ($offers_sxe as $item_sxe) { $item_code = false; if (preg_match('/(.+)#(.+)/', $item_sxe->{'Ид'}, $matches)) { $good_code = strval($matches[1]); $item_code = strval($matches[2]); } else { $good_code = strval($item_sxe->{'Ид'}); } if (!isset($good_min_price[$good_code])) { $good_min_price[$good_code] = array(); } foreach ($item_sxe->{'Цены'}->{'Цена'} as $item_price) { $code = strval($item_price->{'ИдТипаЦены'}); if (!isset($good_min_price[$good_code][$price_types[$code]])) { $good_min_price[$good_code][$price_types[$code]] = floatval($item_price->{'ЦенаЗаЕдиницу'}); } else { if ($good_min_price[$good_code][$price_types[$code]] > floatval($item_price->{'ЦенаЗаЕдиницу'})) { $good_min_price[$good_code][$price_types[$code]] = floatval($item_price->{'ЦенаЗаЕдиницу'}); } } if ($item_code) { $item = ShItems::findOne(['code' => $item_code]); if ($item) { $price_item = new ShPriceItem(); $price_item->items_id = $item->id; $price_item->price_types_id = $price_types[$code]; $price_item->price = floatval($item_price->{'ЦенаЗаЕдиницу'}); $price_item->save(); } } } } foreach ($good_min_price as $good_code => $good_prices) { $good = ShGoods::findOne(['code' => $good_code]); if ($good) { foreach ($good_prices as $price_types_id => $good_price) { $price_good = new ShPriceGood(); $price_good->goods_id = $good->id; $price_good->price_types_id = $price_types_id; $price_good->price = $good_price; $price_good->save(); } } } }