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