bom.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/admin/bom.php
  19. * \ingroup bom
  20. * \brief Setup page of module BOM
  21. */
  22. // Load Dolibarr environment
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('admin', 'errors', 'mrp', 'other'));
  30. if (!$user->admin) {
  31. accessforbidden();
  32. }
  33. $action = GETPOST('action', 'aZ09');
  34. $value = GETPOST('value', 'alpha');
  35. $modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
  36. $label = GETPOST('label', 'alpha');
  37. $scandir = GETPOST('scan_dir', 'alpha');
  38. $type = 'bom';
  39. /*
  40. * Actions
  41. */
  42. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  43. if ($action == 'updateMask') {
  44. $maskconstbom = GETPOST('maskconstBom', 'aZ09');
  45. $maskbom = GETPOST('maskBom', 'alpha');
  46. if ($maskconstbom && preg_match('/_MASK$/', $maskconstbom)) {
  47. $res = dolibarr_set_const($db, $maskconstbom, $maskbom, 'chaine', 0, '', $conf->entity);
  48. }
  49. if (!($res > 0)) {
  50. $error++;
  51. }
  52. if (!$error) {
  53. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  54. } else {
  55. setEventMessages($langs->trans("Error"), null, 'errors');
  56. }
  57. } elseif ($action == 'specimen') {
  58. $modele = GETPOST('module', 'alpha');
  59. $bom = new BOM($db);
  60. $bom->initAsSpecimen();
  61. // Search template files
  62. $file = ''; $classname = ''; $filefound = 0;
  63. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  64. foreach ($dirmodels as $reldir) {
  65. $file = dol_buildpath($reldir."core/modules/bom/doc/pdf_".$modele.".modules.php", 0);
  66. if (file_exists($file)) {
  67. $filefound = 1;
  68. $classname = "pdf_".$modele;
  69. break;
  70. }
  71. }
  72. if ($filefound) {
  73. require_once $file;
  74. $module = new $classname($db);
  75. if ($module->write_file($bom, $langs) > 0) {
  76. header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bom&file=SPECIMEN.pdf");
  77. return;
  78. } else {
  79. setEventMessages($module->error, $module->errors, 'errors');
  80. dol_syslog($module->error, LOG_ERR);
  81. }
  82. } else {
  83. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  84. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  85. }
  86. } elseif ($action == 'set') {
  87. // Activate a model
  88. $ret = addDocumentModel($value, $type, $label, $scandir);
  89. } elseif ($action == 'del') {
  90. $ret = delDocumentModel($value, $type);
  91. if ($ret > 0) {
  92. if ($conf->global->BOM_ADDON_PDF == "$value") {
  93. dolibarr_del_const($db, 'BOM_ADDON_PDF', $conf->entity);
  94. }
  95. }
  96. } elseif ($action == 'setdoc') {
  97. // Set default model
  98. if (dolibarr_set_const($db, "BOM_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
  99. // The constant that was read before the new set
  100. // We therefore requires a variable to have a coherent view
  101. $conf->global->BOM_ADDON_PDF = $value;
  102. }
  103. // On active le modele
  104. $ret = delDocumentModel($value, $type);
  105. if ($ret > 0) {
  106. $ret = addDocumentModel($value, $type, $label, $scandir);
  107. }
  108. } elseif ($action == 'setmod') {
  109. // TODO Check if numbering module chosen can be activated
  110. // by calling method canBeActivated
  111. dolibarr_set_const($db, "BOM_ADDON", $value, 'chaine', 0, '', $conf->entity);
  112. } elseif ($action == 'set_BOM_DRAFT_WATERMARK') {
  113. $draft = GETPOST("BOM_DRAFT_WATERMARK");
  114. $res = dolibarr_set_const($db, "BOM_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
  115. if (!($res > 0)) {
  116. $error++;
  117. }
  118. if (!$error) {
  119. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  120. } else {
  121. setEventMessages($langs->trans("Error"), null, 'errors');
  122. }
  123. } elseif ($action == 'set_BOM_FREE_TEXT') {
  124. $freetext = GETPOST("BOM_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
  125. $res = dolibarr_set_const($db, "BOM_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
  126. if (!($res > 0)) {
  127. $error++;
  128. }
  129. if (!$error) {
  130. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  131. } else {
  132. setEventMessages($langs->trans("Error"), null, 'errors');
  133. }
  134. }
  135. /*
  136. * View
  137. */
  138. $form = new Form($db);
  139. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  140. llxHeader("", $langs->trans("BOMsSetup"));
  141. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  142. print load_fiche_titre($langs->trans("BOMsSetup"), $linkback, 'title_setup');
  143. $head = bomAdminPrepareHead();
  144. print dol_get_fiche_head($head, 'settings', $langs->trans("BOMs"), -1, 'bom');
  145. /*
  146. * Numbering module
  147. */
  148. print load_fiche_titre($langs->trans("BOMsNumberingModules"), '', '');
  149. print '<div class="div-table-responsive-no-min">';
  150. print '<table class="noborder centpercent">';
  151. print '<tr class="liste_titre">';
  152. print '<td>'.$langs->trans("Name").'</td>';
  153. print '<td>'.$langs->trans("Description").'</td>';
  154. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  155. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  156. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  157. print '</tr>'."\n";
  158. clearstatcache();
  159. foreach ($dirmodels as $reldir) {
  160. $dir = dol_buildpath($reldir."core/modules/bom/");
  161. if (is_dir($dir)) {
  162. $handle = opendir($dir);
  163. if (is_resource($handle)) {
  164. while (($file = readdir($handle)) !== false) {
  165. if (substr($file, 0, 8) == 'mod_bom_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  166. $file = substr($file, 0, dol_strlen($file) - 4);
  167. $classname = $file;
  168. require_once $dir.$file.'.php';
  169. $module = new $classname($db);
  170. // Show modules according to features level
  171. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  172. continue;
  173. }
  174. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  175. continue;
  176. }
  177. if ($module->isEnabled()) {
  178. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  179. print $module->info($langs);
  180. print '</td>';
  181. // Show example of numbering module
  182. print '<td class="nowrap">';
  183. $tmp = $module->getExample();
  184. if (preg_match('/^Error/', $tmp)) {
  185. $langs->load("errors");
  186. print '<div class="error">'.$langs->trans($tmp).'</div>';
  187. } elseif ($tmp == 'NotConfigured') {
  188. print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
  189. } else {
  190. print $tmp;
  191. }
  192. print '</td>'."\n";
  193. print '<td class="center">';
  194. if ($conf->global->BOM_ADDON == $file) {
  195. print img_picto($langs->trans("Activated"), 'switch_on');
  196. } else {
  197. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
  198. print img_picto($langs->trans("Disabled"), 'switch_off');
  199. print '</a>';
  200. }
  201. print '</td>';
  202. $bom = new BOM($db);
  203. $bom->initAsSpecimen();
  204. // Info
  205. $htmltooltip = '';
  206. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  207. $bom->type = 0;
  208. $nextval = $module->getNextValue($mysoc, $bom);
  209. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  210. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  211. if ($nextval) {
  212. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
  213. $nextval = $langs->trans($nextval);
  214. }
  215. $htmltooltip .= $nextval.'<br>';
  216. } else {
  217. $htmltooltip .= $langs->trans($module->error).'<br>';
  218. }
  219. }
  220. print '<td class="center">';
  221. print $form->textwithpicto('', $htmltooltip, 1, 0);
  222. print '</td>';
  223. print "</tr>\n";
  224. }
  225. }
  226. }
  227. closedir($handle);
  228. }
  229. }
  230. }
  231. print "</table>";
  232. print "</div>";
  233. /*
  234. * Document templates generators
  235. */
  236. print "<br>\n";
  237. print load_fiche_titre($langs->trans("BOMsModelModule"), '', '');
  238. // Load array def with activated templates
  239. $def = array();
  240. $sql = "SELECT nom";
  241. $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
  242. $sql .= " WHERE type = '".$db->escape($type)."'";
  243. $sql .= " AND entity = ".$conf->entity;
  244. $resql = $db->query($sql);
  245. if ($resql) {
  246. $i = 0;
  247. $num_rows = $db->num_rows($resql);
  248. while ($i < $num_rows) {
  249. $array = $db->fetch_array($resql);
  250. array_push($def, $array[0]);
  251. $i++;
  252. }
  253. } else {
  254. dol_print_error($db);
  255. }
  256. print '<div class="div-table-responsive-no-min">';
  257. print '<table class="noborder centpercent">';
  258. print '<tr class="liste_titre">';
  259. print '<td>'.$langs->trans("Name").'</td>';
  260. print '<td>'.$langs->trans("Description").'</td>';
  261. print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
  262. print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
  263. print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
  264. print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
  265. print "</tr>\n";
  266. clearstatcache();
  267. foreach ($dirmodels as $reldir) {
  268. foreach (array('', '/doc') as $valdir) {
  269. $realpath = $reldir."core/modules/bom".$valdir;
  270. $dir = dol_buildpath($realpath);
  271. if (is_dir($dir)) {
  272. $handle = opendir($dir);
  273. if (is_resource($handle)) {
  274. while (($file = readdir($handle)) !== false) {
  275. $filelist[] = $file;
  276. }
  277. closedir($handle);
  278. arsort($filelist);
  279. foreach ($filelist as $file) {
  280. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  281. if (file_exists($dir.'/'.$file)) {
  282. $name = substr($file, 4, dol_strlen($file) - 16);
  283. $classname = substr($file, 0, dol_strlen($file) - 12);
  284. require_once $dir.'/'.$file;
  285. $module = new $classname($db);
  286. $modulequalified = 1;
  287. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  288. $modulequalified = 0;
  289. }
  290. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  291. $modulequalified = 0;
  292. }
  293. if ($modulequalified) {
  294. print '<tr class="oddeven"><td width="100">';
  295. print (empty($module->name) ? $name : $module->name);
  296. print "</td><td>\n";
  297. if (method_exists($module, 'info')) {
  298. print $module->info($langs);
  299. } else {
  300. print $module->description;
  301. }
  302. print '</td>';
  303. // Active
  304. if (in_array($name, $def)) {
  305. print '<td class="center">'."\n";
  306. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($name).'">';
  307. print img_picto($langs->trans("Enabled"), 'switch_on');
  308. print '</a>';
  309. print '</td>';
  310. } else {
  311. print '<td class="center">'."\n";
  312. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=set&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
  313. print "</td>";
  314. }
  315. // Default
  316. print '<td class="center">';
  317. if ($conf->global->BOM_ADDON_PDF == $name) {
  318. print img_picto($langs->trans("Default"), 'on');
  319. } else {
  320. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  321. }
  322. print '</td>';
  323. // Info
  324. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  325. $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
  326. if ($module->type == 'pdf') {
  327. $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  328. }
  329. $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
  330. $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  331. $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
  332. $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftBOMs").': '.yn($module->option_draft_watermark, 1, 1);
  333. print '<td class="center">';
  334. print $form->textwithpicto('', $htmltooltip, 1, 0);
  335. print '</td>';
  336. // Preview
  337. print '<td class="center">';
  338. if ($module->type == 'pdf') {
  339. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
  340. } else {
  341. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  342. }
  343. print '</td>';
  344. print "</tr>\n";
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. print '</table>';
  354. print '</div>';
  355. /*
  356. * Other options
  357. */
  358. print "<br>";
  359. print load_fiche_titre($langs->trans("OtherOptions"), '', '');
  360. print '<div class="div-table-responsive-no-min">';
  361. print '<table class="noborder centpercent">';
  362. print '<tr class="liste_titre">';
  363. print '<td>'.$langs->trans("Parameter").'</td>';
  364. print '<td class="center" width="60">'.$langs->trans("Value").'</td>';
  365. print "<td>&nbsp;</td>\n";
  366. print "</tr>\n";
  367. $substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
  368. $substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
  369. $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
  370. foreach ($substitutionarray as $key => $val) {
  371. $htmltext .= $key.'<br>';
  372. }
  373. $htmltext .= '</i>';
  374. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  375. print '<input type="hidden" name="token" value="'.newToken().'">';
  376. print '<input type="hidden" name="action" value="set_BOM_FREE_TEXT">';
  377. print '<tr class="oddeven"><td colspan="2">';
  378. print $form->textwithpicto($langs->trans("FreeLegalTextOnBOMs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
  379. $variablename = 'BOM_FREE_TEXT';
  380. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) {
  381. print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
  382. } else {
  383. include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  384. $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
  385. print $doleditor->Create();
  386. }
  387. print '</td><td class="right">';
  388. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  389. print "</td></tr>\n";
  390. print '</form>';
  391. //Use draft Watermark
  392. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  393. print '<input type="hidden" name="token" value="'.newToken().'">';
  394. print '<input type="hidden" name="action" value="set_BOM_DRAFT_WATERMARK">';
  395. print '<tr class="oddeven"><td>';
  396. print $form->textwithpicto($langs->trans("WatermarkOnDraftBOMs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
  397. print '</td><td>';
  398. print '<input class="flat minwidth200" type="text" name="BOM_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('BOM_DRAFT_WATERMARK')).'">';
  399. print '</td><td class="right">';
  400. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  401. print "</td></tr>\n";
  402. print '</form>';
  403. print '</table>';
  404. print '</div>';
  405. print '<br>';
  406. // End of page
  407. llxFooter();
  408. $db->close();