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