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