dolibarr 21.0.3
mrp.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.'/mrp/class/mo.class.php';
31require_once DOL_DOCUMENT_ROOT.'/mrp/lib/mrp_mo.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/mrp/lib/mrp.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
34
44// Load translation files required by the page
45$langs->loadLangs(array('admin', 'errors', 'mrp', 'other'));
46
47if (!$user->admin) {
49}
50
51$action = GETPOST('action', 'aZ09');
52$value = GETPOST('value', 'alpha');
53$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
54
55$label = GETPOST('label', 'alpha');
56$scandir = GETPOST('scan_dir', 'alpha');
57$type = 'mrp';
58
59
60/*
61 * Actions
62 */
63$error = 0;
64
65include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
66
67if ($action == 'updateMask') {
68 $maskconstmrp = GETPOST('maskconstMo', 'aZ09');
69 $maskmrp = GETPOST('maskMo', 'alpha');
70
71 $res = 0;
72
73 if ($maskconstmrp && preg_match('/_MASK$/', $maskconstmrp)) {
74 $res = dolibarr_set_const($db, $maskconstmrp, $maskmrp, 'chaine', 0, '', $conf->entity);
75 }
76
77 if (!($res > 0)) {
78 $error++;
79 }
80
81 if (!$error) {
82 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
83 } else {
84 setEventMessages($langs->trans("Error"), null, 'errors');
85 }
86} elseif ($action == 'specimen') {
87 $modele = GETPOST('module', 'alpha');
88
89 $mo = new Mo($db);
90 $mo->initAsSpecimen();
91
92 // Search template files
93 $file = '';
94 $classname = '';
95 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
96 foreach ($dirmodels as $reldir) {
97 $file = dol_buildpath($reldir."core/modules/mrp/doc/pdf_".$modele.".modules.php", 0);
98 if (file_exists($file)) {
99 $classname = "pdf_".$modele;
100 break;
101 }
102 }
103
104 if ($classname !== '') {
105 require_once $file;
106
107 $module = new $classname($db);
108 '@phan-var-force ModelePDFMo $module';
109
110 if ($module->write_file($mo, $langs) > 0) {
111 header("Location: ".DOL_URL_ROOT."/document.php?modulepart=mrp&file=SPECIMEN.pdf");
112 return;
113 } else {
114 setEventMessages($module->error, null, 'errors');
115 dol_syslog($module->error, LOG_ERR);
116 }
117 } else {
118 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
119 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
120 }
121} elseif ($action == 'set') {
122 // Activate a model
123 $ret = addDocumentModel($value, $type, $label, $scandir);
124} elseif ($action == 'del') {
125 $ret = delDocumentModel($value, $type);
126 if ($ret > 0) {
127 if ($conf->global->MRP_MO_ADDON_PDF == "$value") {
128 dolibarr_del_const($db, 'MRP_MO_ADDON_PDF', $conf->entity);
129 }
130 }
131} elseif ($action == 'setdoc') {
132 // Set default model
133 if (dolibarr_set_const($db, "MRP_MO_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
134 // The constant that was read before the new set
135 // We therefore requires a variable to have a coherent view
136 $conf->global->MRP_MO_ADDON_PDF = $value;
137 }
138
139 // On active le modele
140 $ret = delDocumentModel($value, $type);
141 if ($ret > 0) {
142 $ret = addDocumentModel($value, $type, $label, $scandir);
143 }
144} elseif ($action == 'setmod') {
145 // TODO Check if numbering module chosen can be activated
146 // by calling method canBeActivated
147
148 dolibarr_set_const($db, "MRP_MO_ADDON", $value, 'chaine', 0, '', $conf->entity);
149} elseif ($action == 'set_MRP_MO_DRAFT_WATERMARK') {
150 $draft = GETPOST("MRP_MO_DRAFT_WATERMARK");
151 $res = dolibarr_set_const($db, "MRP_MO_DRAFT_WATERMARK", trim($draft), '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} elseif ($action == 'set_MRP_MO_FREE_TEXT') {
163 $freetext = GETPOST("MRP_MO_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
164
165 $res = dolibarr_set_const($db, "MRP_MO_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
166
167 if (!($res > 0)) {
168 $error++;
169 }
170
171 if (!$error) {
172 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
173 } else {
174 setEventMessages($langs->trans("Error"), null, 'errors');
175 }
176}
177
178
179/*
180 * View
181 */
182
183$form = new Form($db);
184
185$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
186
187llxHeader("", $langs->trans("MrpSetupPage"), '', '', 0, 0, '', '', '', 'mod-admin page-mrp');
188
189$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
190print load_fiche_titre($langs->trans("MrpSetupPage"), $linkback, 'title_setup');
191
192$head = mrpAdminPrepareHead();
193
194print dol_get_fiche_head($head, 'settings', $langs->trans("MOs"), -1, 'mrp');
195
196/*
197 * MOs Numbering model
198 */
199
200print load_fiche_titre($langs->trans("MOsNumberingModules"), '', '');
201
202print '<table class="noborder centpercent">';
203print '<tr class="liste_titre">';
204print '<td>'.$langs->trans("Name").'</td>';
205print '<td>'.$langs->trans("Description").'</td>';
206print '<td class="nowrap">'.$langs->trans("Example").'</td>';
207print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
208print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
209print '</tr>'."\n";
210
211clearstatcache();
212
213foreach ($dirmodels as $reldir) {
214 $dir = dol_buildpath($reldir."core/modules/mrp/");
215
216 if (is_dir($dir)) {
217 $handle = opendir($dir);
218 if (is_resource($handle)) {
219 while (($file = readdir($handle)) !== false) {
220 if (substr($file, 0, 7) == 'mod_mo_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
221 $file = substr($file, 0, dol_strlen($file) - 4);
222
223 require_once $dir.$file.'.php';
224
225 $module = new $file($db);
226
227 '@phan-var-force ModeleNumRefMos $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 model
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->MRP_MO_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 $mrp = new Mo($db);
266 $mrp->initAsSpecimen();
267
268 // Info
269 $htmltooltip = '';
270 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
271 $mrp->type = 0;
272 $nextval = $module->getNextValue($mysoc, $mrp);
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><br>\n";
298
299
300/*
301 * Document templates generators
302 */
303
304print load_fiche_titre($langs->trans("MOsModelModule"), '', '');
305
306// Load array def with activated templates
307$def = array();
308$sql = "SELECT nom";
309$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
310$sql .= " WHERE type = '".$db->escape($type)."'";
311$sql .= " AND entity = ".$conf->entity;
312$resql = $db->query($sql);
313if ($resql) {
314 $i = 0;
315 $num_rows = $db->num_rows($resql);
316 while ($i < $num_rows) {
317 $array = $db->fetch_array($resql);
318 if (is_array($array)) {
319 array_push($def, $array[0]);
320 }
321 $i++;
322 }
323} else {
324 dol_print_error($db);
325}
326
327
328print "<table class=\"noborder\" width=\"100%\">\n";
329print "<tr class=\"liste_titre\">\n";
330print '<td>'.$langs->trans("Name").'</td>';
331print '<td>'.$langs->trans("Description").'</td>';
332print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
333print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
334print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
335print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
336print "</tr>\n";
337
338clearstatcache();
339
340foreach ($dirmodels as $reldir) {
341 foreach (array('', '/doc') as $valdir) {
342 $realpath = $reldir."core/modules/mrp".$valdir;
343 $dir = dol_buildpath($realpath);
344
345 if (is_dir($dir)) {
346 $handle = opendir($dir);
347 if (is_resource($handle)) {
348 $filelist = array();
349 while (($file = readdir($handle)) !== false) {
350 $filelist[] = $file;
351 }
352 closedir($handle);
353 arsort($filelist);
354
355 foreach ($filelist as $file) {
356 if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
357 if (file_exists($dir.'/'.$file)) {
358 $name = substr($file, 4, dol_strlen($file) - 16);
359 $classname = substr($file, 0, dol_strlen($file) - 12);
360
361 require_once $dir.'/'.$file;
362 $module = new $classname($db);
363
364 '@phan-var-force ModelePDFMo $module';
365
366 $modulequalified = 1;
367 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
368 $modulequalified = 0;
369 }
370 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
371 $modulequalified = 0;
372 }
373
374 if ($modulequalified) {
375 print '<tr class="oddeven"><td width="100">';
376 print(empty($module->name) ? $name : $module->name);
377 print "</td><td>\n";
378 if (method_exists($module, 'info')) {
379 print $module->info($langs); // @phan-suppress-current-line PhanUndeclaredMethod
380 } else {
381 print $module->description;
382 }
383 print '</td>';
384
385 // Active
386 if (in_array($name, $def)) {
387 print '<td class="center">'."\n";
388 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($name).'">';
389 print img_picto($langs->trans("Enabled"), 'switch_on');
390 print '</a>';
391 print '</td>';
392 } else {
393 print '<td class="center">'."\n";
394 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>';
395 print "</td>";
396 }
397
398 // Default
399 print '<td class="center">';
400 if (getDolGlobalString("MRP_MO_ADDON_PDF") == $name) {
401 print img_picto($langs->trans("Default"), 'on');
402 } else {
403 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
404 }
405 print '</td>';
406
407 // Info
408 $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
409 $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
410 if ($module->type == 'pdf') {
411 $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
412 }
413 $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
414
415 $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
416 $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
417 $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftMOs").': '.yn($module->option_draft_watermark, 1, 1);
418
419
420 print '<td class="center">';
421 print $form->textwithpicto('', $htmltooltip, 1, 0);
422 print '</td>';
423
424 // Preview
425 print '<td class="center">';
426 if ($module->type == 'pdf') {
427 print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
428 } else {
429 print img_object($langs->transnoentitiesnoconv("PreviewNotAvailable"), 'generic');
430 }
431 print '</td>';
432
433 print "</tr>\n";
434 }
435 }
436 }
437 }
438 }
439 }
440 }
441}
442
443print '</table>';
444print "<br>";
445
446/*
447 * Other options
448 */
449
450print load_fiche_titre($langs->trans("OtherOptions"), '', '');
451print '<table class="noborder centpercent">';
452print '<tr class="liste_titre">';
453print '<td>'.$langs->trans("Parameter").'</td>';
454print '<td class="center" width="60"></td>';
455print "<td>&nbsp;</td>\n";
456print "</tr>\n";
457
458$substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
459$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
460$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
461foreach ($substitutionarray as $key => $val) {
462 $htmltext .= $key.'<br>';
463}
464$htmltext .= '</i>';
465
466print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
467print '<input type="hidden" name="token" value="'.newToken().'">';
468print '<input type="hidden" name="action" value="set_MRP_MO_FREE_TEXT">';
469print '<tr class="oddeven"><td colspan="2">';
470print $form->textwithpicto($langs->trans("FreeLegalTextOnMOs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
471$variablename = 'MRP_MO_FREE_TEXT';
472if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) {
473 print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
474} else {
475 include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
476 $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
477 print $doleditor->Create();
478}
479print '</td><td class="right">';
480print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
481print "</td></tr>\n";
482print '</form>';
483
484//Use draft Watermark
485
486print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
487print '<input type="hidden" name="token" value="'.newToken().'">';
488print "<input type=\"hidden\" name=\"action\" value=\"set_MRP_MO_DRAFT_WATERMARK\">";
489print '<tr class="oddeven"><td>';
490print $form->textwithpicto($langs->trans("WatermarkOnDraftMOs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
491print '</td><td>';
492print '<input class="flat minwidth200" type="text" name="MRP_MO_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('MRP_MO_DRAFT_WATERMARK')).'">';
493print '</td><td class="right">';
494print '<input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'">';
495print "</td></tr>\n";
496print '</form>';
497
498print '</table>';
499print '<br>';
500
501
502// End of page
503llxFooter();
504$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.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:87
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
Class to manage a WYSIWYG editor.
Class to manage generation of HTML components Only common components must be here.
Class for Mo.
Definition mo.class.php:34
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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, $allowothertags=array())
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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.
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
mrpAdminPrepareHead()
Prepare admin pages header.
Definition mrp.lib.php:31
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:153
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.