mrp.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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) {
  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 = 'mrp';
  39. /*
  40. * Actions
  41. */
  42. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  43. if ($action == 'updateMask') {
  44. $maskconstmrp = GETPOST('maskconstMo', 'alpha');
  45. $maskmrp = GETPOST('maskMo', 'alpha');
  46. if ($maskconstmrp) {
  47. $res = dolibarr_set_const($db, $maskconstmrp, $maskmrp, '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. $mo = new MO($db);
  60. $mo->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/mrp/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($mo, $langs) > 0) {
  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. } 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->MRP_MO_ADDON_PDF == "$value") {
  93. dolibarr_del_const($db, 'MRP_MO_ADDON_PDF', $conf->entity);
  94. }
  95. }
  96. } elseif ($action == 'setdoc') {
  97. // Set default model
  98. if (dolibarr_set_const($db, "MRP_MO_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->MRP_MO_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, "MRP_MO_ADDON", $value, 'chaine', 0, '', $conf->entity);
  112. } elseif ($action == 'set_MRP_MO_DRAFT_WATERMARK') {
  113. $draft = GETPOST("MRP_MO_DRAFT_WATERMARK");
  114. $res = dolibarr_set_const($db, "MRP_MO_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_MRP_MO_FREE_TEXT') {
  124. $freetext = GETPOST("MRP_MO_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
  125. $res = dolibarr_set_const($db, "MRP_MO_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("MrpSetupPage"));
  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("MrpSetupPage"), $linkback, 'title_setup');
  143. $head = mrpAdminPrepareHead();
  144. print dol_get_fiche_head($head, 'settings', $langs->trans("MOs"), -1, 'mrp');
  145. /*
  146. * MOs Numbering model
  147. */
  148. print load_fiche_titre($langs->trans("MOsNumberingModules"), '', '');
  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/mrp/");
  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, 7) == 'mod_mo_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  165. $file = substr($file, 0, dol_strlen($file) - 4);
  166. require_once $dir.$file.'.php';
  167. $module = new $file($db);
  168. // Show modules according to features level
  169. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  170. continue;
  171. }
  172. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  173. continue;
  174. }
  175. if ($module->isEnabled()) {
  176. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  177. print $module->info();
  178. print '</td>';
  179. // Show example of numbering model
  180. print '<td class="nowrap">';
  181. $tmp = $module->getExample();
  182. if (preg_match('/^Error/', $tmp)) {
  183. $langs->load("errors");
  184. print '<div class="error">'.$langs->trans($tmp).'</div>';
  185. } elseif ($tmp == 'NotConfigured') {
  186. print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
  187. } else {
  188. print $tmp;
  189. }
  190. print '</td>'."\n";
  191. print '<td class="center">';
  192. if ($conf->global->MRP_MO_ADDON == $file) {
  193. print img_picto($langs->trans("Activated"), 'switch_on');
  194. } else {
  195. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
  196. print img_picto($langs->trans("Disabled"), 'switch_off');
  197. print '</a>';
  198. }
  199. print '</td>';
  200. $mrp = new MO($db);
  201. $mrp->initAsSpecimen();
  202. // Info
  203. $htmltooltip = '';
  204. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  205. $mrp->type = 0;
  206. $nextval = $module->getNextValue($mysoc, $mrp);
  207. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  208. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  209. if ($nextval) {
  210. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
  211. $nextval = $langs->trans($nextval);
  212. }
  213. $htmltooltip .= $nextval.'<br>';
  214. } else {
  215. $htmltooltip .= $langs->trans($module->error).'<br>';
  216. }
  217. }
  218. print '<td class="center">';
  219. print $form->textwithpicto('', $htmltooltip, 1, 0);
  220. print '</td>';
  221. print "</tr>\n";
  222. }
  223. }
  224. }
  225. closedir($handle);
  226. }
  227. }
  228. }
  229. print "</table><br>\n";
  230. /*
  231. * Document templates generators
  232. */
  233. print load_fiche_titre($langs->trans("MOsModelModule"), '', '');
  234. // Load array def with activated templates
  235. $def = array();
  236. $sql = "SELECT nom";
  237. $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
  238. $sql .= " WHERE type = '".$db->escape($type)."'";
  239. $sql .= " AND entity = ".$conf->entity;
  240. $resql = $db->query($sql);
  241. if ($resql) {
  242. $i = 0;
  243. $num_rows = $db->num_rows($resql);
  244. while ($i < $num_rows) {
  245. $array = $db->fetch_array($resql);
  246. array_push($def, $array[0]);
  247. $i++;
  248. }
  249. } else {
  250. dol_print_error($db);
  251. }
  252. print "<table class=\"noborder\" width=\"100%\">\n";
  253. print "<tr class=\"liste_titre\">\n";
  254. print '<td>'.$langs->trans("Name").'</td>';
  255. print '<td>'.$langs->trans("Description").'</td>';
  256. print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
  257. print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
  258. print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
  259. print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
  260. print "</tr>\n";
  261. clearstatcache();
  262. foreach ($dirmodels as $reldir) {
  263. foreach (array('', '/doc') as $valdir) {
  264. $realpath = $reldir."core/modules/mrp".$valdir;
  265. $dir = dol_buildpath($realpath);
  266. if (is_dir($dir)) {
  267. $handle = opendir($dir);
  268. if (is_resource($handle)) {
  269. while (($file = readdir($handle)) !== false) {
  270. $filelist[] = $file;
  271. }
  272. closedir($handle);
  273. arsort($filelist);
  274. foreach ($filelist as $file) {
  275. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  276. if (file_exists($dir.'/'.$file)) {
  277. $name = substr($file, 4, dol_strlen($file) - 16);
  278. $classname = substr($file, 0, dol_strlen($file) - 12);
  279. require_once $dir.'/'.$file;
  280. $module = new $classname($db);
  281. $modulequalified = 1;
  282. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  283. $modulequalified = 0;
  284. }
  285. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  286. $modulequalified = 0;
  287. }
  288. if ($modulequalified) {
  289. print '<tr class="oddeven"><td width="100">';
  290. print (empty($module->name) ? $name : $module->name);
  291. print "</td><td>\n";
  292. if (method_exists($module, 'info')) {
  293. print $module->info($langs);
  294. } else {
  295. print $module->description;
  296. }
  297. print '</td>';
  298. // Active
  299. if (in_array($name, $def)) {
  300. print '<td class="center">'."\n";
  301. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($name).'">';
  302. print img_picto($langs->trans("Enabled"), 'switch_on');
  303. print '</a>';
  304. print '</td>';
  305. } else {
  306. print '<td class="center">'."\n";
  307. 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>';
  308. print "</td>";
  309. }
  310. // Default
  311. print '<td class="center">';
  312. if ($conf->global->MRP_MO_ADDON_PDF == $name) {
  313. print img_picto($langs->trans("Default"), 'on');
  314. } else {
  315. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  316. }
  317. print '</td>';
  318. // Info
  319. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  320. $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
  321. if ($module->type == 'pdf') {
  322. $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  323. }
  324. $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
  325. $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  326. $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
  327. $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftMOs").': '.yn($module->option_draft_watermark, 1, 1);
  328. print '<td class="center">';
  329. print $form->textwithpicto('', $htmltooltip, 1, 0);
  330. print '</td>';
  331. // Preview
  332. print '<td class="center">';
  333. if ($module->type == 'pdf') {
  334. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
  335. } else {
  336. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  337. }
  338. print '</td>';
  339. print "</tr>\n";
  340. }
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. print '</table>';
  349. print "<br>";
  350. /*
  351. * Other options
  352. */
  353. print load_fiche_titre($langs->trans("OtherOptions"), '', '');
  354. print '<table class="noborder centpercent">';
  355. print '<tr class="liste_titre">';
  356. print '<td>'.$langs->trans("Parameter").'</td>';
  357. print '<td class="center" width="60">'.$langs->trans("Value").'</td>';
  358. print "<td>&nbsp;</td>\n";
  359. print "</tr>\n";
  360. $substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
  361. $substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
  362. $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
  363. foreach ($substitutionarray as $key => $val) {
  364. $htmltext .= $key.'<br>';
  365. }
  366. $htmltext .= '</i>';
  367. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  368. print '<input type="hidden" name="token" value="'.newToken().'">';
  369. print '<input type="hidden" name="action" value="set_MRP_MO_FREE_TEXT">';
  370. print '<tr class="oddeven"><td colspan="2">';
  371. print $form->textwithpicto($langs->trans("FreeLegalTextOnMOs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
  372. $variablename = 'MRP_MO_FREE_TEXT';
  373. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) {
  374. print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
  375. } else {
  376. include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  377. $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
  378. print $doleditor->Create();
  379. }
  380. print '</td><td class="right">';
  381. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  382. print "</td></tr>\n";
  383. print '</form>';
  384. //Use draft Watermark
  385. print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
  386. print '<input type="hidden" name="token" value="'.newToken().'">';
  387. print "<input type=\"hidden\" name=\"action\" value=\"set_MRP_MO_DRAFT_WATERMARK\">";
  388. print '<tr class="oddeven"><td>';
  389. print $form->textwithpicto($langs->trans("WatermarkOnDraftMOs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
  390. print '</td><td>';
  391. print '<input class="flat minwidth200" type="text" name="MRP_MO_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('MRP_MO_DRAFT_WATERMARK')).'">';
  392. print '</td><td class="right">';
  393. print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
  394. print "</td></tr>\n";
  395. print '</form>';
  396. print '</table>';
  397. print '<br>';
  398. // End of page
  399. llxFooter();
  400. $db->close();