FindController.php 3.47 KB
<?php

namespace frontend\modules\map\controllers;

use Yii;
use yii\{
    data\ArrayDataProvider, filters\VerbFilter, helpers\HtmlPurifier, helpers\Url, web\Response
};
//
use frontend\components\BaseController;
use frontend\modules\map\models\Item;
use frontend\modules\map\models\ItemLang;

/**
 * Class PageController
 *
 * @package frontend\modules\map\controllers
 * @author Alla Kuzmenko
 * @copyright (c) 2016
 */
class FindController extends BaseController
{
    public $title = "Map";
    public $layout = "@app/layouts/base";
    public $defaultAction = 'index';
    public $model;
    const LIMIT_COUNT = 4;

    /**
     *
     */
    public function init()
    {
        parent::init();

        $this->breadcrumbs = [
            [
                'label' => Yii::t('map', 'Objects base'),
                'url' => Url::toRoute(['/map/list/list'])
            ],
            [
                'label' => Yii::t('front', 'search for objects'),
            ]
        ];
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'index' => ['get'],
                    'ajax' => ['post', 'get']
                ],
            ]
        ];
    }

    /**
     * @param $condition - строка поиска
     * @return mixed
     */
    public function actionIndex($condition)
    {
        $condition = HtmlPurifier::process($condition);
        $condition = str_replace('_', ' ', $condition);
        $condition = str_replace('+', '', $condition);
        $condition = trim($condition);
        $items = $this->getResults($condition);
        $numberPage = Yii::$app->controller->module->itemOnPage;
        $provider = new ArrayDataProvider([
            'allModels' => $items,
            'pagination' => [
                'pageSize' => $numberPage
            ],
        ]);
        if (!empty($items)) {
            $lastPage = ceil((int)count($items) / (int)$numberPage);

            return $this->render('_result', [
                'condition' => $condition,
                'items' => $provider->getModels(),
                'count' => count($items),
                'lastPage' => $lastPage,
                'pages' => $provider->getPagination(),
            ]);
        } else {
            return $this->render('_nothing_search',
                ['condition' => $condition]);

        }

    }


    /**
     * @return array
     */
    public function actionAjax()
    {
        $condition = Yii::$app->getRequest()->post('condition');
        $condition = HtmlPurifier::process($condition);
        $condition = str_replace('_', ' ', $condition);
        $condition = trim($condition);
        $items = $this->getResults($condition, self::LIMIT_COUNT);
        Yii::$app->getResponse()->format = Response::FORMAT_JSON;
        return [
            'result' => $this->renderPartial('ajax', ['items' => $items, 'condition' => $condition, 'limit' => self::LIMIT_COUNT])
        ];

    }

    /**
     * @param null $condition
     * @param bool $limit
     * @return array
     */
    private function getResults($condition = null, $limit = false)
    {
        $query = Item::findBase()->innerJoinWith('lang')
            ->andFilterWhere(
                ['like', ItemLang::tableName() . '.title', $condition]
            )
            ->enabled();
        if ($limit) {
            $query->limit($limit);
        }
        return $query->all();
    }


}