|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Model\Gui\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Gui\Components\uiCheckbox; |
|
6
|
|
|
use eXpansion\Framework\Gui\Components\uiInput; |
|
7
|
|
|
use eXpansion\Framework\Gui\Components\uiTooltip; |
|
8
|
|
|
use FML\Controls\Control; |
|
9
|
|
|
use FML\Controls\Frame; |
|
10
|
|
|
use FML\Controls\Label; |
|
11
|
|
|
use oliverde8\AssociativeArraySimplified\AssociativeArray; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class LineBuilder |
|
15
|
|
|
* |
|
16
|
|
|
* @package eXpansion\Framework\Core\Model\Gui\Builders; |
|
17
|
|
|
* @author oliver de Cramer <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class LineFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var BackGroundFactory */ |
|
22
|
|
|
protected $backGroundFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** @var LabelFactory */ |
|
25
|
|
|
protected $labelFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $type; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* TitleLineFactory constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param BackGroundFactory $backGroundFactory |
|
34
|
|
|
* @param LabelFactory $labelFactory |
|
35
|
|
|
* @param string $type |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function __construct( |
|
38
|
|
|
BackGroundFactory $backGroundFactory, |
|
39
|
|
|
LabelFactory $labelFactory, |
|
40
|
|
|
$type = LabelFactory::TYPE_NORMAL |
|
41
|
|
|
) { |
|
42
|
9 |
|
$this->backGroundFactory = $backGroundFactory; |
|
43
|
9 |
|
$this->labelFactory = $labelFactory; |
|
44
|
9 |
|
$this->type = $type; |
|
45
|
9 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create a multi column line. |
|
49
|
|
|
* |
|
50
|
|
|
* @param float $totalWidth |
|
51
|
|
|
* @param array $columns |
|
52
|
|
|
* @param int $index |
|
53
|
|
|
* @param float $height |
|
54
|
|
|
* @param bool $autoNewLine |
|
55
|
|
|
* @param int $maxLines |
|
56
|
|
|
* |
|
57
|
|
|
* @return Frame |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public function create($totalWidth, $columns, $index = 0, $height = 5.0, $autoNewLine = false, $maxLines = 1) |
|
62
|
|
|
{ |
|
63
|
|
|
$totalCoef |
|
64
|
6 |
|
= ($totalWidth - 1) / array_reduce($columns, function ($carry, $item) { |
|
65
|
6 |
|
return $carry + $item['width']; |
|
66
|
6 |
|
}); |
|
67
|
|
|
|
|
68
|
6 |
|
$frame = new Frame(); |
|
69
|
|
|
//$frame->setHeight($height); |
|
|
|
|
|
|
70
|
|
|
//$frame->setWidth($totalWidth); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
6 |
|
$postX = 1; |
|
73
|
6 |
|
foreach ($columns as $columnData) { |
|
74
|
6 |
|
if (isset($columnData['text'])) { |
|
75
|
6 |
|
$element = $this->createTextColumn($totalCoef, $columnData, $postX, $height, $autoNewLine, $maxLines); |
|
76
|
6 |
|
} elseif (isset($columnData['input'])) { |
|
77
|
|
|
$element = $this->createInputColumn($totalCoef, $columnData, $postX); |
|
78
|
6 |
|
} elseif (isset($columnData['renderer'])) { |
|
79
|
6 |
|
$element = $this->createRendererColumn($columnData, $postX); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
if (!isset($element)) { |
|
83
|
|
|
throw new \Exception('Element not found.'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
if (isset($columnData['action'])) { |
|
87
|
6 |
|
$element->setAction($columnData['action']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
6 |
|
$frame->addChild($element); |
|
91
|
6 |
|
$postX += $columnData["width"] * $totalCoef; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
$frame->addChild($this->backGroundFactory->create($totalWidth, $height, $index)); |
|
95
|
|
|
|
|
96
|
6 |
|
return $frame; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param float $totalCoef |
|
101
|
|
|
* @param array $columnData |
|
102
|
|
|
* @param float $postX |
|
103
|
|
|
* @param float $height |
|
104
|
|
|
* @param bool $autoNewLine |
|
105
|
|
|
* @param int $maxLines |
|
106
|
|
|
* |
|
107
|
|
|
* @return Label |
|
108
|
|
|
*/ |
|
109
|
6 |
|
protected function createTextColumn($totalCoef, $columnData, $postX, $height, $autoNewLine = false, $maxLines = 1) |
|
110
|
|
|
{ |
|
111
|
6 |
|
$label = $this->labelFactory->create( |
|
112
|
6 |
|
$columnData['text'], |
|
113
|
6 |
|
AssociativeArray::getFromKey($columnData, 'translatable', false), |
|
114
|
6 |
|
$this->type |
|
115
|
|
|
); |
|
116
|
6 |
|
$label->setHeight($height - 1); |
|
117
|
6 |
|
$label->setWidth(($columnData["width"] * $totalCoef) - 0.5); |
|
118
|
6 |
|
$label->setPosition($postX, -0.5); |
|
119
|
6 |
|
$label->setAutoNewLine($autoNewLine); |
|
120
|
6 |
|
$label->setMaxLines($maxLines); |
|
121
|
|
|
|
|
122
|
6 |
|
return $label; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
protected function createInputColumn($totalCoef, $columnData, $postX) |
|
127
|
|
|
{ |
|
128
|
|
|
/** @var uiTooltip $tooltip */ |
|
129
|
|
|
$tooltip = $columnData['tooltip']; |
|
130
|
|
|
$value = $columnData['input']; |
|
131
|
|
|
$i = $columnData['index']; |
|
132
|
|
|
$type = gettype($value); |
|
133
|
|
|
|
|
134
|
|
|
if ($type == "boolean") { |
|
135
|
|
|
$element = new uiCheckbox("", "entry_".$i."_boolean", true); |
|
136
|
|
|
if ($value === false) { |
|
137
|
|
|
$element = new uiCheckbox("", "entry_".$i."_boolean", false); |
|
138
|
|
|
} |
|
139
|
|
|
$element->setY(3); |
|
140
|
|
|
} else { |
|
141
|
|
|
$element = new uiInput("entry_".$i."_".$type); |
|
142
|
|
|
$element->setDefault($value); |
|
143
|
|
|
} |
|
144
|
|
|
$element->setPosition($postX, -0.5); |
|
145
|
|
|
$element->setWidth(($columnData["width"] * $totalCoef) - 0.5); |
|
146
|
|
|
$element->setHeight(4); |
|
147
|
|
|
|
|
148
|
|
|
$tooltip->addTooltip($element, $type); |
|
149
|
|
|
|
|
150
|
|
|
return $element; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $columnData |
|
156
|
|
|
* @param $postX |
|
157
|
|
|
* |
|
158
|
|
|
* @return Control |
|
159
|
|
|
*/ |
|
160
|
6 |
|
protected function createRendererColumn($columnData, $postX) |
|
161
|
|
|
{ |
|
162
|
|
|
/** @var Control $element */ |
|
163
|
6 |
|
$element = $columnData['renderer']; |
|
164
|
6 |
|
$element->setPosition($postX, -0.5); |
|
165
|
6 |
|
$element->setHeight(4); |
|
166
|
|
|
|
|
167
|
6 |
|
return $element; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.