barcode.php 13 KB

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