1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\DataView\Column; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Yii\DataView\Column\Base\Cell; |
11
|
|
|
use Yiisoft\Yii\DataView\Column\Base\DataContext; |
12
|
|
|
use Yiisoft\Yii\DataView\Column\Base\GlobalContext; |
13
|
|
|
|
14
|
|
|
final class CheckboxColumnRenderer implements ColumnRendererInterface |
15
|
|
|
{ |
16
|
|
|
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell |
17
|
|
|
{ |
18
|
|
|
$this->checkColumn($column); |
19
|
|
|
return $cell->addAttributes($column->getColumnAttributes()); |
20
|
|
|
} |
21
|
|
|
|
22
|
8 |
|
public function renderHeader(ColumnInterface $column, Cell $cell, GlobalContext $context): ?Cell |
23
|
|
|
{ |
24
|
8 |
|
$this->checkColumn($column); |
25
|
|
|
|
26
|
8 |
|
$header = $column->getHeader(); |
27
|
8 |
|
if ($header === null) { |
28
|
5 |
|
if (!$column->isMultiple()) { |
29
|
1 |
|
return null; |
30
|
|
|
} |
31
|
4 |
|
$header = Html::checkbox('checkbox-selection-all', 1); |
32
|
|
|
} |
33
|
|
|
|
34
|
7 |
|
return $cell |
35
|
7 |
|
->addAttributes($column->getHeaderAttributes()) |
36
|
7 |
|
->content($header); |
37
|
|
|
} |
38
|
|
|
|
39
|
8 |
|
public function renderFilter(ColumnInterface $column, Cell $cell, GlobalContext $context): ?Cell |
40
|
|
|
{ |
41
|
8 |
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
8 |
|
public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell |
45
|
|
|
{ |
46
|
8 |
|
$this->checkColumn($column); |
47
|
|
|
|
48
|
8 |
|
$inputAttributes = $column->getInputAttributes(); |
49
|
8 |
|
$name = null; |
50
|
8 |
|
$value = null; |
51
|
|
|
|
52
|
8 |
|
if (!array_key_exists('name', $inputAttributes)) { |
53
|
7 |
|
$name = 'checkbox-selection'; |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
if (!array_key_exists('value', $inputAttributes)) { |
57
|
8 |
|
$key = $context->getKey(); |
58
|
8 |
|
$value = is_array($key) |
59
|
|
|
? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) |
60
|
8 |
|
: (string)$key; |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
$input = Html::checkbox($name, $value, $inputAttributes); |
64
|
|
|
|
65
|
8 |
|
$contentClosure = $column->getContent(); |
66
|
|
|
/** @var string|Stringable $content */ |
67
|
8 |
|
$content = $contentClosure === null ? $input : $contentClosure($input, $context); |
68
|
|
|
|
69
|
8 |
|
return $cell |
70
|
8 |
|
->addAttributes($column->getBodyAttributes()) |
71
|
8 |
|
->content($content) |
72
|
8 |
|
->encode(false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell |
76
|
|
|
{ |
77
|
|
|
$this->checkColumn($column); |
78
|
|
|
|
79
|
|
|
if ($column->getFooter() !== null) { |
80
|
|
|
$cell = $cell->content($column->getFooter()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $cell; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @psalm-assert CheckboxColumn $column |
88
|
|
|
*/ |
89
|
8 |
|
private function checkColumn(ColumnInterface $column): void |
90
|
|
|
{ |
91
|
8 |
|
if (!$column instanceof CheckboxColumn) { |
92
|
|
|
throw new InvalidArgumentException( |
93
|
|
|
sprintf( |
94
|
|
|
'Expected "%s", but "%s" given.', |
95
|
|
|
CheckboxColumn::class, |
96
|
|
|
$column::class |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|