bom.php 16 KB

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