configureUrl(); $file = ''; $file .= Html::beginTag( 'yml_catalog', [ 'date' => date('Y-m-d H:i'), ] ); $file .= Html::beginTag('shop'); $file .= Html::tag('name', $this->shopName); $file .= Html::tag( 'url', $this->urlManager->createAbsoluteUrl([ 'site/index' ], $this->secured ? 'https' : 'http') ); $file .= Html::beginTag('currencies'); $file .= ''; $file .= Html::endTag('currencies'); try { $file .= Html::beginTag('catalog'); /** * @var Category[] $categories */ $categories = Category::find() ->with('lang') ->all(); foreach ($categories as $category) { if (empty($category->parent_id)) { $file .= Html::tag( 'category', $category->lang->title, [ 'id' => $category->id, ] ); } else { $file .= Html::tag( 'category', $category->lang->title, [ 'id' => $category->id, 'parentId' => $category->parent_id, ] ); } $this->logSuccess('Category inserted: ' . $category->lang->title); } $file .= Html::endTag('catalog'); $file .= Html::beginTag('items'); $productQuery = Variant::find() ->with( [ 'product' => function ($query) { /** * @var \yii\db\ActiveQuery $query */ $query->with('brand.lang') ->with('image') ->with('lang.alias') ->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; } $file .= Html::beginTag( 'item', [ 'id' => $variant->id, ] ); $file .= Html::tag( 'name', htmlspecialchars( $variant->product->lang->title . ' (' . $variant->lang->title . ')' ) ); $file .= Html::tag( 'url', $this->urlManager->createAbsoluteUrl( [ 'product/view', 'alias' => $variant->product->lang->alias, ], $this->secured ? 'https' : 'http' ) ); $file .= Html::tag('price', $variant->price); $file .= Html::tag('categoryId', $variant->product->category->id); $file .= Html::tag('vendor', $variant->product->brand->lang->title); if (!empty($variant->product->image)) { $file .= Html::tag( 'image', $this->urlManager->getHostInfo() . $variant->product->image->getUrl() ); } $file .= Html::endTag('item'); $this->logSuccess( 'Product inserted: ' . $variant->product->lang->title . ' (' . $variant->lang->title . ')' ); } $file .= Html::endTag('items'); $file .= Html::endTag('shop'); $file .= Html::endTag('yml_catalog'); $handle = fopen(\Yii::getAlias($this->nadaviPath), 'w+'); fwrite($handle, $file); fclose($handle); } catch (\Exception $error) { return $error->getMessage(); } return 1; } /** * Generate price-list for Hotline * * @see http://hotline.ua/about/pricelists_specs/ * @return int */ public function actionHotline() { set_time_limit(0); $this->configureUrl(); $file = ''; $file .= Html::beginTag('price'); $file .= Html::tag('date', date('Y-m-d H:i')); $file .= Html::tag('firmName', $this->firmName); $file .= Html::tag('firmId', $this->firmId); // try { /** Categories export begin */ $file .= Html::beginTag('categories'); /** * @var Category[] $categories */ $categories = Category::find() ->with('lang') ->all(); foreach ($categories as $category) { $file .= Html::beginTag('category'); $file .= Html::tag('id', $category->id); $file .= Html::tag('name', $category->lang->title); if (!empty($category->parent_id)) { $file .= Html::tag('parentId', $category->parent_id); } $file .= Html::endTag('category'); $this->logSuccess('Category inserted: ' . $category->lang->title); } $file .= Html::endTag('categories'); /** End categories export */ /** Products export begin */ $file .= Html::beginTag('items'); $productQuery = Variant::find() ->with( [ 'product' => function ($query) { /** * @var \yii\db\ActiveQuery $query */ $query->with('brand.lang') ->with('image') ->with('lang.alias') ->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; } $file .= Html::beginTag('item'); $file .= Html::tag('id', $variant->id); $file .= Html::tag('categoryId', $variant->product->category->id); $file .= Html::tag('vendor', $variant->product->brand->lang->title); $file .= Html::tag( 'name', htmlspecialchars( $variant->product->lang->title . ' (' . $variant->lang->title . ')' ) ); $file .= Html::tag('code', $variant->sku); $file .= Html::tag( 'url', $this->urlManager->createAbsoluteUrl( [ 'product/view', 'alias' => $variant->product->lang->alias, ], $this->secured ? 'https' : 'http' ) ); if (!empty($variant->product->image)) { $file .= Html::tag( 'image', $this->urlManager->getHostInfo() . $variant->product->image->getUrl() ); } $file .= Html::tag('priceRUAH', $variant->price); if (!empty($variant->price_old)) { $file .= Html::tag('oldprice', $variant->price_old); } if (empty($variant->stock)) { $file .= Html::tag('stock', 'Нет'); } else { $file .= Html::tag('stock', 'В наличии'); } $file .= Html::endTag('item'); $this->logSuccess( 'Product inserted: ' . $variant->product->lang->title . ' (' . $variant->lang->title . ')' ); } $file .= Html::endTag('items'); /** End of products */ $file .= Html::endTag('price'); $handle = fopen(\Yii::getAlias($this->hotlinePath), 'w+'); fwrite($handle, $file); fclose($handle); // } catch (\Exception $error) { // return $error->getMessage(); // } return 1; } 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) { return 0; } protected function logError(string $msg) { return 0; } }