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