|
1
|
|
|
<?php namespace VojtaSvoboda\Brands\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Model; |
|
4
|
|
|
use October\Rain\Database\Traits\SoftDelete as SoftDeletingTrait; |
|
5
|
|
|
use October\Rain\Database\Traits\Sortable as SortableTrait; |
|
6
|
|
|
use October\Rain\Database\Traits\Validation as ValidationTrait; |
|
7
|
|
|
|
|
8
|
|
|
class Brand extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
use SoftDeletingTrait; |
|
11
|
|
|
|
|
12
|
|
|
use SortableTrait; |
|
13
|
|
|
|
|
14
|
|
|
use ValidationTrait; |
|
15
|
|
|
|
|
16
|
|
|
public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel']; |
|
17
|
|
|
|
|
18
|
|
|
public $table = 'vojtasvoboda_brands_brands'; |
|
19
|
|
|
|
|
20
|
|
|
public $rules = [ |
|
21
|
|
|
'name' => 'required|max:255', |
|
22
|
|
|
'slug' => 'required|unique:vojtasvoboda_brands_brands', |
|
23
|
|
|
'enabled' => 'boolean', |
|
24
|
|
|
'description' => 'max:10000', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
public $translatable = ['name', 'slug', 'description']; |
|
28
|
|
|
|
|
29
|
|
|
public $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
30
|
|
|
|
|
31
|
|
|
public $attachOne = [ |
|
32
|
|
|
'logo' => ['System\Models\File'], |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
public $attachMany = [ |
|
36
|
|
|
'images' => ['System\Models\File'], |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
public $belongsToMany = [ |
|
40
|
|
|
'categories' => [ |
|
41
|
|
|
'VojtaSvoboda\Brands\Models\Category', |
|
42
|
|
|
'table' => 'vojtasvoboda_brands_brand_category', |
|
43
|
|
|
'order' => 'name asc', |
|
44
|
|
|
'scope' => 'isEnabled', |
|
45
|
|
|
'timestamps' => true, |
|
46
|
|
|
] |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
public function scopeIsEnabled($query) |
|
50
|
|
|
{ |
|
51
|
|
|
return $query->where('enabled', true); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Lists brands for the front end. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \October\Rain\Database\Builder $query |
|
58
|
|
|
* @param array $options Display options |
|
59
|
|
|
* |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
|
|
public function scopeListFrontEnd($query, $options) |
|
63
|
|
|
{ |
|
64
|
|
|
// default config |
|
65
|
|
|
extract(array_merge([ |
|
|
|
|
|
|
66
|
|
|
'page' => 1, |
|
67
|
|
|
'perPage' => 10, |
|
68
|
|
|
'sort' => 'sort_order', |
|
69
|
|
|
'sortOrder' => 'ASC', |
|
70
|
|
|
'search' => '', |
|
71
|
|
|
'enabled' => true, |
|
72
|
|
|
'category' => null, |
|
73
|
|
|
'letter' => null, |
|
74
|
|
|
], $options)); |
|
75
|
|
|
|
|
76
|
|
|
// search by config |
|
77
|
|
|
$searchableFields = ['name']; |
|
78
|
|
|
|
|
79
|
|
|
// only enabled |
|
80
|
|
|
if ($enabled) { |
|
81
|
|
|
$query->isEnabled(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// category filtration |
|
85
|
|
|
if ($category instanceof Category) { |
|
86
|
|
|
$query->whereHas('categories', function ($query) use ($category) { |
|
87
|
|
|
$query->where('category_id', $category->id); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($letter && strlen($letter) == 1) { |
|
92
|
|
|
$query |
|
93
|
|
|
->where(function($q) use ($letter) { |
|
94
|
|
|
$q->where('name', 'LIKE', mb_strtolower($letter) . '%') |
|
95
|
|
|
->orWhere('name', 'LIKE', mb_strtoupper($letter) . '%'); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// order by |
|
100
|
|
|
$query->orderBy($sort, $sortOrder); |
|
101
|
|
|
|
|
102
|
|
|
// search by |
|
103
|
|
|
$search = trim($search); |
|
104
|
|
|
if (strlen($search)) { |
|
105
|
|
|
$query->searchWhere($search, $searchableFields); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $query->paginate($perPage, $page); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|