Blame view

frontend/modules/map/controllers/FindController.php 3.47 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  <?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();
      }
  
  
  }