'nadaviPath', 'hp' => 'hotlinePath', ]; } /** * Generate price-list for Nadavi * * @see http://nadavi.net/nadavi.php?idPage_=57&idBookmark_=5 * @return int */ public function actionNadavi() { $this->configureUrl(); $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $root = $dom->createElement('yml_catalog'); $root->setAttribute('date', date('Y-m-d H:i')); $dom->appendChild($root); $shop = $root->appendChild($dom->createElement('shop')); $shop->appendChild($dom->createElement('name', $this->shopName)); $shop->appendChild( $dom->createElement( 'url', $this->urlManager->createAbsoluteUrl([ 'site/index' ], $this->secured ? 'https' : 'http') ) ); $currencies = $shop->appendChild($dom->createElement('currencies')); $currency = $dom->createElement('currency'); $currency->setAttribute('id', 'UAH'); $currency->setAttribute('rate', 1); $currencies->appendChild($currency); $catalog = $shop->appendChild($dom->createElement('catalog')); /** * @var Category[] $categories */ $categories = Category::find() ->with('lang') ->all(); foreach ($categories as $category) { $categoryElement = $dom->createElement('category', $category->lang->title); $categoryElement->setAttribute('id', $category->id); if (!empty($category->parent_id)) { $categoryElement->setAttribute('parentId', $category->parent_id); } $catalog->appendChild($categoryElement); $this->logSuccess('Category inserted: ' . $category->lang->title); } $items = $shop->appendChild($dom->createElement('items')); $productQuery = Variant::find() ->with( [ 'product' => function ($query) { /** * @var \yii\db\ActiveQuery $query */ $query->with('brand.lang') ->with('image') ->with('lang') ->with('category'); }, ] ) ->with('image') ->with('lang'); if (!$this->empty) { $productQuery->andWhere( [ '>', 'stock', 0, ] ); } foreach ($productQuery->each() as $variant) { /** * @var Variant $variant */ if (empty($variant->product->category)) { $this->logError('Variant with id ' . $variant->id . ' hasn\'t got category'); continue; } if (empty($variant->product->brand)) { $this->logError('Variant with id ' . $variant->id . ' hasn\'t got brand'); continue; } $item = $dom->createElement('item'); $item->setAttribute('id', $variant->id); try { $item->appendChild( $dom->createElement( 'name', htmlentities( $variant->product->lang->title . ' (' . $variant->lang->title . ')' ) ) ); } catch (Exception $exception) { var_dump($variant->product->lang->title, $variant->lang->title); die(); } $item->appendChild( $dom->createElement( 'url', $this->urlManager->createAbsoluteUrl( [ 'product/view', 'id' => $variant->product->id, ], $this->secured ? 'https' : 'http' ) ) ); $item->appendChild($dom->createElement('price', $variant->price)); $item->appendChild($dom->createElement('categoryId', $variant->product->category->id)); $item->appendChild($dom->createElement('vendor', $variant->product->brand->lang->title)); if (!empty($variant->image)) { $imageModel = ImageHelper::set($variant->image->getPath()) ->setWidth(200); $image = $this->urlManager->createAbsoluteUrl( $imageModel->render(), $this->secured ? 'https' : 'http' ); unset($imageModel); } elseif (!empty($variant->product->image)) { $imageModel = ImageHelper::set($variant->product->image->getPath()) ->setWidth(200); $image = $this->urlManager->createAbsoluteUrl( $imageModel->render(), $this->secured ? 'https' : 'http' ); unset($imageModel); } if (!empty($image)) { $item->appendChild($dom->createElement('image', $image)); unset($image); } if (!empty($variant->lang->description)) { $item->appendChild($dom->createElement('description', Html::encode($variant->lang->description))); } elseif (!empty($variant->product->lang->description)) { $item->appendChild( $dom->createElement('description', Html::encode($variant->product->lang->description)) ); } $items->appendChild($item); $this->logSuccess( 'Product inserted: ' . $variant->product->lang->title . ' (' . $variant->lang->title . ')' ); } file_put_contents(\Yii::getAlias($this->nadaviPath), $dom->saveXML()); return 0; } /** * Generate price-list for Hotline * * @see http://hotline.ua/about/pricelists_specs/ * @return int */ public function actionHotline() { $this->configureUrl(); $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $price = $dom->appendChild($dom->createElement('price')); $price->appendChild($dom->createElement('date', date('Y-m-d H:i'))); if (!empty($this->firmName)) { $price->appendChild($dom->createElement('firmName', $this->firmName)); } if (!empty($this->firmId)) { $price->appendChild($dom->createElement('firmId', $this->firmId)); } $categoriesNode = $price->appendChild($dom->createElement('categories')); /** * @var Category[] $categories */ $categories = Category::find() ->with('lang') ->all(); foreach ($categories as $category) { if (empty($category->lang)) { $this->logError('Category with id ' . $category->id . ' has no Lang'); continue; } $categoryElement = $dom->createElement('category'); $categoryElement->appendChild($dom->createElement('id', $category->id)); $categoryElement->appendChild($dom->createElement('name', $category->lang->title)); if (!empty($category->parent_id)) { $categoryElement->appendChild($dom->createElement('parentId', $category->parent_id)); } $categoriesNode->appendChild($categoryElement); $this->logSuccess('Category inserted: ' . $category->lang->title); } $items = $price->appendChild($dom->createElement('items')); $productQuery = Variant::find() ->with( [ 'product' => function ($query) { /** * @var \yii\db\ActiveQuery $query */ $query->with('brand.lang') ->with('image') ->with('lang') ->with('category'); }, ] ) ->with('image') ->with('lang'); if (!$this->empty) { $productQuery->andWhere( [ '>', 'stock', 0, ] ); } foreach ($productQuery->each() as $variant) { /** * @var Variant $variant */ if (empty($variant->product->category)) { $this->logError('Variant with id ' . $variant->id . ' hasn\'t got category'); continue; } if (empty($variant->product->brand)) { $this->logError('Variant with id ' . $variant->id . ' hasn\'t got brand'); continue; } $item = $dom->createElement('item'); $item->appendChild( $dom->createElement('id', $variant->id) ); $item->appendChild($dom->createElement('categoryId', $variant->product->category->id)); $item->appendChild( $dom->createElement('code', $variant->sku) ); $item->appendChild($dom->createElement('vendor', $variant->product->brand->lang->title)); $item->appendChild( $dom->createElement( 'name', htmlentities( $variant->product->lang->title . ' (' . $variant->lang->title . ')' ) ) ); if (!empty($variant->lang->description)) { $item->appendChild($dom->createElement('description', Html::encode($variant->lang->description))); } elseif (!empty($variant->product->lang->description)) { $item->appendChild( $dom->createElement('description', Html::encode($variant->product->lang->description)) ); } $item->appendChild( $dom->createElement( 'url', $this->urlManager->createAbsoluteUrl( [ 'product/view', 'id' => $variant->product->id, ], $this->secured ? 'https' : 'http' ) ) ); if (!empty($variant->image)) { $image = $this->urlManager->hostInfo . ImageHelper::set( $variant->image->getPath() ) ->setWidth(200) ->render(); } elseif (!empty($variant->product->image)) { $image = $this->urlManager->hostInfo . ImageHelper::set( $variant->product->image->getPath() ) ->setWidth(200) ->render(); } if (!empty($image)) { $item->appendChild($dom->createElement('image', $image)); unset($image); } $item->appendChild($dom->createElement('priceRUAH', $variant->price)); if (!empty($variant->price_old)) { $item->appendChild($dom->createElement('oldprice', $variant->price_old)); } if ($variant->stock) { $item->appendChild($dom->createElement('stock', 'В наличии')); } else { $item->appendChild($dom->createElement('stock', 'Под заказ')); } $items->appendChild($item); $this->logSuccess( 'Product inserted: ' . $variant->product->lang->title . ' (' . $variant->lang->title . ')' ); } file_put_contents(\Yii::getAlias($this->hotlinePath), $dom->saveXML()); return 0; } protected function configureUrl() { $config = ArrayHelper::merge( require \Yii::getAlias('@frontend/config/main.php'), require \Yii::getAlias('@common/config/main.php') ); if (!empty($config[ 'components' ]) && !empty($config[ 'components' ][ 'urlManager' ])) { $urlManagerConfig = $config[ 'components' ][ 'urlManager' ]; } else { die('Param error'); } if (empty($this->host)) { $root = dirname(\Yii::getAlias('@common')); if (preg_match('/([^\/]*)$/', $root, $matches)) { if ($this->secured) { $urlManagerConfig[ 'hostInfo' ] = 'https://' . $matches[ 1 ]; } else { $urlManagerConfig[ 'hostInfo' ] = 'http://' . $matches[ 1 ]; } } else { die('Host not set and cannot be gained automatically.'); } } else { if (substr($this->host, 0, 4) == 'http') { $urlManagerConfig[ 'hostInfo' ] = $this->host; } else { if ($this->secured) { $urlManagerConfig[ 'hostInfo' ] = 'https://' . $this->host; } else { $urlManagerConfig[ 'hostInfo' ] = 'http://' . $this->host; } } } $this->urlManager = \Yii::createObject($urlManagerConfig); } protected function logSuccess(string $msg) { $this->stdout($msg . "\n", Console::FG_GREEN, Console::BOLD); } protected function logError(string $msg) { $this->stderr($msg . "\n", Console::FG_RED, Console::BOLD); } }