|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Translation\Observers; |
|
4
|
|
|
|
|
5
|
|
|
use Translation\Models\Element; |
|
|
|
|
|
|
6
|
|
|
use Translation\Models\Translation as TranslationModel; |
|
7
|
|
|
use Config; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Event; |
|
10
|
|
|
use Log; |
|
11
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
|
|
|
|
|
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use Illuminate\Support\Facades\Schema; |
|
14
|
|
|
use Auth; |
|
15
|
|
|
use Translation; |
|
16
|
|
|
use Translation\Services\GoogleTranslate; |
|
17
|
|
|
use Templeiro\Services\CmsService; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generate a locale_group attribute for localized models if |
|
21
|
|
|
* one doesn't already exist. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Localize |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Called on model saving |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $event |
|
29
|
|
|
* @param array $payload Contains: |
|
30
|
|
|
* - |
|
31
|
|
|
* Translation\Models\Base |
|
32
|
|
|
* $model |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function handle($event, $payload) |
|
36
|
|
|
{ |
|
37
|
|
|
list($model) = $payload; |
|
38
|
|
|
|
|
39
|
|
|
// dd(app()->bound(Translation::class)); |
|
40
|
|
|
// // Ignore |
|
41
|
|
|
// if (!app()->bound(Translation::class)) { |
|
42
|
|
|
// return true; |
|
43
|
|
|
// } |
|
44
|
|
|
|
|
45
|
|
|
// Get the action from the event name |
|
46
|
|
|
preg_match('#\.(\w+)#', $event, $matches); |
|
47
|
|
|
$action = $matches[1]; |
|
48
|
|
|
|
|
49
|
|
|
// If there is matching callback method on the model, call it, passing |
|
50
|
|
|
// any additional event arguments to it |
|
51
|
|
|
$method = 'on'.Str::studly($action); |
|
52
|
|
|
|
|
53
|
|
|
if (method_exists($this, $method)) { |
|
54
|
|
|
return $this->$method($model); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @param object $payload |
|
62
|
|
|
*/ |
|
63
|
|
|
public function onSaving($model) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->onCreating($model); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* After the item is created in the database. |
|
70
|
|
|
* |
|
71
|
|
|
* @param object $payload |
|
72
|
|
|
*/ |
|
73
|
|
|
public function onCreating($model) |
|
74
|
|
|
{ |
|
75
|
|
|
if (Schema::hasColumn($model->getTable(), 'locale') |
|
76
|
|
|
&& !empty($model->locale) |
|
77
|
|
|
&& empty($model->locale_group) |
|
78
|
|
|
&& !is_a($model, Element::class) // Elements don't have groups |
|
79
|
|
|
&& ($locales = Config::get('sitec.site.locales')) |
|
80
|
|
|
&& count($locales) > 1 |
|
81
|
|
|
) { |
|
82
|
|
|
$model->setAttribute('locale_group', Str::random()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (Schema::hasColumn($model->getTable(), 'language_code') && empty($model->language_code)) { |
|
86
|
|
|
$model->language_code = Translation::getLanguageCode(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (Schema::hasColumn($model->getTable(), 'country_code') && empty($model->country_code)) { |
|
90
|
|
|
$model->language_code = Translation::getCountryCode(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* After the item is created in the database. |
|
100
|
|
|
* |
|
101
|
|
|
* @param object $model |
|
102
|
|
|
*/ |
|
103
|
|
|
public function onCreated($model) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!Schema::hasColumn($model->getTable(), 'language_code')) { |
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!config('siravel.auto-translate', false)) { |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$entry = $model->toArray(); |
|
114
|
|
|
|
|
115
|
|
|
unset($entry['created_at']); |
|
116
|
|
|
unset($entry['updated_at']); |
|
117
|
|
|
unset($entry['translations']); |
|
118
|
|
|
unset($entry['is_published']); |
|
119
|
|
|
unset($entry['published_at']); |
|
120
|
|
|
unset($entry['id']); |
|
121
|
|
|
|
|
122
|
|
|
foreach (config('siravel.languages') as $code => $language) { |
|
123
|
|
|
if ($code != config('siravel.default-language')) { |
|
|
|
|
|
|
124
|
|
|
$tr = new GoogleTranslate(config('siravel.default-language'), $code); |
|
125
|
|
|
$translation = [ |
|
126
|
|
|
'lang' => $code, |
|
127
|
|
|
'template' => 'show', |
|
128
|
|
|
]; |
|
129
|
|
|
|
|
130
|
|
|
foreach ($entry as $key => $value) { |
|
131
|
|
|
if (!empty($value)) { |
|
132
|
|
|
try { |
|
133
|
|
|
$translation[$key] = json_decode(json_encode($tr->translate(strip_tags($value)))); |
|
134
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
135
|
|
|
Log::info('[Translate] Erro> '.$e->getMessage()); |
|
136
|
|
|
unset($translation[$key]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (isset($translation['url'])) { |
|
142
|
|
|
$translation['url'] = app(CmsService::class)->convertToURL($translation['url']); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$entityId = $payload->id; |
|
|
|
|
|
|
146
|
|
|
$entityType = get_class($payload); |
|
147
|
|
|
app(ModelTranslationRepository::class)->createOrUpdate($entityId, $entityType, $code, $translation); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* When the item is being deleted. |
|
155
|
|
|
* |
|
156
|
|
|
* @param object $payload |
|
157
|
|
|
*/ |
|
158
|
|
|
public function onDeleting($payload) |
|
159
|
|
|
{ |
|
160
|
|
|
$type = get_class($payload); |
|
161
|
|
|
$id = $payload->id; |
|
162
|
|
|
|
|
163
|
|
|
TranslationModel::where('entity_id', $id)->where('entity_type', $type)->delete(); |
|
164
|
|
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths