1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FacturaScripts |
4
|
|
|
* Copyright (C) 2017-2021 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\Lib\Widget; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Description of RowButton |
24
|
|
|
* |
25
|
|
|
* @author Carlos García Gómez <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RowButton extends VisualItem |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $action; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $color; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
public $confirm; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $icon; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $label; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Indicates the security level of the button |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
public $level; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $target; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $type; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $data |
74
|
|
|
*/ |
75
|
|
|
public function __construct($data) |
76
|
|
|
{ |
77
|
|
|
parent::__construct($data); |
78
|
|
|
$this->action = $data['action'] ?? ''; |
79
|
|
|
$this->color = $data['color'] ?? ''; |
80
|
|
|
$this->confirm = isset($data['confirm']); |
81
|
|
|
$this->icon = $data['icon'] ?? ''; |
82
|
|
|
$this->label = isset($data['label']) ? static::$i18n->trans($data['label']) : ''; |
83
|
|
|
$this->level = isset($data['level']) ? (int)$data['level'] : 0; |
84
|
|
|
$this->target = $data['target'] ?? ''; |
85
|
|
|
$this->type = $data['type'] ?? 'action'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $small |
90
|
|
|
* @param string $viewName |
91
|
|
|
* @param string $jsFunction |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function render($small = false, $viewName = '', $jsFunction = '') |
96
|
|
|
{ |
97
|
|
|
if ($this->getLevel() < $this->level) { |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$cssClass = $small ? 'btn mr-1 ' : 'btn btn-sm mr-1 '; |
102
|
|
|
$cssClass .= empty($this->color) ? 'btn-light' : $this->colorToClass($this->color, 'btn-'); |
103
|
|
|
$icon = empty($this->icon) ? '' : '<i class="' . $this->icon . ' fa-fw"></i> '; |
104
|
|
|
$label = $small ? '' : $this->label; |
105
|
|
|
$divID = empty($this->id) ? '' : ' id="' . $this->id . '"'; |
106
|
|
|
if ($small && empty($icon)) { |
107
|
|
|
$icon = $this->label; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
switch ($this->type) { |
111
|
|
|
case 'js': |
112
|
|
|
return '<button type="button"' . $divID . ' class="' . $cssClass . '" onclick="' . $this->action |
113
|
|
|
. '" title="' . $this->label . '">' . $icon . $label . '</button>'; |
114
|
|
|
|
115
|
|
|
case 'link': |
116
|
|
|
$target = empty($this->target) ? '' : ' target="' . $this->target . '"'; |
117
|
|
|
return '<a ' . $target . $divID . ' class="' . $cssClass . '" href="' . $this->asset($this->action) . '"' |
118
|
|
|
. ' title="' . $this->label . '">' . $icon . $label . '</a>'; |
119
|
|
|
|
120
|
|
|
case 'modal': |
121
|
|
|
return '<button type="button"' . $divID . ' class="' . $cssClass . '" data-toggle="modal" data-target="#modal' |
122
|
|
|
. $this->action . '" title="' . $this->label . '">' . $icon . $label . '</button>'; |
123
|
|
|
|
124
|
|
|
default: |
125
|
|
|
$onclick = $this->getOnClickValue($viewName, $jsFunction); |
126
|
|
|
return '<button type="button"' . $divID . ' class="' . $cssClass . '" onclick="' . $onclick |
127
|
|
|
. '" title="' . $this->label . '">' . $icon . $label . '</button>'; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function renderTop() |
135
|
|
|
{ |
136
|
|
|
if ($this->getLevel() < $this->level) { |
137
|
|
|
return ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$cssClass = 'btn btn-sm '; |
141
|
|
|
$cssClass .= empty($this->color) ? 'btn-outline-secondary' : $this->colorToClass($this->color, 'btn-outline-'); |
142
|
|
|
$icon = empty($this->icon) ? '' : '<i class="' . $this->icon . ' fa-fw"></i> '; |
143
|
|
|
$divID = empty($this->id) ? '' : ' id="' . $this->id . '"'; |
144
|
|
|
|
145
|
|
|
switch ($this->type) { |
146
|
|
|
case 'js': |
147
|
|
|
return '<button type="button"' . $divID . ' class="' . $cssClass . '" onclick="' . $this->action |
148
|
|
|
. '" title="' . $this->label . '">' . $icon . $this->label . '</button> '; |
149
|
|
|
|
150
|
|
|
case 'link': |
151
|
|
|
$target = empty($this->target) ? '' : ' target="' . $this->target . '"'; |
152
|
|
|
return '<a ' . $target . $divID . ' class="' . $cssClass . '" href="' . $this->asset($this->action) . '"' |
153
|
|
|
. ' title="' . $this->label . '">' . $icon . $this->label . '</a> '; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return ''; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Fix url. |
161
|
|
|
* |
162
|
|
|
* @param string $url |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function asset($url) |
167
|
|
|
{ |
168
|
|
|
$path = \FS_ROUTE . '/'; |
169
|
|
|
if (\substr($url, 0, \strlen($path)) == $path) { |
170
|
|
|
return $url; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/// external link? |
174
|
|
|
$parts = \explode(':', $url); |
175
|
|
|
if (\in_array($parts[0], ['http', 'https'])) { |
176
|
|
|
return $url; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return \str_replace('//', '/', $path . $url); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $viewName |
184
|
|
|
* @param string $jsFunction |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
protected function getOnClickValue($viewName, $jsFunction) |
189
|
|
|
{ |
190
|
|
|
if ($this->confirm) { |
191
|
|
|
return 'confirmAction(\'' . $viewName . '\',\'' . $this->action . '\',\'' |
192
|
|
|
. $this->label . '\',\'' . self::$i18n->trans('are-you-sure-action') . '\',\'' |
193
|
|
|
. self::$i18n->trans('cancel') . '\',\'' . self::$i18n->trans('confirm') . '\');'; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (empty($jsFunction)) { |
197
|
|
|
return 'this.form.action.value=\'' . $this->action . '\';this.form.submit();'; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $jsFunction . '(\'' . $viewName . '\',\'' . $this->action . '\');'; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|