1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2013-2015 Jean-François FERRY <[email protected]> |
4
|
|
|
* Copyright (C) 2019-2024 Frédéric France <[email protected]> |
5
|
|
|
* Copyright (C) 2022 Ferran Marcet <[email protected]> |
6
|
|
|
* Copyright (C) 2023 William Mead <[email protected]> |
7
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
21
|
|
|
* or see https://www.gnu.org/ |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Dolibarr\Code\Resource\Classes; |
25
|
|
|
|
26
|
|
|
use DoliDB; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* \file resource/class/html.formresource.class.php |
30
|
|
|
* \ingroup core |
31
|
|
|
* \brief Class file to manage forms into resource module |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
require_once DOL_DOCUMENT_ROOT . "/core/class/html.form.class.php"; |
35
|
|
|
require_once DOL_DOCUMENT_ROOT . "/resource/class/dolresource.class.php"; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class to manage forms for the module resource |
40
|
|
|
* |
41
|
|
|
* \remarks Utilisation: $formresource = new FormResource($db) |
42
|
|
|
* \remarks $formplace->proprietes=1 ou chaine ou tableau de valeurs |
43
|
|
|
*/ |
44
|
|
|
class FormResource |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var DoliDB Database handler. |
48
|
|
|
*/ |
49
|
|
|
public $db; |
50
|
|
|
|
51
|
|
|
public $substit = array(); |
52
|
|
|
|
53
|
|
|
public $param = array(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string Error code (or message) |
57
|
|
|
*/ |
58
|
|
|
public $error = ''; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor |
63
|
|
|
* |
64
|
|
|
* @param DoliDB $db Database handler |
65
|
|
|
*/ |
66
|
|
|
public function __construct($db) |
67
|
|
|
{ |
68
|
|
|
$this->db = $db; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
73
|
|
|
/** |
74
|
|
|
* Output html form to select a resource |
75
|
|
|
* |
76
|
|
|
* @param int $selected Preselected resource id |
77
|
|
|
* @param string $htmlname Name of field in form |
78
|
|
|
* @param array $filter Optional filters criteria (example: 's.rowid <> x') |
79
|
|
|
* @param int $showempty Add an empty field |
80
|
|
|
* @param int $showtype Show third party type in combo list (customer, prospect or supplier) |
81
|
|
|
* @param int $forcecombo Force to use combo box |
82
|
|
|
* @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) |
83
|
|
|
* @param array $filterkey Filter on key value |
84
|
|
|
* @param int $outputmode 0=HTML select string, 1=Array, 2=without form tag |
85
|
|
|
* @param int $limit Limit number of answers, 0 for no limit |
86
|
|
|
* @param string $morecss More css |
87
|
|
|
* @param bool $multiple add [] in the name of element and add 'multiple' attribute |
88
|
|
|
* @return string|array HTML string with |
89
|
|
|
*/ |
90
|
|
|
public function select_resource_list($selected = 0, $htmlname = 'fk_resource', array $filter = [], $showempty = 0, $showtype = 0, $forcecombo = 0, $event = [], $filterkey = [], $outputmode = 0, $limit = 20, $morecss = 'minwidth100', $multiple = false) |
91
|
|
|
{ |
92
|
|
|
// phpcs:enable |
93
|
|
|
global $conf, $langs; |
94
|
|
|
|
95
|
|
|
$out = ''; |
96
|
|
|
$outarray = array(); |
97
|
|
|
|
98
|
|
|
$resourcestat = new Dolresource($this->db); |
99
|
|
|
|
100
|
|
|
$resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter); |
101
|
|
|
|
102
|
|
|
if (!empty($selected) && !is_array($selected)) { |
103
|
|
|
$selected = array($selected); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($outputmode != 2) { |
107
|
|
|
$out = '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">'; |
108
|
|
|
$out .= '<input type="hidden" name="token" value="' . newToken() . '">'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($resourcestat) { |
112
|
|
|
// Construct $out and $outarray |
113
|
|
|
$out .= '<select id="' . $htmlname . '" class="flat' . ($morecss ? ' ' . $morecss : '') . '" name="' . $htmlname . ($multiple ? '[]' : '') . '" ' . ($multiple ? 'multiple' : '') . '>' . "\n"; |
114
|
|
|
if ($showempty) { |
115
|
|
|
$out .= '<option value="-1"> </option>' . "\n"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$num = 0; |
119
|
|
|
if (is_array($resourcestat->lines)) { |
120
|
|
|
$num = count($resourcestat->lines); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
//var_dump($resourcestat->lines); |
124
|
|
|
$i = 0; |
125
|
|
|
if ($num) { |
126
|
|
|
while ($i < $num) { |
127
|
|
|
$resourceclass = ucfirst($resourcestat->lines[$i]->element); |
128
|
|
|
|
129
|
|
|
$label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : '' . $resourcestat->lines[$i]->label; |
130
|
|
|
if ($resourceclass != 'Dolresource') { |
131
|
|
|
$label .= ' (' . $langs->trans($resourceclass) . ')'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Test if entry is the first element of $selected. |
135
|
|
|
if ((isset($selected[0]) && is_object($selected[0]) && $selected[0]->id == $resourcestat->lines[$i]->id) || ((!isset($selected[0]) || !is_object($selected[0])) && !empty($selected) && in_array($resourcestat->lines[$i]->id, $selected))) { |
136
|
|
|
$out .= '<option value="' . $resourcestat->lines[$i]->id . '" selected>' . $label . '</option>'; |
137
|
|
|
} else { |
138
|
|
|
$out .= '<option value="' . $resourcestat->lines[$i]->id . '">' . $label . '</option>'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
array_push($outarray, array('key' => $resourcestat->lines[$i]->id, 'value' => $resourcestat->lines[$i]->id, 'label' => $label)); |
142
|
|
|
|
143
|
|
|
$i++; |
144
|
|
|
if (($i % 10) == 0) { |
145
|
|
|
$out .= "\n"; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
$out .= '</select>' . "\n"; |
150
|
|
|
|
151
|
|
|
if (!empty($conf->use_javascript_ajax) && getDolGlobalString('RESOURCE_USE_SEARCH_TO_SELECT') && !$forcecombo) { |
152
|
|
|
//$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2); |
153
|
|
|
$out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT); |
154
|
|
|
} else { |
155
|
|
|
$out .= ajax_combobox($htmlname); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($outputmode != 2) { |
159
|
|
|
$out .= '<input type="submit" class="button" value="' . $langs->trans("Search") . '"> '; |
160
|
|
|
|
161
|
|
|
$out .= '</form>'; |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
|
|
dol_print_error($this->db); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($outputmode && $outputmode != 2) { |
168
|
|
|
return $outarray; |
169
|
|
|
} |
170
|
|
|
return $out; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
174
|
|
|
/** |
175
|
|
|
* Return html list of tickets type |
176
|
|
|
* |
177
|
|
|
* @param string $selected Id du type pre-selectionne |
178
|
|
|
* @param string $htmlname Nom de la zone select |
179
|
|
|
* @param string $filtertype To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz)) |
180
|
|
|
* @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code |
181
|
|
|
* @param int $empty 1=peut etre vide, 0 sinon |
182
|
|
|
* @param int $noadmininfo 0=Add admin info, 1=Disable admin info |
183
|
|
|
* @param int $maxlength Max length of label |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0) |
187
|
|
|
{ |
188
|
|
|
// phpcs:enable |
189
|
|
|
global $langs, $user; |
190
|
|
|
|
191
|
|
|
$resourcestat = new Dolresource($this->db); |
192
|
|
|
|
193
|
|
|
dol_syslog(get_class($this) . "::select_types_resource " . $selected . ", " . $htmlname . ", " . $filtertype . ", " . $format, LOG_DEBUG); |
194
|
|
|
|
195
|
|
|
$filterarray = array(); |
196
|
|
|
|
197
|
|
|
if ($filtertype != '' && $filtertype != '-1') { |
198
|
|
|
$filterarray = explode(',', $filtertype); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$resourcestat->loadCacheCodeTypeResource(); |
202
|
|
|
print '<select id="select' . $htmlname . '" class="flat maxwidthonsmartphone select_' . $htmlname . '" name="' . $htmlname . '">'; |
203
|
|
|
if ($empty) { |
204
|
|
|
print '<option value=""> </option>'; |
205
|
|
|
} |
206
|
|
|
if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) { |
207
|
|
|
foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) { |
208
|
|
|
// We discard empty line if showempty is on because an empty line has already been output. |
209
|
|
|
if ($empty && empty($arraytypes['code'])) { |
210
|
|
|
continue; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ($format == 0) { |
214
|
|
|
print '<option value="' . $id . '"'; |
215
|
|
|
} elseif ($format == 1) { |
216
|
|
|
print '<option value="' . $arraytypes['code'] . '"'; |
217
|
|
|
} elseif ($format == 2) { |
218
|
|
|
print '<option value="' . $arraytypes['code'] . '"'; |
219
|
|
|
} elseif ($format == 3) { |
220
|
|
|
print '<option value="' . $id . '"'; |
221
|
|
|
} |
222
|
|
|
// Si selected est text, on compare avec code, sinon avec id |
223
|
|
|
if (!empty($selected) && preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) { |
224
|
|
|
print ' selected'; |
225
|
|
|
} elseif ($selected == $id) { |
226
|
|
|
print ' selected'; |
227
|
|
|
} |
228
|
|
|
print '>'; |
229
|
|
|
if ($format == 0) { |
230
|
|
|
$value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']); |
231
|
|
|
} elseif ($format == 1) { |
232
|
|
|
$value = $arraytypes['code']; |
233
|
|
|
} elseif ($format == 2) { |
234
|
|
|
$value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']); |
235
|
|
|
} elseif ($format == 3) { |
236
|
|
|
$value = $arraytypes['code']; |
237
|
|
|
} |
238
|
|
|
if (empty($value)) { |
239
|
|
|
$value = ' '; |
240
|
|
|
} |
241
|
|
|
print $value; |
242
|
|
|
print '</option>'; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
print '</select>'; |
246
|
|
|
if ($user->admin && !$noadmininfo) { |
247
|
|
|
print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
252
|
|
|
/** |
253
|
|
|
* Return a select list with zip codes and their town |
254
|
|
|
* |
255
|
|
|
* @param string $selected Preselected value |
256
|
|
|
* @param string $htmlname HTML select name |
257
|
|
|
* @param array $fields Array with key of fields to refresh after selection |
258
|
|
|
* @param int $fieldsize Field size |
259
|
|
|
* @param int $disableautocomplete 1 To disable ajax autocomplete features (browser autocomplete may still occurs) |
260
|
|
|
* @param string $moreattrib Add more attribute on HTML input field |
261
|
|
|
* @param string $morecss More css |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function select_ziptown($selected = '', $htmlname = 'zipcode', $fields = array(), $fieldsize = 0, $disableautocomplete = 0, $moreattrib = '', $morecss = '') |
265
|
|
|
{ |
266
|
|
|
// phpcs:enable |
267
|
|
|
global $conf; |
268
|
|
|
|
269
|
|
|
$out = ''; |
270
|
|
|
|
271
|
|
|
$size = ''; |
272
|
|
|
if (!empty($fieldsize)) { |
273
|
|
|
$size = 'size="' . $fieldsize . '"'; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ($conf->use_javascript_ajax && empty($disableautocomplete)) { |
277
|
|
|
$out .= ajax_multiautocompleter($htmlname, $fields, constant('BASE_URL') . '/core/ajax/ziptown.php') . "\n"; |
278
|
|
|
$moreattrib .= ' autocomplete="off"'; |
279
|
|
|
} |
280
|
|
|
$out .= '<input id="' . $htmlname . '" class="maxwidthonsmartphone' . ($morecss ? ' ' . $morecss : '') . '" type="text"' . ($moreattrib ? ' ' . $moreattrib : '') . ' name="' . $htmlname . '" ' . $size . ' value="' . $selected . '">' . "\n"; |
281
|
|
|
|
282
|
|
|
return $out; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
286
|
|
|
/** |
287
|
|
|
* Returns the drop-down list of departments/provinces/cantons for all countries or for a given country. |
288
|
|
|
* In the case of an all-country list, the display breaks on the country. |
289
|
|
|
* The key of the list is the code (there can be several entries for a given code but in this case, the country field differs). |
290
|
|
|
* Thus the links with the departments are done on a department independently of its name. |
291
|
|
|
* |
292
|
|
|
* @param int $selected Code state preselected (mus be state id) |
293
|
|
|
* @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show |
294
|
|
|
* @param string $htmlname Id of department. If '', we want only the string with <option> |
295
|
|
|
* @param string $morecss Add more css |
296
|
|
|
* @return string String with HTML select |
297
|
|
|
* @see select_country() |
298
|
|
|
*/ |
299
|
|
|
public function select_state($selected = 0, $country_codeid = 0, $htmlname = 'state_id', $morecss = 'maxwidth200onsmartphone minwidth300') |
300
|
|
|
{ |
301
|
|
|
// phpcs:enable |
302
|
|
|
global $conf, $langs, $user; |
303
|
|
|
|
304
|
|
|
dol_syslog(get_class($this) . "::select_departement selected=" . $selected . ", country_codeid=" . $country_codeid, LOG_DEBUG); |
305
|
|
|
|
306
|
|
|
$langs->load("dict"); |
307
|
|
|
|
308
|
|
|
$out = ''; |
309
|
|
|
|
310
|
|
|
// Search active departements/cantons/province of a region and actif country |
311
|
|
|
$sql = "SELECT d.rowid, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.nom as region_name FROM"; |
312
|
|
|
$sql .= " " . $this->db->prefix() . "c_departements as d, " . $this->db->prefix() . "c_regions as r," . $this->db->prefix() . "c_country as c"; |
313
|
|
|
$sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid"; |
314
|
|
|
$sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1"; |
315
|
|
|
if ($country_codeid && is_numeric($country_codeid)) { |
316
|
|
|
$sql .= " AND c.rowid = '" . $this->db->escape($country_codeid) . "'"; |
317
|
|
|
} |
318
|
|
|
if ($country_codeid && !is_numeric($country_codeid)) { |
319
|
|
|
$sql .= " AND c.code = '" . $this->db->escape($country_codeid) . "'"; |
320
|
|
|
} |
321
|
|
|
$sql .= " ORDER BY c.code, d.code_departement"; |
322
|
|
|
|
323
|
|
|
$result = $this->db->query($sql); |
324
|
|
|
if ($result) { |
325
|
|
|
if (!empty($htmlname)) { |
326
|
|
|
$out .= '<select id="' . $htmlname . '" class="flat' . ($morecss ? ' ' . $morecss : '') . '" name="' . $htmlname . '">'; |
327
|
|
|
} |
328
|
|
|
if ($country_codeid) { |
329
|
|
|
$out .= '<option value="0"> </option>'; |
330
|
|
|
} |
331
|
|
|
$num = $this->db->num_rows($result); |
332
|
|
|
$i = 0; |
333
|
|
|
dol_syslog(get_class($this) . "::select_departement num=" . $num, LOG_DEBUG); |
334
|
|
|
if ($num) { |
335
|
|
|
$country = ''; |
336
|
|
|
while ($i < $num) { |
337
|
|
|
$obj = $this->db->fetch_object($result); |
338
|
|
|
if ($obj->code == '0') { // Le code peut etre une chaine |
339
|
|
|
$out .= '<option value="0"> </option>'; |
340
|
|
|
} else { |
341
|
|
|
if (!$country || $country != $obj->country) { |
342
|
|
|
// Show break if we are in list with multiple countries |
343
|
|
|
if (!$country_codeid && $obj->country_code) { |
344
|
|
|
$out .= '<option value="-1" disabled data-html="----- ' . $obj->country . ' -----">----- ' . $obj->country . " -----</option>\n"; |
345
|
|
|
$country = $obj->country; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if (!empty($selected) && $selected == $obj->rowid) { |
350
|
|
|
$out .= '<option value="' . $obj->rowid . '" selected>'; |
351
|
|
|
} else { |
352
|
|
|
$out .= '<option value="' . $obj->rowid . '">'; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
// If translation exists use it, otherwise use default name |
356
|
|
|
if ( |
357
|
|
|
getDolGlobalString('MAIN_SHOW_STATE_CODE') && |
358
|
|
|
(getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 1 || getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 2 || $conf->global->MAIN_SHOW_STATE_CODE === 'all') |
359
|
|
|
) { |
360
|
|
|
if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) { |
361
|
|
|
$out .= $obj->region_name . ' - ' . $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : '')); |
362
|
|
|
} else { |
363
|
|
|
$out .= $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : '')); |
364
|
|
|
} |
365
|
|
|
} else { |
366
|
|
|
if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) { |
367
|
|
|
$out .= $obj->region_name . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : '')); |
368
|
|
|
} else { |
369
|
|
|
$out .= ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : '')); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
$out .= '</option>'; |
374
|
|
|
} |
375
|
|
|
$i++; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
if (!empty($htmlname)) { |
379
|
|
|
$out .= '</select>'; |
380
|
|
|
} |
381
|
|
|
if (!empty($htmlname) && $user->admin) { |
382
|
|
|
$out .= ' ' . info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); |
383
|
|
|
} |
384
|
|
|
} else { |
385
|
|
|
dol_print_error($this->db); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
// Make select dynamic |
389
|
|
|
if (!empty($htmlname)) { |
390
|
|
|
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; |
391
|
|
|
$out .= ajax_combobox($htmlname); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return $out; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|