dolibarr 20.0.2
html.formresource.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) - 2013-2015 Jean-François FERRY <jfefe@aternatik.fr>
3 * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2022 Ferran Marcet <fmarcet@2byte.es>
5 * Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27require_once DOL_DOCUMENT_ROOT."/core/class/html.form.class.php";
28require_once DOL_DOCUMENT_ROOT."/resource/class/dolresource.class.php";
29
30
38{
42 public $db;
43
44 public $substit = array();
45
46 public $param = array();
47
51 public $error = '';
52
53
59 public function __construct($db)
60 {
61 $this->db = $db;
62 }
63
64
65 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
83 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)
84 {
85 // phpcs:enable
86 global $conf, $langs;
87
88 $out = '';
89 $outarray = array();
90
91 $resourcestat = new Dolresource($this->db);
92
93 $resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter);
94
95 if (!empty($selected) && !is_array($selected)) {
96 $selected = array($selected);
97 }
98
99 if ($outputmode != 2) {
100 $out = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
101 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
102 }
103
104 if ($resourcestat) {
105 // Construct $out and $outarray
106 $out .= '<select id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.($multiple ? '[]' : '').'" '.($multiple ? 'multiple' : '').'>'."\n";
107 if ($showempty) {
108 $out .= '<option value="-1">&nbsp;</option>'."\n";
109 }
110
111 $num = 0;
112 if (is_array($resourcestat->lines)) {
113 $num = count($resourcestat->lines);
114 }
115
116 //var_dump($resourcestat->lines);
117 $i = 0;
118 if ($num) {
119 while ($i < $num) {
120 $resourceclass = ucfirst($resourcestat->lines[$i]->element);
121
122 $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : ''.$resourcestat->lines[$i]->label;
123 if ($resourceclass != 'Dolresource') {
124 $label .= ' ('.$langs->trans($resourceclass).')';
125 }
126
127 // Test if entry is the first element of $selected.
128 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))) {
129 $out .= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
130 } else {
131 $out .= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
132 }
133
134 array_push($outarray, array('key'=>$resourcestat->lines[$i]->id, 'value'=>$resourcestat->lines[$i]->id, 'label'=>$label));
135
136 $i++;
137 if (($i % 10) == 0) {
138 $out .= "\n";
139 }
140 }
141 }
142 $out .= '</select>'."\n";
143
144 if (!empty($conf->use_javascript_ajax) && getDolGlobalString('RESOURCE_USE_SEARCH_TO_SELECT') && !$forcecombo) {
145 //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
146 $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
147 } else {
148 $out .= ajax_combobox($htmlname);
149 }
150
151 if ($outputmode != 2) {
152 $out .= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
153
154 $out .= '</form>';
155 }
156 } else {
157 dol_print_error($this->db);
158 }
159
160 if ($outputmode && $outputmode != 2) {
161 return $outarray;
162 }
163 return $out;
164 }
165
166 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
181 public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $usejscombo = 0, $morecss = 'minwidth100')
182 {
183 // phpcs:enable
184 global $langs, $user;
185
186 $resourcestat = new Dolresource($this->db);
187
188 dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG);
189
190 $filterarray = array();
191
192 if ($filtertype != '' && $filtertype != '-1') {
193 $filterarray = explode(',', $filtertype);
194 }
195
196 $resourcestat->loadCacheCodeTypeResource();
197 print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
198 if ($empty) {
199 print '<option value="">&nbsp;</option>';
200 }
201 if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
202 foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
203 // We discard empty line if showempty is on because an empty line has already been output.
204 if ($empty && empty($arraytypes['code'])) {
205 continue;
206 }
207
208 if ($format == 0) {
209 print '<option value="'.$id.'"';
210 } elseif ($format == 1) {
211 print '<option value="'.$arraytypes['code'].'"';
212 } elseif ($format == 2) {
213 print '<option value="'.$arraytypes['code'].'"';
214 } elseif ($format == 3) {
215 print '<option value="'.$id.'"';
216 }
217 // Si selected est text, on compare avec code, sinon avec id
218 if (!empty($selected) && preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) {
219 print ' selected';
220 } elseif ($selected == $id) {
221 print ' selected';
222 }
223 print '>';
224 if ($format == 0) {
225 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
226 } elseif ($format == 1) {
227 $value = $arraytypes['code'];
228 } elseif ($format == 2) {
229 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
230 } elseif ($format == 3) {
231 $value = $arraytypes['code'];
232 }
233 if (empty($value)) {
234 $value = '&nbsp;';
235 }
236 print $value;
237 print '</option>';
238 }
239 }
240 print '</select>';
241 if ($usejscombo) {
242 print ajax_combobox("select".$htmlname);
243 }
244
245 if ($user->admin && !$noadmininfo) {
246 print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
247 }
248 }
249
250 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
263 public function select_ziptown($selected = '', $htmlname = 'zipcode', $fields = array(), $fieldsize = 0, $disableautocomplete = 0, $moreattrib = '', $morecss = '')
264 {
265 // phpcs:enable
266 global $conf;
267
268 $out = '';
269
270 $size = '';
271 if (!empty($fieldsize)) {
272 $size = 'size="' . $fieldsize . '"';
273 }
274
275 if ($conf->use_javascript_ajax && empty($disableautocomplete)) {
276 $out .= ajax_multiautocompleter($htmlname, $fields, DOL_URL_ROOT . '/core/ajax/ziptown.php') . "\n";
277 $moreattrib .= ' autocomplete="off"';
278 }
279 $out .= '<input id="' . $htmlname . '" class="maxwidthonsmartphone' . ($morecss ? ' ' . $morecss : '') . '" type="text"' . ($moreattrib ? ' ' . $moreattrib : '') . ' name="' . $htmlname . '" ' . $size . ' value="' . $selected . '">' . "\n";
280
281 return $out;
282 }
283
284 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
298 public function select_state($selected = 0, $country_codeid = 0, $htmlname = 'state_id', $morecss = 'maxwidth200onsmartphone minwidth300')
299 {
300 // phpcs:enable
301 global $conf, $langs, $user;
302
303 dol_syslog(get_class($this) . "::select_departement selected=" . $selected . ", country_codeid=" . $country_codeid, LOG_DEBUG);
304
305 $langs->load("dict");
306
307 $out = '';
308
309 // Search active departements/cantons/province of a region and actif country
310 $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";
311 $sql .= " " . $this->db->prefix() . "c_departements as d, " . $this->db->prefix() . "c_regions as r," . $this->db->prefix() . "c_country as c";
312 $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid";
313 $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
314 if ($country_codeid && is_numeric($country_codeid)) {
315 $sql .= " AND c.rowid = '" . $this->db->escape($country_codeid) . "'";
316 }
317 if ($country_codeid && !is_numeric($country_codeid)) {
318 $sql .= " AND c.code = '" . $this->db->escape($country_codeid) . "'";
319 }
320 $sql .= " ORDER BY c.code, d.code_departement";
321
322 $result = $this->db->query($sql);
323 if ($result) {
324 if (!empty($htmlname)) {
325 $out .= '<select id="' . $htmlname . '" class="flat' . ($morecss ? ' ' . $morecss : '') . '" name="' . $htmlname . '">';
326 }
327 if ($country_codeid) {
328 $out .= '<option value="0">&nbsp;</option>';
329 }
330 $num = $this->db->num_rows($result);
331 $i = 0;
332 dol_syslog(get_class($this) . "::select_departement num=" . $num, LOG_DEBUG);
333 if ($num) {
334 $country = '';
335 while ($i < $num) {
336 $obj = $this->db->fetch_object($result);
337 if ($obj->code == '0') { // Le code peut etre une chaine
338 $out .= '<option value="0">&nbsp;</option>';
339 } else {
340 if (!$country || $country != $obj->country) {
341 // Show break if we are in list with multiple countries
342 if (!$country_codeid && $obj->country_code) {
343 $out .= '<option value="-1" disabled data-html="----- ' . $obj->country . ' -----">----- ' . $obj->country . " -----</option>\n";
344 $country = $obj->country;
345 }
346 }
347
348 if (!empty($selected) && $selected == $obj->rowid) {
349 $out .= '<option value="' . $obj->rowid . '" selected>';
350 } else {
351 $out .= '<option value="' . $obj->rowid . '">';
352 }
353
354 // If translation exists use it, otherwise use default name
355 if (
356 getDolGlobalString('MAIN_SHOW_STATE_CODE') &&
357 (getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 1 || getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 2 || $conf->global->MAIN_SHOW_STATE_CODE === 'all')
358 ) {
359 if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) {
360 $out .= $obj->region_name . ' - ' . $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
361 } else {
362 $out .= $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
363 }
364 } else {
365 if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) {
366 $out .= $obj->region_name . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
367 } else {
368 $out .= ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
369 }
370 }
371
372 $out .= '</option>';
373 }
374 $i++;
375 }
376 }
377 if (!empty($htmlname)) {
378 $out .= '</select>';
379 }
380 if (!empty($htmlname) && $user->admin) {
381 $out .= ' ' . info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
382 }
383 } else {
384 dol_print_error($this->db);
385 }
386
387 // Make select dynamic
388 if (!empty($htmlname)) {
389 include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
390 $out .= ajax_combobox($htmlname);
391 }
392
393 return $out;
394 }
395}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:456
ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLength=2, $autoselect=0)
Generic function that return javascript to add to a page to transform a common input text field into ...
Definition ajax.lib.php:305
DAO Resource object.
Class to manage forms for the module resource.
select_types_resource($selected='', $htmlname='type_resource', $filtertype='', $format=0, $empty=0, $noadmininfo=0, $maxlength=0, $usejscombo=0, $morecss='minwidth100')
Return html list of tickets type.
__construct($db)
Constructor.
select_state($selected=0, $country_codeid=0, $htmlname='state_id', $morecss='maxwidth200onsmartphone minwidth300')
Returns the drop-down list of departments/provinces/cantons for all countries or for a given country.
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)
Output html form to select a resource.
select_ziptown($selected='', $htmlname='zipcode', $fields=array(), $fieldsize=0, $disableautocomplete=0, $moreattrib='', $morecss='')
Return a select list with zip codes and their town.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.