dolibarr 21.0.0-beta
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
48 public $substit = array();
49
53 public $param = array();
54
58 public $error = '';
59
60
66 public function __construct($db)
67 {
68 $this->db = $db;
69 }
70
71
72 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
90 public function select_resource_list($selected = 0, $htmlname = 'fk_resource', $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">&nbsp;</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 // @phan-suppress-next-line PhanTypeExpectedObjectPropAccess
136 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))) {
137 $out .= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
138 } else {
139 $out .= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
140 }
141
142 array_push($outarray, array('key' => (int) $resourcestat->lines[$i]->id, 'value' => (int) $resourcestat->lines[$i]->id, 'label' => (string) $label));
143
144 $i++;
145 if (($i % 10) == 0) {
146 $out .= "\n";
147 }
148 }
149 }
150 $out .= '</select>'."\n";
151
152 if (!empty($conf->use_javascript_ajax) && getDolGlobalString('RESOURCE_USE_SEARCH_TO_SELECT') && !$forcecombo) {
153 //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
154 $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
155 } else {
156 $out .= ajax_combobox($htmlname);
157 }
158
159 if ($outputmode != 2) {
160 $out .= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
161
162 $out .= '</form>';
163 }
164 } else {
165 dol_print_error($this->db);
166 }
167
168 if ($outputmode && $outputmode != 2) {
169 return $outarray;
170 }
171 return $out;
172 }
173
174 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
189 public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $usejscombo = 0, $morecss = 'minwidth100')
190 {
191 // phpcs:enable
192 global $langs, $user;
193
194 $resourcestat = new Dolresource($this->db);
195
196 dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG);
197
198 $filterarray = array();
199
200 if ($filtertype != '' && $filtertype != '-1') {
201 $filterarray = explode(',', $filtertype);
202 }
203
204 $resourcestat->loadCacheCodeTypeResource();
205 print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
206 if ($empty) {
207 print '<option value="">&nbsp;</option>';
208 }
209 if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
210 foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
211 // We discard empty line if showempty is on because an empty line has already been output.
212 if ($empty && empty($arraytypes['code'])) {
213 continue;
214 }
215
216 if ($format == 0) {
217 print '<option value="'.$id.'"';
218 } elseif ($format == 1) {
219 print '<option value="'.$arraytypes['code'].'"';
220 } elseif ($format == 2) {
221 print '<option value="'.$arraytypes['code'].'"';
222 } elseif ($format == 3) {
223 print '<option value="'.$id.'"';
224 }
225 // Si selected est text, on compare avec code, sinon avec id
226 if (!empty($selected) && preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) {
227 print ' selected';
228 } elseif ($selected == $id) {
229 print ' selected';
230 }
231 print '>';
232 if ($format == 0) {
233 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
234 } elseif ($format == 1) {
235 $value = $arraytypes['code'];
236 } elseif ($format == 2) {
237 $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
238 } elseif ($format == 3) {
239 $value = $arraytypes['code'];
240 }
241 if (empty($value)) {
242 $value = '&nbsp;';
243 }
244 print $value;
245 print '</option>';
246 }
247 }
248 print '</select>';
249 if ($usejscombo) {
250 print ajax_combobox("select".$htmlname);
251 }
252
253 if ($user->admin && !$noadmininfo) {
254 print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
255 }
256 }
257
258 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271 public function select_ziptown($selected = '', $htmlname = 'zipcode', $fields = array(), $fieldsize = 0, $disableautocomplete = 0, $moreattrib = '', $morecss = '')
272 {
273 // phpcs:enable
274 global $conf;
275
276 $out = '';
277
278 $size = '';
279 if (!empty($fieldsize)) {
280 $size = 'size="' . $fieldsize . '"';
281 }
282
283 if ($conf->use_javascript_ajax && empty($disableautocomplete)) {
284 $out .= ajax_multiautocompleter($htmlname, $fields, DOL_URL_ROOT . '/core/ajax/ziptown.php') . "\n";
285 $moreattrib .= ' autocomplete="off"';
286 }
287 $out .= '<input id="' . $htmlname . '" class="maxwidthonsmartphone' . ($morecss ? ' ' . $morecss : '') . '" type="text"' . ($moreattrib ? ' ' . $moreattrib : '') . ' name="' . $htmlname . '" ' . $size . ' value="' . $selected . '">' . "\n";
288
289 return $out;
290 }
291
292 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
306 public function select_state($selected = 0, $country_codeid = 0, $htmlname = 'state_id', $morecss = 'maxwidth200onsmartphone minwidth300')
307 {
308 // phpcs:enable
309 global $conf, $langs, $user;
310
311 dol_syslog(get_class($this) . "::select_departement selected=" . $selected . ", country_codeid=" . $country_codeid, LOG_DEBUG);
312
313 $langs->load("dict");
314
315 $out = '';
316
317 // Search active departements/cantons/province of a region and actif country
318 $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";
319 $sql .= " " . $this->db->prefix() . "c_departements as d, " . $this->db->prefix() . "c_regions as r," . $this->db->prefix() . "c_country as c";
320 $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid";
321 $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
322 if ($country_codeid && is_numeric($country_codeid)) {
323 $sql .= " AND c.rowid = '" . $this->db->escape($country_codeid) . "'";
324 }
325 if ($country_codeid && !is_numeric($country_codeid)) {
326 $sql .= " AND c.code = '" . $this->db->escape($country_codeid) . "'";
327 }
328 $sql .= " ORDER BY c.code, d.code_departement";
329
330 $result = $this->db->query($sql);
331 if ($result) {
332 if (!empty($htmlname)) {
333 $out .= '<select id="' . $htmlname . '" class="flat' . ($morecss ? ' ' . $morecss : '') . '" name="' . $htmlname . '">';
334 }
335 if ($country_codeid) {
336 $out .= '<option value="0">&nbsp;</option>';
337 }
338 $num = $this->db->num_rows($result);
339 $i = 0;
340 dol_syslog(get_class($this) . "::select_departement num=" . $num, LOG_DEBUG);
341 if ($num) {
342 $country = '';
343 while ($i < $num) {
344 $obj = $this->db->fetch_object($result);
345 if ($obj->code == '0') { // Le code peut etre une chaine
346 $out .= '<option value="0">&nbsp;</option>';
347 } else {
348 if (!$country || $country != $obj->country) {
349 // Show break if we are in list with multiple countries
350 if (!$country_codeid && $obj->country_code) {
351 $out .= '<option value="-1" disabled data-html="----- ' . $obj->country . ' -----">----- ' . $obj->country . " -----</option>\n";
352 $country = $obj->country;
353 }
354 }
355
356 if (!empty($selected) && $selected == $obj->rowid) {
357 $out .= '<option value="' . $obj->rowid . '" selected>';
358 } else {
359 $out .= '<option value="' . $obj->rowid . '">';
360 }
361
362 // If translation exists use it, otherwise use default name
363 if (
364 getDolGlobalString('MAIN_SHOW_STATE_CODE') &&
365 (getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 1 || getDolGlobalInt('MAIN_SHOW_STATE_CODE') == 2 || getDolGlobalString('MAIN_SHOW_STATE_CODE') === 'all')
366 ) {
367 if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) {
368 $out .= $obj->region_name . ' - ' . $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
369 } else {
370 $out .= $obj->code . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
371 }
372 } else {
373 if (getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1) {
374 $out .= $obj->region_name . ' - ' . ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
375 } else {
376 $out .= ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
377 }
378 }
379
380 $out .= '</option>';
381 }
382 $i++;
383 }
384 }
385 if (!empty($htmlname)) {
386 $out .= '</select>';
387 }
388 if (!empty($htmlname) && $user->admin) {
389 $out .= ' ' . info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
390 }
391 } else {
392 dol_print_error($this->db);
393 }
394
395 // Make select dynamic
396 if (!empty($htmlname)) {
397 include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
398 $out .= ajax_combobox($htmlname);
399 }
400
401 return $out;
402 }
403}
$id
Definition account.php:48
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:459
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:308
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.
select_resource_list($selected=0, $htmlname='fk_resource', $filter='', $showempty=0, $showtype=0, $forcecombo=0, $event=[], $filterkey='', $outputmode=0, $limit=20, $morecss='minwidth100', $multiple=false)
Output html form to select a resource.
__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_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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79