html.formbarcode.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /* Copyright (C) 2007-2012 Regis Houssin <regis.houssin@inodbox.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 (!empty($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. if (!empty($conf->use_javascript_ajax))
  79. {
  80. $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="POST" id="form'.$idForm.'">';
  81. $select_encoder.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  82. $select_encoder.= '<input type="hidden" name="action" value="update">';
  83. $select_encoder.= '<input type="hidden" name="code_id" value="'.$code_id.'">';
  84. }
  85. $selectname=(!empty($conf->use_javascript_ajax)?'coder':'coder'.$code_id);
  86. $select_encoder.= '<select id="select'.$idForm.'" class="flat" name="'.$selectname.'">';
  87. $select_encoder.= '<option value="0"'.($selected==0?' selected':'').' '.$disable.'>'.$langs->trans('Disable').'</option>';
  88. $select_encoder.= '<option value="-1" disabled>--------------------</option>';
  89. foreach($barcodelist as $key => $value)
  90. {
  91. $select_encoder.= '<option value="'.$key.'"'.($selected==$key?' selected':'').'>'.$value.'</option>';
  92. }
  93. $select_encoder.= '</select>';
  94. if (!empty($conf->use_javascript_ajax))
  95. {
  96. $select_encoder.= '</form>';
  97. }
  98. return $select_encoder;
  99. }
  100. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  101. /**
  102. * Print form to select type of barcode
  103. *
  104. * @param int $selected Id code pre-selected
  105. * @param string $htmlname Name of HTML select field
  106. * @param int $useempty Affiche valeur vide dans liste
  107. * @return void
  108. * @deprecated
  109. */
  110. function select_barcode_type($selected='', $htmlname='barcodetype_id', $useempty=0)
  111. {
  112. // phpcs:enable
  113. print $this->selectBarcodeType($selected, $htmlname, $useempty);
  114. }
  115. /**
  116. * Return html form to select type of barcode
  117. *
  118. * @param int $selected Id code pre-selected
  119. * @param string $htmlname Name of HTML select field
  120. * @param int $useempty Display empty value in select
  121. * @return string
  122. */
  123. function selectBarcodeType($selected='', $htmlname='barcodetype_id', $useempty=0)
  124. {
  125. global $langs, $conf;
  126. $out = '';
  127. $sql = "SELECT rowid, code, libelle";
  128. $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
  129. $sql.= " WHERE coder <> '0'";
  130. $sql.= " AND entity = ".$conf->entity;
  131. $sql.= " ORDER BY code";
  132. $result = $this->db->query($sql);
  133. if ($result) {
  134. $num = $this->db->num_rows($result);
  135. $i = 0;
  136. if ($useempty && $num > 0) {
  137. $out .= '<select class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
  138. $out .= '<option value="0">&nbsp;</option>';
  139. } else {
  140. $langs->load("errors");
  141. $out .= '<select disabled class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
  142. $out .= '<option value="0" selected>' . $langs->trans('ErrorNoActivatedBarcode') . '</option>';
  143. }
  144. while ($i < $num) {
  145. $obj = $this->db->fetch_object($result);
  146. if ($selected == $obj->rowid) {
  147. $out .= '<option value="' . $obj->rowid . '" selected>';
  148. } else {
  149. $out .= '<option value="' . $obj->rowid . '">';
  150. }
  151. $out .= $obj->libelle;
  152. $out .= '</option>';
  153. $i++;
  154. }
  155. $out .= "</select>";
  156. $out .= ajax_combobox("select_".$htmlname);
  157. }
  158. else {
  159. dol_print_error($this->db);
  160. }
  161. return $out;
  162. }
  163. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  164. /**
  165. * Show form to select type of barcode
  166. *
  167. * @param string $page Page
  168. * @param int $selected Id condition preselected
  169. * @param string $htmlname Nom du formulaire select
  170. * @return void
  171. * @deprecated
  172. */
  173. function form_barcode_type($page, $selected='', $htmlname='barcodetype_id')
  174. {
  175. // phpcs:enable
  176. print $this->formBarcodeType($page, $selected, $htmlname);
  177. }
  178. /**
  179. * Return html form to select type of barcode
  180. *
  181. * @param string $page Page
  182. * @param int $selected Id condition preselected
  183. * @param string $htmlname Nom du formulaire select
  184. * @return string
  185. */
  186. function formBarcodeType($page, $selected='', $htmlname='barcodetype_id')
  187. {
  188. global $langs, $conf;
  189. $out = '';
  190. if ($htmlname != "none") {
  191. $out .= '<form method="post" action="' . $page . '">';
  192. $out .= '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  193. $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
  194. $out .= '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  195. $out .= '<tr><td>';
  196. $out .= $this->selectBarcodeType($selected, $htmlname, 1);
  197. $out .= '</td>';
  198. $out .= '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '">';
  199. $out .= '</td></tr></table></form>';
  200. }
  201. return $out;
  202. }
  203. }