|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\form_kw\Fields; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_connect\core\Interfaces\IFilterFactory; |
|
7
|
|
|
use kalanis\kw_connect\core\Interfaces\IIterableConnector; |
|
8
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
|
9
|
|
|
use kalanis\kw_forms\Form; |
|
10
|
|
|
use kalanis\kw_table\core\Interfaces\Table\IFilterMulti; |
|
11
|
|
|
use kalanis\kw_table\core\Interfaces\Table\IFilterRender; |
|
12
|
|
|
use kalanis\kw_table\core\TableException; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Multiple |
|
17
|
|
|
* @package kalanis\kw_table\form_kw\Fields |
|
18
|
|
|
* Simulates work with the form as it contains a multiple fields over column to process them as one |
|
19
|
|
|
* It behaves like "AND" over fields - entry must pass through every field |
|
20
|
|
|
* It is not probably possible to make "OR" variant due behavior underneath on storage connection (database, files) level |
|
21
|
|
|
* It's usable with limits GT, GTE, LT, LTE and string CONTAINS |
|
22
|
|
|
* |
|
23
|
|
|
* Example: |
|
24
|
|
|
$table->addSortedColumn('images.size', new Columns\Basic('size'), new Fields\Multiple([ |
|
25
|
|
|
new Fields\MultipleValue(new Fields\NumFrom(), 'From'), |
|
26
|
|
|
new Fields\MultipleValue(new Fields\NumToWith(), 'To') |
|
27
|
|
|
])); |
|
28
|
|
|
*/ |
|
29
|
|
|
class Multiple extends AField implements IFilterRender, IFilterMulti |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var MultipleValue[] */ |
|
32
|
|
|
protected array $fields = []; |
|
33
|
|
|
protected string $separator = '<br />'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param MultipleValue[] $fields |
|
37
|
|
|
* @param array<string, string> $attributes |
|
38
|
|
|
* @throws TableException |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(array $fields = [], array $attributes = []) |
|
41
|
|
|
{ |
|
42
|
3 |
|
parent::__construct($attributes); |
|
43
|
3 |
|
$this->setFields($fields); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function setForm(Form $form): void |
|
47
|
|
|
{ |
|
48
|
2 |
|
parent::setForm($form); |
|
49
|
2 |
|
foreach ($this->fields as &$field) { |
|
50
|
2 |
|
$field->setForm($form); |
|
51
|
|
|
} |
|
52
|
2 |
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function setAlias(string $alias): void |
|
55
|
|
|
{ |
|
56
|
1 |
|
parent::setAlias($alias); |
|
57
|
1 |
|
foreach ($this->fields as $i => &$field) { |
|
58
|
1 |
|
$field->setColumn($alias); |
|
59
|
1 |
|
$field->setAlias(sprintf('%s_%d', $alias, $i)); |
|
60
|
|
|
} |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function setDataSourceConnector(IIterableConnector $dataSource): void |
|
64
|
|
|
{ |
|
65
|
2 |
|
parent::setDataSourceConnector($dataSource); |
|
66
|
2 |
|
foreach ($this->fields as &$field) { |
|
67
|
2 |
|
$field->setDataSourceConnector($dataSource); |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param MultipleValue[] $fields |
|
73
|
|
|
* @throws TableException |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function setFields(array $fields): void |
|
76
|
|
|
{ |
|
77
|
3 |
|
foreach ($fields as $i => $field) { |
|
78
|
3 |
|
if (!$field instanceof MultipleValue) { |
|
79
|
1 |
|
throw new TableException(sprintf('Field at position *%s* is type of *%s*, not instance of MultipleValue', $i, gettype($field))); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
2 |
|
$this->fields = $fields; |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
public function getFilterAction(): string |
|
86
|
|
|
{ |
|
87
|
2 |
|
return IFilterFactory::ACTION_MULTIPLE; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function add(): void |
|
91
|
|
|
{ |
|
92
|
2 |
|
foreach ($this->fields as &$field) { |
|
93
|
2 |
|
$field->add(); |
|
94
|
|
|
} |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public function renderContent(): string |
|
98
|
|
|
{ |
|
99
|
2 |
|
return implode($this->separator, array_map([$this, 'renderField'], $this->fields)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param MultipleValue $field |
|
104
|
|
|
* @throws RenderException |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function renderField(MultipleValue $field): string |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $field->renderContent(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws TableException |
|
114
|
|
|
* @return array<int, array<int, float|int|string|true>> |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function getPairs(): array |
|
117
|
|
|
{ |
|
118
|
2 |
|
$values = []; |
|
119
|
2 |
|
foreach ($this->fields as $field) { |
|
120
|
2 |
|
$control = $this->getFormInstance()->getControl($field->getAlias()); |
|
121
|
2 |
|
if (!empty($control->getValue())) { |
|
122
|
2 |
|
$values[] = [$field->getField()->getFilterAction(), $control->getValue()]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
2 |
|
return $values; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|