Skip to content

Commit

Permalink
GH #2589: CA Admin: Allow updating library translations in database
Browse files Browse the repository at this point in the history
  • Loading branch information
chrieinv committed Nov 23, 2023
1 parent 634a178 commit 81db288
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 3 deletions.
5 changes: 5 additions & 0 deletions sourcecode/apis/contentauthor/app/H5PLibraryLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $library_id
* @property string $language_code
* @property string $translation JSON string
*/
class H5PLibraryLanguage extends Model
{
use HasFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use App\Content;
use App\H5PContent;
use App\H5PLibrary;
use App\H5PLibraryLanguage;
use App\H5PLibraryLibrary;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminTranslationUpdateRequest;
use App\Libraries\ContentAuthorStorage;
use Cerpus\VersionClient\VersionData;
use Exception;
Expand Down Expand Up @@ -102,6 +104,7 @@ public function checkLibrary(H5PLibrary $library): View
'usedBy' => H5PLibraryLibrary::where('required_library_id', $library->id)->get(),
'info' => $validator->h5pF->getMessages('info'),
'error' => $validator->h5pF->getMessages('error'),
'languages' => H5PLibraryLanguage::select('language_code')->where('library_id', $library->id)->pluck('language_code'),
]);
}

Expand Down Expand Up @@ -185,6 +188,68 @@ public function contentHistory(H5PContent $content): View
]);
}

public function libraryTranslation(H5PLibrary $library, string $locale): View
{
$libLang = H5PLibraryLanguage::where('library_id', $library->id)
->where('language_code', $locale)
->first();

return view('admin.library-upgrade.translation', [
'library' => $library,
'languageCode' => $locale,
'translationDb' => $libLang?->translation,
'translationFile' => Storage::disk()->get(
sprintf('libraries/%s/language/%s.json', $library->getFolderName(), $locale)
),
]);
}

public function libraryTranslationUpdate(AdminTranslationUpdateRequest $request, H5PLibrary $library, string $locale): View
{
$errorMsg = '';
$success = -1;
$translation = '';
$input = $request->validated();

try {
if (array_key_exists('translationFile', $input) && $request->file('translationFile')->isValid()) {
$translation = $request->file('translationFile')->getContent();
} else {
$translation = $input['translation'];
}
if (empty($translation)) {
throw new Exception('Empty content');

Check warning on line 221 in sourcecode/apis/contentauthor/app/Http/Controllers/Admin/AdminH5PDetailsController.php

View check run for this annotation

Codecov / codecov/patch

sourcecode/apis/contentauthor/app/Http/Controllers/Admin/AdminH5PDetailsController.php#L221

Added line #L221 was not covered by tests
}
json_decode($translation, flags: JSON_THROW_ON_ERROR);

$success = $library->languages()
->where('language_code', $locale)
->limit(1)
->update(['translation' => $translation]);

if ($success === 0) {
$errorMsg = "No changes to save";
}
} catch (Exception $e) {
$errorMsg = $e->getMessage();

Check warning on line 234 in sourcecode/apis/contentauthor/app/Http/Controllers/Admin/AdminH5PDetailsController.php

View check run for this annotation

Codecov / codecov/patch

sourcecode/apis/contentauthor/app/Http/Controllers/Admin/AdminH5PDetailsController.php#L233-L234

Added lines #L233 - L234 were not covered by tests
}

$libLang = H5PLibraryLanguage::where('library_id', $library->id)
->where('language_code', $locale)
->first();

return view('admin.library-upgrade.translation', [
'library' => $library,
'languageCode' => $locale,
'translationDb' => $success === 0 ? $translation : $libLang?->translation,
'translationFile' => Storage::disk()->get(
sprintf('libraries/%s/language/%s.json', $library->getFolderName(), $locale)
),
'success' => $success > 0,
'errorMessage' => $errorMsg,
]);
}

