|
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
|
|
|
|
|
67
|
|
|
return <<<HTML |
|
68
|
|
|
<tr> |
|
69
|
|
|
<td>{$history->oldValue()}</td> |
|
70
|
|
|
<td>{$history->userResponsible()->first_name} {$history->userResponsible()->last_name}</td> |
|
71
|
|
|
<td>$edited</td> |
|
72
|
|
|
<td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td> |
|
73
|
|
|
<td><a href="{$revertRoute}"><i class="fa fa-history"></i></a></td> |
|
74
|
|
|
</tr> |
|
75
|
|
|
HTML; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getCreatedRevisionTemplate($history) |
|
79
|
|
|
{ |
|
80
|
|
|
$timeAgo = $history->created_at->diffForHumans(); |
|
81
|
|
|
$created = trans('translation::translations.created'); |
|
82
|
|
|
|
|
83
|
|
|
return <<<HTML |
|
84
|
|
|
<tr> |
|
85
|
|
|
<td></td> |
|
86
|
|
|
<td>{$history->userResponsible()->first_name} {$history->userResponsible()->last_name}</td> |
|
87
|
|
|
<td>$created</td> |
|
88
|
|
|
<td><a data-toggle="tooltip" title="{$history->created_at}">{$timeAgo}</a></td> |
|
89
|
|
|
<td></td> |
|
90
|
|
|
</tr> |
|
91
|
|
|
HTML; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|