1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larrock\Core\Traits; |
4
|
|
|
|
5
|
|
|
use Cache; |
6
|
|
|
use Larrock\Core\Models\Link; |
7
|
|
|
|
8
|
|
|
trait GetLink |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get link query builder. |
12
|
|
|
* @param $childModel |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
|
|
public function linkQuery($childModel) |
16
|
|
|
{ |
17
|
|
|
return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get link data. |
22
|
|
|
* @param $childModel |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public function link($childModel) |
26
|
|
|
{ |
27
|
|
|
return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel)->get(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get link data. |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function linkParam() |
35
|
|
|
{ |
36
|
|
|
return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model) |
37
|
|
|
->whereModelChild('Larrock\ComponentCatalog\Models\Param'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get Model Component + link in attrubute. |
42
|
|
|
* @param $childModel |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function linkAttribute($childModel) |
46
|
|
|
{ |
47
|
|
|
$this->{$childModel} = $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel)->get(); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Метод для attach() и detach(). |
54
|
|
|
* @param string $childModel |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function getLink($childModel) |
58
|
|
|
{ |
59
|
|
|
return $this->belongsToMany($childModel, 'link', 'id_parent', 'id_child') |
|
|
|
|
60
|
|
|
->whereModelParent($this->config->getModelName())->whereModelChild($childModel); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Метод для совместимости привязки сторонних расширений (роли у пользователей) |
65
|
|
|
* Например: $data->getLinkWithParams($row_settings->modelChild, 'role_user', 'role_id', 'user_id')->get() |
66
|
|
|
* для получения прилинкованных ролей пользователя. |
67
|
|
|
* @param $childModel |
68
|
|
|
* @param $tableLink |
69
|
|
|
* @param $parentRow |
70
|
|
|
* @param $childRow |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function getLinkWithParams($childModel, $tableLink, $parentRow, $childRow) |
74
|
|
|
{ |
75
|
|
|
return $this->belongsToMany($childModel, $tableLink, $parentRow, $childRow); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Получение всех связей. |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function getAllLinks() |
83
|
|
|
{ |
84
|
|
|
return $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Получение связи модификации цены. |
89
|
|
|
* @param $childModel |
90
|
|
|
* @return null |
91
|
|
|
*/ |
92
|
|
|
public function getCostLink($childModel) |
93
|
|
|
{ |
94
|
|
|
$cache_key = sha1('getCostLink'.$this->config->model.$childModel.$this->id); |
95
|
|
|
|
96
|
|
|
return Cache::rememberForever($cache_key, function () use ($childModel) { |
97
|
|
|
$query = $this->hasMany(Link::class, 'id_parent')->whereModelParent($this->config->model)->whereModelChild($childModel); |
98
|
|
|
if ($getLink = $query->get()) { |
99
|
|
|
foreach ($getLink as $key => $item) { |
100
|
|
|
$class = new $childModel(); |
101
|
|
|
if ($param = $class->whereId($item->id_child)->first()) { |
102
|
|
|
$getLink[$key]->title = $param->title; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $getLink; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Получение модификаций товаров |
115
|
|
|
* Товар->cost_value. |
116
|
|
|
* @return mixed|null |
117
|
|
|
*/ |
118
|
|
|
public function getCostValuesAttribute() |
119
|
|
|
{ |
120
|
|
|
$cache_key = sha1('cost_value'.$this->id.$this->getConfig()->name); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return Cache::rememberForever($cache_key, function () { |
123
|
|
|
$values = null; |
124
|
|
|
foreach ($this->config->rows as $row) { |
125
|
|
|
if (isset($row->costValue) && $row->costValue) { |
126
|
|
|
if (null === $values) { |
127
|
|
|
$values = $this->getCostLink($row->modelChild, $row->modelChildWhereKey, $row->modelChildWhereValue); |
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
$values = $values->merge($this->getCostLink($row->modelChild, $row->modelChildWhereKey, $row->modelChildWhereValue)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $values; |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Получение первой цены модификации товара. |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function getFirstCostValueAttribute() |
143
|
|
|
{ |
144
|
|
|
if (($costValues = $this->getCostValuesAttribute()) && $costValues->count() > 0) { |
145
|
|
|
return $costValues->first()->cost; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->cost; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Получение ID первой цены модификации товара. |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public function getFirstCostValueIdAttribute() |
156
|
|
|
{ |
157
|
|
|
if ($costValues = $this->getCostValuesAttribute()) { |
158
|
|
|
if ($costValues->count() > 0) { |
159
|
|
|
return $costValues->first()->id; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Получение title первой цены модификации товара. |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function getFirstCostValueTitleAttribute() |
171
|
|
|
{ |
172
|
|
|
if ($costValues = $this->getCostValuesAttribute()) { |
173
|
|
|
if ($costValues->count() > 0) { |
174
|
|
|
return $costValues->first()->title; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|