diff --git a/Modules/Translation/Http/Controllers/Api/TranslationController.php b/Modules/Translation/Http/Controllers/Api/TranslationController.php index 0c5e60309..f1de2f12e 100644 --- a/Modules/Translation/Http/Controllers/Api/TranslationController.php +++ b/Modules/Translation/Http/Controllers/Api/TranslationController.php @@ -2,12 +2,13 @@ namespace Modules\Translation\Http\Controllers\Api; -use Cartalyst\Sentinel\Laravel\Facades\Sentinel; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; use Illuminate\Routing\Controller; -use Modules\Translation\Repositories\TranslationRepository; +use Illuminate\Database\Eloquent\Collection; +use Cartalyst\Sentinel\Laravel\Facades\Sentinel; use Modules\User\Traits\CanFindUserWithBearerToken; +use Modules\Translation\Services\TranslationRevisions; +use Modules\Translation\Repositories\TranslationRepository; class TranslationController extends Controller { @@ -38,67 +39,11 @@ public function clearCache() $this->translation->clearCache(); } - public function revisions(Request $request) + public function revisions(TranslationRevisions $revisions, Request $request) { - $translation = $this->translation->findTranslationByKey($request->get('key')); - $translation = $translation->translate($request->get('locale')); - - if (null === $translation) { - return response()->json(['' . trans('translation::translations.No Revisions yet') . '']); - } - - return response()->json($this->formatRevisionHistory($translation->revisionHistory)); - } - - private function formatRevisionHistory(Collection $revisionHistory) - { - $formattedHistory = []; - - foreach ($revisionHistory as $history) { - if ($history->key == 'created_at' && !$history->old_value) { - $formattedHistory[] = $this->getCreatedRevisionTemplate($history); - } else { - $formattedHistory[] = $this->getRevisionTemplate($history); - } - } - - return array_reverse($formattedHistory); - } - - private function getRevisionTemplate($history) - { - $timeAgo = $history->created_at->diffForHumans(); - $revertRoute = route('admin.translation.translation.update', [$history->revisionable_id, 'oldValue' => $history->oldValue()]); - $edited = trans('translation::translations.edited'); - $firstName = $history->userResponsible() ? $history->userResponsible()->first_name : ''; - $lastName = $history->userResponsible() ? $history->userResponsible()->last_name : ''; - - return << - {$history->oldValue()} - {$firstName} {$lastName} - $edited - {$timeAgo} - - -HTML; - } - - private function getCreatedRevisionTemplate($history) - { - $timeAgo = $history->created_at->diffForHumans(); - $created = trans('translation::translations.created'); - $firstName = $history->userResponsible() ? $history->userResponsible()->first_name : ''; - $lastName = $history->userResponsible() ? $history->userResponsible()->last_name : ''; - - return << - - {$firstName} {$lastName} - $created - {$timeAgo} - - -HTML; + return $revisions->get( + $request->get('key'), + $request->get('locale') + ); } } diff --git a/Modules/Translation/Resources/lang/translation/en/translations.php b/Modules/Translation/Resources/lang/translation/en/translations.php index a575cd643..6aff3a0b6 100644 --- a/Modules/Translation/Resources/lang/translation/en/translations.php +++ b/Modules/Translation/Resources/lang/translation/en/translations.php @@ -32,6 +32,7 @@ 'event' => 'Event', 'created' => 'Created', 'edited' => 'Edited', + 'revert history' => 'Revert History', 'list resource' => 'List translations', 'edit resource' => 'Edit translations', 'import resource' => 'Import translations', diff --git a/Modules/Translation/Resources/views/admin/translations/partials/revision.blade.php b/Modules/Translation/Resources/views/admin/translations/partials/revision.blade.php new file mode 100644 index 000000000..b0bd23c9a --- /dev/null +++ b/Modules/Translation/Resources/views/admin/translations/partials/revision.blade.php @@ -0,0 +1,29 @@ + + + @if ($history->oldValue() !== null) + {{ $history->oldValue() }} + @endif + + {{ $history->userResponsible()->present()->fullname() }} + + @if ($history->oldValue() === null) + {{ trans('translation::translations.created') }} + @else + {{ trans('translation::translations.edited') }} + @endif + + + + {{ $history->created_at->diffForHumans() }} + + + + @if ($history->oldValue() !== null) + + + + + + @endif + + diff --git a/Modules/Translation/Services/TranslationRevisions.php b/Modules/Translation/Services/TranslationRevisions.php new file mode 100644 index 000000000..c69e79643 --- /dev/null +++ b/Modules/Translation/Services/TranslationRevisions.php @@ -0,0 +1,60 @@ +translation = $translation; + } + + /** + * Get revisions for the given key and locale. + * + * @param string $key + * @param string $locale + * @return \Illuminate\Http\JsonResponse + */ + public function get($key, $locale) + { + $translation = $this->translation->findTranslationByKey($key); + $translation = $translation->translate($locale); + + if ($translation === null) { + return response()->json(['' . trans('translation::translations.No Revisions yet') . '']); + } + + return response()->json( + $this->formatRevisionHistory( + $translation->revisionHistory->reverse() + ) + ); + } + + /** + * Format revision history. + * + * @param Collection $revisionHistory + * @return array + */ + private function formatRevisionHistory(Collection $revisionHistory) + { + return $revisionHistory->reduce(function ($formattedHistory, $history) { + $formattedHistory[] = view('translation::admin.translations.partials.revision', compact('history'))->render(); + + return $formattedHistory; + }); + } +}