1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FacturaScripts |
4
|
|
|
* Copyright (C) 2023 Carlos Garcia Gomez <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
9
|
|
|
* License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Lesser General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace FacturaScripts\Core\UI\Widget; |
21
|
|
|
|
22
|
|
|
use FacturaScripts\Core\Template\UI\Widget; |
23
|
|
|
use FacturaScripts\Dinamic\Lib\AssetManager; |
24
|
|
|
|
25
|
|
|
class WidgetSelect extends Widget |
26
|
|
|
{ |
27
|
|
|
/** @var array */ |
28
|
|
|
public $options = []; |
29
|
|
|
|
30
|
|
|
public function __construct(string $name, ?string $field = null, ?string $label = null) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($name, $field, $label); |
33
|
|
|
|
34
|
|
|
AssetManager::add('css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css'); |
35
|
|
|
AssetManager::add('css', 'https://cdn.jsdelivr.net/npm/@ttskch/[email protected]/dist/select2-bootstrap4.min.css'); |
36
|
|
|
AssetManager::add('js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js'); |
37
|
|
|
AssetManager::add('js', 'Dinamic/Assets/js/UIWidgetSelect.js'); |
38
|
|
|
|
39
|
|
|
// añadimos datos de prueba |
40
|
|
|
foreach (range(1, rand(4, 10)) as $i) { |
41
|
|
|
if (rand(0, 3)) { |
42
|
|
|
$this->options[$i] = 'Opción ' . $i; |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// añadimos un grupo |
47
|
|
|
$this->options[$i] = [ |
48
|
|
|
'label' => 'Grupo ' . $i, |
49
|
|
|
'options' => [] |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
foreach (range(1, rand(2, 5)) as $j) { |
53
|
|
|
$this->options[$i]['options'][$j] = 'Opción ' . $i . '.' . $j; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function render(string $context = ''): string |
59
|
|
|
{ |
60
|
|
|
return '<div class="form-group">' |
61
|
|
|
. '<label for="' . $this->id() . '">' . $this->label(true) . '</label><br/>' |
62
|
|
|
. '<select class="form-control ui-widget-select" id="' . $this->id() . '" name="' . $this->field . '">' |
63
|
|
|
. $this->renderOptions() |
64
|
|
|
. '</select>' |
65
|
|
|
. '</div>'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function renderOptions(): string |
69
|
|
|
{ |
70
|
|
|
$html = ''; |
71
|
|
|
foreach ($this->options as $key => $value) { |
72
|
|
|
if (is_array($value)) { |
73
|
|
|
$html .= '<optgroup label="' . $value['label'] . '">'; |
74
|
|
|
foreach ($value['options'] as $key2 => $value2) { |
75
|
|
|
$html .= '<option value="' . $key2 . '">' . $value2 . '</option>'; |
76
|
|
|
} |
77
|
|
|
$html .= '</optgroup>'; |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$html .= '<option value="' . $key . '">' . $value . '</option>'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $html; |
85
|
|
|
} |
86
|
|
|
} |