dolibarr  17.0.4
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 Frédéric France <frederic.france@netlogic.fr>
4  * Copyright (C) 2022 Ferran Marcet <fmarcet@2byte.es>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
26 require_once DOL_DOCUMENT_ROOT."/core/class/html.form.class.php";
27 require_once DOL_DOCUMENT_ROOT."/resource/class/dolresource.class.php";
28 
29 
37 {
41  public $db;
42 
43  public $substit = array();
44 
45  public $param = array();
46 
50  public $error = '';
51 
52 
58  public function __construct($db)
59  {
60  $this->db = $db;
61  }
62 
63 
64  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
82  public function select_resource_list($selected = '', $htmlname = 'fk_resource', $filter = '', $showempty = 0, $showtype = 0, $forcecombo = 0, $event = array(), $filterkey = '', $outputmode = 0, $limit = 20, $morecss = '', $multiple = false)
83  {
84  // phpcs:enable
85  global $conf, $user, $langs;
86 
87  $out = '';
88  $outarray = array();
89 
90  $resourcestat = new Dolresource($this->db);
91 
92  $resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter);
93 
94  if (!empty($selected) && !is_array($selected)) {
95  $selected = array($selected);
96  }
97 
98  if ($outputmode != 2) {
99  $out = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
100  $out .= '<input type="hidden" name="token" value="'.newToken().'">';
101  }
102 
103  if ($resourcestat) {
104  // Construct $out and $outarray
105  $out .= '<select id="'.$htmlname.'" class="flat minwidth100'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.($multiple ? '[]' : '').'" '.($multiple ? 'multiple' : '').'>'."\n";
106  if ($showempty) {
107  $out .= '<option value="-1">&nbsp;</option>'."\n";
108  }
109 
110  $num = 0;
111  if (is_array($resourcestat->lines)) {
112  $num = count($resourcestat->lines);
113  }
114 
115  //var_dump($resourcestat->lines);
116  $i = 0;
117  if ($num) {
118  while ($i < $num) {
119  $resourceclass = ucfirst($resourcestat->lines[$i]->element);
120 
121  $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : ''.$resourcestat->lines[$i]->label;
122  if ($resourceclass != 'Dolresource') {
123  $label .= ' ('.$langs->trans($resourceclass).')';
124  }
125 
126  // Test if entry is the first element of $selected.
127  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))) {
128  $out .= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
129  } else {
130  $out .= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
131  }
132 
133  array_push($outarray, array('key'=>$resourcestat->lines[$i]->id, 'value'=>$resourcestat->lines[$i]->id, 'label'=>$label));
134 
135  $i++;
136  if (($i % 10) == 0) {
137  $out .= "\n";
138  }
139  }
140  }
141  $out .= '</select>'."\n";
142 
143  if (!empty($conf->use_javascript_ajax) && !empty($conf->global->RESOURCE_USE_SEARCH_TO_SELECT) && !$forcecombo) {
144  //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
145  $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
146  } else {
147  $out .= ajax_combobox($htmlname);
148  }
149 
150  if ($outputmode != 2) {
151  $out .= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
152 
153  $out .= '</form>';
154  }
155  } else {
156  dol_print_error($this->db);
157  }
158 
159  if ($outputmode && $outputmode != 2) {
160  return $outarray;
161  }
162  return $out;
163  }
164 
165  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
178  public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0)
179  {
180  // phpcs:enable
181  global $langs, $user;
182 
183  $resourcestat = new Dolresource($this->db);
184 
185  dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG);
186 
187  $filterarray = array();
188 
189  if ($filtertype != '' && $filtertype != '-1') {
190  $filterarray = explode(',', $filtertype);
191  }
192 
193  $resourcestat->load_cache_code_type_resource();
194  print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.'" name="'.$htmlname.'">';
195  if ($empty) {
196  print '<option value="">&nbsp;</option>';
197  }
198  if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
199  foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
200  // We discard empty line if showempty is on because an empty line has already been output.
201  if ($empty && empty($arraytypes['code'])) {
202  continue;
203  }
204 
205  if ($format == 0) {
206  print '<option value="'.$id.'"';
207  } elseif ($format == 1) {
208  print '<option value="'.$arraytypes['code'].'"';
209  } elseif ($format == 2) {
210  print '<option value="'.$arraytypes['code'].'"';
211  } elseif ($format == 3) {
212  print '<option value="'.$id.'"';
213  }
214  // Si selected est text, on compare avec code, sinon avec id
215  if (preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) {
216  print ' selected';
217  } elseif ($selected == $id) {
218  print ' selected';
219  }
220  print '>';
221  if ($format == 0) {
222  $value = ($maxlength ?dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
223  } elseif ($format == 1) {
224  $value = $arraytypes['code'];
225  } elseif ($format == 2) {
226  $value = ($maxlength ?dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
227  } elseif ($format == 3) {
228  $value = $arraytypes['code'];
229  }
230  if (empty($value)) {
231  $value = '&nbsp;';
232  }
233  print $value;
234  print '</option>';
235  }
236  }
237  print '</select>';
238  if ($user->admin && !$noadmininfo) {
239  print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
240  }
241  }
242 }
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:449
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)
Return html list of tickets type.
__construct($db)
Constructor.
select_resource_list($selected='', $htmlname='fk_resource', $filter='', $showempty=0, $showtype=0, $forcecombo=0, $event=array(), $filterkey='', $outputmode=0, $limit=20, $morecss='', $multiple=false)
Output html form to select a resource.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41