|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Encore\Admin\Form\Field; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
|
|
9
|
|
|
class ValuePicker |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $modal; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Text|File |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $field; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $column; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $selecteable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $separator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $multiple = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* ValuePicker constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $selecteable |
|
45
|
|
|
* @param string $column |
|
46
|
|
|
* @param bool $multiple |
|
47
|
|
|
* @param string $separator |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($selecteable, $column = '', $multiple = false, $separator = ';') |
|
50
|
|
|
{ |
|
51
|
|
|
$this->selecteable = $selecteable; |
|
52
|
|
|
$this->column = $column; |
|
53
|
|
|
$this->multiple = $multiple; |
|
54
|
|
|
$this->separator = $separator; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param int $multiple |
|
|
|
|
|
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getLoadUrl() |
|
63
|
|
|
{ |
|
64
|
|
|
$selectable = str_replace('\\', '_', $this->selecteable); |
|
65
|
|
|
|
|
66
|
|
|
$args = [$this->multiple, $this->column]; |
|
67
|
|
|
|
|
68
|
|
|
return route('admin.handle-selectable', compact('selectable', 'args')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Field $field |
|
73
|
|
|
* @param \Closure|null $callback |
|
74
|
|
|
*/ |
|
75
|
|
|
public function mount(Field $field, \Closure $callback = null) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->field = $field; |
|
|
|
|
|
|
78
|
|
|
$this->modal = sprintf('picker-modal-%s', $field->getElementClassString()); |
|
79
|
|
|
|
|
80
|
|
|
$this->addPickBtn($callback); |
|
81
|
|
|
|
|
82
|
|
|
Admin::component('admin::components.filepicker', [ |
|
83
|
|
|
'url' => $this->getLoadUrl(), |
|
84
|
|
|
'modal' => $this->modal, |
|
85
|
|
|
'selector' => $this->field->getElementClassSelector(), |
|
86
|
|
|
'separator' => $this->separator, |
|
87
|
|
|
'multiple' => $this->multiple, |
|
88
|
|
|
'is_file' => $this->field instanceof File, |
|
89
|
|
|
'is_image' => $this->field instanceof Image, |
|
90
|
|
|
'url_tpl' => $this->field instanceof File ? $this->field->objectUrl('__URL__') : '', |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \Closure|null $callback |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function addPickBtn(\Closure $callback = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$text = admin_trans('admin.browse'); |
|
100
|
|
|
|
|
101
|
|
|
$btn = <<<HTML |
|
102
|
|
|
<a class="btn btn-primary" data-toggle="modal" data-target="#{$this->modal}"> |
|
103
|
|
|
<i class="fa fa-folder-open"></i> {$text} |
|
104
|
|
|
</a> |
|
105
|
|
|
HTML; |
|
106
|
|
|
|
|
107
|
|
|
if ($callback) { |
|
108
|
|
|
$callback($btn); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->field->addVariables(compact('btn')); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $field |
|
116
|
|
|
* @return array|\Illuminate\Support\Collection |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getPreview(string $field) |
|
119
|
|
|
{ |
|
120
|
|
|
if (empty($value = $this->field->value())) { |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this->multiple) { |
|
125
|
|
|
$value = explode($this->separator, $value); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return collect(Arr::wrap($value))->map(function ($item) use ($field) { |
|
129
|
|
|
return [ |
|
130
|
|
|
'url' => $this->field->objectUrl($item), |
|
|
|
|
|
|
131
|
|
|
'value' => $item, |
|
132
|
|
|
'is_file' => $field == File::class, |
|
133
|
|
|
]; |
|
134
|
|
|
}); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.