mrp.php 16 KB

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