Skip to content

Commit c4df0d6

Browse files
author
Alexander Stoimenov
committed
Use observer and fix change detection
1 parent b16f8fc commit c4df0d6

File tree

5 files changed

+55
-63
lines changed

5 files changed

+55
-63
lines changed

composer.json

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
"localization",
99
"database"
1010
],
11-
"license": "MIT",
12-
"authors": [
13-
{
14-
"name": "Tihomir Banov",
15-
"email": "tbanov@despark.com"
16-
}
17-
],
11+
"license": "GPL-3.0",
12+
"authors": [{
13+
"name": "Tihomir Banov",
14+
"email": "tbanov@despark.com"
15+
}],
1816
"require": {
19-
"php": ">=5.4.0",
17+
"php": ">=5.5.0",
2018
"illuminate/auth": "^5.1",
2119
"illuminate/support": "^5.1",
2220
"illuminate/routing": "^5.1",
@@ -30,6 +28,5 @@
3028
"Despark\\LaravelDbLocalization\\": "src/"
3129
}
3230
},
33-
"minimum-stability": "stable",
34-
"version": "0.0.1"
35-
}
31+
"minimum-stability": "stable"
32+
}

src/LocalizationObserver.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Despark\LaravelDbLocalization;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Class LocalizationObserver.
9+
*/
10+
class LocalizationObserver
11+
{
12+
/**
13+
* @param Model $model
14+
*/
15+
public function saved(Model $model)
16+
{
17+
$model->saveTranslations($model->id);
18+
}
19+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
return array(
3+
return [
44

55
/*
66
|--------------------------------------------------------------------------
@@ -11,5 +11,5 @@
1111
| Please provide the full namespaced path.
1212
|
1313
*/
14-
'locale_class' => 'Despark\LaravelDbLocalization\I18n',
15-
);
14+
'locale_class' => \Despark\LaravelDbLocalization\I18n::class,
15+
];

src/i18nModelTrait.php

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getI18nId($locale = null)
7676
}
7777

7878
$localeModel = config('laravel-db-localization.locale_class');
79-
$i18n = $localeModel::select('id')->where('locale', $locale)->first();
79+
$i18n = app($localeModel)->select('id')->where('locale', $locale)->first();
8080

8181
$i18nId = null;
8282

@@ -92,10 +92,10 @@ public function getI18nId($locale = null)
9292
*
9393
* @param false $locale
9494
*/
95-
public function translate($locale = false, $alowRevision = false)
95+
public function translate($locale = null, $alowRevision = false)
9696
{
9797
$translation = null;
98-
$translationModel = new $this->translator();
98+
$translationModel = app($this->translator);
9999

100100
if (!is_int($locale)) {
101101
$locale = $this->getI18nId($locale);
@@ -126,7 +126,7 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
126126
// get i18n id by locale
127127
$i18nId = $this->getI18nId($locale);
128128

129-
$translatorTable = new $this->translator();
129+
$translatorTable = app($this->translator);
130130
$translatorTableName = $translatorTable->getTable();
131131
$translatableTable = $this->getTable();
132132

@@ -163,45 +163,30 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
163163
}
164164

165165
/**
166-
* Save record.
167-
*
168-
* @param array $options
166+
* Boot the trait.
169167
*/
170-
public function save(array $options = [])
168+
public static function bootI18nModelTrait()
171169
{
172-
if (empty($options)) {
173-
$options = \Request::all();
174-
}
175-
176-
parent::save($options);
177-
178-
$this->saveTranslations($this->id, $options);
179-
180-
return $this;
170+
static::observe(LocalizationObserver::class);
181171
}
182172

183173
/**
184174
* Insert translation values.
185175
*
186176
* @param array translatable Id
187-
* @param array $options
188177
*/
189-
public function saveTranslations($translatableId, $options)
178+
public function saveTranslations($translatableId)
190179
{
191180
$translationsArray = [];
192-
$explode = [];
193-
194-
$fillables = $this->translatedAttributes;
195181

196-
foreach ($options as $input) {
182+
foreach (\Request::all() as $input) {
197183
if (is_array($input)) {
198184
foreach ($input as $i18n => $i18nValue) {
199-
$explode = explode('_', $i18n);
200-
$i18nId = array_last($explode, function ($first, $last) {
201-
return $last;
202-
});
185+
$i18nId = array_last(explode('_', $i18n), function ($first, $last) {
186+
return $last;
187+
});
203188
$filedName = str_replace('_'.$i18nId, '', $i18n);
204-
if (in_array($filedName, $fillables)) {
189+
if (in_array($filedName, $this->translatedAttributes)) {
205190
$translationsArray[$i18nId][$filedName] = $i18nValue;
206191
$translationsArray[$i18nId][$this->localeField] = $i18nId;
207192
$translationsArray[$i18nId][$this->translatorField] = $translatableId;
@@ -210,31 +195,22 @@ public function saveTranslations($translatableId, $options)
210195
}
211196
}
212197

213-
$modelName = $this->translator;
214-
215198
foreach ($translationsArray as $translationValues) {
216-
$translation = $modelName::where($this->translatorField, array_get($translationValues, $this->translatorField))
217-
->where($this->localeField, array_get($translationValues, $this->localeField))
199+
$translatorId = array_get($translationValues, $this->translatorField);
200+
$localeId = array_get($translationValues, $this->localeField);
201+
202+
$translation = $this->translator::where($this->translatorField, $translatorId)
203+
->where($this->localeField, $localeId)
218204
->first();
219205

220-
// check if translation exists with same values
221-
$query = $modelName::select('id');
222-
foreach ($translationValues as $key => $val) {
223-
$query->where($key, $val);
206+
if (!$translation) {
207+
$translation = app($this->translator);
224208
}
225-
$result = $query->first();
226-
227-
if (!isset($result->id)) {
228-
if (!isset($translation->id)) {
229-
$translation = new $modelName();
230-
}
231209

232-
foreach ($translationValues as $key => $val) {
233-
$translation->$key = $val;
234-
}
210+
$translation->fillable = array_keys($translationValues);
235211

236-
$translation->save();
237-
}
212+
$translation->fill($translationValues);
213+
$translation->save();
238214
}
239215
}
240216

src/seeds/DatabaseSeeder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class DatabaseSeeder extends \Seeder
1010
public function run()
1111
{
1212
\DB::table('i18n')->insert([
13-
'name' => 'English',
13+
'name' => 'English',
1414
'locale' => 'en',
1515
]);
1616

1717
\DB::table('i18n')->insert([
18-
'name' => 'Български',
18+
'name' => 'Български',
1919
'locale' => 'bg',
2020
]);
2121
}

0 commit comments

Comments
 (0)