dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
28require_once DOL_DOCUMENT_ROOT."/core/class/html.form.class.php";
29require_once DOL_DOCUMENT_ROOT."/resource/class/dolresource.class.php";
30
31
39{
43 public $db;
44
45 public $substit = array();
46
47 public $param = array();
48
52 public $error = '';
53
54
60 public function __construct($db)
61 {
62 $this->db = $db;
63 }
64
65
66 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
84 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)
85 {
86 // phpcs:enable
87 global $conf, $langs;
88
89 $out = '';
90 $outarray = array();
91
92 $resourcestat = new Dolresource($this->db);
93
94 $resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter);
95
96 if (!empty($selected) && !is_array($selected)) {
97 $selected = array($selected);
98 }
99
100 if ($outputmode != 2) {
101 $out = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
102 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
103 }
104
105 if ($resourcestat) {
106 // Construct $out and $outarray
107 $out .= '<select id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.($multiple ? '[]' : '').'" '.($multiple ? 'multiple' : '').'>'."\n";
108 if ($showempty) {
109 $out .= '<option value="-1">&nbsp;</option>'."\n";
110 }
111
112 $num = 0;
113 if (is_array($resourcestat->lines)) {
114 $num = count($resourcestat->lines);
115 }
116
117 //var_dump($resourcestat->lines);
118 $i = 0;
119 if ($num) {
120 while ($i < $num) {
121 $resourceclass = ucfirst($resourcestat->lines[$i]->element);
122
123 $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : ''.$resourcestat->lines[$i]->label;
124 if ($resourceclass != 'Dolresource') {
125 $label .= ' ('.$langs->trans($resourceclass).')';
126 }
127
128 // Test if entry is the first element of $selected.
129 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))) {
130 $out .= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
131 } else {
132 $out .= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
133 }
134
135 array_push($outarray, array('key'=>$resourcestat->lines[$i]->id, 'value'=>$resourcestat->lines[$i]->id, 'label'=>$label));
136
137 $i++;
138 if (($i % 10) == 0) {
139 $out .= "\n";
140 }
141 }
142 }
143 $out .= '</select>'."\n";
144
145 if (!empty($conf->use_javascript_ajax) && getDolGlobalString('RESOURCE_USE_SEARCH_TO_SELECT') && !$forcecombo) {
146 //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
147 $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
148 } else {
149 $out .= ajax_combobox($htmlname);
150 }
151
152 if ($outputmode != 2) {
153 $out .= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
154
155 $out .= '</form>';
156 }
157 } else {
158 dol_print_error($this->db);
159 }
160
161 if ($outputmode && $outputmode != 2) {
162 return $outarray;
163 }
164 return $out;
165 }
166
167 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
182 public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $usejscombo = 0, $morecss = 'minwidth100')
183 {
184 // phpcs:enable
185 global $langs, $user;
186
187 $resourcestat = new Dolresource($this->db);
188
189 dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG);
190
191 $filterarray = array();
192
193 if ($filtertype != '' && $filtertype != '-1') {
194 $filterarray = explode(',', $filtertype);
195 }
196
197 $resourcestat->loadCacheCodeTypeResource();
198 print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
199 if ($empty) {
200 print '<option value="">&nbsp;</option>';
201 }
202 if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
203 foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
204 // We discard empty line if showempty is on because an empty line has already been output.
205 if ($empty && empty($arraytypes['code'])) {
206 continue;
207 }
208
209 if ($format == 0) {
210 print '<option value="'.$id.'"';
211 } elseif ($format == 1) {
212 print '<option value="'.$arraytypes['code'].'"';
213 } elseif ($format == 2) {
214 print '<option value="'.$arraytypes['code'].'"';
215 } elseif ($format == 3) {
216 print '<option value="'.$id.'"';
217 }
218 // Si selected est text, on compare avec code, sinon avec id
219 if (!empty($selected) && preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) {
220 print ' selected';
221 } elseif ($selected == $id) {
222 print ' selected';
223 }
224 print '>';
225 if ($format == 0) {
226 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
227 } elseif ($format == 1) {
228 $value = $arraytypes['code'];
229 } elseif ($format == 2) {
230 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
231 } elseif ($format == 3) {
232 $value = $arraytypes['code'];
233 }
234 if (empty($value)) {
235 $value = '&nbsp;';
236 }
237 print $value;
238 print '</option>';
239 }
240 }
241 print '</select>';
242 if ($usejscombo) {
243 print ajax_combobox("select".$htmlname);
244 }
245
246 if ($user->admin && !$noadmininfo) {
247 print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
248 }
249 }
250
251 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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, DOL_URL_ROOT . '/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
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">&nbsp;</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">&nbsp;</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 || getDolGlobalString('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}
$id
Definition account.php:39
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:457
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:306
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 a 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.