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\LineKey; |
8
|
|
|
use Thinktomorrow\Squanto\Domain\Page; |
9
|
|
|
use Thinktomorrow\Squanto\Exceptions\InvalidLineKeyException; |
10
|
|
|
|
11
|
|
|
class LineController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create new line |
15
|
|
|
* |
16
|
|
|
* @param null $page_id |
17
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
18
|
|
|
*/ |
19
|
|
|
public function create($page_id = null) |
20
|
|
|
{ |
21
|
|
|
// If pageid is passed, the first key (pagekey) is prefilled |
22
|
|
|
$page = $page_id ? Page::find($page_id) : null; |
23
|
|
|
$line = new Line(); |
24
|
|
|
$available_locales = config('squanto.locales'); |
25
|
|
|
|
26
|
|
|
return view('squanto::create',compact('page','line','available_locales')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function store(Request $request) |
30
|
|
|
{ |
31
|
|
|
$this->validate($request,[ |
32
|
|
|
'key' => 'required|min:3|max:100|unique:squanto_lines,key', |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
try{ |
36
|
|
|
$linekey = new LineKey($request->get('key')); |
37
|
|
|
|
38
|
|
|
$page_is_created = !(Page::findByKey($linekey->getPageKey())); |
39
|
|
|
|
40
|
|
|
$line = Line::make($linekey->get()); |
41
|
|
|
$this->saveValueTranslations($line,$request->get('trans')); |
42
|
|
|
|
43
|
|
|
$line->saveSuggestedType(); |
44
|
|
|
|
45
|
|
|
$message = $line->key. ' translation line created!' . (($page_is_created) ? ' Since the '.$linekey->getPageKey(). ' page didn\'t exist, it was added as well' : null); |
|
|
|
|
46
|
|
|
|
47
|
|
|
return redirect()->route('back.squanto.edit',$line->page_id)->with('messages.success',$message); |
|
|
|
|
48
|
|
|
|
49
|
|
|
}catch(InvalidLineKeyException $e) |
50
|
|
|
{ |
51
|
|
|
return redirect()->back()->withInput()->withErrors('Invalid format for key. Must contain at least one dot as divider of the page identifier and the key itself: e.g. foo.bar'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return redirect()->back()->withInput()->withErrors('The line could not be created due to an unknown error.'); |
|
|
|
|
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function edit($id) |
59
|
|
|
{ |
60
|
|
|
$available_locales = config('squanto.locales'); |
61
|
|
|
$line = Line::find($id); |
62
|
|
|
|
63
|
|
|
return view('squanto::lines.edit', compact('line','available_locales')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function update(Request $request, $id) |
67
|
|
|
{ |
68
|
|
|
$line = Line::find($id); |
69
|
|
|
|
70
|
|
|
$this->validate($request,[ |
71
|
|
|
'key' => 'required|min:3|max:100|unique:squanto_lines,key,'.$line->id, |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
try{ |
75
|
|
|
|
76
|
|
|
$line->key = $request->get('key'); // Note that the page connection will not get updated! |
77
|
|
|
$line->type = $request->get('type'); |
78
|
|
|
$line->label = $request->get('label'); |
79
|
|
|
$line->description = $request->get('description'); |
80
|
|
|
$line->save(); |
81
|
|
|
$this->saveValueTranslations($line,$request->get('trans')); |
82
|
|
|
|
83
|
|
|
$linekey = new LineKey($line->key); |
84
|
|
|
$page_is_created = !(Page::findByKey($linekey->getPageKey())); |
85
|
|
|
|
86
|
|
|
// TODO: alert developer that keys will need be be changed as well / or autosuggest this with easy CONFIRM TO CHANGE IN FOLLOWING FILES: |
87
|
|
|
|
88
|
|
|
$message = $line->key. ' translation line updated!' . (($page_is_created) ? ' Since the '.$linekey->getPageKey(). ' page didn\'t exist, it was added as well' : null); |
89
|
|
|
|
90
|
|
|
return redirect()->route('back.squanto.lines.edit',$line->id)->with('messages.success',$message); |
91
|
|
|
|
92
|
|
|
}catch(InvalidLineKeyException $e) |
93
|
|
|
{ |
94
|
|
|
return redirect()->back()->withInput()->withErrors('Invalid format for key. Must contain at least one dot as divider of the page identifier and the key itself: e.g. foo.bar'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return redirect()->back()->withInput()->withErrors('The line could not be updated due to an unknown error.'); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function destroy($id) |
101
|
|
|
{ |
102
|
|
|
$line = Line::findOrFail($id); |
103
|
|
|
$key = $line->key; |
104
|
|
|
$page = Page::find($line->page_id); |
105
|
|
|
|
106
|
|
|
$line->delete(); |
107
|
|
|
|
108
|
|
|
// If page has no more lines, we delete it as well |
109
|
|
|
if($page->lines()->count() < 1) |
110
|
|
|
{ |
111
|
|
|
$page->delete(); |
112
|
|
|
return redirect()->route('back.squanto.index')->with('messages.warning','Line '.$key.' is verwijderd'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return redirect()->route('back.squanto.edit',$page->id)->with('messages.warning','Line '.$key.' is verwijderd'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function saveValueTranslations(Line $line, array $translations) |
119
|
|
|
{ |
120
|
|
|
collect($translations)->map(function($value,$locale) use($line){ |
121
|
|
|
|
122
|
|
|
$value = cleanupHTML($value); |
123
|
|
|
|
124
|
|
View Code Duplication |
if(is_null($value) || "" === $value) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$line->removeValue($locale); |
127
|
|
|
} |
128
|
|
|
else |
129
|
|
|
{ |
130
|
|
|
$line->saveValue($locale,$value); |
131
|
|
|
} |
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.