Commit dec0d481 authored by Mehedi Hassan's avatar Mehedi Hassan

Make TranslationController slim

parent f06b6a2d
......@@ -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 @@ class TranslationController extends Controller
$this->translation->clearCache();
}
public function revisions(Request $request)
{
$translation = $this->translation->findTranslationByKey($request->get('key'));
$translation = $translation->translate($request->get('locale'));
if (null === $translation) {
return response()->json(['<tr><td>' . trans('translation::translations.No Revisions yet') . '</td></tr>']);
}
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)
public function revisions(TranslationRevisions $revisions, Request $request)
{
$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 <<<HTML
<tr>
<td>{$history->oldValue()}</td>
<td>{$firstName} {$lastName}</td>
<td>$edited</td>
<td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td>
<td><a href="{$revertRoute}"><i class="fa fa-history"></i></a></td>
</tr>
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 <<<HTML
<tr>
<td></td>
<td>{$firstName} {$lastName}</td>
<td>$created</td>
<td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td>
<td></td>
</tr>
HTML;
return $revisions->get(
$request->get('key'),
$request->get('locale')
);
}
}
......@@ -32,6 +32,7 @@ return [
'event' => 'Event',
'created' => 'Created',
'edited' => 'Edited',
'revert history' => 'Revert History',
'list resource' => 'List translations',
'edit resource' => 'Edit translations',
'import resource' => 'Import translations',
......
<tr>
<td>
@if ($history->oldValue() !== null)
{{ $history->oldValue() }}
@endif
</td>
<td>{{ $history->userResponsible()->present()->fullname() }}</td>
<td>
@if ($history->oldValue() === null)
{{ trans('translation::translations.created') }}
@else
{{ trans('translation::translations.edited') }}
@endif
</td>
<td>
<span data-toggle="tooltip" title="{{ $history->created_at }}">
{{ $history->created_at->diffForHumans() }}
</span>
</td>
<td>
@if ($history->oldValue() !== null)
<span data-toggle="tooltip" title="{{ trans('translation::translations.revert history') }}">
<a href="{{ route('admin.translation.translation.update', [$history->revisionable_id, 'oldValue' => $history->oldValue()]) }}">
<i class="fa fa-history"></i>
</a>
</span>
@endif
</td>
</tr>
<?php
namespace Modules\Translation\Services;
use Illuminate\Database\Eloquent\Collection;
use Modules\Translation\Repositories\TranslationRepository;
class TranslationRevisions
{
/**
* @var TranslationRepository
*/
private $translation;
/**
* @param TranslationRepository $translation
*/
public function __construct(TranslationRepository $translation)
{
$this->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(['<tr><td>' . trans('translation::translations.No Revisions yet') . '</td></tr>']);
}
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;
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment