1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Manager\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Thinktomorrow\Squanto\Domain\Line; |
7
|
|
|
use Thinktomorrow\Squanto\Domain\Page; |
8
|
|
|
use Thinktomorrow\Squanto\Services\CachedTranslationFile; |
9
|
|
|
|
10
|
|
|
class TranslationController extends Controller |
11
|
|
|
{ |
12
|
|
|
public function index() |
13
|
|
|
{ |
14
|
|
|
$pages = Page::sequence()->get(); |
|
|
|
|
15
|
|
|
|
16
|
|
|
return view('squanto::index', compact('pages')); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function edit($id) |
20
|
|
|
{ |
21
|
|
|
$available_locales = config('squanto.locales'); |
22
|
|
|
|
23
|
|
|
$page = Page::find($id); |
24
|
|
|
|
25
|
|
|
$groupedLines = $this->groupLinesByKey($page); |
26
|
|
|
|
27
|
|
|
return view('squanto::edit', compact('page', 'available_locales', 'groupedLines')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function update(Request $request, $page_id) |
31
|
|
|
{ |
32
|
|
|
$page = Page::find($page_id); |
33
|
|
|
|
34
|
|
|
$this->saveValueTranslations($request->get('trans')); |
35
|
|
|
|
36
|
|
|
// Rebuild the translations cache |
37
|
|
|
app(CachedTranslationFile::class)->delete()->write(); |
38
|
|
|
|
39
|
|
|
return redirect()->route('squanto.edit', $page->id)->with('messages.success', $page->label .' translations have been updated'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function saveValueTranslations(array $translations) |
43
|
|
|
{ |
44
|
|
|
collect($translations)->map(function ($translation, $locale) { |
45
|
|
|
collect($translation)->map(function ($value, $id) use ($locale) { |
46
|
|
|
|
47
|
|
|
$line = Line::find($id); |
48
|
|
|
|
49
|
|
|
$value = squantoCleanupHTML($value); |
50
|
|
|
|
51
|
|
|
if(false == config('squanto.paragraphize') && !$line->areParagraphsAllowed()) |
52
|
|
|
{ |
53
|
|
|
$value = $this->replaceParagraphsByLinebreaks($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// If line value is not meant to contain tags, we should strip them |
57
|
|
|
if (!$line->editInEditor()) { |
58
|
|
|
$value = squantoCleanupString($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
if (is_null($value) || "" === $value) { |
|
|
|
|
62
|
|
|
$line->removeValue($locale); |
63
|
|
|
} else { |
64
|
|
|
$line->saveValue($locale, $value); |
65
|
|
|
} |
66
|
|
|
}); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $page |
72
|
|
|
* @return \Illuminate\Support\Collection |
73
|
|
|
*/ |
74
|
|
|
private function groupLinesByKey($page) |
75
|
|
|
{ |
76
|
|
|
$groupedLines = collect(['general' => []]); |
77
|
|
|
$groups = []; |
78
|
|
|
|
79
|
|
|
foreach ($page->lines as $line) { |
80
|
|
|
$keysegment = $this->getFirstSegmentOfKey($line); |
81
|
|
|
|
82
|
|
|
if (!isset($groups[$keysegment])) { |
83
|
|
|
$groups[$keysegment] = []; |
84
|
|
|
} |
85
|
|
|
$groups[$keysegment][] = $line; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If firstkey occurs more than once, we will group it |
89
|
|
|
foreach ($groups as $group => $lines) { |
90
|
|
|
if (count($lines) < 2) { |
91
|
|
|
$groupedLines['general'] = array_merge($groupedLines['general'], $lines); |
92
|
|
|
} else { |
93
|
|
|
$groupedLines[$group] = $lines; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $groupedLines; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get suggestion for a label based on the key |
102
|
|
|
* e.g. foo.bar.title return bar |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
private function getFirstSegmentOfKey(Line $line) |
106
|
|
|
{ |
107
|
|
|
// Remove first part since that part equals the page |
108
|
|
|
$key = substr($line->key, strpos($line->key, '.')+1); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$length = strpos($key, '.')?: strlen($key); |
111
|
|
|
$key = substr($key, 0, $length); |
112
|
|
|
|
113
|
|
|
return $key; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function replaceParagraphsByLinebreaks($value) |
117
|
|
|
{ |
118
|
|
|
$value = preg_replace('/<p[^>]*?>/', '', $value); |
119
|
|
|
|
120
|
|
|
// Last paragraph is just removed, not a linebreak |
121
|
|
|
if (substr($value, -mb_strlen('</p>')) === '</p>') { |
122
|
|
|
$value = substr($value, 0, -mb_strlen('</p>')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$value = str_replace('</p>', '<br>', $value); |
126
|
|
|
|
127
|
|
|
return $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.