1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Block\Repositories\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Modules\Block\Events\BlockIsCreating; |
8
|
|
|
use Modules\Block\Events\BlockIsUpdating; |
9
|
|
|
use Modules\Block\Events\BlockWasCreated; |
10
|
|
|
use Modules\Block\Events\BlockWasUpdated; |
11
|
|
|
use Modules\Block\Repositories\BlockRepository; |
12
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
13
|
|
|
|
14
|
|
|
class EloquentBlockRepository extends EloquentBaseRepository implements BlockRepository |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param mixed $data |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function create($data) |
21
|
|
|
{ |
22
|
|
|
$this->sluggify($data); |
23
|
|
|
|
24
|
|
|
event($event = new BlockIsCreating($data)); |
25
|
|
|
|
26
|
|
|
$block = $this->model->create($event->getAttributes()); |
27
|
|
|
|
28
|
|
|
event(new BlockWasCreated($block, $data)); |
29
|
|
|
|
30
|
|
|
return $block; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $model |
35
|
|
|
* @param array $data |
36
|
|
|
* @return object |
37
|
|
|
*/ |
38
|
|
|
public function update($model, $data) |
39
|
|
|
{ |
40
|
|
|
if ($this->needsSlugging($model, $data)) { |
41
|
|
|
$this->sluggify($data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
event($event = new BlockIsUpdating($model, $data)); |
45
|
|
|
|
46
|
|
|
$model->update($event->getAttributes()); |
47
|
|
|
|
48
|
|
|
event(new BlockWasUpdated($model, $data)); |
49
|
|
|
|
50
|
|
|
return $model; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all online blocks in the given language |
55
|
|
|
* @param string $lang |
56
|
|
|
* @return object |
57
|
|
|
*/ |
58
|
|
|
public function allOnlineInLang($lang) |
59
|
|
|
{ |
60
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($lang) { |
61
|
|
|
$q->where('locale', "$lang"); |
62
|
|
|
$q->where('online', true); |
63
|
|
|
})->with('translations')->orderBy('created_at', 'DESC')->get(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get a block body by its name if it's online |
68
|
|
|
* @param string $name |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function get($name) |
72
|
|
|
{ |
73
|
|
|
$lang = $this->getCurrentLocale(); |
74
|
|
|
|
75
|
|
|
$block = $this->model->whereHas('translations', function (Builder $q) use ($lang) { |
76
|
|
|
$q->where('locale', "$lang"); |
77
|
|
|
$q->where('online', true); |
78
|
|
|
})->with('translations')->whereName($name)->first(); |
79
|
|
|
|
80
|
|
|
return $block ? $block->body : ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the current application locale |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function getCurrentLocale() |
88
|
|
|
{ |
89
|
|
|
return App::getLocale(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Normalize the request data |
94
|
|
|
* @param array $data |
95
|
|
|
*/ |
96
|
|
|
private function sluggify(array &$data) |
97
|
|
|
{ |
98
|
|
|
$data['name'] = $this->makeNameUnique($data['name']); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Make the name unique by appending a counter |
103
|
|
|
* @param string $slug |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
private function makeNameUnique($slug) |
107
|
|
|
{ |
108
|
|
|
$slug = str_slug($slug); |
109
|
|
|
|
110
|
|
|
$list = $this->getExistingSlugs($slug); |
111
|
|
|
|
112
|
|
|
if ( |
113
|
|
|
$this->isEmptyArray($list) || |
114
|
|
|
! $this->isSlugInList($slug, $list) || |
115
|
|
|
($this->isForModel($list) && $this->slugIsInList($slug, $list)) |
116
|
|
|
) { |
117
|
|
|
return $slug; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// map our list to keep only the increments |
121
|
|
|
$len = strlen($slug . '-'); |
122
|
|
|
|
123
|
|
|
array_walk($list, function (&$value, $key) use ($len) { |
124
|
|
|
$value = intval(substr($value, $len)); |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
return $slug . '-' . $this->findHighestIncrement($list); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the existing models matching the given slug |
132
|
|
|
* @param string $slug |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function getExistingSlugs($slug) |
136
|
|
|
{ |
137
|
|
|
$query = $this->model->where('name', 'LIKE', $slug . '%'); |
138
|
|
|
|
139
|
|
|
$list = $query->pluck('name', $this->model->getKeyName())->toArray(); |
140
|
|
|
|
141
|
|
|
return $list; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Check if given array is empty |
146
|
|
|
* @param array $list |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
private function isEmptyArray(array $list) |
150
|
|
|
{ |
151
|
|
|
return count($list) === 0; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if the given slug is present in the given array |
156
|
|
|
* @param string $slug |
157
|
|
|
* @param array $list |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
private function isSlugInList($slug, array $list) |
161
|
|
|
{ |
162
|
|
|
return in_array($slug, $list); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if we have our model |
167
|
|
|
* @param $list |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
private function isForModel($list) |
171
|
|
|
{ |
172
|
|
|
return array_key_exists($this->model->getKey(), $list); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check if the given slug is in the given list |
177
|
|
|
* @param string $slug |
178
|
|
|
* @param array $list |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
private function slugIsInList($slug, array $list) |
182
|
|
|
{ |
183
|
|
|
return $list[$this->model->getKey()] === $slug; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
private function findHighestIncrement($list) |
190
|
|
|
{ |
191
|
|
|
rsort($list); |
192
|
|
|
$increment = reset($list) + 1; |
193
|
|
|
|
194
|
|
|
return $increment; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Check if the given model needs to be slugged |
199
|
|
|
* @param object $model |
200
|
|
|
* @param array $data |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
private function needsSlugging($model, array $data) |
204
|
|
|
{ |
205
|
|
|
return $model->name != array_get($data, 'name'); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|