|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MikeZange\LaravelDatabaseTranslation\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Cache\Repository as CacheRepository; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
use MikeZange\LaravelDatabaseTranslation\Models\Translation; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class TranslationRepository. |
|
11
|
|
|
*/ |
|
12
|
|
|
class TranslationRepository |
|
13
|
|
|
{ |
|
14
|
|
|
protected $model; |
|
15
|
|
|
protected $app; |
|
16
|
|
|
protected $cache; |
|
17
|
|
|
protected $errors = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Application $app |
|
23
|
|
|
* @param CacheRepository $cache |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Application $app, CacheRepository $cache) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->model = $app->make(config('database.translations.model')); |
|
28
|
|
|
$this->app = $app; |
|
29
|
|
|
$this->cache = $cache; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return the model related to this finder. |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getModel() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->model; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check if the model's table exists. |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function tableExists() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->model->getConnection()->getSchemaBuilder()->hasTable($this->model->getTable()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retrieve all entries. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $related Related object to include. |
|
56
|
|
|
* @param int $perPage Number of records to retrieve per page. If zero the whole result set is returned. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
59
|
|
|
*/ |
|
60
|
|
|
public function all($related = [], $perPage = 0) |
|
61
|
|
|
{ |
|
62
|
|
|
$results = $this->model->with($related)->orderBy('created_at', 'DESC'); |
|
63
|
|
|
|
|
64
|
|
|
return $perPage ? $results->paginate($perPage) : $results->get(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieve a single entry by id. |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $id |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
73
|
|
|
*/ |
|
74
|
|
|
public function find($id) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->model->find($id); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Remove an entry. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $id |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function delete($id) |
|
87
|
|
|
{ |
|
88
|
|
|
$model = $this->model->where('id', $id)->first(); |
|
89
|
|
|
|
|
90
|
|
|
if (!$model) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $model->delete(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns total number of entries in DB. |
|
99
|
|
|
* |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
|
|
public function count() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->model->count(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return all items for a given namespace and group. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $namespace |
|
111
|
|
|
* @param string $group |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getItems($namespace, $group) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->model |
|
118
|
|
|
->where('namespace', $namespace) |
|
119
|
|
|
->where('group', $group) |
|
120
|
|
|
->get(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Return a specific item with its translation for a given namespace, group and key. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $namespace |
|
127
|
|
|
* @param string $group |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getItem($namespace, $group, $key) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->model |
|
135
|
|
|
->where('namespace', $namespace) |
|
136
|
|
|
->where('group', $group) |
|
137
|
|
|
->where('key', $key) |
|
138
|
|
|
->first(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Insert a new translation into the database. |
|
143
|
|
|
* If the attributes are not valid. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $attributes |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool |
|
148
|
|
|
**/ |
|
149
|
|
|
public function create(array $attributes) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->validate($attributes) ? $this->model->create($attributes) : null; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Update the translations of an existing key and locale by id. |
|
156
|
|
|
* |
|
157
|
|
|
* @param int $id |
|
158
|
|
|
* @param string $locale |
|
159
|
|
|
* @param string $value |
|
160
|
|
|
* @param bool $overwrite |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
**/ |
|
164
|
|
|
public function updateById($id, $locale, $value, $overwrite = true) : bool |
|
165
|
|
|
{ |
|
166
|
|
|
$line = $this->model->find($id); |
|
167
|
|
|
|
|
168
|
|
|
return $this->updateTranslation($line, $locale, $value, $overwrite); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Update the translations of an existing key and locale. |
|
173
|
|
|
* |
|
174
|
|
|
* @param Translation $line |
|
175
|
|
|
* @param string $locale |
|
176
|
|
|
* @param string $value |
|
177
|
|
|
* @param bool $overwrite |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool |
|
180
|
|
|
**/ |
|
181
|
|
|
public function updateTranslation(Translation $line, $locale, $value, $overwrite = true) : bool |
|
182
|
|
|
{ |
|
183
|
|
|
if (empty($line->getTranslation('values', $locale, false)) || $overwrite) { |
|
184
|
|
|
$line->setTranslation('values', $locale, $value); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this->save($line); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param Translation $translation |
|
192
|
|
|
* |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
public function save(Translation $translation) : bool |
|
196
|
|
|
{ |
|
197
|
|
|
return $translation->save(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Validate the given attributes. |
|
202
|
|
|
* |
|
203
|
|
|
* @param array $attributes |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
public function validate(array $attributes) : bool |
|
208
|
|
|
{ |
|
209
|
|
|
$table = $this->model->getTable(); |
|
210
|
|
|
$namespace = array_get($attributes, 'namespace'); |
|
211
|
|
|
$group = array_get($attributes, 'group'); |
|
212
|
|
|
|
|
213
|
|
|
$rules = [ |
|
214
|
|
|
'group' => 'required', |
|
215
|
|
|
'key' => "required|unique:{$table},key,NULL,id,namespace,{$namespace},group,{$group}", |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
$validator = $this->app['validator']->make($attributes, $rules); |
|
219
|
|
|
|
|
220
|
|
|
if ($validator->fails()) { |
|
221
|
|
|
$this->errors = $validator->errors(); |
|
222
|
|
|
|
|
223
|
|
|
return false; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return true; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|