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