html.formbarcode.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <https://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. public 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. public function setBarcodeEncoder($selected, $barcodelist, $code_id, $idForm = 'formbarcode')
  56. {
  57. global $conf, $langs;
  58. $disable = '';
  59. if (!empty($conf->use_javascript_ajax)) {
  60. print "\n".'<script type="text/javascript">';
  61. print 'jQuery(document).ready(function () {
  62. jQuery("#select'.$idForm.'").change(function() {
  63. var formName = document.getElementById("form'.$idForm.'");
  64. formName.action.value="setcoder";
  65. formName.submit();
  66. });
  67. });';
  68. print '</script>'."\n";
  69. //onChange="barcode_coder_save(\''.$idForm.'\')
  70. }
  71. // We check if barcode is already selected by default
  72. if (((isModEnabled("product") || isModEnabled("service")) && $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE == $code_id) ||
  73. (isModEnabled("societe") && $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY == $code_id)) {
  74. $disable = 'disabled';
  75. }
  76. if (!empty($conf->use_javascript_ajax)) {
  77. $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="POST" id="form'.$idForm.'">';
  78. $select_encoder .= '<input type="hidden" name="token" value="'.newToken().'">';
  79. $select_encoder .= '<input type="hidden" name="action" value="update">';
  80. $select_encoder .= '<input type="hidden" name="code_id" value="'.$code_id.'">';
  81. }
  82. $selectname = (!empty($conf->use_javascript_ajax) ? 'coder' : 'coder'.$code_id);
  83. $select_encoder .= '<select id="select'.$idForm.'" class="flat" name="'.$selectname.'">';
  84. $select_encoder .= '<option value="0"'.($selected == 0 ? ' selected' : '').' '.$disable.'>'.$langs->trans('Disable').'</option>';
  85. $select_encoder .= '<option value="-1" disabled>--------------------</option>';
  86. foreach ($barcodelist as $key => $value) {
  87. $select_encoder .= '<option value="'.$key.'"'.($selected == $key ? ' selected' : '').'>'.$value.'</option>';
  88. }
  89. $select_encoder .= '</select>';
  90. if (!empty($conf->use_javascript_ajax)) {
  91. $select_encoder .= '</form>';
  92. }
  93. return $select_encoder;
  94. }
  95. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  96. /**
  97. * Print form to select type of barcode
  98. *
  99. * @param int $selected Id code pre-selected
  100. * @param string $htmlname Name of HTML select field
  101. * @param int $useempty Affiche valeur vide dans liste
  102. * @return void
  103. * @deprecated
  104. */
  105. public function select_barcode_type($selected = '', $htmlname = 'barcodetype_id', $useempty = 0)
  106. {
  107. // phpcs:enable
  108. print $this->selectBarcodeType($selected, $htmlname, $useempty);
  109. }
  110. /**
  111. * Return html form to select type of barcode
  112. *
  113. * @param int $selected Id code pre-selected
  114. * @param string $htmlname Name of HTML select field
  115. * @param int $useempty Display empty value in select
  116. * @return string
  117. */
  118. public function selectBarcodeType($selected = '', $htmlname = 'barcodetype_id', $useempty = 0)
  119. {
  120. global $langs, $conf;
  121. $out = '';
  122. $sql = "SELECT rowid, code, libelle";
  123. $sql .= " FROM ".$this->db->prefix()."c_barcode_type";
  124. $sql .= " WHERE coder <> '0'";
  125. $sql .= " AND entity = ".$conf->entity;
  126. $sql .= " ORDER BY code";
  127. $result = $this->db->query($sql);
  128. if ($result) {
  129. $num = $this->db->num_rows($result);
  130. $i = 0;
  131. if ($useempty && $num > 0) {
  132. $out .= '<select class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
  133. $out .= '<option value="0">&nbsp;</option>';
  134. } else {
  135. $langs->load("errors");
  136. $out .= '<select disabled class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
  137. $out .= '<option value="0" selected>'.$langs->trans('ErrorNoActivatedBarcode').'</option>';
  138. }
  139. while ($i < $num) {
  140. $obj = $this->db->fetch_object($result);
  141. if ($selected == $obj->rowid) {
  142. $out .= '<option value="'.$obj->rowid.'" selected>';
  143. } else {
  144. $out .= '<option value="'.$obj->rowid.'">';
  145. }
  146. $out .= $obj->libelle;
  147. $out .= '</option>';
  148. $i++;
  149. }
  150. $out .= "</select>";
  151. $out .= ajax_combobox("select_".$htmlname);
  152. } else {
  153. dol_print_error($this->db);
  154. }
  155. return $out;
  156. }
  157. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  158. /**
  159. * Show form to select type of barcode
  160. *
  161. * @param string $page Page
  162. * @param int $selected Id condition preselected
  163. * @param string $htmlname Nom du formulaire select
  164. * @return void
  165. * @deprecated
  166. */
  167. public function form_barcode_type($page, $selected = '', $htmlname = 'barcodetype_id')
  168. {
  169. // phpcs:enable
  170. print $this->formBarcodeType($page, $selected, $htmlname);
  171. }
  172. /**
  173. * Return html form to select type of barcode
  174. *
  175. * @param string $page Page
  176. * @param int $selected Id condition preselected
  177. * @param string $htmlname Nom du formulaire select
  178. * @return string
  179. */
  180. public function formBarcodeType($page, $selected = '', $htmlname = 'barcodetype_id')
  181. {
  182. global $langs, $conf;
  183. $out = '';
  184. if ($htmlname != "none") {
  185. $out .= '<form method="post" action="'.$page.'">';
  186. $out .= '<input type="hidden" name="token" value="'.newToken().'">';
  187. $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
  188. $out .= '<table class="nobordernopadding">';
  189. $out .= '<tr><td>';
  190. $out .= $this->selectBarcodeType($selected, $htmlname, 1);
  191. $out .= '</td>';
  192. $out .= '<td class="left"><input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'">';
  193. $out .= '</td></tr></table></form>';
  194. }
  195. return $out;
  196. }
  197. }