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