dolibarr  9.0.0
html.formbarcode.class.php
Go to the documentation of this file.
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 
31 {
35  public $db;
36 
40  public $error='';
41 
42 
48  function __construct($db)
49  {
50  $this->db = $db;
51  }
52 
53 
63  function setBarcodeEncoder($selected,$barcodelist,$code_id,$idForm='formbarcode')
64  {
65  global $conf, $langs;
66 
67  $disable = '';
68 
69  if (!empty($conf->use_javascript_ajax))
70  {
71  print "\n".'<script type="text/javascript" language="javascript">';
72  print 'jQuery(document).ready(function () {
73  jQuery("#select'.$idForm.'").change(function() {
74  var formName = document.getElementById("form'.$idForm.'");
75  formName.action.value="setcoder";
76  formName.submit();
77  });
78  });';
79  print '</script>'."\n";
80  //onChange="barcode_coder_save(\''.$idForm.'\')
81  }
82 
83  // We check if barcode is already selected by default
84  if (((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE == $code_id) ||
85  (! empty($conf->societe->enabled) && $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY == $code_id))
86  {
87  $disable = 'disabled';
88  }
89 
90  if (!empty($conf->use_javascript_ajax))
91  {
92  $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="POST" id="form'.$idForm.'">';
93  $select_encoder.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
94  $select_encoder.= '<input type="hidden" name="action" value="update">';
95  $select_encoder.= '<input type="hidden" name="code_id" value="'.$code_id.'">';
96  }
97 
98  $selectname=(!empty($conf->use_javascript_ajax)?'coder':'coder'.$code_id);
99  $select_encoder.= '<select id="select'.$idForm.'" class="flat" name="'.$selectname.'">';
100  $select_encoder.= '<option value="0"'.($selected==0?' selected':'').' '.$disable.'>'.$langs->trans('Disable').'</option>';
101  $select_encoder.= '<option value="-1" disabled>--------------------</option>';
102  foreach($barcodelist as $key => $value)
103  {
104  $select_encoder.= '<option value="'.$key.'"'.($selected==$key?' selected':'').'>'.$value.'</option>';
105  }
106  $select_encoder.= '</select>';
107 
108  if (!empty($conf->use_javascript_ajax))
109  {
110  $select_encoder.= '</form>';
111  }
112 
113  return $select_encoder;
114  }
115 
116  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
126  function select_barcode_type($selected='', $htmlname='barcodetype_id', $useempty=0)
127  {
128  // phpcs:enable
129  print $this->selectBarcodeType($selected, $htmlname, $useempty);
130  }
131 
140  function selectBarcodeType($selected='', $htmlname='barcodetype_id', $useempty=0)
141  {
142  global $langs, $conf;
143 
144  $out = '';
145 
146  $sql = "SELECT rowid, code, libelle";
147  $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
148  $sql.= " WHERE coder <> '0'";
149  $sql.= " AND entity = ".$conf->entity;
150  $sql.= " ORDER BY code";
151 
152  $result = $this->db->query($sql);
153  if ($result) {
154  $num = $this->db->num_rows($result);
155  $i = 0;
156 
157  if ($useempty && $num > 0) {
158  $out .= '<select class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
159  $out .= '<option value="0">&nbsp;</option>';
160  } else {
161  $langs->load("errors");
162  $out .= '<select disabled class="flat minwidth75imp" name="' . $htmlname . '" id="select_' . $htmlname . '">';
163  $out .= '<option value="0" selected>' . $langs->trans('ErrorNoActivatedBarcode') . '</option>';
164  }
165 
166  while ($i < $num) {
167  $obj = $this->db->fetch_object($result);
168  if ($selected == $obj->rowid) {
169  $out .= '<option value="' . $obj->rowid . '" selected>';
170  } else {
171  $out .= '<option value="' . $obj->rowid . '">';
172  }
173  $out .= $obj->libelle;
174  $out .= '</option>';
175  $i++;
176  }
177  $out .= "</select>";
178  $out .= ajax_combobox("select_".$htmlname);
179  }
180  else {
181  dol_print_error($this->db);
182  }
183  return $out;
184  }
185 
186  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
196  function form_barcode_type($page, $selected='', $htmlname='barcodetype_id')
197  {
198  // phpcs:enable
199  print $this->formBarcodeType($page, $selected, $htmlname);
200  }
201 
210  function formBarcodeType($page, $selected='', $htmlname='barcodetype_id')
211  {
212  global $langs, $conf;
213  $out = '';
214  if ($htmlname != "none") {
215  $out .= '<form method="post" action="' . $page . '">';
216  $out .= '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
217  $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
218  $out .= '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
219  $out .= '<tr><td>';
220  $out .= $this->selectBarcodeType($selected, $htmlname, 1);
221  $out .= '</td>';
222  $out .= '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '">';
223  $out .= '</td></tr></table></form>';
224  }
225  return $out;
226  }
227 }
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:374
setBarcodeEncoder($selected, $barcodelist, $code_id, $idForm='formbarcode')
Return HTML select with list of bar code generators.
print
Draft customers invoices.
Definition: index.php:91
form_barcode_type($page, $selected='', $htmlname='barcodetype_id')
Show form to select type of barcode.
select_barcode_type($selected='', $htmlname='barcodetype_id', $useempty=0)
Print form to select type of barcode.
__construct($db)
Constructor.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage barcode HTML.
selectBarcodeType($selected='', $htmlname='barcodetype_id', $useempty=0)
Return html form to select type of barcode.
formBarcodeType($page, $selected='', $htmlname='barcodetype_id')
Return html form to select type of barcode.