html.formresource.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /**
  21. * \file resource/class/html.formresource.class.php
  22. * \ingroup core
  23. * \brief Class file to manage forms into resource module
  24. */
  25. require_once DOL_DOCUMENT_ROOT."/core/class/html.form.class.php";
  26. require_once DOL_DOCUMENT_ROOT."/resource/class/dolresource.class.php";
  27. /**
  28. * Class to manage forms for the module resource
  29. *
  30. * \remarks Utilisation: $formresource = new FormResource($db)
  31. * \remarks $formplace->proprietes=1 ou chaine ou tableau de valeurs
  32. */
  33. class FormResource
  34. {
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. public $substit = array();
  40. public $param = array();
  41. /**
  42. * @var string Error code (or message)
  43. */
  44. public $error = '';
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $db Database handler
  49. */
  50. public function __construct($db)
  51. {
  52. $this->db = $db;
  53. }
  54. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  55. /**
  56. * Output html form to select a resource
  57. *
  58. * @param int $selected Preselected resource id
  59. * @param string $htmlname Name of field in form
  60. * @param string $filter Optionnal filters criteras (example: 's.rowid <> x')
  61. * @param int $showempty Add an empty field
  62. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  63. * @param int $forcecombo Force to use combo box
  64. * @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')))
  65. * @param string $filterkey Filter on key value
  66. * @param int $outputmode 0=HTML select string, 1=Array, 2=without form tag
  67. * @param int $limit Limit number of answers
  68. * @param string $morecss More css
  69. * @param bool $multiple add [] in the name of element and add 'multiple' attribut
  70. * @return string HTML string with
  71. */
  72. 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)
  73. {
  74. // phpcs:enable
  75. global $conf, $user, $langs;
  76. $out = '';
  77. $outarray = array();
  78. $resourcestat = new Dolresource($this->db);
  79. $resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter);
  80. if (!empty($selected) && !is_array($selected)) {
  81. $selected = array($selected);
  82. }
  83. if ($outputmode != 2) {
  84. $out = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  85. $out .= '<input type="hidden" name="token" value="'.newToken().'">';
  86. }
  87. if ($resourcestat) {
  88. // Construct $out and $outarray
  89. $out .= '<select id="'.$htmlname.'" class="flat minwidth100'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.($multiple ? '[]' : '').'" '.($multiple ? 'multiple' : '').'>'."\n";
  90. if ($showempty) {
  91. $out .= '<option value="-1">&nbsp;</option>'."\n";
  92. }
  93. $num = 0;
  94. if (is_array($resourcestat->lines)) {
  95. $num = count($resourcestat->lines);
  96. }
  97. //var_dump($resourcestat->lines);
  98. $i = 0;
  99. if ($num) {
  100. while ($i < $num) {
  101. $resourceclass = ucfirst($resourcestat->lines[$i]->element);
  102. $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : ''.$resourcestat->lines[$i]->label;
  103. if ($resourceclass != 'Dolresource') {
  104. $label .= ' ('.$langs->trans($resourceclass).')';
  105. }
  106. // Test if entry is the first element of $selected.
  107. 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))) {
  108. $out .= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
  109. } else {
  110. $out .= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
  111. }
  112. array_push($outarray, array('key'=>$resourcestat->lines[$i]->id, 'value'=>$resourcestat->lines[$i]->id, 'label'=>$label));
  113. $i++;
  114. if (($i % 10) == 0) {
  115. $out .= "\n";
  116. }
  117. }
  118. }
  119. $out .= '</select>'."\n";
  120. if (!empty($conf->use_javascript_ajax) && !empty($conf->global->RESOURCE_USE_SEARCH_TO_SELECT) && !$forcecombo) {
  121. //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
  122. $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
  123. } else {
  124. $out .= ajax_combobox($htmlname);
  125. }
  126. if ($outputmode != 2) {
  127. $out .= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
  128. $out .= '</form>';
  129. }
  130. } else {
  131. dol_print_error($this->db);
  132. }
  133. if ($outputmode && $outputmode != 2) {
  134. return $outarray;
  135. }
  136. return $out;
  137. }
  138. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  139. /**
  140. * Return html list of tickets type
  141. *
  142. * @param string $selected Id du type pre-selectionne
  143. * @param string $htmlname Nom de la zone select
  144. * @param string $filtertype To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz))
  145. * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code
  146. * @param int $empty 1=peut etre vide, 0 sinon
  147. * @param int $noadmininfo 0=Add admin info, 1=Disable admin info
  148. * @param int $maxlength Max length of label
  149. * @return void
  150. */
  151. public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0)
  152. {
  153. // phpcs:enable
  154. global $langs, $user;
  155. $resourcestat = new Dolresource($this->db);
  156. dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG);
  157. $filterarray = array();
  158. if ($filtertype != '' && $filtertype != '-1') {
  159. $filterarray = explode(',', $filtertype);
  160. }
  161. $resourcestat->load_cache_code_type_resource();
  162. print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.'" name="'.$htmlname.'">';
  163. if ($empty) {
  164. print '<option value="">&nbsp;</option>';
  165. }
  166. if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
  167. foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
  168. // We discard empty line if showempty is on because an empty line has already been output.
  169. if ($empty && empty($arraytypes['code'])) {
  170. continue;
  171. }
  172. if ($format == 0) {
  173. print '<option value="'.$id.'"';
  174. } elseif ($format == 1) {
  175. print '<option value="'.$arraytypes['code'].'"';
  176. } elseif ($format == 2) {
  177. print '<option value="'.$arraytypes['code'].'"';
  178. } elseif ($format == 3) {
  179. print '<option value="'.$id.'"';
  180. }
  181. // Si selected est text, on compare avec code, sinon avec id
  182. if (preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) {
  183. print ' selected';
  184. } elseif ($selected == $id) {
  185. print ' selected';
  186. }
  187. print '>';
  188. if ($format == 0) {
  189. $value = ($maxlength ?dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
  190. } elseif ($format == 1) {
  191. $value = $arraytypes['code'];
  192. } elseif ($format == 2) {
  193. $value = ($maxlength ?dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
  194. } elseif ($format == 3) {
  195. $value = $arraytypes['code'];
  196. }
  197. if (empty($value)) {
  198. $value = '&nbsp;';
  199. }
  200. print $value;
  201. print '</option>';
  202. }
  203. }
  204. print '</select>';
  205. if ($user->admin && !$noadmininfo) {
  206. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  207. }
  208. }
  209. }