Blame view

common/models/SearchForm.php 2.65 KB
fe6d4ac1   Yarik   Layout
1
  <?php
8e13c18c   Yarik   Search
2
      
fe6d4ac1   Yarik   Layout
3
4
      namespace common\models;
      
8e13c18c   Yarik   Search
5
6
      use artbox\catalog\models\Category;
      use artbox\catalog\models\Product;
fe6d4ac1   Yarik   Layout
7
      use yii\base\Model;
8e13c18c   Yarik   Search
8
      use yii\db\ActiveQuery;
fe6d4ac1   Yarik   Layout
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      
      /**
       * Class SearchForm for performing search around site.
       */
      class SearchForm extends Model
      {
          /**
           * @var string $word
           */
          public $word;
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [ 'word' ],
8e13c18c   Yarik   Search
28
29
30
31
32
                      'required',
                      'message' => \Yii::t('app', 'Write text to search for.'),
                  ],
                  [
                      [ 'word' ],
fe6d4ac1   Yarik   Layout
33
                      'string',
8e13c18c   Yarik   Search
34
35
36
                      'min'      => 3,
                      'message'  => \Yii::t('app', 'Write at least 3 symbols.'),
                      'tooShort' => \Yii::t('app', 'Write at least 3 symbols.'),
fe6d4ac1   Yarik   Layout
37
38
39
40
41
42
43
44
45
46
47
48
49
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'word' => \Yii::t('app', 'Поиск по сайту'),
              ];
          }
8e13c18c   Yarik   Search
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
          
          public function search()
          {
              return Product::find()
                            ->joinWith('lang')
                            ->joinWith('variants.lang')
                            ->andWhere(
                                [
                                    'product.status' => true,
                                    'variant.status' => true,
                                ]
                            )
                            ->andWhere(
                                [
                                    'like',
                                    'product_lang.title',
                                    $this->word,
                                ]
                            )
                            ->orWhere(
                                [
                                    'like',
                                    '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,
                               ]
                           );
          }
fe6d4ac1   Yarik   Layout
89
      }