Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TranslationController slim #363

Merged
merged 2 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Modules/Core/macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,11 @@

return new HtmlString($string);
});

Response::macro('csv', function ($file, $filename, $status = 200, $headers = []) {
return response($file, $status, array_merge([
'Content-Type' => 'application/csv',
'Content-Disposition' => "attachment; filename={$filename}",
'Pragma' => 'no-cache',
], $headers));
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public function update(TranslationTranslation $translationTranslation, Request $

public function export(TranslationsExporter $exporter)
{
return response()->make($exporter->export(), 200, [
'Content-Type' => 'application/csv',
'Content-Disposition' => "attachment; filename={$exporter->getFileName()}",
'Pragma' => 'no-cache',
]);
return response()->csv($exporter->export(), $exporter->getFileName());
}

public function import(ImportTranslationsRequest $request, TranslationsImporter $importer)
Expand Down
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;
});
}
}