1<?php
2/* Copyright (C) 2003-2004 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) 2008      Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
9 * Copyright (C) 2011-2013 Juanjo Menent               <jmenent@2byte.es>
10 * Copyright (C) 2015      Jean-François Ferry		   <jfefe@aternatik.fr>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/supplier_proposal.lib.php';
31
32// Load translation files required by the page
33$langs->loadLangs(array("admin", "errors", "other", "supplier_proposal"));
34
35if (!$user->admin) accessforbidden();
36
37$action = GETPOST('action', 'aZ09');
38$value = GETPOST('value', 'alpha');
39$label = GETPOST('label', 'alpha');
40$scandir = GETPOST('scan_dir', 'alpha');
41$type = 'supplier_proposal';
42
43$error = 0;
44
45
46/*
47 * Actions
48 */
49
50include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
51
52if ($action == 'updateMask')
53{
54	$maskconstsupplier_proposal = GETPOST('maskconstsupplier_proposal', 'alpha');
55	$masksupplier_proposal = GETPOST('masksupplier_proposal', 'alpha');
56	if ($maskconstsupplier_proposal) $res = dolibarr_set_const($db, $maskconstsupplier_proposal, $masksupplier_proposal, 'chaine', 0, '', $conf->entity);
57
58	if (!($res > 0)) $error++;
59
60 	if (!$error)
61	{
62		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
63	} else {
64		setEventMessages($langs->trans("Error"), null, 'errors');
65	}
66}
67
68if ($action == 'specimen')
69{
70	$modele = GETPOST('module', 'alpha');
71
72	$supplier_proposal = new SupplierProposal($db);
73	$supplier_proposal->initAsSpecimen();
74
75	// Search template files
76	$file = ''; $classname = ''; $filefound = 0;
77	$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
78	foreach ($dirmodels as $reldir)
79	{
80		$file = dol_buildpath($reldir."core/modules/supplier_proposal/doc/pdf_".$modele.".modules.php");
81		if (file_exists($file))
82		{
83			$filefound = 1;
84			$classname = "pdf_".$modele;
85			break;
86		}
87	}
88
89	if ($filefound)
90	{
91		require_once $file;
92
93		$module = new $classname($db);
94
95		if ($module->write_file($supplier_proposal, $langs) > 0)
96		{
97			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=supplier_proposal&file=SPECIMEN.pdf");
98			return;
99		} else {
100			setEventMessages($module->error, null, 'errors');
101			dol_syslog($module->error, LOG_ERR);
102		}
103	} else {
104		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
105		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
106	}
107}
108
109if ($action == 'set_SUPPLIER_PROPOSAL_DRAFT_WATERMARK')
110{
111	$draft = GETPOST('SUPPLIER_PROPOSAL_DRAFT_WATERMARK', 'alpha');
112
113	$res = dolibarr_set_const($db, "SUPPLIER_PROPOSAL_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
114	if (!($res > 0)) $error++;
115
116 	if (!$error)
117	{
118		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
119	} else {
120		setEventMessages($langs->trans("Error"), null, 'errors');
121	}
122}
123
124if ($action == 'set_SUPPLIER_PROPOSAL_FREE_TEXT')
125{
126	$freetext = GETPOST('SUPPLIER_PROPOSAL_FREE_TEXT', 'restricthtml'); // No alpha here, we want exact string
127
128	$res = dolibarr_set_const($db, "SUPPLIER_PROPOSAL_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
129
130	if (!($res > 0)) $error++;
131
132 	if (!$error)
133	{
134		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
135	} else {
136		setEventMessages($langs->trans("Error"), null, 'errors');
137	}
138}
139
140if ($action == 'set_BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL')
141{
142	$res = dolibarr_set_const($db, "BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL", $value, 'chaine', 0, '', $conf->entity);
143
144	if (!($res > 0)) $error++;
145
146	if (!$error)
147	{
148		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
149	} else {
150		setEventMessages($langs->trans("Error"), null, 'errors');
151	}
152}
153
154// Activate a model
155if ($action == 'set')
156{
157	$ret = addDocumentModel($value, $type, $label, $scandir);
158} elseif ($action == 'del')
159{
160	$ret = delDocumentModel($value, $type);
161	if ($ret > 0)
162	{
163		if ($conf->global->SUPPLIER_PROPOSAL_ADDON_PDF == "$value") dolibarr_del_const($db, 'SUPPLIER_PROPOSAL_ADDON_PDF', $conf->entity);
164	}
165} elseif ($action == 'setdoc')
166{
167	if (dolibarr_set_const($db, "SUPPLIER_PROPOSAL_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
168	{
169		$conf->global->SUPPLIER_PROPOSAL_ADDON_PDF = $value;
170	}
171
172	// On active le modele
173	$ret = delDocumentModel($value, $type);
174	if ($ret > 0)
175	{
176		$ret = addDocumentModel($value, $type, $label, $scandir);
177	}
178} elseif ($action == 'setmod')
179{
180	// TODO Verifier si module numerotation choisi peut etre active
181	// par appel methode canBeActivated
182
183	dolibarr_set_const($db, "SUPPLIER_PROPOSAL_ADDON", $value, 'chaine', 0, '', $conf->entity);
184}
185
186
187/*
188 * Affiche page
189 */
190
191$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
192
193
194llxHeader('', $langs->trans("SupplierProposalSetup"));
195
196$form = new Form($db);
197
198//if ($mesg) print $mesg;
199
200$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
201print load_fiche_titre($langs->trans("SupplierProposalSetup"), $linkback, 'title_setup');
202
203$head = supplier_proposal_admin_prepare_head();
204
205print dol_get_fiche_head($head, 'general', $langs->trans("CommRequests"), -1, 'supplier_proposal');
206
207/*
208 *  Module numerotation
209 */
210print load_fiche_titre($langs->trans("SupplierProposalNumberingModules"), '', '');
211
212print '<table class="noborder centpercent">';
213print '<tr class="liste_titre">';
214print '<td>'.$langs->trans("Name")."</td>\n";
215print '<td>'.$langs->trans("Description")."</td>\n";
216print '<td class="nowrap">'.$langs->trans("Example")."</td>\n";
217print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
218print '<td align="center" width="16">'.$langs->trans("ShortInfo").'</td>';
219print '</tr>'."\n";
220
221clearstatcache();
222foreach ($dirmodels as $reldir)
223{
224	$dir = dol_buildpath($reldir."core/modules/supplier_proposal");
225
226	if (is_dir($dir))
227	{
228		$handle = opendir($dir);
229		if (is_resource($handle))
230		{
231			while (($file = readdir($handle)) !== false)
232			{
233				if (substr($file, 0, 22) == 'mod_supplier_proposal_' && substr($file, dol_strlen($file) - 3, 3) == 'php')
234				{
235					$file = substr($file, 0, dol_strlen($file) - 4);
236
237					require_once $dir.'/'.$file.'.php';
238
239					$module = new $file;
240
241					// Show modules according to features level
242					if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
243					if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
244
245					if ($module->isEnabled())
246					{
247						print '<tr class="oddeven"><td>'.$module->nom."</td><td>\n";
248						print $module->info();
249						print '</td>';
250
251						// Show example of numbering module
252						print '<td class="nowrap">';
253						$tmp = $module->getExample();
254						if (preg_match('/^Error/', $tmp)) {
255							$langs->load("errors");
256							print '<div class="error">'.$langs->trans($tmp).'</div>';
257						} elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
258						else print $tmp;
259						print '</td>'."\n";
260
261						print '<td class="center">';
262						if ($conf->global->SUPPLIER_PROPOSAL_ADDON == "$file")
263						{
264							print img_picto($langs->trans("Activated"), 'switch_on');
265						} else {
266							print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;token='.newToken().'&amp;value='.urlencode($file).'">';
267							print img_picto($langs->trans("Disabled"), 'switch_off');
268							print '</a>';
269						}
270						print '</td>';
271
272						$supplier_proposal = new SupplierProposal($db);
273						$supplier_proposal->initAsSpecimen();
274
275						// Info
276						$htmltooltip = '';
277						$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
278						$nextval = $module->getNextValue($mysoc, $supplier_proposal);
279						if ("$nextval" != $langs->trans("NotAvailable")) {  // Keep " on nextval
280							$htmltooltip .= ''.$langs->trans("NextValue").': ';
281							if ($nextval) {
282								if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
283									$nextval = $langs->trans($nextval);
284								$htmltooltip .= $nextval.'<br>';
285							} else {
286								$htmltooltip .= $langs->trans($module->error).'<br>';
287							}
288						}
289
290						print '<td class="center">';
291						print $form->textwithpicto('', $htmltooltip, 1, 0);
292						print '</td>';
293
294						print "</tr>\n";
295					}
296				}
297			}
298			closedir($handle);
299		}
300	}
301}
302print "</table><br>\n";
303
304
305/*
306 * Document templates generators
307 */
308
309print load_fiche_titre($langs->trans("SupplierProposalPDFModules"), '', '');
310
311// Load array def with activated templates
312$def = array();
313$sql = "SELECT nom";
314$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
315$sql .= " WHERE type = '".$db->escape($type)."'";
316$sql .= " AND entity = ".$conf->entity;
317$resql = $db->query($sql);
318if ($resql)
319{
320	$i = 0;
321	$num_rows = $db->num_rows($resql);
322	while ($i < $num_rows)
323	{
324		$array = $db->fetch_array($resql);
325		array_push($def, $array[0]);
326		$i++;
327	}
328} else {
329	dol_print_error($db);
330}
331
332
333print "<table class=\"noborder\" width=\"100%\">\n";
334print "<tr class=\"liste_titre\">\n";
335print "  <td>".$langs->trans("Name")."</td>\n";
336print "  <td>".$langs->trans("Description")."</td>\n";
337print '<td align="center" width="40">'.$langs->trans("Status")."</td>\n";
338print '<td align="center" width="40">'.$langs->trans("Default")."</td>\n";
339print '<td align="center" width="40">'.$langs->trans("ShortInfo").'</td>';
340print '<td align="center" width="40">'.$langs->trans("Preview").'</td>';
341print "</tr>\n";
342
343clearstatcache();
344
345foreach ($dirmodels as $reldir)
346{
347	foreach (array('', '/doc') as $valdir)
348	{
349		$realpath = $reldir."core/modules/supplier_proposal".$valdir;
350		$dir = dol_buildpath($realpath);
351
352		if (is_dir($dir))
353		{
354			$handle = opendir($dir);
355			if (is_resource($handle))
356			{
357				while (($file = readdir($handle)) !== false)
358				{
359					$filelist[] = $file;
360				}
361				closedir($handle);
362				arsort($filelist);
363
364				foreach ($filelist as $file)
365				{
366					if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
367					{
368						if (file_exists($dir.'/'.$file))
369						{
370							$name = substr($file, 4, dol_strlen($file) - 16);
371							$classname = substr($file, 0, dol_strlen($file) - 12);
372
373							require_once $dir.'/'.$file;
374							$module = new $classname($db);
375
376							$modulequalified = 1;
377							if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified = 0;
378							if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified = 0;
379
380							if ($modulequalified)
381							{
382								print '<tr class="oddeven"><td width="100">';
383								print (empty($module->name) ? $name : $module->name);
384								print "</td><td>\n";
385								if (method_exists($module, 'info')) print $module->info($langs);
386								else print $module->description;
387								print '</td>';
388
389								// Active
390								if (in_array($name, $def))
391								{
392									print '<td class="center">'."\n";
393									print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&amp;token='.newToken().'&amp;value='.$name.'">';
394									print img_picto($langs->trans("Enabled"), 'switch_on');
395									print '</a>';
396									print '</td>';
397								} else {
398									print "<td align=\"center\">\n";
399									print '<a 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>';
400									print "</td>";
401								}
402
403								// Defaut
404								print "<td align=\"center\">";
405								if ($conf->global->SUPPLIER_PROPOSAL_ADDON_PDF == "$name")
406								{
407									print img_picto($langs->trans("Default"), 'on');
408								} else {
409									print '<a 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>';
410								}
411								print '</td>';
412
413								// Info
414								$htmltooltip = ''.$langs->trans("Name").': '.$module->name;
415								$htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
416								if ($module->type == 'pdf')
417								{
418									$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
419								}
420								$htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
421
422								$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
423								$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
424								$htmltooltip .= '<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg, 1, 1);
425								$htmltooltip .= '<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg, 1, 1);
426								$htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
427								//$htmltooltip.='<br>'.$langs->trans("Discounts").': '.yn($module->option_escompte,1,1);
428								//$htmltooltip.='<br>'.$langs->trans("CreditNote").': '.yn($module->option_credit_note,1,1);
429								$htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftProposal").': '.yn($module->option_draft_watermark, 1, 1);
430
431
432								print '<td class="center">';
433								print $form->textwithpicto('', $htmltooltip, 1, 0);
434								print '</td>';
435
436								// Preview
437								print '<td class="center">';
438								if ($module->type == 'pdf')
439								{
440									print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
441								} else {
442									print img_object($langs->trans("PreviewNotAvailable"), 'generic');
443								}
444								print '</td>';
445
446								print "</tr>\n";
447							}
448						}
449					}
450				}
451			}
452		}
453	}
454}
455
456print '</table>';
457print '<br>';
458
459
460/*
461 * Other options
462 *
463 */
464print load_fiche_titre($langs->trans("OtherOptions"), '', '');
465
466print "<table class=\"noborder\" width=\"100%\">";
467print "<tr class=\"liste_titre\">";
468print "<td>".$langs->trans("Parameter")."</td>\n";
469print '<td width="60" align="center">'.$langs->trans("Value")."</td>\n";
470print "<td>&nbsp;</td>\n";
471print "</tr>";
472
473$substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
474$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
475$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
476foreach ($substitutionarray as $key => $val)	$htmltext .= $key.'<br>';
477$htmltext .= '</i>';
478
479print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
480print '<input type="hidden" name="token" value="'.newToken().'">';
481print '<input type="hidden" name="action" value="set_SUPPLIER_PROPOSAL_FREE_TEXT">';
482print '<tr class="oddeven"><td colspan="2">';
483print $form->textwithpicto($langs->trans("FreeLegalTextOnSupplierProposal"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
484$variablename = 'SUPPLIER_PROPOSAL_FREE_TEXT';
485if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
486{
487	print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
488} else {
489	include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
490	$doleditor = new DolEditor($variablename, $conf->global->$variablename, '', 80, 'dolibarr_notes');
491	print $doleditor->Create();
492}
493print '</td><td class="right">';
494print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
495print "</td></tr>\n";
496print '</form>';
497
498
499print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
500print '<input type="hidden" name="token" value="'.newToken().'">';
501print "<input type=\"hidden\" name=\"action\" value=\"set_SUPPLIER_PROPOSAL_DRAFT_WATERMARK\">";
502print '<tr class="oddeven"><td>';
503print $form->textwithpicto($langs->trans("WatermarkOnDraftProposal"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
504print '</td><td>';
505print '<input size="50" class="flat" type="text" name="SUPPLIER_PROPOSAL_DRAFT_WATERMARK" value="'.$conf->global->SUPPLIER_PROPOSAL_DRAFT_WATERMARK.'">';
506print '</td><td class="right">';
507print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
508print "</td></tr>\n";
509print '</form>';
510
511if ($conf->banque->enabled)
512{
513	print '<tr class="oddeven"><td>';
514	print $langs->trans("BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL").'</td><td>&nbsp</td><td class="right">';
515	if (!empty($conf->use_javascript_ajax))
516	{
517		print ajax_constantonoff('BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL');
518	} else {
519		if (empty($conf->global->BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL))
520		{
521			print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL&amp;token='.newToken().'&amp;value=1">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
522		} else {
523			print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL&amp;token='.newToken().'&amp;value=0">'.img_picto($langs->trans("Enabled"), 'switch_on').'</a>';
524		}
525	}
526	print '</td></tr>';
527} else {
528	print '<tr class="oddeven"><td>';
529	print $langs->trans("BANK_ASK_PAYMENT_BANK_DURING_SUPPLIER_PROPOSAL").'</td><td>&nbsp;</td><td align="center">'.$langs->trans('NotAvailable').'</td></tr>';
530}
531
532print '</table>';
533
534
535
536/*
537 *  Directory
538 */
539print '<br>';
540print load_fiche_titre($langs->trans("PathToDocuments"), '', '');
541
542print "<table class=\"noborder\" width=\"100%\">\n";
543print "<tr class=\"liste_titre\">\n";
544print "  <td>".$langs->trans("Name")."</td>\n";
545print "  <td>".$langs->trans("Value")."</td>\n";
546print "</tr>\n";
547print "<tr class=\"oddeven\">\n  <td width=\"140\">".$langs->trans("PathDirectory")."</td>\n  <td>".$conf->supplier_proposal->dir_output."</td>\n</tr>\n";
548print "</table>\n<br>";
549
550// End of page
551llxFooter();
552$db->close();
553