Skip to content

Commit 5ac4924

Browse files
author
Tihomir Banov
committed
Alow empty values, add revisions option
1 parent 14f33ce commit 5ac4924

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/Despark/LaravelDbLocalization/i18nModelTrait.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getI18nId($locale = null)
7676
if (!$locale) {
7777
$locale = \App::getLocale();
7878
}
79-
$localeModel = Config::get('laravel-db-localization::locale_class');
79+
$localeModel = Config::get('laravel-db-localization::locale_class');
8080
$i18n = $localeModel::select('id')->where('locale', $locale)->first();
8181

8282
$i18nId = null;
@@ -91,22 +91,30 @@ public function getI18nId($locale = null)
9191
/**
9292
* Get specific translation.
9393
*
94-
* @param null $locale
94+
* @param false $locale
9595
*/
96-
public function translate($locale = false)
96+
public function translate($locale = false, $alowRevision = false)
9797
{
98+
$translation = null;
9899
$translationModel = new $this->translator();
99100

100101
if (!is_int($locale)) {
101102
$locale = $this->getI18nId($locale);
102103
}
103-
$translation = null;
104104

105105
if (isset($this->id) && $locale) {
106106
$translation = $translationModel::where($this->translatorField, $this->id)
107107
->where($this->localeField, $locale)->first();
108108
}
109109

110+
if ($alowRevision == true) {
111+
if (isset($translation->show_revision)) {
112+
if ($translation->show_revision == 1) {
113+
$translation = $translation->setAttributeNames(unserialize($translation->revision));
114+
}
115+
}
116+
}
117+
110118
return $translation;
111119
}
112120

@@ -162,54 +170,36 @@ public function save(array $options = [])
162170
$options = \Input::all();
163171
}
164172

165-
$translatableId = $this->saveTranslatable($options);
166-
$this->saveTranslations($translatableId, $options);
167-
}
173+
$this->saveTranslations($this->id, $options);
168174

169-
/**
170-
* Save untranslatable falues.
171-
*
172-
* @param array $options
173-
*
174-
* @return translatable Id
175-
*/
176-
public function saveTranslatable($options)
177-
{
178-
parent::save($options);
179-
180-
$translatableId = $this->id;
181-
182-
return $translatableId;
175+
return $this;
183176
}
184177

185178
/**
186179
* Insert translation values.
187180
*
188181
* @param array translatable Id
189182
* @param array $options
190-
*
191-
* @return id
192183
*/
193184
public function saveTranslations($translatableId, $options)
194185
{
195186
$translationsArray = [];
196187
$explode = [];
197188

198189
$fillables = $this->translatedAttributes;
190+
199191
foreach ($options as $input) {
200192
if (is_array($input)) {
201193
foreach ($input as $i18n => $i18nValue) {
202-
if ($i18nValue) {
203-
$explode = explode('_', $i18n);
204-
$i18nId = array_last($explode, function ($first, $last) {
194+
$explode = explode('_', $i18n);
195+
$i18nId = array_last($explode, function ($first, $last) {
205196
return $last;
206197
});
207-
$filedName = str_replace('_'.$i18nId, '', $i18n);
208-
if (in_array($filedName, $fillables)) {
209-
$translationsArray[$i18nId][$filedName] = $i18nValue;
210-
$translationsArray[$i18nId][$this->localeField] = $i18nId;
211-
$translationsArray[$i18nId][$this->translatorField] = $translatableId;
212-
}
198+
$filedName = str_replace('_'.$i18nId, '', $i18n);
199+
if (in_array($filedName, $fillables)) {
200+
$translationsArray[$i18nId][$filedName] = $i18nValue;
201+
$translationsArray[$i18nId][$this->localeField] = $i18nId;
202+
$translationsArray[$i18nId][$this->translatorField] = $translatableId;
213203
}
214204
}
215205
}
@@ -222,13 +212,24 @@ public function saveTranslations($translatableId, $options)
222212
->where($this->localeField, array_get($translationValues, $this->localeField))
223213
->first();
224214

225-
if (! isset($translation->id)) {
226-
$translation = new $modelName();
227-
}
215+
// check if translation exists with same values
216+
$query = $modelName::select('id');
228217
foreach ($translationValues as $key => $val) {
229-
$translation->$key = $val;
218+
$query->where($key, $val);
219+
}
220+
$result = $query->first();
221+
222+
if (!isset($result->id)) {
223+
if (! isset($translation->id)) {
224+
$translation = new $modelName();
225+
}
226+
227+
foreach ($translationValues as $key => $val) {
228+
$translation->$key = $val;
229+
}
230+
231+
$translation->save();
230232
}
231-
$translation->save();
232233
}
233234
}
234235
}

0 commit comments

Comments
 (0)