SiteController.php 6.24 KB
<?php

namespace frontend\controllers;

use common\modules\product\models\Category;
use common\modules\product\models\CategorySearch;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use Yii;
use yii\web\Controller;
use common\models\Page;
use common\models\News;
use common\models\Catalog;
use common\models\Products;

class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'thumb' => 'iutbay\yii2imagecache\ThumbAction',
        ];
    }

    public function actionIndex()
    {

        $modelText = Page::find()->where(['translit'=>'home'])->one();

        return $this->render('index', [
            'text'=>$modelText,

        ]);
    }


    public function actionRobots()
    {

        $file = file_get_contents(robotsTxt, true);//получаем его содержимое
        return $this->renderPartial('robots', [

        ]);
    }

    public function actionError(){

        return $this->render('error', [
            'code'=>'404',
//            'message'=>Yii::$app->errorHandler->exception->getMessage(),
        ]);
    }

    public function actionPriceparam ()
    {
        $category_alias = Yii::$app->request->get('category');
        $is_count = Yii::$app->request->get('count', false);

        if ( !($category = CategorySearch::findByAlias($category_alias)))
        {
            throw new HttpException(404, 'Данной страницы не существует!');
        }

        $products = $category->products;

        if ($is_count) {
            print (count($products));
            exit;
        }

        set_time_limit (0);
        header ("Content-Type: text/xml");
        print '<?xml version="1.0" encoding="UTF-8" ?>';
        print "<price>";
        print "<date>" . date ("Y-m-d h:m") . "</date>";
        print "<firmName>Rukzachok</firmName>";
        print "<firmId></firmId>";
        print "<rate></rate>";
        print "<categories>";

        print "<category>";
        print "<id>" . $category->category_id . "</id>";
        print "<parentId></parentId>";
        print "<name>" . $category->name . "</name>";
        print "</category>";


        print "</categories>";
        print "<items>";
        /** @var Product $product */
        foreach ($products as $product) {
            /** @var ProductVariant $variant */
            foreach ($product->enabledVariants as $variant) {

                print "<item>";
                print "<id>" . htmlspecialchars($variant->product_variant_id) . "</id>";
                print "<categoryId>" . htmlspecialchars($product->category->category_id) . "</categoryId>";
                print "<vendor>" . $product->brand->name . "</vendor>";
                print "<code>" . htmlspecialchars($variant->sku) . "</code>";
                print "<typeprefix>" . htmlspecialchars(substr($category->name, 0, -2)) . " </typeprefix>";
                print "<name>" . htmlspecialchars($product->name) . "  </name>";
                print "<param>" . $variant->name . " </param>";
                print "<description>" . htmlspecialchars($product->description) . "</description>";
                print "<url>http://rukzachok.com.ua{$product->url}#{$variant->product_variant_id}</url>";
                print "<image>http://rukzachok.com.ua{$variant->imageUrl}</image>";
                print "<priceRUAH>" . $variant->price . "</priceRUAH>";
                print "<oldprice>" . $variant->price_old . "</oldprice>";
                print "<priceRUSD></priceRUSD>";
                print "<stock>В наличии</stock>";
                print "<guarantee></guarantee>";
                print "</item>";
            }
        }
        print "</items>";

        print "</price>";
    }

    public function actionPrice()
    {
        $category_alias = Yii::$app->request->get('category');
        $is_count = Yii::$app->request->get('count', false);

        if ( !($category = CategorySearch::findByAlias($category_alias)))
        {
            throw new HttpException(404, 'Данной страницы не существует!');
        }

        $products = $category->products;

        if ($is_count) {
            print (count($products));
            exit;
        }


//var_dump($products);die();
        set_time_limit (0);
        header ("Content-Type: text/xml");
        print '<?xml version="1.0" encoding="UTF-8" ?>';
        print "<price>";
        print "<date>" . date ("Y-m-d h:m") . "</date>";
        print "<firmName>Rukzachok</firmName>";
        print "<firmId></firmId>";
        print "<rate></rate>";
        print "<categories>";

        print "<category>";
        print "<id>" . $category->category_id . "</id>";
        print "<parentId></parentId>";
        print "<name>" . $category->name . "</name>";
        print "</category>";

        print "</categories>";
        print "<items>";
        /** @var Product $product */
        foreach ($products as $product)
        {
            /** @var ProductVariant $variant */
            foreach ($product->enabledVariants as $variant) {

                print "<item>";
                print "<id>" . htmlspecialchars($variant->product_variant_id) . "</id>";
                print "<categoryId>" . htmlspecialchars($product->category->category_id) . "</categoryId>";
                print "<vendor>" . $product->brand->name . "</vendor>";
                print "<code>" . htmlspecialchars($variant->sku) . "</code>";
                print "<name>" . htmlspecialchars($product->name)." " .htmlspecialchars($variant->name). " </name>";
                print "<description>" . htmlspecialchars($product->description) . "</description>";
                print "<url>http://rukzachok.com.ua{$product->url}#{$variant->product_variant_id}</url>";
                print "<image>http://rukzachok.com.ua{$variant->imageUrl}</image>";
                print "<priceRUAH>" . $variant->price . "</priceRUAH>";
                print "<oldprice>" . $variant->price_old . "</oldprice>";
                print "<priceRUSD></priceRUSD>";
                print "<stock>В наличии</stock>";
                print "<guarantee></guarantee>";
                print "</item>";

            }
        }


        print "</items>";

        print "</price>";


    }


}