mrp.php 15 KB

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