|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\Base\CommonData\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use atk4\data\Field_SQL; |
|
6
|
|
|
use atk4\core\InitializerTrait; |
|
7
|
|
|
use Epesi\Base\CommonData\Models\CommonData as CommonDataModel; |
|
8
|
|
|
|
|
9
|
|
|
class CommonData extends Field_SQL |
|
10
|
|
|
{ |
|
11
|
|
|
use InitializerTrait { |
|
12
|
|
|
init as _init; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public $type = 'enum'; |
|
16
|
|
|
|
|
17
|
|
|
public $path = ''; |
|
18
|
|
|
|
|
19
|
|
|
public $serialize = [ |
|
20
|
|
|
[__CLASS__, 'serialize'], |
|
21
|
|
|
[__CLASS__, 'unserialize'], |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public $multiple = false; |
|
25
|
|
|
|
|
26
|
|
|
public $values; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Initialization. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->_init(); |
|
34
|
|
|
|
|
35
|
|
|
if ($this->getDependency()) { |
|
36
|
|
|
$this->ui['form'] = [ |
|
37
|
|
|
'AutoComplete', |
|
38
|
|
|
'model' => CommonDataModel::create(), |
|
39
|
|
|
'dependency' => function($model, $data) { |
|
40
|
|
|
$model->addCondition('parent', CommonDataModel::getId($this->getDependencyPath($data))); |
|
41
|
|
|
}, |
|
42
|
|
|
'renderRowFunction' => function($field, $row) { |
|
43
|
|
|
return [ |
|
44
|
|
|
'value' => $row['key'], |
|
45
|
|
|
'title' => $row['value'] ?: $row['key'], |
|
46
|
|
|
]; |
|
47
|
|
|
}, |
|
48
|
|
|
'multiple' => $this->multiple |
|
49
|
|
|
]; |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->values = CommonDataModel::getArray($this->path); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->ui = array_merge([ |
|
55
|
|
|
'table' => ['Multiformat', [$this, 'format']], |
|
56
|
|
|
'form' => ['isMultiple' => $this->multiple] |
|
57
|
|
|
], $this->ui); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getDependency() |
|
61
|
|
|
{ |
|
62
|
|
|
$fields = (array) $this->path; |
|
63
|
|
|
|
|
64
|
|
|
$path = array_shift($fields); |
|
65
|
|
|
|
|
66
|
|
|
return $fields? compact('path', 'fields'): false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getDependencyPath($data) |
|
70
|
|
|
{ |
|
71
|
|
|
if (! $dependency = $this->getDependency()) return false; |
|
72
|
|
|
|
|
73
|
|
|
return implode('/', array_merge([$dependency['path']], array_intersect_key($data, array_flip($dependency['fields'])))); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getValues($data) |
|
77
|
|
|
{ |
|
78
|
|
|
$path = $this->getDependencyPath($data) ?: $this->path; |
|
79
|
|
|
|
|
80
|
|
|
return CommonDataModel::getArray($path); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function serialize($field, $value, $persistence) |
|
84
|
|
|
{ |
|
85
|
|
|
return $field->multiple? implode(',', (array) $value): $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function unserialize($field, $value, $persistence) |
|
89
|
|
|
{ |
|
90
|
|
|
return $field->multiple? explode(',', $value): $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function format($row, $field) |
|
94
|
|
|
{ |
|
95
|
|
|
$values = array_intersect_key($this->getValues($row->get()), array_flip((array) $row[$field])); |
|
96
|
|
|
|
|
97
|
|
|
return [[ |
|
98
|
|
|
'Template', |
|
99
|
|
|
implode('<br>', $values), |
|
100
|
|
|
]]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function normalize($value) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->multiple? parent::normalize($value): $value; |
|
106
|
|
|
} |
|
107
|
|
|
} |