Blame view

common/models/SearchForm.php 2.65 KB
950817c6   Alex Savenko   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
  <?php
      
      namespace common\models;
      
      use artbox\catalog\models\Category;
      use artbox\catalog\models\Product;
      use yii\base\Model;
      use yii\db\ActiveQuery;
      
      /**
       * Class SearchForm for performing search around site.
       */
      class SearchForm extends Model
      {
          /**
           * @var string $word
           */
          public $word;
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [ 'word' ],
                      'required',
                      'message' => \Yii::t('app', 'Write text to search for.'),
                  ],
                  [
                      [ 'word' ],
                      'string',
                      'min'      => 3,
                      'message'  => \Yii::t('app', 'Write at least 3 symbols.'),
                      'tooShort' => \Yii::t('app', 'Write at least 3 symbols.'),
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
e226698d   mzavalniuk   translate shop pa...
47
                  'word' => \Yii::t('app', 'Пошук по сайту'),
950817c6   Alex Savenko   first commit
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
              ];
          }
          
          public function search()
          {
              return Product::find()
                            ->joinWith('lang')
                            ->joinWith('variants.lang')
                            ->andWhere(
                                [
                                    'product.status' => true,
                                    'variant.status' => true,
                                ]
                            )
                            ->andWhere(
                                [
                                    'ilike',
                                    'product_lang.title',
                                    $this->word,
                                ]
                            )
                            ->orWhere(
                                [
                                    'ilike',
                                    'variant_lang.title',
                                    $this->word,
                                ]
                            )
                            ->orWhere([ 'variant.sku' => $this->word ])
                            ->groupBy('product.id');
          }
          
          public function searchCategory(Category $category, ActiveQuery $query)
          {
              return $query->joinWith('productToCategories')
                           ->andWhere(
                               [
                                   'product_to_category.category_id' => $category->id,
                               ]
                           );
          }
      }