<?php

namespace frontend\controllers;
use frontend\models\ProductFrontendSearch;
use Yii;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\product\models\ProductCategory;
use common\modules\product\models\ProductVariant;
use yii\web\HttpException;

class SearchController extends \yii\web\Controller
{
    public function actionMain()
    {

        $word = trim(Yii::$app->request->get('word', ''));

        if (!empty($word))
        {


            if( preg_match('/^\+?\d+$/', $word) && (iconv_strlen($word) > 4)){

                $params['keywords'][] = $word;

                $categoriesQuery = Category::find()
                    ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id')
                    ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id')
                    ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id');
                $categoriesQuery->andWhere(['ilike', 'product.name', $params['keywords'][0]]);
                $categories = $categoriesQuery->all();

            } else {

                $params['keywords'] = explode(' ', preg_replace("|[\s,.!:&?~();-]|i", " ", $word));

                foreach($params['keywords'] as $i => &$keyword) {
                    $keyword = trim($keyword);
                    if (empty($keyword)) {
                        unset($params['keywords'][$i]);
                    }
                }
                array_unshift($params['keywords'], $word);

                $categoriesQuery = Category::find()
                    ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id')
                    ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id')
                    ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id');
                foreach ($params['keywords'] as $keyword) {
                    $categoriesQuery->andWhere(['ilike', 'product.name', $keyword]);
                }
                $categories = $categoriesQuery->all();
            }

            $productModel = new ProductFrontendSearch();
            $productProvider = $productModel->search(null, $params, false);


            return $this->render(
                'index',
                [
                    'keywords'          => $params['keywords'],
                    'productModel'      => $productModel,
                    'productProvider'   => $productProvider,
                    'categories'        => $categories,
                ]
            );
        }
        else
        {
            throw new HttpException(404, 'Данной странице не существует!');
        }
    }
}