1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Field; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\View; |
6
|
|
|
use League\Flysystem\Adapter\Local; |
7
|
|
|
use function localizer\locale; |
8
|
|
|
use Terranet\Administrator\Exception; |
9
|
|
|
use Terranet\Localizer\Locale; |
10
|
|
|
|
11
|
|
|
class Translatable |
12
|
|
|
{ |
13
|
|
|
protected $field; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Translatable constructor. |
17
|
|
|
* |
18
|
|
|
* @param Generic $field |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Generic $field) |
21
|
|
|
{ |
22
|
|
|
$this->field = $field; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Proxy field methods calls. |
27
|
|
|
* |
28
|
|
|
* @param $method |
29
|
|
|
* @param $args |
30
|
|
|
*/ |
31
|
|
|
public function __call($method, $args) |
32
|
|
|
{ |
33
|
|
|
if (method_exists($this->field, $method)) { |
34
|
|
|
return call_user_func_array([$this->field, $method], $args); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new Exception(sprintf('Unknown method [%s]', $method)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $page |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function render(string $page = 'index') |
45
|
|
|
{ |
46
|
|
|
if ($this->field->hasFormat()) { |
47
|
|
|
// Each Field can define its own data for custom formatter. |
48
|
|
|
$withData = method_exists($this, 'renderWith') |
49
|
|
|
? $this->renderWith() |
|
|
|
|
50
|
|
|
: [$this->field->value(), $this->field->getModel()]; |
51
|
|
|
|
52
|
|
|
return $this->field->callFormatter($withData); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$data = [ |
56
|
|
|
'field' => $this->field, |
57
|
|
|
'model' => $this->field->getModel(), |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
if (method_exists($this, $dataGetter = 'on'.title_case($page))) { |
61
|
|
|
$data += call_user_func([$this, $dataGetter]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return View::make('administrator::fields.translatable.'.$page, $data); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
protected function onEdit(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'languages' => \localizer\locales(), |
74
|
|
|
'locale' => \localizer\locale(), |
75
|
|
|
'container' => $this, |
76
|
|
|
'translations' => app('scaffold.translations'), |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Language $language |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function name(Locale $language) |
84
|
|
|
{ |
85
|
|
|
return "translatable[{$language->id()}][{$this->field->id()}]"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Locale $language |
90
|
|
|
*/ |
91
|
|
|
public function value(Locale $language) |
92
|
|
|
{ |
93
|
|
|
$model = $this->field->getModel(); |
94
|
|
|
|
95
|
|
|
return $model->translate($language->id())->{$this->field->id()}; |
96
|
|
|
} |
97
|
|
|
} |