html.formresource.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /* Copyright (C) - 2013-2015 Jean-François FERRY <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file resource/class/html.formresource.class.php
  20. * \ingroup core
  21. * \brief Class file to manage forms into resource module
  22. */
  23. require_once DOL_DOCUMENT_ROOT ."/core/class/html.form.class.php";
  24. require_once DOL_DOCUMENT_ROOT ."/resource/class/dolresource.class.php";
  25. /**
  26. * Classe permettant la gestion des formulaire du module resource
  27. *
  28. * \remarks Utilisation: $formresource = new FormResource($db)
  29. * \remarks $formplace->proprietes=1 ou chaine ou tableau de valeurs
  30. */
  31. class FormResource
  32. {
  33. /**
  34. * @var DoliDB Database handler.
  35. */
  36. public $db;
  37. var $substit=array();
  38. var $param=array();
  39. /**
  40. * @var string Error code (or message)
  41. */
  42. public $error='';
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. */
  48. function __construct($db)
  49. {
  50. $this->db = $db;
  51. }
  52. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  53. /**
  54. * Output html form to select a resource
  55. *
  56. * @param int $selected Preselected resource id
  57. * @param string $htmlname Name of field in form
  58. * @param string $filter Optionnal filters criteras (example: 's.rowid <> x')
  59. * @param int $showempty Add an empty field
  60. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  61. * @param int $forcecombo Force to use combo box
  62. * @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')))
  63. * @param string $filterkey Filter on key value
  64. * @param int $outputmode 0=HTML select string, 1=Array, 2=without form tag
  65. * @param int $limit Limit number of answers
  66. * @return string HTML string with
  67. */
  68. function select_resource_list($selected='',$htmlname='fk_resource',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array(), $filterkey='', $outputmode=0, $limit=20)
  69. {
  70. // phpcs:enable
  71. global $conf,$user,$langs;
  72. $out='';
  73. $outarray=array();
  74. $resourcestat = new Dolresource($this->db);
  75. $resources_used = $resourcestat->fetch_all('ASC', 't.rowid', $limit, $offset, $filter='');
  76. if ($outputmode != 2)
  77. {
  78. $out = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  79. $out.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  80. }
  81. //$out.= '<input type="hidden" name="action" value="search">';
  82. //$out.= '<input type="hidden" name="id" value="'.$theme->id.'">';
  83. if ($resourcestat)
  84. {
  85. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->RESOURCE_USE_SEARCH_TO_SELECT) && ! $forcecombo)
  86. {
  87. //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
  88. $out.= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
  89. }
  90. // Construct $out and $outarray
  91. $out.= '<select id="'.$htmlname.'" class="flat minwidth200" name="'.$htmlname.'">'."\n";
  92. if ($showempty) $out.= '<option value="-1">&nbsp;</option>'."\n";
  93. $num = count($resourcestat->lines);
  94. //var_dump($resourcestat->lines);
  95. $i = 0;
  96. if ($num)
  97. {
  98. while ( $i < $num)
  99. {
  100. $resourceclass=ucfirst($resourcestat->lines[$i]->element);
  101. $label=$resourcestat->lines[$i]->ref?$resourcestat->lines[$i]->ref:''.$resourcestat->lines[$i]->label;
  102. if ($resourceclass != 'Dolresource') $label.=' ('.$langs->trans($resourceclass).')';
  103. if ($selected > 0 && $selected == $resourcestat->lines[$i]->id)
  104. {
  105. $out.= '<option value="'.$resourcestat->lines[$i]->id.'" selected>'.$label.'</option>';
  106. }
  107. else
  108. {
  109. $out.= '<option value="'.$resourcestat->lines[$i]->id.'">'.$label.'</option>';
  110. }
  111. array_push($outarray, array('key'=>$resourcestat->lines[$i]->id, 'value'=>$resourcestat->lines[$i]->label, 'label'=>$resourcestat->lines[$i]->label));
  112. $i++;
  113. if (($i % 10) == 0) $out.="\n";
  114. }
  115. }
  116. $out.= '</select>'."\n";
  117. $out.= ajax_combobox($htmlname);
  118. if ($outputmode != 2)
  119. {
  120. $out.= '<input type="submit" class="button" value="'.$langs->trans("Search").'"> &nbsp; &nbsp; ';
  121. $out.= '</form>';
  122. }
  123. }
  124. else
  125. {
  126. dol_print_error($this->db);
  127. }
  128. if ($outputmode && $outputmode != 2) return $outarray;
  129. return $out;
  130. }
  131. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  132. /**
  133. * Return html list of tickets type
  134. *
  135. * @param string $selected Id du type pre-selectionne
  136. * @param string $htmlname Nom de la zone select
  137. * @param string $filtertype To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz))
  138. * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code
  139. * @param int $empty 1=peut etre vide, 0 sinon
  140. * @param int $noadmininfo 0=Add admin info, 1=Disable admin info
  141. * @param int $maxlength Max length of label
  142. * @return void
  143. */
  144. function select_types_resource($selected='',$htmlname='type_resource',$filtertype='',$format=0, $empty=0, $noadmininfo=0,$maxlength=0)
  145. {
  146. // phpcs:enable
  147. global $langs,$user;
  148. $resourcestat = new Dolresource($this->db);
  149. dol_syslog(get_class($this)."::select_types_resource ".$selected.", ".$htmlname.", ".$filtertype.", ".$format,LOG_DEBUG);
  150. $filterarray=array();
  151. if ($filtertype != '' && $filtertype != '-1') $filterarray=explode(',',$filtertype);
  152. $resourcestat->load_cache_code_type_resource();
  153. print '<select id="select'.$htmlname.'" class="flat maxwidthonsmartphone select_'.$htmlname.'" name="'.$htmlname.'">';
  154. if ($empty) print '<option value="">&nbsp;</option>';
  155. if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource))
  156. {
  157. foreach($resourcestat->cache_code_type_resource as $id => $arraytypes)
  158. {
  159. // We discard empty line if showempty is on because an empty line has already been output.
  160. if ($empty && empty($arraytypes['code'])) continue;
  161. if ($format == 0) print '<option value="'.$id.'"';
  162. if ($format == 1) print '<option value="'.$arraytypes['code'].'"';
  163. if ($format == 2) print '<option value="'.$arraytypes['code'].'"';
  164. if ($format == 3) print '<option value="'.$id.'"';
  165. // Si selected est text, on compare avec code, sinon avec id
  166. if (preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) print ' selected';
  167. elseif ($selected == $id) print ' selected';
  168. print '>';
  169. if ($format == 0) $value=($maxlength?dol_trunc($arraytypes['label'],$maxlength):$arraytypes['label']);
  170. if ($format == 1) $value=$arraytypes['code'];
  171. if ($format == 2) $value=($maxlength?dol_trunc($arraytypes['label'],$maxlength):$arraytypes['label']);
  172. if ($format == 3) $value=$arraytypes['code'];
  173. print $value?$value:'&nbsp;';
  174. print '</option>';
  175. }
  176. }
  177. print '</select>';
  178. if ($user->admin && ! $noadmininfo) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  179. }
  180. }