1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Manager\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Thinktomorrow\Squanto\Domain\Line; |
7
|
|
|
use Thinktomorrow\Squanto\Domain\Page; |
8
|
|
|
|
9
|
|
|
class TranslationController extends Controller |
10
|
|
|
{ |
11
|
|
|
public function index() |
12
|
|
|
{ |
13
|
|
|
$pages = Page::sequence()->get(); |
|
|
|
|
14
|
|
|
|
15
|
|
|
return view('squanto::index',compact('pages')); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function edit($id) |
19
|
|
|
{ |
20
|
|
|
$available_locales = config('squanto.locales'); |
21
|
|
|
|
22
|
|
|
$page = Page::find($id); |
23
|
|
|
|
24
|
|
|
$groupedLines = $this->groupLinesByKey($page); |
25
|
|
|
|
26
|
|
|
return view('squanto::edit', compact('page','available_locales','groupedLines')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function update(Request $request, $page_id) |
30
|
|
|
{ |
31
|
|
|
$page = Page::find($page_id); |
32
|
|
|
|
33
|
|
|
$this->saveValueTranslations($request->get('trans')); |
34
|
|
|
|
35
|
|
|
// TODO: Resave our cached translation |
36
|
|
|
|
37
|
|
|
return redirect()->route('back.squanto.edit',$page->id)->with('messages.success', $page->label .' translations have been updated'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function saveValueTranslations(array $translations) |
41
|
|
|
{ |
42
|
|
|
collect($translations)->map(function($translation,$locale){ |
43
|
|
|
collect($translation)->map(function($value,$id) use($locale){ |
44
|
|
|
|
45
|
|
|
$value = cleanupHTML($value); |
46
|
|
|
$line = Line::find($id); |
47
|
|
|
|
48
|
|
|
// If line value is not meant to contain tags, we should strip them |
49
|
|
|
if(!$line->editInEditor()) $value = cleanupString($value); |
50
|
|
|
|
51
|
|
View Code Duplication |
if(is_null($value) || "" === $value) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$line->removeValue($locale); |
54
|
|
|
} |
55
|
|
|
else |
56
|
|
|
{ |
57
|
|
|
$line->saveValue($locale,$value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
}); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $page |
66
|
|
|
* @return \Illuminate\Support\Collection |
67
|
|
|
*/ |
68
|
|
|
private function groupLinesByKey($page) |
69
|
|
|
{ |
70
|
|
|
$groupedLines = collect(['general' => []]); |
71
|
|
|
$groups = []; |
72
|
|
|
|
73
|
|
|
foreach ($page->lines as $line) |
74
|
|
|
{ |
75
|
|
|
$keysegment = $this->getFirstSegmentOfKey($line); |
76
|
|
|
|
77
|
|
|
if (!isset($groups[$keysegment])) |
78
|
|
|
{ |
79
|
|
|
$groups[$keysegment] = []; |
80
|
|
|
} |
81
|
|
|
$groups[$keysegment][] = $line; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// If firstkey occurs more than once, we will group it |
85
|
|
|
foreach ($groups as $group => $lines) |
86
|
|
|
{ |
87
|
|
|
if (count($lines) < 2) |
88
|
|
|
{ |
89
|
|
|
$groupedLines['general'] = array_merge($groupedLines['general'], $lines); |
90
|
|
|
} else |
91
|
|
|
{ |
92
|
|
|
$groupedLines[$group] = $lines; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $groupedLines; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get suggestion for a label based on the key |
101
|
|
|
* e.g. foo.bar.title return bar |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function getFirstSegmentOfKey(Line $line) |
105
|
|
|
{ |
106
|
|
|
// Remove first part since that part equals the page |
107
|
|
|
$key = substr($line->key, strpos($line->key, '.')+1); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$length = strpos($key, '.')?: strlen($key); |
110
|
|
|
$key = substr($key,0,$length); |
111
|
|
|
|
112
|
|
|
return $key; |
113
|
|
|
} |
114
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.