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
|
|
|
/** |
12
|
|
|
* Class Translatable |
13
|
|
|
* |
14
|
|
|
* @package Terranet\Administrator\Field |
15
|
|
|
* @method switchTo(string $className) |
16
|
|
|
* @method tinymce() |
17
|
|
|
* @method ckeditor() |
18
|
|
|
* @method markdown() |
19
|
|
|
* @method medium() |
20
|
|
|
* @method hideLabel() |
21
|
|
|
* @method sortable(\Closure $callback = null) |
22
|
|
|
* @method disableSorting() |
23
|
|
|
*/ |
24
|
|
|
class Translatable |
25
|
|
|
{ |
26
|
|
|
/** @var Generic */ |
27
|
|
|
protected $field; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Translatable constructor. |
31
|
|
|
* |
32
|
|
|
* @param Generic $field |
33
|
|
|
*/ |
34
|
|
|
protected function __construct(Generic $field) |
35
|
|
|
{ |
36
|
|
|
$this->field = $field; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Generic $field |
41
|
|
|
* @return Translatable |
42
|
|
|
*/ |
43
|
|
|
public static function make(Generic $field) |
44
|
|
|
{ |
45
|
|
|
return new static($field); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Proxy field methods calls. |
50
|
|
|
* |
51
|
|
|
* @param $method |
52
|
|
|
* @param $args |
53
|
|
|
*/ |
54
|
|
|
public function __call($method, $args) |
55
|
|
|
{ |
56
|
|
|
if (method_exists($this->field, $method)) { |
57
|
|
|
return call_user_func_array([$this->field, $method], $args); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $page |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function render(string $page = 'index') |
66
|
|
|
{ |
67
|
|
|
if ($this->field->hasFormat()) { |
68
|
|
|
// Each Field can define its own data for custom formatter. |
69
|
|
|
$withData = method_exists($this, 'renderWith') |
70
|
|
|
? $this->renderWith() |
|
|
|
|
71
|
|
|
: [$this->field->value(), $this->field->getModel()]; |
72
|
|
|
|
73
|
|
|
return $this->field->callFormatter($withData); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$data = [ |
77
|
|
|
'field' => $this->field, |
78
|
|
|
'model' => $this->field->getModel(), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
if (method_exists($this, $dataGetter = 'on'.title_case($page))) { |
82
|
|
|
$data += call_user_func([$this, $dataGetter]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return View::make('administrator::fields.translatable.'.$page, $data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function onEdit(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'languages' => \localizer\locales(), |
95
|
|
|
'locale' => \localizer\locale(), |
96
|
|
|
'container' => $this, |
97
|
|
|
'translations' => app('scaffold.translations'), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Language $language |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public function name(Locale $language) |
105
|
|
|
{ |
106
|
|
|
return "translatable[{$language->id()}][{$this->field->id()}]"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Locale $language |
111
|
|
|
*/ |
112
|
|
|
public function value(Locale $language) |
113
|
|
|
{ |
114
|
|
|
$model = $this->field->getModel(); |
115
|
|
|
$entity = $model->translate($language->id()); |
116
|
|
|
|
117
|
|
|
return $entity ? $entity->getAttribute($this->field->id()) : null; |
118
|
|
|
} |
119
|
|
|
} |