dolibarr 21.0.0-alpha
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-2024 Frédéric France <frederic.france@free.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
31{
35 public $db;
36
40 public $error = '';
41
42
48 public function __construct($db)
49 {
50 $this->db = $db;
51 }
52
53
63 public 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 print "\n".'<script nonce="'.getNonce().'" type="text/javascript">';
71 print 'jQuery(document).ready(function () {
72 jQuery("#select'.$idForm.'").change(function() {
73 var formName = document.getElementById("form'.$idForm.'");
74 formName.action.value="setcoder";
75 formName.submit();
76 });
77 });';
78 print '</script>'."\n";
79 //onChange="barcode_coder_save(\''.$idForm.'\')
80 }
81
82 // We check if barcode is already selected by default
83 if (((isModEnabled("product") || isModEnabled("service")) && getDolGlobalString('PRODUIT_DEFAULT_BARCODE_TYPE') == $code_id) ||
84 (isModEnabled("societe") && getDolGlobalString('GENBARCODE_BARCODETYPE_THIRDPARTY') == $code_id)) {
85 $disable = 'disabled';
86 }
87
88 if (!empty($conf->use_javascript_ajax)) {
89 $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="POST" id="form'.$idForm.'">';
90 $select_encoder .= '<input type="hidden" name="token" value="'.newToken().'">';
91 $select_encoder .= '<input type="hidden" name="action" value="update">';
92 $select_encoder .= '<input type="hidden" name="code_id" value="'.$code_id.'">';
93 }
94
95 $selectname = (!empty($conf->use_javascript_ajax) ? 'coder' : 'coder'.$code_id);
96 $select_encoder .= '<select id="select'.$idForm.'" class="flat" name="'.$selectname.'">';
97 $select_encoder .= '<option value="0"'.($selected == 0 ? ' selected' : '').' '.$disable.'>'.$langs->trans('Disable').'</option>';
98 $select_encoder .= '<option value="-1" disabled>--------------------</option>';
99 foreach ($barcodelist as $key => $value) {
100 $select_encoder .= '<option value="'.$key.'"'.($selected == $key ? ' selected' : '').'>'.$value.'</option>';
101 }
102 $select_encoder .= '</select>';
103
104 if (!empty($conf->use_javascript_ajax)) {
105 $select_encoder .= '</form>';
106 }
107
108 return $select_encoder;
109 }
110
111 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
121 public function select_barcode_type($selected = 0, $htmlname = 'barcodetype_id', $useempty = 0)
122 {
123 // phpcs:enable
124 print $this->selectBarcodeType($selected, $htmlname, $useempty);
125 }
126
135 public function selectBarcodeType($selected = 0, $htmlname = 'barcodetype_id', $useempty = 0)
136 {
137 global $langs, $conf;
138
139 $out = '';
140
141 $sql = "SELECT rowid, code, libelle as label";
142 $sql .= " FROM ".$this->db->prefix()."c_barcode_type";
143 $sql .= " WHERE coder <> '0'";
144 $sql .= " AND entity = ".$conf->entity;
145 $sql .= " ORDER BY code";
146
147 $result = $this->db->query($sql);
148 if ($result) {
149 $num = $this->db->num_rows($result);
150 $i = 0;
151
152 if ($useempty && $num > 0) {
153 $out .= '<select class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
154 $out .= '<option value="0">&nbsp;</option>';
155 } else {
156 $langs->load("errors");
157 $out .= '<select disabled class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
158 $out .= '<option value="0" selected>'.$langs->trans('ErrorNoActivatedBarcode').'</option>';
159 }
160
161 while ($i < $num) {
162 $obj = $this->db->fetch_object($result);
163 if ($selected == $obj->rowid) {
164 $out .= '<option value="'.$obj->rowid.'" selected>';
165 } else {
166 $out .= '<option value="'.$obj->rowid.'">';
167 }
168 $out .= $obj->label;
169 $out .= '</option>';
170 $i++;
171 }
172 $out .= "</select>";
173 $out .= ajax_combobox("select_".$htmlname);
174 } else {
175 dol_print_error($this->db);
176 }
177 return $out;
178 }
179
180 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
190 public function form_barcode_type($page, $selected = 0, $htmlname = 'barcodetype_id')
191 {
192 // phpcs:enable
193 print $this->formBarcodeType($page, $selected, $htmlname);
194 }
195
204 public function formBarcodeType($page, $selected = 0, $htmlname = 'barcodetype_id')
205 {
206 global $langs, $conf;
207 $out = '';
208 if ($htmlname != "none") {
209 $out .= '<form method="post" action="'.$page.'">';
210 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
211 $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
212 $out .= '<table class="nobordernopadding">';
213 $out .= '<tr><td>';
214 $out .= $this->selectBarcodeType($selected, $htmlname, 1);
215 $out .= '</td>';
216 $out .= '<td class="left"><input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'">';
217 $out .= '</td></tr></table></form>';
218 }
219 return $out;
220 }
221}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:456
Class to manage barcode HTML.
__construct($db)
Constructor.
setBarcodeEncoder($selected, $barcodelist, $code_id, $idForm='formbarcode')
Return HTML select with list of bar code generators.
selectBarcodeType($selected=0, $htmlname='barcodetype_id', $useempty=0)
Return html form to select type of barcode.
formBarcodeType($page, $selected=0, $htmlname='barcodetype_id')
Return html form to select type of barcode.
select_barcode_type($selected=0, $htmlname='barcodetype_id', $useempty=0)
Print form to select type of barcode.
form_barcode_type($page, $selected=0, $htmlname='barcodetype_id')
Show form to select type of barcode.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getNonce()
Return a random string to be used as a nonce value for js.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.