private function getVersions(VersionData $versionData, Collection $stack, $getChildren = true): Collection
{
$versionArray = $versionData->toArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Gate;

class AdminTranslationUpdateRequest extends Request
{
public function authorize(): bool
{
return Gate::allows('superadmin');
}

public function rules(): array
{
return [
'translationFile' => [
'required_without:translation',
'filled',
'file',
'max:50',
],
'translation' => [
'required_without:translationFile',
'filled',
'json',
'max:51200',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<form action="{{ route('admin.check-for-updates') }}" method="post">
@csrf
<h4>Content types from h5p.org</h4>
<p>Last updated: {{ \Carbon\Carbon::createFromTimestamp($contentTypeCacheUpdatedAt)->format('Y-m-d H:i:s e') }}</p>
<button type="submit" class="btn btn-success">Check for updates</button>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@
@endisset
</td>
</tr>
<tr>
<th>Translations</th>
<td colspan="2">
@foreach($languages as $lang)
<a
href="{{ route('admin.library-translation', [$library->id, $lang]) }}"
class="btn btn-default"
>
{{ $lang }}
</a>
@endforeach
</td>
<td></td>
</tr>
</table>
</div>
</div>
Expand All @@ -171,14 +185,22 @@
<th>semantics.json</th>
</tr>
<tr>
<td>
<td style="width:50%">
@if(!empty($library->semantics))
<textarea wrap="off" readonly style="width:400px;height:300px;">{!!$library->semantics!!}</textarea>
<textarea
readonly
rows="15"
style="width:100%;white-space:pre"
>{!!$library->semantics!!}</textarea>
@endif
</td>
<td>
@if(!empty($libData['semantics']))
<textarea wrap="off" readonly style="width:400px;height:300px;">{!! $libData['semantics'] !!}</textarea>
<textarea
readonly
rows="15"
style="width:100%;white-space:pre"
>{!! $libData['semantics'] !!}</textarea>
@endif
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
@extends ('layouts.admin')
@section ('content')
<div class="container" style="width:80vw">
<a href="{{ route('admin.check-library', [$library->id]) }}">Back to library</a>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
"<b>{{ $languageCode }}</b>"
translation for {{ $library->getLibraryString(true) }}
</h3>
</div>
<div class="panel-body row">
<div class="alert alert-warning">
<ul>
<li>This will be a local change only, for exported content the original file is included
<li>If a new patch version of this library is installed, changes done may be lost
</ul>
</div>
</div>
@isset($success)
@if($success)
<div class="alert alert-success">
Database updated
</div>
@endif
@empty(!$errorMessage)
<div class="alert alert-danger">
Update failed
<pre style="margin-top:1em;">{{ $errorMessage }}</pre>
</div>
@endif
@endisset
@if($errors->isNotEmpty())
<div class="alert alert-danger">
Update failed
@foreach($errors->all() as $error)
<pre style="margin-top:1em;">{{ $error }}</pre>
@endforeach
</div>
@endif
<div class="panel-body row">
<div class="panel panel-default">
<div class="panel-heading">
<h5>Upload language file</h5>
</div>
<div class="panel-body row">
<form method="post" accept-charset="utf-8" enctype="multipart/form-data" >
@csrf
<input
type="file"
name="translationFile"
accept=".json"
>
<br>
<button type="submit" class="btn btn-primary btn-lg">
Upload
</button>
</form>
</div>
</div>
</div>
<div class="panel-body row">
<table class="table table-striped">
<tr>
<th>Database</th>
<th>File (read only)</th>
</tr>
<tr>
<td style="width: 50%;">
@empty($translationDb)
No data found
@else
<form method="post" accept-charset="utf-8">
@csrf
<textarea
name="translation"
autocomplete="off"
required
style="width:100%;height:70vh;white-space:pre;"
>{{$translationDb}}</textarea>
<br>
<button type="submit" class="btn btn-primary btn-lg">
Save
</button>
</form>
@endempty
</td>
<td style="width: 50%;">
@empty($translationFile)
No data found
@else
<textarea
autocomplete="off"
readonly
style="width:100%;height:70vh;white-space:pre;"
>{{$translationFile}}</textarea>
@endempty
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
3 changes: 3 additions & 0 deletions sourcecode/apis/contentauthor/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ function () {
->name('admin.content-library');
Route::get('content/{content}/details', [AdminH5PDetailsController::class, 'contentHistory'])
->name('admin.content-details');
Route::get('libraries/{library}/translation/{locale}', [AdminH5PDetailsController::class, 'libraryTranslation'])
->name('admin.library-translation');
Route::post('libraries/{library}/translation/{locale}', [AdminH5PDetailsController::class, 'libraryTranslationUpdate']);

Route::get('libraries/{library}', [ContentUpgradeController::class, 'upgrade'])->name('admin.library');

Expand Down
Loading

0 comments on commit 81db288

Please sign in to comment.