@@ -76,7 +76,7 @@ public function getI18nId($locale = null)
76
76
if (!$ locale ) {
77
77
$ locale = \App::getLocale ();
78
78
}
79
- $ localeModel = Config::get ('laravel-db-localization::locale_class ' );
79
+ $ localeModel = Config::get ('laravel-db-localization::locale_class ' );
80
80
$ i18n = $ localeModel ::select ('id ' )->where ('locale ' , $ locale )->first ();
81
81
82
82
$ i18nId = null ;
@@ -91,22 +91,30 @@ public function getI18nId($locale = null)
91
91
/**
92
92
* Get specific translation.
93
93
*
94
- * @param null $locale
94
+ * @param false $locale
95
95
*/
96
- public function translate ($ locale = false )
96
+ public function translate ($ locale = false , $ alowRevision = false )
97
97
{
98
+ $ translation = null ;
98
99
$ translationModel = new $ this ->translator ();
99
100
100
101
if (!is_int ($ locale )) {
101
102
$ locale = $ this ->getI18nId ($ locale );
102
103
}
103
- $ translation = null ;
104
104
105
105
if (isset ($ this ->id ) && $ locale ) {
106
106
$ translation = $ translationModel ::where ($ this ->translatorField , $ this ->id )
107
107
->where ($ this ->localeField , $ locale )->first ();
108
108
}
109
109
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
+
110
118
return $ translation ;
111
119
}
112
120
@@ -162,54 +170,36 @@ public function save(array $options = [])
162
170
$ options = \Input::all ();
163
171
}
164
172
165
- $ translatableId = $ this ->saveTranslatable ($ options );
166
- $ this ->saveTranslations ($ translatableId , $ options );
167
- }
173
+ $ this ->saveTranslations ($ this ->id , $ options );
168
174
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 ;
183
176
}
184
177
185
178
/**
186
179
* Insert translation values.
187
180
*
188
181
* @param array translatable Id
189
182
* @param array $options
190
- *
191
- * @return id
192
183
*/
193
184
public function saveTranslations ($ translatableId , $ options )
194
185
{
195
186
$ translationsArray = [];
196
187
$ explode = [];
197
188
198
189
$ fillables = $ this ->translatedAttributes ;
190
+
199
191
foreach ($ options as $ input ) {
200
192
if (is_array ($ input )) {
201
193
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 ) {
205
196
return $ last ;
206
197
});
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 ;
213
203
}
214
204
}
215
205
}
@@ -222,13 +212,24 @@ public function saveTranslations($translatableId, $options)
222
212
->where ($ this ->localeField , array_get ($ translationValues , $ this ->localeField ))
223
213
->first ();
224
214
225
- if (! isset ($ translation ->id )) {
226
- $ translation = new $ modelName ();
227
- }
215
+ // check if translation exists with same values
216
+ $ query = $ modelName ::select ('id ' );
228
217
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 ();
230
232
}
231
- $ translation ->save ();
232
233
}
233
234
}
234
235
}
0 commit comments