1 | <?php |
||
2 | |||
3 | namespace Arbory\Base\Admin\Form\Fields; |
||
4 | |||
5 | use Arbory\Base\Admin\Form\Fields\Renderer\TranslatableFieldRenderer; |
||
6 | use Arbory\Base\Admin\Form\FieldSet; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Http\Request; |
||
9 | use Illuminate\Support\Facades\App; |
||
10 | use Waavi\Translation\Models\Language; |
||
11 | use Waavi\Translation\Repositories\LanguageRepository; |
||
12 | |||
13 | /** |
||
14 | * Class Translatable. |
||
15 | */ |
||
16 | class Translatable extends AbstractField implements ProxyFieldInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var FieldInterface |
||
20 | */ |
||
21 | protected $field; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $locales = []; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $currentLocale; |
||
32 | |||
33 | protected $style = 'raw'; |
||
34 | |||
35 | protected $rendererClass = TranslatableFieldRenderer::class; |
||
36 | |||
37 | /** |
||
38 | * Translatable constructor. |
||
39 | * |
||
40 | * @param FieldInterface $field |
||
41 | */ |
||
42 | public function __construct(FieldInterface $field) |
||
43 | { |
||
44 | /** @var LanguageRepository $languages */ |
||
45 | $languages = App::make(LanguageRepository::class); |
||
46 | |||
47 | $this->field = $field; |
||
48 | $this->currentLocale = App::getLocale(); |
||
49 | |||
50 | $this->locales = $languages->all()->map(function (Language $language) { |
||
51 | return $language->locale; |
||
52 | })->toArray(); |
||
53 | |||
54 | parent::__construct('translations'); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return Model |
||
59 | */ |
||
60 | public function getModel() |
||
61 | { |
||
62 | return parent::getModel(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getLocales() |
||
69 | { |
||
70 | return $this->locales; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param array $locales |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setLocales(array $locales = []) |
||
78 | { |
||
79 | $this->locales = $locales; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getCurrentLocale() |
||
88 | { |
||
89 | return $this->currentLocale; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param $locale |
||
94 | * @return FieldSet |
||
95 | */ |
||
96 | public function getTranslatableResource($locale) |
||
97 | { |
||
98 | return $this->getLocaleFieldSet( |
||
99 | $this->getModel()->translateOrNew($locale), |
||
100 | $locale |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param $model |
||
106 | * @param $locale |
||
107 | * @return FieldSet |
||
108 | */ |
||
109 | public function getLocaleFieldSet($model, $locale) |
||
110 | { |
||
111 | $fieldSet = new FieldSet( |
||
112 | $model, |
||
113 | $this->getNameSpacedName().'.'.$locale |
||
114 | ); |
||
115 | |||
116 | $field = clone $this->field; |
||
117 | $field->setFieldSet($fieldSet); |
||
118 | $field->rules(implode('|', $this->rules)); |
||
119 | |||
120 | $defaultResource = $this->getDefaultResourceForLocale($locale); |
||
121 | |||
122 | if ($defaultResource && ! $field->getValue()) { |
||
123 | $field->setValue($defaultResource->{$field->getName()}); |
||
124 | } |
||
125 | |||
126 | $fieldSet->getFields()->push($field); |
||
127 | |||
128 | return $fieldSet; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @see \Arbory\Base\Http\Controllers\Admin\SettingsController::getField |
||
133 | * |
||
134 | * @param $locale |
||
135 | * @return Model|null |
||
136 | */ |
||
137 | public function getDefaultResourceForLocale($locale) |
||
138 | { |
||
139 | $resource = null; |
||
140 | |||
141 | if ($this->getValue() && ! $this->getValue()->isEmpty()) { |
||
142 | foreach ($this->getValue() as $index => $item) { |
||
143 | if ($item->{$this->getModel()->getLocaleKey()} === $locale) { |
||
144 | $resource = $item; |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return $resource; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param Request $request |
||
154 | */ |
||
155 | public function beforeModelSave(Request $request) |
||
156 | { |
||
157 | foreach ($this->locales as $locale) { |
||
158 | foreach ($this->getTranslatableResource($locale)->getFields() as $field) { |
||
159 | $field->beforeModelSave($request); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param Request $request |
||
166 | */ |
||
167 | public function afterModelSave(Request $request) |
||
168 | { |
||
169 | foreach ($this->locales as $locale) { |
||
170 | foreach ($this->getTranslatableResource($locale)->getFields() as $field) { |
||
171 | $field->afterModelSave($request); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getRules(): array |
||
180 | { |
||
181 | $rules = []; |
||
182 | |||
183 | $translationsClass = $this->getModel()->getTranslationModelName(); |
||
184 | |||
185 | foreach ($this->getLocaleFieldSet(new $translationsClass, '*')->getFields() as $field) { |
||
186 | $rules = array_merge($rules, $field->getRules()); |
||
187 | } |
||
188 | |||
189 | return $rules; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getFieldTypeName() |
||
196 | { |
||
197 | return $this->field->getFieldTypeName(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return FieldInterface |
||
202 | */ |
||
203 | public function getField(): FieldInterface |
||
204 | { |
||
205 | return $this->field; |
||
206 | } |
||
207 | } |
||
208 |