Skip to content

Commit

Permalink
Make TranslationController slim
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehedi Hassan authored and Mehedi Hassan committed Aug 3, 2017
1 parent f06b6a2 commit dec0d48
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 64 deletions.
73 changes: 9 additions & 64 deletions Modules/Translation/Http/Controllers/Api/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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(['<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)
{
$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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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>
60 changes: 60 additions & 0 deletions Modules/Translation/Services/TranslationRevisions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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;
});
}
}

0 comments on commit dec0d48

Please sign in to comment.