1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services\Html; |
4
|
|
|
|
5
|
|
|
use Html; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use App\Models\Tag; |
8
|
|
|
use App\Models\ContentBlock; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Collective\Html\FormBuilder as BaseFormBuilder; |
11
|
|
|
use Spatie\Blender\Model\Transformers\MediaTransformer; |
12
|
|
|
use Spatie\Blender\Model\Transformers\ContentBlockTransformer; |
13
|
|
|
|
14
|
|
|
class FormBuilder extends BaseFormBuilder |
15
|
|
|
{ |
16
|
|
|
public function openDraftable(array $options, Model $subject): string |
17
|
|
|
{ |
18
|
|
|
$identifier = class_basename($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
|
|
|
$initial = $this->useInitialValue($subject, $fieldName, $locale); |
46
|
|
|
$fieldName = $locale ? translate_field_name($fieldName, $locale) : $fieldName; |
47
|
|
|
|
48
|
|
|
return $this->textarea( |
49
|
|
|
$fieldName, |
50
|
|
|
$initial, |
51
|
|
|
array_merge($options, [ |
52
|
|
|
'data-editor', |
53
|
|
|
'data-editor-medialibrary-url' => action( |
54
|
|
|
'Back\Api\MediaLibraryController@add', |
55
|
|
|
[short_class_name($subject), $subject->id, 'redactor'] |
56
|
|
|
), |
57
|
|
|
]) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function checkboxWithLabel($subject, string $fieldName, string $label, array $options = []): string |
62
|
|
|
{ |
63
|
|
|
$options = array_merge(['class' => 'form-control'], $options); |
64
|
|
|
|
65
|
|
|
return el( |
66
|
|
|
'label.-checkbox', |
67
|
|
|
$this->checkbox($fieldName, 1, $this->useInitialValue($subject, $fieldName), $options) |
|
|
|
|
68
|
|
|
.' '.$label |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function datePicker(string $name, string $value): string |
73
|
|
|
{ |
74
|
|
|
return $this->text($name, $value, [ |
75
|
|
|
'data-datetimepicker', |
76
|
|
|
'class' => '-datetime', |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function tags($subject, string $type, array $options = []): string |
81
|
|
|
{ |
82
|
|
|
$tags = Tag::getWithType($type)->pluck('name', 'name')->toArray(); |
83
|
|
|
$subjectTags = $subject->tagsWithType($type)->pluck('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
|
|
|
$categories = Tag::getWithType($type)->pluck('name', 'name')->toArray(); |
93
|
|
|
$subjectCategory = $subject->tagsWithType($type)->first()->name ?? null; |
94
|
|
|
|
95
|
|
|
return $this->select("{$type}_tags[]", $categories, $subjectCategory, $options); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function locales(array $locales, string $current): string |
99
|
|
|
{ |
100
|
|
|
$list = array_reduce($locales, function (array $list, string $locale) { |
101
|
|
|
$list[$locale] = trans("locales.{$locale}"); |
102
|
|
|
|
103
|
|
|
return $list; |
104
|
|
|
}, []); |
105
|
|
|
|
106
|
|
|
return $this->select('locale', $list, $current, ['data-select' => 'select']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function media($subject, string $collection, string $type, $associated = []): string |
110
|
|
|
{ |
111
|
|
|
$initialMedia = fractal() |
112
|
|
|
->collection($subject->getMedia($collection)) |
113
|
|
|
->transformWith(new MediaTransformer()) |
114
|
|
|
->toJson(); |
115
|
|
|
|
116
|
|
|
$model = collect([ |
117
|
|
|
'name' => get_class($subject), |
118
|
|
|
'id' => $subject->id, |
119
|
|
|
])->toJson(); |
120
|
|
|
|
121
|
|
|
return el('blender-media', [ |
122
|
|
|
'collection' => $collection, |
123
|
|
|
'type' => $type, |
124
|
|
|
'upload-url' => action('Back\Api\MediaLibraryController@add'), |
125
|
|
|
':model' => htmlspecialchars($model), |
126
|
|
|
':initial' => htmlspecialchars($initialMedia), |
127
|
|
|
':data' => htmlspecialchars($this->getAssociatedData($associated)), |
128
|
|
|
':debug' => htmlspecialchars(json_encode(config('app.debug', false))), |
129
|
|
|
], ''); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function contentBlocks(Model $subject, string $collectionName, string $editor, array $associated = []): string |
133
|
|
|
{ |
134
|
|
|
$initialContentBlocks = fractal() |
135
|
|
|
->collection($subject->getContentBlocks($collectionName)) |
136
|
|
|
->transformWith(new ContentBlockTransformer()) |
137
|
|
|
->toJson(); |
138
|
|
|
|
139
|
|
|
$model = collect([ |
140
|
|
|
'name' => get_class($subject), |
141
|
|
|
'id' => $subject->id, |
142
|
|
|
])->toJson(); |
143
|
|
|
|
144
|
|
|
$associatedData = $this->getAssociatedData(array_merge($associated, [ |
145
|
|
|
'locales' => config('app.locales'), |
146
|
|
|
'contentLocale' => content_locale(), |
147
|
|
|
'mediaModel' => ContentBlock::class, |
148
|
|
|
'mediaUploadUrl' => action('Back\Api\MediaLibraryController@add'), |
149
|
|
|
])); |
150
|
|
|
|
151
|
|
|
return el('blender-content-blocks', [ |
152
|
|
|
'collection' => $collectionName, |
153
|
|
|
'editor' => $editor, |
154
|
|
|
'create-url' => action('Back\Api\ContentBlockController@add'), |
155
|
|
|
':model' => htmlspecialchars($model), |
156
|
|
|
':input' => htmlspecialchars($initialContentBlocks), |
157
|
|
|
':data' => htmlspecialchars($associatedData), |
158
|
|
|
':debug' => htmlspecialchars(json_encode(config('app.debug', false))), |
159
|
|
|
], ''); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function getAssociatedData($associated = []): string |
163
|
|
|
{ |
164
|
|
|
$associated = collect($associated); |
165
|
|
|
|
166
|
|
|
$associated->put('locales', config('app.locales')); |
167
|
|
|
$associated->put('contentLocale', content_locale()); |
168
|
|
|
|
169
|
|
|
return $associated->toJson(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function useInitialValue($subject, string $propertyName, string $locale = ''): string |
173
|
|
|
{ |
174
|
|
|
$fieldName = $locale ? translate_field_name($propertyName, $locale) : $propertyName; |
175
|
|
|
$value = $locale ? $subject->translate($propertyName, $locale) : $subject->$propertyName; |
176
|
|
|
|
177
|
|
|
if ($value instanceof Carbon) { |
178
|
|
|
$value = $value->format('d/m/Y'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->getValueAttribute($fieldName, $value) ?? ''; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getLabelForTranslatedField(string $fieldName, string $label, string $locale): string |
185
|
|
|
{ |
186
|
|
|
return Html::decode( |
187
|
|
|
$this->label($fieldName, $label.el('span.label_lang', $locale)) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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: