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
|
|
|
return $this->updateTranslation($line, $locale, $value, $overwrite); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Update the translations of an existing key and locale. |
172
|
|
|
* |
173
|
|
|
* @param Translation $line |
174
|
|
|
* @param string $locale |
175
|
|
|
* @param string $value |
176
|
|
|
* @param bool $overwrite |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
**/ |
180
|
|
|
public function updateTranslation(Translation $line, $locale, $value, $overwrite = true) : bool |
181
|
|
|
{ |
182
|
|
|
if (empty($line->getTranslation('values', $locale)) || $overwrite) { |
183
|
|
|
$line->setTranslation('values', $locale, $value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->save($line); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param Translation $translation |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function save(Translation $translation) : bool |
194
|
|
|
{ |
195
|
|
|
return $translation->save(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Validate the given attributes. |
200
|
|
|
* |
201
|
|
|
* @param array $attributes |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function validate(array $attributes) : bool |
206
|
|
|
{ |
207
|
|
|
$table = $this->model->getTable(); |
208
|
|
|
$namespace = array_get($attributes, 'namespace'); |
209
|
|
|
$group = array_get($attributes, 'group'); |
210
|
|
|
|
211
|
|
|
$rules = [ |
212
|
|
|
'group' => 'required', |
213
|
|
|
'key' => "required|unique:{$table},key,NULL,id,namespace,{$namespace},group,{$group}", |
214
|
|
|
]; |
215
|
|
|
|
216
|
|
|
$validator = $this->app['validator']->make($attributes, $rules); |
217
|
|
|
|
218
|
|
|
if ($validator->fails()) { |
219
|
|
|
$this->errors = $validator->errors(); |
220
|
|
|
|
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|