namespace App\Http\Controllers;
+use App\Models\Word;
+use App\Models\Phrase;
+use App\Helpers\Helper;
+use App\Helpers\DbHelper;
+use App\Models\SProperty;
+use App\Helpers\Pagination;
+use App\Helpers\ViewHelper;
use Illuminate\Http\Request;
+use App\Helpers\ContextLaraKnife;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
-use App\Models\Word;
-use App\Models\SProperty;
-use App\Helpers\ContextLaraKnife;
-use App\Helpers\ViewHelper;
-use App\Helpers\DbHelper;
-use App\Helpers\Helper;
-use App\Helpers\Pagination;
class WordController extends Controller
{
}
$optionsWordtype = SProperty::optionsByScope('wordtype', $fields['wordtype_scope'], '-');
$optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby_id'], __('<Please select>'));
- $context = new ContextLaraKnife($request, $fields);
+ $context = new ContextLaraKnife($request, $fields);
$rc = view('word.create', [
'context' => $context,
'optionsWordtype' => $optionsWordtype,
'optionsVerifiedby' => $optionsVerifiedby,
- ]);
+ ]);
}
return $rc;
}
$rc = redirect('/word-index');
} else {
$fields = $request->all();
- if (count($fields) === 0) {
+ $phrase = Phrase::where(['word_id' => $word->id, 'language_scope' => 1202])->first();
+ if ($phrase == null){
+ $phrase = Phrase::create(['word_id' => $word->id, 'phrase' => $word->word,
+ 'language_scope' => 1202, 'owner_id' => auth()->id()]);
+ }
+ if (count($fields) === 1) {
$fields = [
- 'word' => '',
- 'usage' => '',
- 'wordtype_scope' => '',
- 'verifiedby_id' => ''
- ];
+ 'word' => $word->word,
+ 'usage' => $phrase->phrase,
+ 'wordtype_scope' => $phrase->wordtype_scope,
+ 'language' => $request->language,
+ 'lastLanguage' => '',
+ 'translation' => ''
+ ];
+ } elseif (count($fields) === 0) {
+ $fields = [
+ 'word' => $word->word,
+ 'usage' => $phrase->phrase,
+ 'wordtype_scope' => $phrase->wordtype_scope,
+ 'language' => $request->language,
+ 'language' => SProperty::idOfLocalization(),
+ 'lastLanguage' => '',
+ 'translation' => ''
+ ];
}
+ if ($fields['translation'] === '' || $fields['lastLanguage'] != $fields['language']) {
+ $phraseTranslation = Word::where(['language_scope' => $fields['language'], 'word_id' => $word->id])->first();
+ if ($phraseTranslation != null) {
+ $fields['translation'] = $phraseTranslation->word;
+ }
+ }
+ $fields['lastLanguage'] = $fields['language'];
$optionsWordtype = SProperty::optionsByScope('wordtype', $word->wordtype_scope, '');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby_id'], __('<Please select>'));
- $context = new ContextLaraKnife($request, null, $word);
+ $optionsLanguage = SProperty::optionsByScope('localization', $fields['language'], '');
+ $context = new ContextLaraKnife($request, $fields, $word);
$rc = view('word.edit', [
'context' => $context,
'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
- ]);
+ 'optionsLanguage' => $optionsLanguage,
+ ]);
}
return $rc;
}
if ($request->btnSubmit === 'btnNew') {
return redirect('/word-create');
} else {
- $sql = 'SELECT t0.*, t1.name as wordtype, t2.name as verifiedby, t3.name as owner'
- . ' FROM words t0'
- . ' LEFT JOIN sproperties t1 ON t1.id=t0.wordtype_scope'
- . ' LEFT JOIN sproperties t2 ON t2.id=t0.verifiedby_id'
- . ' LEFT JOIN sproperties t3 ON t3.id=t0.owner_id'
- ;
- $parameters = [];
$fields = $request->all();
+ $lang = count($fields) == 0 ? SProperty::idOfLocalization() : $fields['language'];
+ $sql = "
+SELECT t0.*, t1.name as wordtype,
+ (select word from words t4 where t4.word_id=t0.id and t4.language_scope=$lang) as translation
+FROM words t0
+LEFT JOIN sproperties t1 ON t1.id=t0.wordtype_scope
+";
+ $parameters = [];
+ $conditions = [];
if (count($fields) == 0) {
$fields = [
- 'wordtype' => '',
- 'verifiedby' => '',
- 'owner' => '',
- 'word' => '',
- 'usage' => '',
- '_sortParams' => 't0.word:asc;id:asc'
+ 'language' => SProperty::idOfLocalization(),
+ 'wordtype' => '',
+ 'verified' => '',
+ 'owner' => '',
+ 'word' => '',
+ 'usage' => '',
+ '_sortParams' => 't0.word:asc;id:asc'
];
} else {
- $conditions = [];
ViewHelper::addConditionPattern($conditions, $parameters, 't0.word', 'name');
ViewHelper::addConditionComparism($conditions, $parameters, 'wordtype_scope', 'wordtype');
- ViewHelper::addConditionComparism($conditions, $parameters, 'verifiedby_id', 'verifiedby');
ViewHelper::addConditionComparism($conditions, $parameters, 'owner_id', 'owner');
ViewHelper::addConditionPattern($conditions, $parameters, 'usage');
- $sql = DbHelper::addConditions($sql, $conditions);
}
+ ViewHelper::addConditionConstComparison($conditions, $parameters, 't0.language_scope', 1202);
+ $sql = DbHelper::addConditions($sql, $conditions);
$sql = DbHelper::addOrderBy($sql, $fields['_sortParams']);
$pagination = new Pagination($sql, $parameters, $fields);
$records = $pagination->records;
$optionsWordtype = SProperty::optionsByScope('wordtype', $fields['wordtype'], 'all');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby'], __('<All>'));
+ $optionsLanguage = SProperty::optionsByScope('localization', $fields['language']);
$optionsOwner = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['owner'], __('<All>'));
+ $optionsVerified = [
+ ['text' => __('<All>'), 'value' => '', 'active' => $fields['verified'] == ''],
+ ['text' => __('Not verified'), 'value' => 1, 'active' => $fields['verified'] == 1],
+ ['text' => __('Verified'), 'value' => 2, 'active' => $fields['verified'] == 2]
+ ];
$context = new ContextLaraKnife($request, $fields);
+ $context->setCombobox('language', 'Language', $optionsLanguage, '', 'lkn-autoupdate');
return view('word.index', [
'context' => $context,
'records' => $records,
'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
+ 'optionsVerified' => $optionsVerified,
'optionsOwner' => $optionsOwner,
'pagination' => $pagination
]);
* Returns the validation rules.
* @return array<string, string> The validation rules.
*/
- private function rules(bool $isCreate=false): array
+ private function rules(bool $isCreate = false): array
{
$rc = [
'name' => 'required',
'optionsWordtype' => $optionsWordtype,
'optionsVerifiedby' => $optionsVerifiedby,
'mode' => 'delete'
- ]);
+ ]);
}
return $rc;
}
$rc = back()->withErrors($validator)->withInput();
} else {
$validated = $validator->validated();
- $validated['usage'] = strip_tags($validated['usage']);
+ $validated['usage'] = strip_tags($validated['usage']);
Word::create($validated);
}
}
- if ($rc == null){
+ if ($rc == null) {
$rc = redirect('/word-index');
}
return $rc;
{
$rc = null;
$session = $request->session();
- if ($request->btnSubmit === 'btnStore') {
+ if ($request->btnSubmit === 'btnInternal') {
+ // Language combobox has been changed:
+ $url = "/word-edit/$word->id?language=" . $request->language;
+ $rc = redirect($url);
+ } elseif ($request->btnSubmit === 'btnStore') {
+ $id = $word->id;
$fields = $request->all();
$validator = Validator::make($fields, $this->rules(false));
if ($validator->fails()) {
} else {
$validated = $validator->validated();
$validated['usage'] = strip_tags($validated['usage']);
- $word->update($validated);
+ // Word has been changed?
+ $wordDb = Word::find($id);
+ if ($word->word !== $wordDb->word) {
+ // Invalidate all verifications in all languages:
+ DB::update("UPDATE phrases SET verifiedby_id=NULL WHERE word_id=$id;");
+ // Store new phrase:
+ $word->update(['phrase' => $validated['phrase']]);
+ }
+ // Does translation already exists?
+ $wordTranslation = word::where(['language_scope' => $fields['language'], 'word_id' => $word->id])->first();
+ if ($wordTranslation == null) {
+ // Store the new record:
+ word::create([
+ 'word_id' => $id,
+ 'word' => $fields['translation'],
+ 'wordtype_scope' => $word->wordtype_scope,
+ 'owner_id' => auth()->id(),
+ 'language_scope' => $fields['language']
+ ]);
+ } else {
+ // Update only if changed:
+ if ($wordTranslation->word !== $fields['translation']) {
+ $wordTranslation->update(['word' => $validated['translation'], 'verifiedby_id' => null]);
+ } else if ($word->verifiedby_id == null){
+ $wordTranslation->update(['verifiedby_id' => auth()->id()]);
+ }
+ }
+ }
+ $lang = $fields['language'];
+ $rc = redirect("/word-edit/$id?language=$lang");
}
- }
- if ($rc == null){
+ if ($rc == null) {
$rc = redirect('/word-index');
}
return $rc;