|
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 KeyValue extends Field |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $value = ['' => '']; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Fill data to the field. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $data |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
public function fill($data) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$this->data = $data; |
|
26
|
|
|
|
|
27
|
|
|
$this->value = Arr::get($data, $this->column, $this->value); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->formatValue(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getValidator(array $input) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->validator) { |
|
38
|
|
|
return $this->validator->call($this, $input); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (!is_string($this->column)) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$rules = $attributes = []; |
|
46
|
|
|
|
|
47
|
|
|
if (!$fieldRules = $this->getRules()) { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!Arr::has($input, $this->column)) { |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$rules["{$this->column}.keys.*"] = 'distinct'; |
|
56
|
|
|
$rules["{$this->column}.values.*"] = $fieldRules; |
|
57
|
|
|
$attributes["{$this->column}.keys.*"] = __('Key'); |
|
58
|
|
|
$attributes["{$this->column}.values.*"] = __('Value'); |
|
59
|
|
|
|
|
60
|
|
|
return validator($input, $rules, $this->getValidationMessages(), $attributes); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function setupScript() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->script = <<<SCRIPT |
|
66
|
|
|
|
|
67
|
|
|
$('.{$this->column}-add').on('click', function () { |
|
68
|
|
|
var tpl = $('template.{$this->column}-tpl').html(); |
|
69
|
|
|
$('tbody.kv-{$this->column}-table').append(tpl); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
$('tbody').on('click', '.{$this->column}-remove', function () { |
|
73
|
|
|
$(this).closest('tr').remove(); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
SCRIPT; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function prepare($value) |
|
80
|
|
|
{ |
|
81
|
|
|
return array_combine($value['keys'], $value['values']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function render() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->setupScript(); |
|
87
|
|
|
|
|
88
|
|
|
Admin::style('td .form-group {margin-bottom: 0 !important;}'); |
|
89
|
|
|
|
|
90
|
|
|
return parent::render(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.