|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\Html; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Enums\TagType; |
|
6
|
|
|
use App\Models\Tag; |
|
7
|
|
|
use App\Models\Transformers\MediaTransformer; |
|
8
|
|
|
use App\Repositories\TagRepository; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use Illuminate\Html\FormBuilder as BaseFormBuilder; |
|
12
|
|
|
use HTML; |
|
13
|
|
|
|
|
14
|
|
|
class FormBuilder extends BaseFormBuilder |
|
15
|
|
|
{ |
|
16
|
|
|
public function openDraftable(array $options, Model $subject) : string |
|
17
|
|
|
{ |
|
18
|
|
|
$identifier = short_class_name($subject) . '_' . ($subject->isDraft() ? 'new' : $subject->id); |
|
19
|
|
|
|
|
20
|
|
|
$options = array_merge($options, [ |
|
21
|
|
|
'data-autosave' => '', |
|
22
|
|
|
'name' => $identifier, |
|
23
|
|
|
'id' => $identifier, |
|
24
|
|
|
]); |
|
25
|
|
|
|
|
26
|
|
|
return $this->open($options); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function openButton(array $formOptions = [], array $buttonOptions = []) : string |
|
30
|
|
|
{ |
|
31
|
|
|
if (strtolower($formOptions['method'] ?? '') === 'delete') { |
|
32
|
|
|
$formOptions['data-confirm'] = 'true'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $this->open($formOptions) . substr(el('button', $buttonOptions, ''), 0, -strlen('</button>')); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function closeButton() : string |
|
39
|
|
|
{ |
|
40
|
|
|
return '</button>' . $this->close(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function redactor($subject, string $fieldName, string $locale = '', array $options = []) : string |
|
44
|
|
|
{ |
|
45
|
|
|
$fieldName = $locale ? translate_field_name($fieldName, $locale) : $fieldName; |
|
46
|
|
|
|
|
47
|
|
|
return $this->textarea( |
|
48
|
|
|
$fieldName, |
|
49
|
|
|
$this->useInitialValue($subject, $fieldName, $locale), |
|
50
|
|
|
array_merge($options, [ |
|
51
|
|
|
'data-editor', |
|
52
|
|
|
'data-editor-medialibrary-url' => action( |
|
53
|
|
|
'Back\MediaLibraryApiController@add', |
|
54
|
|
|
[short_class_name($subject), $subject->id, 'redactor'] |
|
55
|
|
|
), |
|
56
|
|
|
]) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function checkboxWithLabel($subject, string $fieldName, string $label, array $options = []) : string |
|
61
|
|
|
{ |
|
62
|
|
|
$options = array_merge(['class' => 'form-control'], $options); |
|
63
|
|
|
|
|
64
|
|
|
return el('label.-checkbox', |
|
65
|
|
|
$this->checkbox($fieldName, 1, $this->useInitialValue($subject, $fieldName), $options) |
|
|
|
|
|
|
66
|
|
|
. ' ' . $label |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function datePicker(string $name, string $value) : string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->text($name, $value, [ |
|
73
|
|
|
'data-datetimepicker', |
|
74
|
|
|
'class' => '-datetime', |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function tags($subject, string $type, array $options = []) : string |
|
79
|
|
|
{ |
|
80
|
|
|
$type = new TagType($type); |
|
81
|
|
|
|
|
82
|
|
|
$tags = Tag::getWithType($type)->lists('name', 'name')->toArray(); |
|
83
|
|
|
$subjectTags = $subject->tagsWithType($type)->lists('name', 'name')->toArray(); |
|
84
|
|
|
|
|
85
|
|
|
$options = array_merge(['multiple', 'data-select' => 'tags'], $options); |
|
86
|
|
|
|
|
87
|
|
|
return $this->select("{$type}_tags[]", $tags, $subjectTags, $options); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function category($subject, $type, array $options = []) : string |
|
91
|
|
|
{ |
|
92
|
|
|
$type = new TagType($type); |
|
93
|
|
|
|
|
94
|
|
|
$categories = Tag::getWithType($type)->lists('name', 'name')->toArray(); |
|
95
|
|
|
$subjectCategory = $subject->tagsWithType($type)->first()->name ?? null; |
|
96
|
|
|
|
|
97
|
|
|
return $this->select("{$type}_tags[]", $categories, $subjectCategory, $options); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function locales(array $locales, string $current) : string |
|
101
|
|
|
{ |
|
102
|
|
|
$list = array_reduce($locales, function (array $list, string $locale) { |
|
103
|
|
|
$list[$locale] = trans("locales.{$locale}"); |
|
104
|
|
|
return $list; |
|
105
|
|
|
}, []); |
|
106
|
|
|
|
|
107
|
|
|
return $this->select('locale', $list, $current, ['data-select' => 'select']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function media($subject, string $collection, string $type, array $associated = []) : string |
|
111
|
|
|
{ |
|
112
|
|
|
$initialMedia = fractal() |
|
113
|
|
|
->collection($subject->getMedia($collection)) |
|
114
|
|
|
->transformWith(new MediaTransformer()) |
|
|
|
|
|
|
115
|
|
|
->toJson(); |
|
116
|
|
|
|
|
117
|
|
|
$model = collect([ |
|
118
|
|
|
'name' => get_class($subject), |
|
119
|
|
|
'id' => $subject->id |
|
120
|
|
|
])->toJson(); |
|
121
|
|
|
|
|
122
|
|
|
$associated = $this->getAssociatedMediaData($associated); |
|
123
|
|
|
|
|
124
|
|
|
return el('div', array_merge($associated, [ |
|
125
|
|
|
'data-media-collection' => $collection, |
|
126
|
|
|
'data-media-type' => $type, |
|
127
|
|
|
'data-initial' => htmlspecialchars($initialMedia), |
|
128
|
|
|
'data-model' => htmlspecialchars($model), |
|
129
|
|
|
]), ''); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function getAssociatedMediaData(array $associated = []) : array |
|
133
|
|
|
{ |
|
134
|
|
|
$associated['locales'] = $associated['locales'] ?? config('app.locales'); |
|
135
|
|
|
|
|
136
|
|
|
$normalized = []; |
|
137
|
|
|
|
|
138
|
|
|
foreach ($associated as $key => $value) { |
|
139
|
|
|
$normalized["data-{$key}"] = htmlspecialchars(json_encode($value)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $normalized; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function useInitialValue($subject, string $propertyName, string $locale = '') : string |
|
146
|
|
|
{ |
|
147
|
|
|
$fieldName = $locale ? translate_field_name($propertyName, $locale) : $propertyName; |
|
148
|
|
|
$value = $locale ? $subject->translate($locale)->$propertyName : $subject->$propertyName; |
|
149
|
|
|
|
|
150
|
|
|
if ($value instanceof Carbon) { |
|
151
|
|
|
$value = $value->format('d/m/Y'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $this->getValueAttribute($fieldName, $value) ?? ''; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getLabelForTranslatedField(string $fieldName, string $label, string $locale) : string |
|
158
|
|
|
{ |
|
159
|
|
|
return HTML::decode( |
|
160
|
|
|
$this->label($fieldName, $label . el('span.label_lang', $locale)) |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: