mrp.php 16 KB

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