html.formbarcode.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* Copyright (C) 2007-2012 Regis Houssin <regis.houssin@capnetworks.com>
  3. * Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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 <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. /**
  21. * \file htdocs/core/class/html.formbarcode.class.php
  22. * \brief Fichier de la classe des fonctions predefinie de composants html
  23. */
  24. /**
  25. * Class to manage barcode HTML
  26. */
  27. class FormBarCode
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error='';
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db)
  43. {
  44. $this->db = $db;
  45. }
  46. /**
  47. * Return HTML select with list of bar code generators
  48. *
  49. * @param int $selected Id code pre-selected
  50. * @param array $barcodelist Array of barcodes generators
  51. * @param int $code_id Id du code barre
  52. * @param int $idForm Id du formulaire
  53. * @return string HTML select string
  54. */
  55. function setBarcodeEncoder($selected,$barcodelist,$code_id,$idForm='formbarcode')
  56. {
  57. global $conf, $langs;
  58. $disable = '';
  59. if ($conf->use_javascript_ajax)
  60. {
  61. print "\n".'<script type="text/javascript" language="javascript">';
  62. print 'jQuery(document).ready(function () {
  63. jQuery("#select'.$idForm.'").change(function() {
  64. var formName = document.getElementById("form'.$idForm.'");
  65. formName.action.value="setcoder";
  66. formName.submit();
  67. });
  68. });';
  69. print '</script>'."\n";
  70. //onChange="barcode_coder_save(\''.$idForm.'\')
  71. }
  72. // We check if barcode is already selected by default
  73. if (((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE == $code_id) ||
  74. (! empty($conf->societe->enabled) && $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY == $code_id))
  75. {
  76. $disable = 'disabled';
  77. }
  78. $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="post" id="form'.$idForm.'">';
  79. $select_encoder.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  80. $select_encoder.= '<input type="hidden" name="action" value="update">';
  81. $select_encoder.= '<input type="hidden" name="code_id" value="'.$code_id.'">';
  82. $select_encoder.= '<select id="select'.$idForm.'" class="flat" name="coder">';
  83. $select_encoder.= '<option value="0"'.($selected==0?' selected':'').' '.$disable.'>'.$langs->trans('Disable').'</option>';
  84. $select_encoder.= '<option value="-1" disabled>--------------------</option>';
  85. foreach($barcodelist as $key => $value)
  86. {
  87. $select_encoder.= '<option value="'.$key.'"'.($selected==$key?' selected':'').'>'.$value.'</option>';
  88. }
  89. $select_encoder.= '</select></form>';
  90. return $select_encoder;
  91. }
  92. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  93. /**
  94. * Print form to select type of barcode
  95. *
  96. * @param int $selected Id code pre-selected
  97. * @param string $htmlname Name of HTML select field
  98. * @param int $useempty Affiche valeur vide dans liste
  99. * @return void
  100. * @deprecated
  101. */
  102. function select_barcode_type($selected='', $htmlname='barcodetype_id', $useempty=0)
  103. {
  104. // phpcs:enable
  105. print $this->selectBarcodeType($selected, $htmlname, $useempty);
  106. }
  107. /**
  108. * Return html form to select type of barcode
  109. *
  110. * @param int $selected Id code pre-selected
  111. * @param string $htmlname Name of HTML select field
  112. * @param int $useempty Display empty value in select
  113. * @return string
  114. */
  115. function selectBarcodeType($selected='', $htmlname='barcodetype_id', $useempty=0)
  116. {
  117. global $langs, $conf;
  118. $out = '';
  119. $sql = "SELECT rowid, code, libelle";
  120. $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
  121. $sql.= " WHERE coder <> '0'";
  122. $sql.= " AND entity = ".$conf->entity;
  123. $sql.= " ORDER BY code";
  124. $result = $this->db->query($sql);
  125. if ($result) {
  126. $num = $this->db->num_rows($result);
  127. $i = 0;
  128. if ($useempty && $num > 0) {
  129. $out .= '<select class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
  130. $out .= '<option value="0">&nbsp;</option>';
  131. } else {
  132. $langs->load("errors");
  133. $out .= '<select disabled class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
  134. $out .= '<option value="0" selected>' . $langs->trans('ErrorNoActivatedBarcode') . '</option>';
  135. }
  136. while ($i < $num) {
  137. $obj = $this->db->fetch_object($result);
  138. if ($selected == $obj->rowid) {
  139. $out .= '<option value="' . $obj->rowid . '" selected>';
  140. } else {
  141. $out .= '<option value="' . $obj->rowid . '">';
  142. }
  143. $out .= $obj->libelle;
  144. $out .= '</option>';
  145. $i++;
  146. }
  147. $out .= "</select>";
  148. $out .= ajax_combobox("select_".$htmlname);
  149. }
  150. else {
  151. dol_print_error($this->db);
  152. }
  153. return $out;
  154. }
  155. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  156. /**
  157. * Show form to select type of barcode
  158. *
  159. * @param string $page Page
  160. * @param int $selected Id condition preselected
  161. * @param string $htmlname Nom du formulaire select
  162. * @return void
  163. * @deprecated
  164. */
  165. function form_barcode_type($page, $selected='', $htmlname='barcodetype_id')
  166. {
  167. // phpcs:enable
  168. print $this->formBarcodeType($page, $selected, $htmlname);
  169. }
  170. /**
  171. * Return html form to select type of barcode
  172. *
  173. * @param string $page Page
  174. * @param int $selected Id condition preselected
  175. * @param string $htmlname Nom du formulaire select
  176. * @return string
  177. */
  178. function formBarcodeType($page, $selected='', $htmlname='barcodetype_id')
  179. {
  180. global $langs, $conf;
  181. $out = '';
  182. if ($htmlname != "none") {
  183. $out .= '<form method="post" action="' . $page . '">';
  184. $out .= '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  185. $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
  186. $out .= '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  187. $out .= '<tr><td>';
  188. $out .= $this->selectBarcodeType($selected, $htmlname, 1);
  189. $out .= '</td>';
  190. $out .= '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '">';
  191. $out .= '</td></tr></table></form>';
  192. }
  193. return $out;
  194. }
  195. }