1<?php
2/* Copyright (C) 2003-2008	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2011	Laurent Destailleur		<eldy@users.sourceforge.net>
4 * Copyright (C) 2004		Sebastien Di Cintio		<sdicintio@ressource-toi.org>
5 * Copyright (C) 2004		Benoit Mortier			<benoit.mortier@opensides.be>
6 * Copyright (C) 2004		Eric Seigne				<eric.seigne@ryxeo.com>
7 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
8 * Copyright (C) 2011-2012	Juanjo Menent			<jmenent@2byte.es>
9 * Copyright (C) 2011-2018	Philippe Grand			<philippe.grand@atoo-net.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25/**
26 *	\file       htdocs/admin/expedition.php
27 *	\ingroup    expedition
28 *	\brief      Page d'administration/configuration du module Expedition
29 */
30
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/expedition.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
36
37// Load translation files required by the page
38$langs->loadLangs(array("admin", "sendings", "deliveries", "other"));
39
40if (!$user->admin)
41	accessforbidden();
42
43$action = GETPOST('action', 'aZ09');
44$value = GETPOST('value', 'alpha');
45$label = GETPOST('label', 'alpha');
46$scandir = GETPOST('scan_dir', 'alpha');
47$type = 'shipping';
48
49if (empty($conf->global->EXPEDITION_ADDON_NUMBER))
50{
51	$conf->global->EXPEDITION_ADDON_NUMBER = 'mod_expedition_safor';
52}
53
54
55/*
56 * Actions
57 */
58
59include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
60
61if ($action == 'updateMask')
62{
63	$maskconst = GETPOST('maskconstexpedition', 'alpha');
64	$maskvalue = GETPOST('maskexpedition', 'alpha');
65	if (!empty($maskconst))
66		$res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
67
68	if (isset($res))
69	{
70		if ($res > 0)
71			setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
72		else setEventMessages($langs->trans("Error"), null, 'errors');
73	}
74} elseif ($action == 'set_param')
75{
76	$freetext = GETPOST('SHIPPING_FREE_TEXT', 'restricthtml'); // No alpha here, we want exact string
77	$res = dolibarr_set_const($db, "SHIPPING_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
78	if ($res <= 0)
79	{
80		$error++;
81		setEventMessages($langs->trans("Error"), null, 'errors');
82	}
83
84	$draft = GETPOST('SHIPPING_DRAFT_WATERMARK', 'alpha');
85	$res = dolibarr_set_const($db, "SHIPPING_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
86	if ($res <= 0)
87	{
88		$error++;
89		setEventMessages($langs->trans("Error"), null, 'errors');
90	}
91
92	if (!$error)
93	{
94		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
95	}
96} elseif ($action == 'specimen')
97{
98	$modele = GETPOST('module', 'alpha');
99
100	$exp = new Expedition($db);
101	$exp->initAsSpecimen();
102
103	// Search template files
104	$file = ''; $classname = ''; $filefound = 0;
105	$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
106	foreach ($dirmodels as $reldir)
107	{
108		$file = dol_buildpath($reldir."core/modules/expedition/doc/pdf_".$modele.".modules.php", 0);
109		if (file_exists($file))
110		{
111			$filefound = 1;
112			$classname = "pdf_".$modele;
113			break;
114		}
115	}
116
117	if ($filefound)
118	{
119		require_once $file;
120
121		$module = new $classname($db);
122
123		if ($module->write_file($exp, $langs) > 0)
124		{
125			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=expedition&file=SPECIMEN.pdf");
126			return;
127		} else {
128			setEventMessages($module->error, $module->errors, 'errors');
129			dol_syslog($module->error, LOG_ERR);
130		}
131	} else {
132		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
133		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
134	}
135}
136
137// Activate a model
138elseif ($action == 'set')
139{
140	$ret = addDocumentModel($value, $type, $label, $scandir);
141} elseif ($action == 'del')
142{
143	$ret = delDocumentModel($value, $type);
144	if ($ret > 0)
145	{
146		if ($conf->global->EXPEDITION_ADDON_PDF == "$value") dolibarr_del_const($db, 'EXPEDITION_ADDON_PDF', $conf->entity);
147	}
148}
149
150// Set default model
151elseif ($action == 'setdoc')
152{
153	if (dolibarr_set_const($db, "EXPEDITION_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
154	{
155		// La constante qui a ete lue en avant du nouveau set
156		// on passe donc par une variable pour avoir un affichage coherent
157		$conf->global->EXPEDITION_ADDON_PDF = $value;
158	}
159
160	// On active le modele
161	$ret = delDocumentModel($value, $type);
162	if ($ret > 0)
163	{
164		$ret = addDocumentModel($value, $type, $label, $scandir);
165	}
166} elseif ($action == 'setmodel')
167{
168	dolibarr_set_const($db, "EXPEDITION_ADDON_NUMBER", $value, 'chaine', 0, '', $conf->entity);
169}
170
171
172/*
173 * View
174 */
175
176$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
177
178$form = new Form($db);
179
180llxHeader("", $langs->trans("SendingsSetup"));
181
182$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
183print load_fiche_titre($langs->trans("SendingsSetup"), $linkback, 'title_setup');
184print '<br>';
185$head = expedition_admin_prepare_head();
186
187print dol_get_fiche_head($head, 'shipment', $langs->trans("Sendings"), -1, 'shipment');
188
189// Shipment numbering model
190
191print load_fiche_titre($langs->trans("SendingsNumberingModules"), '', '');
192
193print '<table class="noborder centpercent">';
194print '<tr class="liste_titre">';
195print '<td width="100">'.$langs->trans("Name").'</td>';
196print '<td>'.$langs->trans("Description").'</td>';
197print '<td>'.$langs->trans("Example").'</td>';
198print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
199print '<td class="center" width="80">'.$langs->trans("ShortInfo").'</td>';
200print "</tr>\n";
201
202clearstatcache();
203
204foreach ($dirmodels as $reldir)
205{
206	$dir = dol_buildpath($reldir."core/modules/expedition/");
207
208	if (is_dir($dir))
209	{
210		$handle = opendir($dir);
211		if (is_resource($handle))
212		{
213			while (($file = readdir($handle)) !== false)
214			{
215				if (preg_match('/^mod_expedition_([a-z0-9_]*)\.php$/', $file)) {
216					$file = substr($file, 0, dol_strlen($file) - 4);
217
218					require_once $dir.$file.'.php';
219
220					$module = new $file;
221
222					if ($module->isEnabled())
223					{
224						// Show modules according to features level
225						if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
226						if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
227
228						print '<tr><td>'.$module->name."</td>\n";
229						print '<td>';
230						print $module->info();
231						print '</td>';
232
233						// Show example of numbering module
234						print '<td class="nowrap">';
235						$tmp = $module->getExample();
236						if (preg_match('/^Error/', $tmp)) {
237							$langs->load("errors");
238							print '<div class="error">'.$langs->trans($tmp).'</div>';
239						} elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
240						else print $tmp;
241						print '</td>'."\n";
242
243						print '<td class="center">';
244						if ($conf->global->EXPEDITION_ADDON_NUMBER == "$file")
245						{
246							print img_picto($langs->trans("Activated"), 'switch_on');
247						} else {
248							print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmodel&amp;token='.newToken().'&amp;value='.$file.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">';
249							print img_picto($langs->trans("Disabled"), 'switch_off');
250							print '</a>';
251						}
252						print '</td>';
253
254						$expedition = new Expedition($db);
255						$expedition->initAsSpecimen();
256
257						// Info
258						$htmltooltip = '';
259						$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
260						$nextval = $module->getNextValue($mysoc, $expedition);
261						if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
262							$htmltooltip .= ''.$langs->trans("NextValue").': ';
263							if ($nextval) {
264								if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
265									$nextval = $langs->trans($nextval);
266								$htmltooltip .= $nextval.'<br>';
267							} else {
268								$htmltooltip .= $langs->trans($module->error).'<br>';
269							}
270						}
271
272						print '<td class="center">';
273						print $form->textwithpicto('', $htmltooltip, 1, 0);
274						print '</td>';
275
276						print '</tr>';
277					}
278				}
279			}
280			closedir($handle);
281		}
282	}
283}
284
285print '</table><br>';
286
287
288/*
289 *  Documents models for Sendings Receipt
290 */
291print load_fiche_titre($langs->trans("SendingsReceiptModel"), '', '');
292
293// Defini tableau def de modele invoice
294$type = "shipping";
295$def = array();
296
297$sql = "SELECT nom";
298$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
299$sql .= " WHERE type = '".$db->escape($type)."'";
300$sql .= " AND entity = ".$conf->entity;
301
302$resql = $db->query($sql);
303if ($resql)
304{
305	$i = 0;
306	$num_rows = $db->num_rows($resql);
307	while ($i < $num_rows)
308	{
309		$array = $db->fetch_array($resql);
310		array_push($def, $array[0]);
311		$i++;
312	}
313} else {
314	dol_print_error($db);
315}
316
317print '<table class="noborder centpercent">';
318print '<tr class="liste_titre">';
319print '<td width="140">'.$langs->trans("Name").'</td>';
320print '<td>'.$langs->trans("Description").'</td>';
321print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
322print '<td class="center" width="60">'.$langs->trans("Default").'</td>';
323print '<td class="nowrap center" width="80">'.$langs->trans("ShortInfo").'</td>';
324print '<td class="nowrap center" width="80" >'.$langs->trans("Preview").'</td>';
325print "</tr>\n";
326
327clearstatcache();
328
329foreach ($dirmodels as $reldir)
330{
331	foreach (array('', '/doc') as $valdir)
332	{
333		$dir = dol_buildpath($reldir."core/modules/expedition".$valdir);
334
335		if (is_dir($dir))
336		{
337			$handle = opendir($dir);
338			if (is_resource($handle))
339			{
340				while (($file = readdir($handle)) !== false)
341				{
342					$filelist[] = $file;
343				}
344				closedir($handle);
345				arsort($filelist);
346
347				foreach ($filelist as $file)
348				{
349					if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
350					{
351						if (file_exists($dir.'/'.$file))
352						{
353							$name = substr($file, 4, dol_strlen($file) - 16);
354							$classname = substr($file, 0, dol_strlen($file) - 12);
355
356							require_once $dir.'/'.$file;
357							$module = new $classname($db);
358
359							$modulequalified = 1;
360							if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified = 0;
361							if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified = 0;
362
363							if ($modulequalified)
364							{
365								print '<tr><td width="100">';
366								print (empty($module->name) ? $name : $module->name);
367								print "</td><td>\n";
368								if (method_exists($module, 'info')) print $module->info($langs);
369								else print $module->description;
370								print '</td>';
371
372								// Active
373								if (in_array($name, $def))
374								{
375									print '<td class="center">'."\n";
376									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&value='.$name.'">';
377									print img_picto($langs->trans("Enabled"), 'switch_on');
378									print '</a>';
379									print '</td>';
380								} else {
381									print '<td class="center">'."\n";
382									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=set&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
383									print "</td>";
384								}
385
386								// Defaut
387								print '<td class="center">';
388								if ($conf->global->EXPEDITION_ADDON_PDF == $name)
389								{
390									print img_picto($langs->trans("Default"), 'on');
391								} else {
392									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
393								}
394								print '</td>';
395
396								// Info
397								$htmltooltip = ''.$langs->trans("Name").': '.$module->name;
398								$htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
399								if ($module->type == 'pdf')
400								{
401									$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
402								}
403								$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
404								$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
405								$htmltooltip .= '<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg, 1, 1);
406								$htmltooltip .= '<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg, 1, 1);
407								$htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
408								$htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark, 1, 1);
409
410								print '<td class="center">';
411								print $form->textwithpicto('', $htmltooltip, 1, 0);
412								print '</td>';
413
414								// Preview
415								print '<td class="center">';
416								if ($module->type == 'pdf')
417								{
418									print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
419								} else {
420									print img_object($langs->trans("PreviewNotAvailable"), 'generic');
421								}
422								print '</td>';
423
424								print "</tr>\n";
425							}
426						}
427					}
428				}
429			}
430		}
431	}
432}
433
434print '</table>';
435print '<br>';
436
437
438/*
439 * Other options
440 *
441 */
442print load_fiche_titre($langs->trans("OtherOptions"), '', '');
443
444print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
445print '<input type="hidden" name="token" value="'.newToken().'">';
446print '<input type="hidden" name="action" value="set_param">';
447
448print "<table class=\"noborder\" width=\"100%\">";
449print "<tr class=\"liste_titre\">";
450print "<td>".$langs->trans("Parameter")."</td>\n";
451print "</tr>";
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)	$htmltext .= $key.'<br>';
457$htmltext .= '</i>';
458
459print '<tr><td>';
460print $form->textwithpicto($langs->trans("FreeLegalTextOnShippings"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
461$variablename = 'SHIPPING_FREE_TEXT';
462if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
463{
464	print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
465} else {
466	include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
467	$doleditor = new DolEditor($variablename, $conf->global->$variablename, '', 80, 'dolibarr_notes');
468	print $doleditor->Create();
469}
470print "</td></tr>\n";
471
472print '<tr><td>';
473print $form->textwithpicto($langs->trans("WatermarkOnDraftContractCards"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
474print '<input size="50" class="flat" type="text" name="SHIPPING_DRAFT_WATERMARK" value="'.$conf->global->SHIPPING_DRAFT_WATERMARK.'">';
475print "</td></tr>\n";
476
477print '</table>';
478
479print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
480
481print '</form>';
482
483// End of page
484llxFooter();
485$db->close();
486