dolibarr 20.0.0
bom.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
31require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('admin', 'errors', 'mrp', 'other'));
35
36if (!$user->admin) {
38}
39
40$action = GETPOST('action', 'aZ09');
41$value = GETPOST('value', 'alpha');
42$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
43
44$label = GETPOST('label', 'alpha');
45$scandir = GETPOST('scan_dir', 'alpha');
46$type = 'bom';
47
48
49/*
50 * Actions
51 */
52
53include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
54
55if ($action == 'updateMask') {
56 $maskconstbom = GETPOST('maskconstBom', 'aZ09');
57 $maskbom = GETPOST('maskBom', 'alpha');
58
59 if ($maskconstbom && preg_match('/_MASK$/', $maskconstbom)) {
60 $res = dolibarr_set_const($db, $maskconstbom, $maskbom, 'chaine', 0, '', $conf->entity);
61 }
62
63 if (!($res > 0)) {
64 $error++;
65 }
66
67 if (!$error) {
68 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
69 } else {
70 setEventMessages($langs->trans("Error"), null, 'errors');
71 }
72} elseif ($action == 'specimen') {
73 $modele = GETPOST('module', 'alpha');
74
75 $bom = new BOM($db);
76 $bom->initAsSpecimen();
77
78 // Search template files
79 $file = '';
80 $classname = '';
81 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
82 foreach ($dirmodels as $reldir) {
83 $file = dol_buildpath($reldir."core/modules/bom/doc/pdf_".$modele.".modules.php", 0);
84 if (file_exists($file)) {
85 $classname = "pdf_".$modele;
86 break;
87 }
88 }
89
90 if ($classname !== '') {
91 require_once $file;
92
93 $module = new $classname($db);
94 '@phan-var-force ModelePDFBom $module';
95
96 if ($module->write_file($bom, $langs) > 0) {
97 header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bom&file=SPECIMEN.pdf");
98 return;
99 } else {
100 setEventMessages($module->error, $module->errors, 'errors');
101 dol_syslog($module->error, LOG_ERR);
102 }
103 } else {
104 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
105 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
106 }
107} elseif ($action == 'set') {
108 // Activate a model
109 $ret = addDocumentModel($value, $type, $label, $scandir);
110} elseif ($action == 'del') {
111 $ret = delDocumentModel($value, $type);
112 if ($ret > 0) {
113 if ($conf->global->BOM_ADDON_PDF == "$value") {
114 dolibarr_del_const($db, 'BOM_ADDON_PDF', $conf->entity);
115 }
116 }
117} elseif ($action == 'setdoc') {
118 // Set default model
119 if (dolibarr_set_const($db, "BOM_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
120 // The constant that was read before the new set
121 // We therefore requires a variable to have a coherent view
122 $conf->global->BOM_ADDON_PDF = $value;
123 }
124
125 // On active le modele
126 $ret = delDocumentModel($value, $type);
127 if ($ret > 0) {
128 $ret = addDocumentModel($value, $type, $label, $scandir);
129 }
130} elseif ($action == 'setmod') {
131 // TODO Check if numbering module chosen can be activated
132 // by calling method canBeActivated
133
134 dolibarr_set_const($db, "BOM_ADDON", $value, 'chaine', 0, '', $conf->entity);
135} elseif ($action == 'set_BOM_DRAFT_WATERMARK') {
136 $draft = GETPOST("BOM_DRAFT_WATERMARK");
137 $res = dolibarr_set_const($db, "BOM_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
138
139 if (!($res > 0)) {
140 $error++;
141 }
142
143 if (!$error) {
144 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
145 } else {
146 setEventMessages($langs->trans("Error"), null, 'errors');
147 }
148} elseif ($action == 'set_BOM_FREE_TEXT') {
149 $freetext = GETPOST("BOM_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
150
151 $res = dolibarr_set_const($db, "BOM_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
152
153 if (!($res > 0)) {
154 $error++;
155 }
156
157 if (!$error) {
158 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
159 } else {
160 setEventMessages($langs->trans("Error"), null, 'errors');
161 }
162}
163
164
165/*
166 * View
167 */
168
169$form = new Form($db);
170
171$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
172
173llxHeader("", $langs->trans("BOMsSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-bom');
174
175$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
176print load_fiche_titre($langs->trans("BOMsSetup"), $linkback, 'title_setup');
177
178$head = bomAdminPrepareHead();
179
180print dol_get_fiche_head($head, 'settings', $langs->trans("BOMs"), -1, 'bom');
181
182/*
183 * Numbering module
184 */
185
186print load_fiche_titre($langs->trans("BOMsNumberingModules"), '', '');
187
188print '<div class="div-table-responsive-no-min">';
189print '<table class="noborder centpercent">';
190print '<tr class="liste_titre">';
191print '<td>'.$langs->trans("Name").'</td>';
192print '<td>'.$langs->trans("Description").'</td>';
193print '<td class="nowrap">'.$langs->trans("Example").'</td>';
194print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
195print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
196print '</tr>'."\n";
197
198clearstatcache();
199
200foreach ($dirmodels as $reldir) {
201 $dir = dol_buildpath($reldir."core/modules/bom/");
202
203 if (is_dir($dir)) {
204 $handle = opendir($dir);
205 if (is_resource($handle)) {
206 while (($file = readdir($handle)) !== false) {
207 if (substr($file, 0, 8) == 'mod_bom_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
208 $file = substr($file, 0, dol_strlen($file) - 4);
209 $classname = $file;
210
211 require_once $dir.$file.'.php';
212
213 $module = new $classname($db);
214
215 // Show modules according to features level
216 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
217 continue;
218 }
219 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
220 continue;
221 }
222
223 if ($module->isEnabled()) {
224 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
225 print $module->info($langs);
226 print '</td>';
227
228 // Show example of numbering module
229 print '<td class="nowrap">';
230 $tmp = $module->getExample();
231 if (preg_match('/^Error/', $tmp)) {
232 $langs->load("errors");
233 print '<div class="error">'.$langs->trans($tmp).'</div>';
234 } elseif ($tmp == 'NotConfigured') {
235 print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
236 } else {
237 print $tmp;
238 }
239 print '</td>'."\n";
240
241 print '<td class="center">';
242 if ($conf->global->BOM_ADDON == $file) {
243 print img_picto($langs->trans("Activated"), 'switch_on');
244 } else {
245 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
246 print img_picto($langs->trans("Disabled"), 'switch_off');
247 print '</a>';
248 }
249 print '</td>';
250
251 $bom = new BOM($db);
252 $bom->initAsSpecimen();
253
254 // Info
255 $htmltooltip = '';
256 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
257 $bom->type = 0;
258 $nextval = $module->getNextValue($mysoc, $bom);
259 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
260 $htmltooltip .= ''.$langs->trans("NextValue").': ';
261 if ($nextval) {
262 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
263 $nextval = $langs->trans($nextval);
264 }
265 $htmltooltip .= $nextval.'<br>';
266 } else {
267 $htmltooltip .= $langs->trans($module->error).'<br>';
268 }
269 }
270
271 print '<td class="center">';
272 print $form->textwithpicto('', $htmltooltip, 1, 0);
273 print '</td>';
274
275 print "</tr>\n";
276 }
277 }
278 }
279 closedir($handle);
280 }
281 }
282}
283print "</table>";
284print "</div>";
285
286
287/*
288 * Document templates generators
289 */
290
291print "<br>\n";
292print load_fiche_titre($langs->trans("BOMsModelModule"), '', '');
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 $i = 0;
303 $num_rows = $db->num_rows($resql);
304 while ($i < $num_rows) {
305 $array = $db->fetch_array($resql);
306 if (is_array($array)) {
307 array_push($def, $array[0]);
308 }
309 $i++;
310 }
311} else {
312 dol_print_error($db);
313}
314
315
316print '<div class="div-table-responsive-no-min">';
317print '<table class="noborder centpercent">';
318print '<tr class="liste_titre">';
319print '<td>'.$langs->trans("Name").'</td>';
320print '<td>'.$langs->trans("Description").'</td>';
321print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
322print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
323print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
324print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
325print "</tr>\n";
326
327clearstatcache();
328
329foreach ($dirmodels as $reldir) {
330 foreach (array('', '/doc') as $valdir) {
331 $realpath = $reldir."core/modules/bom".$valdir;
332 $dir = dol_buildpath($realpath);
333
334 if (is_dir($dir)) {
335 $handle = opendir($dir);
336 if (is_resource($handle)) {
337 $filelist = array();
338 while (($file = readdir($handle)) !== false) {
339 $filelist[] = $file;
340 }
341 closedir($handle);
342 arsort($filelist);
343
344 foreach ($filelist as $file) {
345 if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
346 if (file_exists($dir.'/'.$file)) {
347 $name = substr($file, 4, dol_strlen($file) - 16);
348 $classname = substr($file, 0, dol_strlen($file) - 12);
349
350 require_once $dir.'/'.$file;
351 $module = new $classname($db);
352
353 $modulequalified = 1;
354 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
355 $modulequalified = 0;
356 }
357 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
358 $modulequalified = 0;
359 }
360
361 if ($modulequalified) {
362 print '<tr class="oddeven"><td width="100">';
363 print(empty($module->name) ? $name : $module->name);
364 print "</td><td>\n";
365 if (method_exists($module, 'info')) {
366 print $module->info($langs);
367 } else {
368 print $module->description;
369 }
370 print '</td>';
371
372 // Active
373 if (in_array($name, $def)) {
374 print '<td class="center">'."\n";
375 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($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&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&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 print img_picto($langs->trans("Default"), 'on');
389 } else {
390 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
391 }
392 print '</td>';
393
394 // Info
395 $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
396 $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
397 if ($module->type == 'pdf') {
398 $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
399 }
400 $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
401
402 $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
403 $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
404 $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftBOMs").': '.yn($module->option_draft_watermark, 1, 1);
405
406
407 print '<td class="center">';
408 print $form->textwithpicto('', $htmltooltip, 1, 0);
409 print '</td>';
410
411 // Preview
412 print '<td class="center">';
413 if ($module->type == 'pdf') {
414 print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
415 } else {
416 print img_object($langs->trans("PreviewNotAvailable"), 'generic');
417 }
418 print '</td>';
419
420 print "</tr>\n";
421 }
422 }
423 }
424 }
425 }
426 }
427 }
428}
429
430print '</table>';
431print '</div>';
432
433/*
434 * Other options
435 */
436
437print "<br>";
438print load_fiche_titre($langs->trans("OtherOptions"), '', '');
439
440print '<div class="div-table-responsive-no-min">';
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) {
452 $htmltext .= $key.'<br>';
453}
454$htmltext .= '</i>';
455
456print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
457print '<input type="hidden" name="token" value="'.newToken().'">';
458print '<input type="hidden" name="action" value="set_BOM_FREE_TEXT">';
459print '<tr class="oddeven"><td colspan="2">';
460print $form->textwithpicto($langs->trans("FreeLegalTextOnBOMs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
461$variablename = 'BOM_FREE_TEXT';
462if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) {
463 print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
464} else {
465 include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
466 $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
467 print $doleditor->Create();
468}
469print '</td><td class="right">';
470print '<input type="submit" class="button button-edit" 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_BOM_DRAFT_WATERMARK">';
479print '<tr class="oddeven"><td>';
480print $form->textwithpicto($langs->trans("WatermarkOnDraftBOMs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
481print '</td><td>';
482print '<input class="flat minwidth200" type="text" name="BOM_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('BOM_DRAFT_WATERMARK')).'">';
483print '</td><td class="right">';
484print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
485print "</td></tr>\n";
486print '</form>';
487
488print '</table>';
489print '</div>';
490print '<br>';
491
492
493// End of page
494llxFooter();
495$db->close();
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
delDocumentModel($name, $type)
Delete document model used by doc generator.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
bomAdminPrepareHead()
Prepare admin pages header.
Definition bom.lib.php:30
Class for BOM.
Definition bom.class.php:45
Class to manage a WYSIWYG editor.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
pdf_getSubstitutionArray($outputlangs, $exclude=null, $object=null, $onlykey=0, $include=null)
Return array of possible substitutions for PDF content (without external module substitutions).
Definition pdf.lib.php:769
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.