1<?php
2/* Copyright (C) 2011-2013      Juanjo Menent	    <jmenent@2byte.es>
3 * Copyright (C) 2011-2018      Philippe Grand	    <philippe.grand@atoo-net.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *	\file       htdocs/admin/contract.php
21 *	\ingroup    contract
22 *	\brief      Setup page of module Contracts
23 */
24
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.'/contrat/class/contrat.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/contract.lib.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array("admin", "errors", "contracts"));
33
34if (!$user->admin) accessforbidden();
35
36$action = GETPOST('action', 'aZ09');
37$value = GETPOST('value', 'alpha');
38$label = GETPOST('label', 'alpha');
39$scandir = GETPOST('scan_dir', 'alpha');
40$type = 'contract';
41
42if (empty($conf->global->CONTRACT_ADDON))
43{
44	$conf->global->CONTRACT_ADDON = 'mod_contract_serpis';
45}
46
47
48/*
49 * Actions
50 */
51
52include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
53
54if ($action == 'updateMask')
55{
56	$maskconst = GETPOST('maskconstcontract', 'alpha');
57	$maskvalue = GETPOST('maskcontract', 'alpha');
58	if ($maskconst) $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
59
60	if (!($res > 0)) $error++;
61
62 	if (!$error)
63	{
64		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
65	} else {
66		setEventMessages($langs->trans("Error"), null, 'errors');
67	}
68} elseif ($action == 'specimen') // For contract
69{
70	$modele = GETPOST('module', 'alpha');
71
72	$contract = new Contrat($db);
73	$contract->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/contract/doc/pdf_".$modele.".modules.php", 0);
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($contract, $langs) > 0)
96		{
97			header("Location: ".DOL_URL_ROOT."/document.php?modulepart=contract&file=SPECIMEN.pdf");
98			return;
99		} else {
100			setEventMessages($obj->error, $obj->errors, 'errors');
101			dol_syslog($obj->error, LOG_ERR);
102		}
103	} else {
104		setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
105		dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
106	}
107}
108
109// Activate a model
110elseif ($action == 'set')
111{
112	$ret = addDocumentModel($value, $type, $label, $scandir);
113} elseif ($action == 'del')
114{
115	$ret = delDocumentModel($value, $type);
116	if ($ret > 0)
117	{
118		if ($conf->global->CONTRACT_ADDON_PDF == "$value") dolibarr_del_const($db, 'CONTRACT_ADDON_PDF', $conf->entity);
119	}
120}
121
122// Set default model
123elseif ($action == 'setdoc')
124{
125	if (dolibarr_set_const($db, "CONTRACT_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
126	{
127		// La constante qui a ete lue en avant du nouveau set
128		// on passe donc par une variable pour avoir un affichage coherent
129		$conf->global->CONTRACT_ADDON_PDF = $value;
130	}
131
132	// On active le modele
133	$ret = delDocumentModel($value, $type);
134	if ($ret > 0)
135	{
136		$ret = addDocumentModel($value, $type, $label, $scandir);
137	}
138} elseif ($action == 'setmod')
139{
140	// TODO Verifier si module numerotation choisi peut etre active
141	// par appel methode canBeActivated
142
143	dolibarr_set_const($db, "CONTRACT_ADDON", $value, 'chaine', 0, '', $conf->entity);
144} elseif ($action == 'set_other')
145{
146	$freetext = GETPOST('CONTRACT_FREE_TEXT', 'restricthtml'); // No alpha here, we want exact string
147	$res1 = dolibarr_set_const($db, "CONTRACT_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
148
149	$draft = GETPOST('CONTRACT_DRAFT_WATERMARK', 'alpha');
150	$res2 = dolibarr_set_const($db, "CONTRACT_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
151
152	$value = GETPOST('activate_hideClosedServiceByDefault', 'alpha');
153	$res3 = dolibarr_set_const($db, "CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT", $value, 'chaine', 0, '', $conf->entity);
154
155	if (!$res1 > 0 || !$res2 > 0 || !$res3 > 0) $error++;
156
157 	if (!$error)
158	{
159		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
160	} else {
161		setEventMessages($langs->trans("Error"), null, 'errors');
162	}
163}
164
165
166/*
167 * View
168 */
169
170$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
171
172llxHeader();
173
174$form = new Form($db);
175
176$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
177print load_fiche_titre($langs->trans("ContractsSetup"), $linkback, 'title_setup');
178
179$head = contract_admin_prepare_head();
180
181print dol_get_fiche_head($head, 'contract', $langs->trans("Contracts"), -1, 'contract');
182
183/*
184 * Contracts Numbering model
185 */
186
187print load_fiche_titre($langs->trans("ContractsNumberingModules"), '', '');
188
189print '<table class="noborder centpercent">';
190print '<tr class="liste_titre">';
191print '<td width="100">'.$langs->trans("Name").'</td>';
192print '<td>'.$langs->trans("Description").'</td>';
193print '<td>'.$langs->trans("Example").'</td>';
194print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
195print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
196print "</tr>\n";
197
198clearstatcache();
199
200foreach ($dirmodels as $reldir)
201{
202	$dir = dol_buildpath($reldir."core/modules/contract/");
203
204	if (is_dir($dir))
205	{
206		$handle = opendir($dir);
207		if (is_resource($handle))
208		{
209			while (($file = readdir($handle)) !== false)
210			{
211				if (substr($file, 0, 13) == 'mod_contract_' && substr($file, dol_strlen($file) - 3, 3) == 'php')
212				{
213					$file = substr($file, 0, dol_strlen($file) - 4);
214
215					require_once $dir.$file.'.php';
216
217					$module = new $file($db);
218
219					// Show modules according to features level
220					if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
221					if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
222
223					if ($module->isEnabled())
224					{
225						print '<tr class="oddeven"><td>'.$module->nom."</td><td>\n";
226						print $module->info();
227						print '</td>';
228
229						// Show example of numbering model
230						print '<td class="nowrap">';
231						$tmp = $module->getExample();
232						if (preg_match('/^Error/', $tmp)) {
233							$langs->load("errors");
234							print '<div class="error">'.$langs->trans($tmp).'</div>';
235						}
236						elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
237						else print $tmp;
238						print '</td>'."\n";
239
240						print '<td class="center">';
241						if ($conf->global->CONTRACT_ADDON == "$file")
242						{
243							print img_picto($langs->trans("Activated"), 'switch_on');
244						} else {
245							print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;token='.newToken().'&amp;value='.urlencode($file).'">';
246							print img_picto($langs->trans("Disabled"), 'switch_off');
247							print '</a>';
248						}
249						print '</td>';
250
251						$contract = new Contrat($db);
252						$contract->initAsSpecimen();
253
254						// Info
255						$htmltooltip = '';
256						$htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
257						$nextval = $module->getNextValue($mysoc, $contract);
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								$htmltooltip .= $nextval.'<br>';
264							} else {
265								$htmltooltip .= $langs->trans($module->error).'<br>';
266							}
267						}
268
269						print '<td class="center">';
270						print $form->textwithpicto('', $htmltooltip, 1, 0);
271						print '</td>';
272
273						print '</tr>';
274					}
275				}
276			}
277			closedir($handle);
278		}
279	}
280}
281
282print '</table><br>';
283
284/*
285 *  Documents models for Contracts
286 */
287
288print load_fiche_titre($langs->trans("TemplatePDFContracts"), '', '');
289
290// Defini tableau def des modeles
291$def = array();
292$sql = "SELECT nom";
293$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
294$sql .= " WHERE type = '".$db->escape($type)."'";
295$sql .= " AND entity = ".$conf->entity;
296$resql = $db->query($sql);
297if ($resql)
298{
299	$i = 0;
300	$num_rows = $db->num_rows($resql);
301	while ($i < $num_rows)
302	{
303		$array = $db->fetch_array($resql);
304		array_push($def, $array[0]);
305		$i++;
306	}
307} else {
308	dol_print_error($db);
309}
310
311
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="80">'.$langs->trans("ShortInfo").'</td>';
319print '<td class="center" width="80">'.$langs->trans("Preview").'</td>';
320print "</tr>\n";
321
322clearstatcache();
323
324foreach ($dirmodels as $reldir)
325{
326	foreach (array('', '/doc') as $valdir)
327	{
328		$realpath = $reldir."core/modules/contract".$valdir;
329		$dir = dol_buildpath($realpath);
330
331		if (is_dir($dir))
332		{
333			$handle = opendir($dir);
334			if (is_resource($handle))
335			{
336				while (($file = readdir($handle)) !== false)
337				{
338					$filelist[] = $file;
339				}
340				closedir($handle);
341				arsort($filelist);
342
343				foreach ($filelist as $file)
344				{
345					if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
346					{
347						if (file_exists($dir.'/'.$file))
348						{
349							$name = substr($file, 4, dol_strlen($file) - 16);
350							$classname = substr($file, 0, dol_strlen($file) - 12);
351
352							require_once $dir.'/'.$file;
353							$module = new $classname($db);
354
355							$modulequalified = 1;
356							if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified = 0;
357							if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified = 0;
358
359							if ($modulequalified)
360							{
361								print '<tr class="oddeven"><td width="100">';
362								print (empty($module->name) ? $name : $module->name);
363								print "</td><td>\n";
364								if (method_exists($module, 'info')) print $module->info($langs);
365								else print $module->description;
366								print '</td>';
367
368								// Active
369								if (in_array($name, $def))
370								{
371									print '<td class="center">'."\n";
372									print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&value='.$name.'">';
373									print img_picto($langs->trans("Enabled"), 'switch_on');
374									print '</a>';
375									print '</td>';
376								} else {
377									print '<td class="center">'."\n";
378									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>';
379									print "</td>";
380								}
381
382								// Defaut
383								print '<td class="center">';
384								if ($conf->global->CONTRACT_ADDON_PDF == $name)
385								{
386									print img_picto($langs->trans("Default"), 'on');
387								} else {
388									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>';
389								}
390								print '</td>';
391
392								// Info
393								$htmltooltip = ''.$langs->trans("Name").': '.$module->name;
394								$htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
395								if ($module->type == 'pdf')
396								{
397									$htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
398								}
399								$htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
400
401								$htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
402								$htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
403								$htmltooltip .= '<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg, 1, 1);
404								$htmltooltip .= '<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg, 1, 1);
405								$htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
406								$htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark, 1, 1);
407
408
409								print '<td class="center">';
410								print $form->textwithpicto('', $htmltooltip, 1, 0);
411								print '</td>';
412
413								// Preview
414								print '<td class="center">';
415								if ($module->type == 'pdf')
416								{
417									print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
418								} else {
419									print img_object($langs->trans("PreviewNotAvailable"), 'generic');
420								}
421								print '</td>';
422
423								print "</tr>\n";
424							}
425						}
426					}
427				}
428			}
429		}
430	}
431}
432
433print '</table>';
434print "<br>";
435
436/*
437 * Other options
438 *
439 */
440
441print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
442print '<input type="hidden" name="token" value="'.newToken().'">';
443print '<input type="hidden" name="action" value="set_other">';
444
445print load_fiche_titre($langs->trans("OtherOptions"), '', '');
446print '<table class="noborder centpercent">';
447print '<tr class="liste_titre">';
448print '<td>'.$langs->trans("Parameter").'</td>';
449print '<td class="center" width="60">'.$langs->trans("Value").'</td>';
450print "</tr>\n";
451
452$substitutionarray = pdf_getSubstitutionArray($langs, array('objectamount'), null, 2);
453$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
454$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
455foreach ($substitutionarray as $key => $val)	$htmltext .= $key.'<br>';
456$htmltext .= '</i>';
457
458print '<tr class="oddeven"><td colspan="2">';
459print $form->textwithpicto($langs->trans("FreeLegalTextOnContracts"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'tooltiphelp');
460print '<br>';
461$variablename = 'CONTRACT_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
472//Use draft Watermark
473
474print '<tr class="oddeven"><td>';
475print $form->textwithpicto($langs->trans("WatermarkOnDraftContractCards"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
476print '</td><td>';
477print '<input size="50" class="flat" type="text" name="CONTRACT_DRAFT_WATERMARK" value="'.$conf->global->CONTRACT_DRAFT_WATERMARK.'">';
478print '</td></tr>'."\n";
479
480print '<tr class="oddeven">';
481print '<td>'.$langs->trans("HideClosedServiceByDefault").'</td>';
482print '<td width="60" class="right">';
483print $form->selectyesno("activate_hideClosedServiceByDefault", (!empty($conf->global->CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT) ? $conf->global->CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT : 0), 1);
484print '</td>';
485print '</tr>';
486
487print '</table>';
488
489print '<div class="center">';
490print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
491print '</div>';
492
493print '</form>';
494
495print dol_get_fiche_end();
496
497// End of page
498llxFooter();
499$db->close();
500