Completed
Push — master ( dbf93a...f52fd4 )
by Nicolas
03:57
created

TranslationController::getRevisionTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4286
cc 1
eloc 12
nc 1
nop 1
1
<?php namespace Modules\Translation\Http\Controllers\Api;
2
3
use Illuminate\Database\Eloquent\Collection;
4
use Illuminate\Http\Request;
5
use Illuminate\Routing\Controller;
6
use Modules\Translation\Repositories\TranslationRepository;
7
8
class TranslationController extends Controller
9
{
10
    /**
11
     * @var TranslationRepository
12
     */
13
    private $translation;
14
15
    public function __construct(TranslationRepository $translation)
16
    {
17
        $this->translation = $translation;
18
    }
19
20
    public function update(Request $request)
21
    {
22
        $this->translation->saveTranslationForLocaleAndKey(
23
            $request->get('locale'),
24
            $request->get('key'),
25
            $request->get('value')
26
        );
27
    }
28
29
    public function clearCache()
30
    {
31
        $this->translation->clearCache();
32
    }
33
34
    public function revisions(Request $request)
35
    {
36
        $translation = $this->translation->findTranslationByKey($request->get('key'));
37
        $translation = $translation->translate($request->get('locale'));
38
39
        if (null === $translation) {
40
            return response()->json(['<tr><td>' . trans('translation::translations.No Revisions yet') . '</td></tr>']);
41
        }
42
43
        return response()->json($this->formatRevisionHistory($translation->revisionHistory));
44
    }
45
46
    private function formatRevisionHistory(Collection $revisionHistory)
47
    {
48
        $formattedHistory = [];
49
50
        foreach ($revisionHistory as $history) {
51
            if ($history->key == 'created_at' && !$history->old_value) {
52
                $formattedHistory[] = $this->getCreatedRevisionTemplate($history);
53
            } else {
54
                $formattedHistory[] = $this->getRevisionTemplate($history);
55
            }
56
        }
57
58
        return array_reverse($formattedHistory);
59
    }
60
61
    private function getRevisionTemplate($history)
62
    {
63
        $timeAgo = $history->created_at->diffForHumans();
64
        $revertRoute = route('admin.translation.translation.update', [$history->revisionable_id, 'oldValue' => $history->oldValue()]);
65
        $edited = trans('translation::translations.edited');
66
        return <<<HTML
67
<tr>
68
    <td>{$history->oldValue()}</td>
69
    <td>{$history->userResponsible()->first_name} {$history->userResponsible()->last_name}</td>
70
    <td>$edited</td>
71
    <td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td>
72
    <td><a href="{$revertRoute}"><i class="fa fa-history"></i></a></td>
73
</tr>
74
HTML;
75
    }
76
77
    private function getCreatedRevisionTemplate($history)
78
    {
79
        $timeAgo = $history->created_at->diffForHumans();
80
        $created = trans('translation::translations.created');
81
        return <<<HTML
82
<tr>
83
    <td></td>
84
    <td>{$history->userResponsible()->first_name} {$history->userResponsible()->last_name}</td>
85
    <td>$created</td>
86
    <td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td>
87
    <td></td>
88
</tr>
89
HTML;
90
91
    }
92
}
93