|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Omatech\Lars\BaseRepository; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Omatech\Mage\Core\Models\LanguageLine; |
|
8
|
|
|
use Omatech\Mage\Core\Events\Translations\TranslationCreated; |
|
9
|
|
|
use Omatech\Mage\Core\Events\Translations\TranslationDeleted; |
|
10
|
|
|
use Omatech\Mage\Core\Events\Translations\TranslationUpdated; |
|
11
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface; |
|
12
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\AllTranslationInterface; |
|
13
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\CreateTranslationInterface; |
|
14
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\DeleteTranslationInterface; |
|
15
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\ExistsTranslationInterface; |
|
16
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\UniqueTranslationInterface; |
|
17
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\UpdateTranslationInterface; |
|
18
|
|
|
|
|
19
|
|
|
class TranslationRepository extends BaseRepository implements |
|
20
|
|
|
AllTranslationInterface, |
|
21
|
|
|
CreateTranslationInterface, |
|
22
|
|
|
DeleteTranslationInterface, |
|
23
|
|
|
UpdateTranslationInterface, |
|
24
|
|
|
ExistsTranslationInterface, |
|
25
|
|
|
UniqueTranslationInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
11 |
|
public function model(): string |
|
31
|
|
|
{ |
|
32
|
11 |
|
return LanguageLine::class; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $locales |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function get($locales) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$select = ['id', 'group', 'key']; |
|
42
|
|
|
|
|
43
|
1 |
|
foreach ($locales as $locale) { |
|
44
|
1 |
|
$select[] = "text->$locale as $locale"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return $this->query() |
|
48
|
1 |
|
->select($select) |
|
49
|
1 |
|
->paginate() |
|
50
|
1 |
|
->toArray(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param int $id |
|
55
|
|
|
* @return TranslationInterface|null |
|
56
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function find(int $id): ?TranslationInterface |
|
59
|
|
|
{ |
|
60
|
2 |
|
$translation = $this->query()->find($id); |
|
61
|
|
|
|
|
62
|
2 |
|
if ($translation === null) { |
|
63
|
1 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$translation = app()->make(TranslationInterface::class)::fromArray([ |
|
|
|
|
|
|
67
|
1 |
|
'id' => $translation->id, |
|
68
|
1 |
|
'key' => $translation->group . '.' . $translation->key, |
|
69
|
1 |
|
'translations' => $translation->text, |
|
70
|
1 |
|
'sync_at' => $translation->sync_at, |
|
71
|
1 |
|
'created_at' => $translation->created_at, |
|
72
|
1 |
|
'updated_at' => $translation->updated_at |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
1 |
|
return $translation; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param TranslationInterface $translation |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
9 |
|
public function create(TranslationInterface $translation): bool |
|
83
|
|
|
{ |
|
84
|
9 |
|
$created = $this->query()->create([ |
|
85
|
9 |
|
'group' => $translation->getGroup(), |
|
|
|
|
|
|
86
|
9 |
|
'key' => $translation->getKey(), |
|
|
|
|
|
|
87
|
9 |
|
'text' => $translation->getTranslations(), |
|
|
|
|
|
|
88
|
9 |
|
'sync_at' => $translation->getSyncAt() |
|
|
|
|
|
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
9 |
|
$translation->setId($created->id); |
|
|
|
|
|
|
92
|
9 |
|
$translation->setSyncAt($created->sync_at); |
|
|
|
|
|
|
93
|
9 |
|
$translation->setCreatedAt($created->created_at); |
|
|
|
|
|
|
94
|
9 |
|
$translation->setUpdatedAt($created->updated_at); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
9 |
|
event(new TranslationCreated($translation, $created->wasRecentlyCreated)); |
|
97
|
|
|
|
|
98
|
9 |
|
return $created->wasRecentlyCreated; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param TranslationInterface $translation |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
9 |
|
public function exists(TranslationInterface $translation): bool |
|
106
|
|
|
{ |
|
107
|
9 |
|
return $this->query() |
|
108
|
|
|
->where(static function ($q) use ($translation) { |
|
109
|
9 |
|
$q->where('group', $translation->getGroup()) |
|
110
|
9 |
|
->where('key', $translation->getKey()); |
|
111
|
9 |
|
}) |
|
112
|
9 |
|
->orWhere('id', $translation->getId()) |
|
|
|
|
|
|
113
|
9 |
|
->exists(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param TranslationInterface $translation |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
3 |
|
public function unique(TranslationInterface $translation): bool |
|
121
|
|
|
{ |
|
122
|
3 |
|
return $this->query() |
|
123
|
3 |
|
->where('group', $translation->getGroup()) |
|
124
|
3 |
|
->where('key', $translation->getKey()) |
|
125
|
3 |
|
->where('id', '!=', $translation->getId()) |
|
126
|
3 |
|
->exists(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param TranslationInterface $translation |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function update(TranslationInterface $translation): bool |
|
134
|
|
|
{ |
|
135
|
1 |
|
$updated = $this->query()->find($translation->getId()); |
|
136
|
|
|
|
|
137
|
1 |
|
$updated->fill([ |
|
138
|
1 |
|
'group' => $translation->getGroup(), |
|
139
|
1 |
|
'key' => $translation->getKey(), |
|
140
|
1 |
|
'text' => $translation->getTranslations(), |
|
141
|
1 |
|
'sync_at' => $translation->getSyncAt() |
|
142
|
1 |
|
])->save(); |
|
143
|
|
|
|
|
144
|
1 |
|
$translation->setId($updated->id); |
|
145
|
1 |
|
$translation->setCreatedAt($updated->created_at); |
|
146
|
1 |
|
$translation->setUpdatedAt($updated->updated_at); |
|
147
|
|
|
|
|
148
|
1 |
|
event(new TranslationUpdated($translation, count($updated->getChanges()) >= 1)); |
|
149
|
|
|
|
|
150
|
1 |
|
return count($updated->getChanges()) >= 1; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param TranslationInterface $translation |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
3 |
|
public function delete(TranslationInterface $translation): bool |
|
158
|
|
|
{ |
|
159
|
3 |
|
$isDeleted = $this->query() |
|
160
|
3 |
|
->where('id', $translation->getId()) |
|
161
|
3 |
|
->delete(); |
|
162
|
|
|
|
|
163
|
3 |
|
$isDeleted = $isDeleted > 0; |
|
164
|
|
|
|
|
165
|
3 |
|
event(new TranslationDeleted($translation, $isDeleted)); |
|
166
|
|
|
|
|
167
|
3 |
|
return $isDeleted; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|