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