1<?php
2/* Copyright (C) 2003-2004	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2015	Laurent Destailleur		<eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
5 * Copyright (C) 2011-2013	Juanjo Menent			<jmenent@2byte.es>
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/**
22 *	\file       htdocs/admin/barcode.php
23 *	\ingroup    barcode
24 *	\brief      Page to setup barcode module
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php';
30
31// Load translation files required by the page
32$langs->load("admin");
33
34if (!$user->admin) accessforbidden();
35
36$action = GETPOST('action', 'aZ09');
37
38
39/*
40 * Actions
41 */
42
43include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
44
45if ($action == 'setbarcodeproducton')
46{
47	$barcodenumberingmodule = GETPOST('value', 'alpha');
48	$res = dolibarr_set_const($db, "BARCODE_PRODUCT_ADDON_NUM", $barcodenumberingmodule, 'chaine', 0, '', $conf->entity);
49	if ($barcodenumberingmodule == 'mod_barcode_product_standard' && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK))
50	{
51		$res = dolibarr_set_const($db, "BARCODE_STANDARD_PRODUCT_MASK", '020{000000000}', 'chaine', 0, '', $conf->entity);
52	}
53} elseif ($action == 'setbarcodeproductoff')
54{
55	$res = dolibarr_del_const($db, "BARCODE_PRODUCT_ADDON_NUM", $conf->entity);
56}
57
58if ($action == 'setcoder')
59{
60	$coder = GETPOST('coder', 'alpha');
61	$code_id = GETPOST('code_id', 'int');
62	$sqlp = "UPDATE ".MAIN_DB_PREFIX."c_barcode_type";
63	$sqlp .= " SET coder = '".$db->escape($coder)."'";
64	$sqlp .= " WHERE rowid = ".((int) $code_id);
65	$sqlp .= " AND entity = ".$conf->entity;
66
67	$resql = $db->query($sqlp);
68	if (!$resql) dol_print_error($db);
69} elseif ($action == 'update')
70{
71	$location = GETPOST('GENBARCODE_LOCATION', 'alpha');
72	$res = dolibarr_set_const($db, "GENBARCODE_LOCATION", $location, 'chaine', 0, '', $conf->entity);
73	$coder_id = GETPOST('PRODUIT_DEFAULT_BARCODE_TYPE', 'alpha');
74	$res = dolibarr_set_const($db, "PRODUIT_DEFAULT_BARCODE_TYPE", $coder_id, 'chaine', 0, '', $conf->entity);
75	$coder_id = GETPOST('GENBARCODE_BARCODETYPE_THIRDPARTY', 'alpha');
76	$res = dolibarr_set_const($db, "GENBARCODE_BARCODETYPE_THIRDPARTY", $coder_id, 'chaine', 0, '', $conf->entity);
77
78	if ($res > 0)
79	{
80		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
81	} else {
82		setEventMessages($langs->trans("Error"), null, 'errors');
83	}
84} elseif ($action == 'updateengine')
85{
86	$sql = "SELECT rowid, coder";
87	$sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
88	$sql .= " WHERE entity = ".$conf->entity;
89	$sql .= " ORDER BY code";
90
91	$resql = $db->query($sql);
92	if ($resql)
93	{
94		$num = $db->num_rows($resql);
95		$i = 0;
96
97		while ($i < $num)
98		{
99			$obj = $db->fetch_object($resql);
100
101			if (GETPOST('coder'.$obj->rowid, 'alpha'))
102			{
103				$coder = GETPOST('coder'.$obj->rowid, 'alpha');
104				$code_id = $obj->rowid;
105
106				$sqlp = "UPDATE ".MAIN_DB_PREFIX."c_barcode_type";
107				$sqlp .= " SET coder = '".$db->escape($coder)."'";
108				$sqlp .= " WHERE rowid = ".((int) $code_id);
109				$sqlp .= " AND entity = ".$conf->entity;
110
111				$upsql = $db->query($sqlp);
112				if (!$upsql) dol_print_error($db);
113			}
114
115			$i++;
116		}
117	}
118}
119
120
121/*
122 * View
123 */
124
125$form = new Form($db);
126$formbarcode = new FormBarCode($db);
127
128$help_url = 'EN:Module_Barcode|FR:Module_Codes_Barre|ES:Módulo Código de barra';
129llxHeader('', $langs->trans("BarcodeSetup"), $help_url);
130
131$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
132print load_fiche_titre($langs->trans("BarcodeSetup"), $linkback, 'title_setup');
133
134// Detect bar codes modules
135$barcodelist = array();
136
137clearstatcache();
138
139
140// Scan list of all barcode included provided by external modules
141$dirbarcode = array_merge(array("/core/modules/barcode/doc/"), $conf->modules_parts['barcode']);
142
143foreach ($dirbarcode as $reldir)
144{
145	$dir = dol_buildpath($reldir);
146	$newdir = dol_osencode($dir);
147
148	// Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
149	if (!is_dir($newdir)) continue;
150
151	$handle = @opendir($newdir);
152	if (is_resource($handle))
153	{
154		while (($file = readdir($handle)) !== false)
155		{
156			if (substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS')
157			{
158				if (is_readable($newdir.$file))
159				{
160					if (preg_match('/(.*)\.modules\.php$/i', $file, $reg))
161					{
162						$filebis = $reg[1];
163
164						// Loading encoding class
165						require_once $newdir.$file;
166						$classname = "mod".ucfirst($filebis);
167						$module = new $classname($db);
168
169						// Show modules according to features level
170						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
171						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
172
173						if ($module->isEnabled())
174						{
175							$barcodelist[$filebis] = $module->info();
176						}
177					}
178				}
179			}
180		}
181	}
182}
183
184/*
185 *  CHOIX ENCODAGE
186 */
187
188print '<br>';
189print load_fiche_titre($langs->trans("BarcodeEncodeModule"), '', '');
190
191if (empty($conf->use_javascript_ajax))
192{
193	print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" id="form_engine">';
194	print '<input type="hidden" name="token" value="'.newToken().'">';
195	print '<input type="hidden" name="action" value="updateengine">';
196}
197
198print '<table class="noborder centpercent">';
199print '<tr class="liste_titre">';
200print '<td>'.$langs->trans("Name").'</td>';
201print '<td>'.$langs->trans("Description").'</td>';
202print '<td width="200" class="center">'.$langs->trans("Example").'</td>';
203print '<td class="center" width="60">'.$langs->trans("CodeBarGenerator").'</td>';
204print "</tr>\n";
205
206$sql = "SELECT rowid, code as encoding, libelle as label, coder, example";
207$sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
208$sql .= " WHERE entity = ".$conf->entity;
209$sql .= " ORDER BY code";
210
211dol_syslog("admin/barcode.php", LOG_DEBUG);
212$resql = $db->query($sql);
213if ($resql)
214{
215	$num = $db->num_rows($resql);
216	$i = 0;
217
218	while ($i < $num)
219	{
220		$obj = $db->fetch_object($resql);
221
222		print '<tr class="oddeven"><td width="100">';
223		print $obj->label;
224		print "</td><td>\n";
225		print $langs->trans('BarcodeDesc'.$obj->encoding);
226		//print "L'EAN se compose de 8 caracteres, 7 chiffres plus une cle de controle.<br>";
227		//print "L'utilisation des symbologies EAN8 impose la souscription et l'abonnement aupres d'organisme tel que GENCOD.<br>";
228		//print "Codes numeriques utilises exclusivement a l'identification des produits susceptibles d'etre vendus au grand public.";
229		print '</td>';
230
231		// Show example
232		print '<td class="center">';
233		if ($obj->coder && $obj->coder != -1)
234		{
235			$result = 0;
236
237			foreach ($dirbarcode as $reldir)
238			{
239				$dir = dol_buildpath($reldir, 0);
240				$newdir = dol_osencode($dir);
241
242				// Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
243				if (!is_dir($newdir)) continue;
244
245				$result = @include_once $newdir.$obj->coder.'.modules.php';
246				if ($result) break;
247			}
248			if ($result)
249			{
250				$classname = "mod".ucfirst($obj->coder);
251				if (class_exists($classname))
252				{
253					$module = new $classname($db);
254					if ($module->encodingIsSupported($obj->encoding))
255					{
256						// Build barcode on disk (not used, this is done to make debug easier)
257						$result = $module->writeBarCode($obj->example, $obj->encoding, 'Y');
258						// Generate on the fly and output barcode with generator
259						$url = DOL_URL_ROOT.'/viewimage.php?modulepart=barcode&amp;generator='.urlencode($obj->coder).'&amp;code='.urlencode($obj->example).'&amp;encoding='.urlencode($obj->encoding);
260						//print $url;
261						print '<img src="'.$url.'" title="'.$obj->example.'" border="0">';
262					} else {
263						print $langs->trans("FormatNotSupportedByGenerator");
264					}
265				} else {
266					print 'ErrorClassNotFoundInModule '.$classname.' '.$obj->coder;
267				}
268			}
269		} else {
270			print $langs->trans("ChooseABarCode");
271		}
272		print '</td>';
273
274		print '<td class="center">';
275		print $formbarcode->setBarcodeEncoder($obj->coder, $barcodelist, $obj->rowid, 'form'.$i);
276		print "</td></tr>\n";
277
278		$i++;
279	}
280}
281print "</table>\n";
282
283if (empty($conf->use_javascript_ajax))
284{
285	print '<div class="center"><input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'"></div>';
286	print '</form>';
287}
288
289print "<br>";
290
291
292/*
293 * Other options
294 */
295print load_fiche_titre($langs->trans("OtherOptions"), '', '');
296
297print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
298print '<input type="hidden" name="token" value="'.newToken().'">';
299print "<input type=\"hidden\" name=\"action\" value=\"update\">";
300
301print '<table class="noborder centpercent">';
302print '<tr class="liste_titre">';
303print '<td>'.$langs->trans("Parameter").'</td>';
304print '<td width="60" class="center">'.$langs->trans("Value").'</td>';
305print '<td>&nbsp;</td>';
306print '</tr>';
307
308// Chemin du binaire genbarcode sous linux
309if (!isset($_SERVER['WINDIR']))
310{
311	print '<tr class="oddeven">';
312	print '<td>'.$langs->trans("GenbarcodeLocation").'</td>';
313	print '<td width="60" class="center">';
314	print '<input type="text" size="40" name="GENBARCODE_LOCATION" value="'.$conf->global->GENBARCODE_LOCATION.'">';
315	if (!empty($conf->global->GENBARCODE_LOCATION) && !@file_exists($conf->global->GENBARCODE_LOCATION))
316	{
317		$langs->load("errors");
318		print '<br><font class="error">'.$langs->trans("ErrorFileNotFound", $conf->global->GENBARCODE_LOCATION).'</font>';
319	}
320	print '</td></tr>';
321}
322
323// Module products
324if (!empty($conf->product->enabled))
325{
326	print '<tr class="oddeven">';
327	print '<td>'.$langs->trans("SetDefaultBarcodeTypeProducts").'</td>';
328	print '<td width="60" class="right">';
329	print $formbarcode->selectBarcodeType($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE, "PRODUIT_DEFAULT_BARCODE_TYPE", 1);
330	print '</td></tr>';
331}
332
333// Module thirdparty
334if (!empty($conf->societe->enabled))
335{
336	print '<tr class="oddeven">';
337	print '<td>'.$langs->trans("SetDefaultBarcodeTypeThirdParties").'</td>';
338	print '<td width="60" class="right">';
339	print $formbarcode->selectBarcodeType($conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY, "GENBARCODE_BARCODETYPE_THIRDPARTY", 1);
340	print '</td></tr>';
341}
342
343print "</table>\n";
344print '<div class="tabsAction">';
345print '<input type="submit" class="button" name="submit_GENBARCODE_BARCODETYPE_THIRDPARTY" value="'.$langs->trans("Modify").'">';
346print "</div>";
347print '</form>';
348
349print '<br>';
350
351
352
353// Select barcode numbering module
354if ($conf->product->enabled)
355{
356	print load_fiche_titre($langs->trans("BarCodeNumberManager")." (".$langs->trans("Product").")", '', '');
357
358	print '<table class="noborder centpercent">';
359	print '<tr class="liste_titre">';
360	print '<td width="140">'.$langs->trans("Name").'</td>';
361	print '<td>'.$langs->trans("Description").'</td>';
362	print '<td>'.$langs->trans("Example").'</td>';
363	print '<td class="center" width="80">'.$langs->trans("Status").'</td>';
364	print '<td class="center" width="60">'.$langs->trans("ShortInfo").'</td>';
365	print "</tr>\n";
366
367	$dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
368
369	foreach ($dirbarcodenum as $dirroot)
370	{
371		$dir = dol_buildpath($dirroot, 0);
372
373		$handle = @opendir($dir);
374		if (is_resource($handle))
375		{
376			while (($file = readdir($handle)) !== false)
377			{
378				if (preg_match('/^mod_barcode_product_.*php$/', $file))
379				{
380					$file = substr($file, 0, dol_strlen($file) - 4);
381
382					try {
383						dol_include_once($dirroot.$file.'.php');
384					} catch (Exception $e)
385					{
386						dol_syslog($e->getMessage(), LOG_ERR);
387					}
388
389					$modBarCode = new $file();
390
391					print '<tr class="oddeven">';
392					print '<td>'.(isset($modBarCode->name) ? $modBarCode->name : $modBarCode->nom)."</td><td>\n";
393					print $modBarCode->info($langs);
394					print '</td>';
395					print '<td class="nowrap">'.$modBarCode->getExample($langs)."</td>\n";
396
397					if ($conf->global->BARCODE_PRODUCT_ADDON_NUM == "$file")
398					{
399						print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodeproductoff&amp;token='.newToken().'&amp;value='.urlencode($file).'">';
400						print img_picto($langs->trans("Activated"), 'switch_on');
401						print '</a></td>';
402					} else {
403						print '<td class="center"><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setbarcodeproducton&amp;token='.newToken().'&amp;value='.urlencode($file).'">';
404						print img_picto($langs->trans("Disabled"), 'switch_off');
405						print '</a></td>';
406					}
407					print '<td class="center">';
408					$s = $modBarCode->getToolTip($langs, null, -1);
409					print $form->textwithpicto('', $s, 1);
410					print '</td>';
411					print "</tr>\n";
412				}
413			}
414			closedir($handle);
415		}
416	}
417	print "</table>\n";
418}
419
420//print '</form>';
421
422print "<br>";
423
424// End of page
425llxFooter();
426$db->close();
427