dolibarr 24.0.0-beta
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-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21
32{
36 public $db;
37
41 public $error = '';
42
43
49 public function __construct($db)
50 {
51 $this->db = $db;
52 }
53
54
64 public function setBarcodeEncoder($selected, $barcodelist, $code_id, $idForm = 'formbarcode')
65 {
66 global $conf, $langs;
67
68 $disable = '';
69
70 if (!empty($conf->use_javascript_ajax)) {
71 print "\n".'<script nonce="'.getNonce().'" type="text/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 (((isModEnabled("product") || isModEnabled("service")) && getDolGlobalString('PRODUIT_DEFAULT_BARCODE_TYPE') == $code_id) ||
85 (isModEnabled("societe") && getDolGlobalString('GENBARCODE_BARCODETYPE_THIRDPARTY') == $code_id)) {
86 $disable = 'disabled';
87 }
88
89 $select_encoder = '';
90 if (!empty($conf->use_javascript_ajax)) {
91 $select_encoder = '<form action="'.DOL_URL_ROOT.'/admin/barcode.php" method="POST" id="form'.$idForm.'">';
92 $select_encoder .= '<input type="hidden" name="token" value="'.newToken().'">';
93 $select_encoder .= '<input type="hidden" name="action" value="update">';
94 $select_encoder .= '<input type="hidden" name="code_id" value="'.$code_id.'">';
95 }
96
97 $selectname = (!empty($conf->use_javascript_ajax) ? 'coder' : 'coder'.$code_id);
98 $select_encoder .= '<select id="select'.$idForm.'" class="flat" name="'.$selectname.'">';
99 $select_encoder .= '<option value="0"'.($selected == 0 ? ' selected' : '').' '.$disable.'>'.$langs->trans('Disable').'</option>';
100 $select_encoder .= '<option value="-1" disabled>--------------------</option>';
101 foreach ($barcodelist as $key => $value) {
102 $select_encoder .= '<option value="'.$key.'"'.($selected == $key ? ' selected' : '').'>'.$value.'</option>';
103 }
104 $select_encoder .= '</select>';
105
106 if (!empty($conf->use_javascript_ajax)) {
107 $select_encoder .= '</form>';
108 }
109
110 return $select_encoder;
111 }
112
113 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
123 public function select_barcode_type($selected = 0, $htmlname = 'barcodetype_id', $useempty = 0)
124 {
125 // phpcs:enable
126 print $this->selectBarcodeType($selected, $htmlname, $useempty);
127 }
128
137 public function selectBarcodeType($selected = 0, $htmlname = 'barcodetype_id', $useempty = 0)
138 {
139 global $langs, $conf;
140
141 $out = '';
142
143 $sql = "SELECT rowid, code, libelle as label";
144 $sql .= " FROM ".$this->db->prefix()."c_barcode_type";
145 $sql .= " WHERE coder <> '0'";
146 $sql .= " AND entity = ".$conf->entity;
147 $sql .= " ORDER BY code";
148
149 $result = $this->db->query($sql);
150 if ($result) {
151 $num = $this->db->num_rows($result);
152 $i = 0;
153
154 if ($useempty && $num > 0) {
155 $out .= '<select class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
156 $out .= '<option value="0">&nbsp;</option>';
157 } else {
158 $langs->load("errors");
159 $out .= '<select disabled class="flat minwidth75imp" name="'.$htmlname.'" id="select_'.$htmlname.'">';
160 $out .= '<option value="0" selected>'.$langs->trans('ErrorNoActivatedBarcode').'</option>';
161 }
162
163 while ($i < $num) {
164 $obj = $this->db->fetch_object($result);
165 if ($selected == $obj->rowid) {
166 $out .= '<option value="'.$obj->rowid.'" selected>';
167 } else {
168 $out .= '<option value="'.$obj->rowid.'">';
169 }
170 $out .= $obj->label;
171 $out .= '</option>';
172 $i++;
173 }
174 $out .= "</select>";
175 $out .= ajax_combobox("select_".$htmlname);
176 } else {
177 dol_print_error($this->db);
178 }
179 return $out;
180 }
181
182 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
192 public function form_barcode_type($page, $selected = 0, $htmlname = 'barcodetype_id')
193 {
194 // phpcs:enable
195 print $this->formBarcodeType($page, $selected, $htmlname);
196 }
197
206 public function formBarcodeType($page, $selected = 0, $htmlname = 'barcodetype_id')
207 {
208 global $langs, $conf;
209 $out = '';
210 if ($htmlname != "none") {
211 $out .= '<form method="post" action="'.$page.'">';
212 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
213 $out .= '<input type="hidden" name="action" value="set'.$htmlname.'">';
214 $out .= '<table class="nobordernopadding">';
215 $out .= '<tr><td>';
216 $out .= $this->selectBarcodeType($selected, $htmlname, 1);
217 $out .= '</td>';
218 $out .= '<td class="left"><input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'">';
219 $out .= '</td></tr></table></form>';
220 }
221 return $out;
222 }
223}
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:476
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.