1<?php
2/* Copyright (C) 2008-2015	Laurent Destailleur		<eldy@users.sourceforge.net>
3 * Copyright (C) 2011-2015	Juanjo Menent			<jmenent@2byte.es>
4 * Copyright (C) 2015       Jean-François Ferry		<jfefe@aternatik.fr>
5 * Copyright (C) 2016       Raphaël Doursenaud      <rdoursenaud@gpcsolutions.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/agenda_extsites.php
23 *      \ingroup    agenda
24 *      \brief      Page to setup external calendars for agenda module
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
34
35if (!$user->admin) accessforbidden();
36
37// Load translation files required by the page
38$langs->loadLangs(array('agenda', 'admin', 'other'));
39
40$def = array();
41$actiontest = GETPOST('test', 'alpha');
42$actionsave = GETPOST('save', 'alpha');
43
44if (empty($conf->global->AGENDA_EXT_NB)) $conf->global->AGENDA_EXT_NB = 5;
45$MAXAGENDA = $conf->global->AGENDA_EXT_NB;
46
47// List of available colors
48$colorlist = array('BECEDD', 'DDBECE', 'BFDDBE', 'F598B4', 'F68654', 'CBF654', 'A4A4A5');
49
50
51/*
52 * Actions
53 */
54
55if ($actionsave)
56{
57	$db->begin();
58
59	$disableext = GETPOST('AGENDA_DISABLE_EXT', 'alpha');
60	$res = dolibarr_set_const($db, 'AGENDA_DISABLE_EXT', $disableext, 'chaine', 0, '', $conf->entity);
61
62	$i = 1; $errorsaved = 0;
63	$error = 0;
64
65	// Save agendas
66	while ($i <= $MAXAGENDA)
67	{
68		$name = trim(GETPOST('AGENDA_EXT_NAME'.$i, 'alpha'));
69		$src = trim(GETPOST('AGENDA_EXT_SRC'.$i, 'alpha'));
70		$offsettz = trim(GETPOST('AGENDA_EXT_OFFSETTZ'.$i, 'alpha'));
71		$color = trim(GETPOST('AGENDA_EXT_COLOR'.$i, 'alpha'));
72		if ($color == '-1') $color = '';
73		$enabled = trim(GETPOST('AGENDA_EXT_ENABLED'.$i, 'alpha'));
74
75		if (!empty($src) && !dol_is_url($src))
76		{
77			setEventMessages($langs->trans("ErrorParamMustBeAnUrl"), null, 'errors');
78			$error++;
79			$errorsaved++;
80			break;
81		}
82
83		//print '-name='.$name.'-color='.$color;
84		$res = dolibarr_set_const($db, 'AGENDA_EXT_NAME'.$i, $name, 'chaine', 0, '', $conf->entity);
85		if (!($res > 0)) $error++;
86		$res = dolibarr_set_const($db, 'AGENDA_EXT_SRC'.$i, $src, 'chaine', 0, '', $conf->entity);
87		if (!($res > 0)) $error++;
88		$res = dolibarr_set_const($db, 'AGENDA_EXT_OFFSETTZ'.$i, $offsettz, 'chaine', 0, '', $conf->entity);
89		if (!($res > 0)) $error++;
90		$res = dolibarr_set_const($db, 'AGENDA_EXT_COLOR'.$i, $color, 'chaine', 0, '', $conf->entity);
91		if (!($res > 0)) $error++;
92		$res = dolibarr_set_const($db, 'AGENDA_EXT_ENABLED'.$i, $enabled, 'chaine', 0, '', $conf->entity);
93		if (!($res > 0)) $error++;
94		$i++;
95	}
96
97	// Save nb of agenda
98	if (!$error)
99	{
100		$res = dolibarr_set_const($db, 'AGENDA_EXT_NB', trim(GETPOST('AGENDA_EXT_NB', 'int')), 'chaine', 0, '', $conf->entity);
101		if (!($res > 0)) $error++;
102		if (empty($conf->global->AGENDA_EXT_NB)) $conf->global->AGENDA_EXT_NB = 5;
103		$MAXAGENDA = empty($conf->global->AGENDA_EXT_NB) ? 5 : $conf->global->AGENDA_EXT_NB;
104	}
105
106	if (!$error)
107	{
108		$db->commit();
109		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
110	} else {
111		$db->rollback();
112		if (empty($errorsaved))	setEventMessages($langs->trans("Error"), null, 'errors');
113	}
114}
115
116/*
117 * View
118 */
119
120$form = new Form($db);
121$formadmin = new FormAdmin($db);
122$formother = new FormOther($db);
123
124$arrayofjs = array();
125$arrayofcss = array();
126
127$wikihelp = 'EN:Module_Agenda_En|FR:Module_Agenda|ES:Módulo_Agenda';
128llxHeader('', $langs->trans("AgendaSetup"), $wikihelp, '', 0, 0, $arrayofjs, $arrayofcss);
129
130$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
131print load_fiche_titre($langs->trans("AgendaSetup"), $linkback, 'title_setup');
132
133print '<form name="extsitesconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
134print '<input type="hidden" name="token" value="'.newToken().'">';
135print '<input type="hidden" name="action" value="save">';
136
137$head = agenda_prepare_head();
138
139print dol_get_fiche_head($head, 'extsites', $langs->trans("Agenda"), -1, 'action');
140
141print '<span class="opacitymedium">'.$langs->trans("AgendaExtSitesDesc")."</span><br>\n";
142print "<br>\n";
143
144
145$selectedvalue = $conf->global->AGENDA_DISABLE_EXT;
146if ($selectedvalue == 1) $selectedvalue = 0; else $selectedvalue = 1;
147
148print "<table class=\"noborder\" width=\"100%\">";
149
150print "<tr class=\"liste_titre\">";
151print '<td>'.$langs->trans("Parameter")."</td>";
152print '<td class="center">'.$langs->trans("Value")."</td>";
153print "</tr>";
154
155// Show external agenda
156
157print '<tr class="oddeven">';
158print "<td>".$langs->trans("ExtSitesEnableThisTool")."</td>";
159print '<td class="center">';
160if ($conf->use_javascript_ajax)
161{
162	print ajax_constantonoff('AGENDA_DISABLE_EXT', array('enabled'=>array(0=>'.hideifnotset')), null, 1);
163} else {
164	if (empty($conf->global->AGENDA_DISABLE_EXT))
165	{
166		print '<a href="'.$_SERVER['PHP_SELF'].'?save=1&AGENDA_DISABLE_EXT=1">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
167	} else {
168		print '<a href="'.$_SERVER['PHP_SELF'].'?save=1&AGENDA_DISABLE_EXT=0">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
169	}
170}
171print "</td>";
172print "</tr>";
173
174// Nb of agenda
175
176print '<tr class="oddeven">';
177print "<td>".$langs->trans("ExtSitesNbOfAgenda")."</td>";
178print '<td class="center">';
179print '<input class="flat hideifnotset" type="text" size="2" id="AGENDA_EXT_NB" name="AGENDA_EXT_NB" value="'.$conf->global->AGENDA_EXT_NB.'">';
180print "</td>";
181print "</tr>";
182
183print "</table>";
184print "<br>";
185
186print "<table class=\"noborder\" width=\"100%\">";
187
188print "<tr class=\"liste_titre\">";
189print "<td>".$langs->trans("Parameter")."</td>";
190print "<td>".$langs->trans("Name")."</td>";
191print "<td>".$langs->trans("ExtSiteUrlAgenda")." (".$langs->trans("Example").': http://yoursite/agenda/agenda.ics)</td>';
192print "<td>".$form->textwithpicto($langs->trans("FixTZ"), $langs->trans("FillFixTZOnlyIfRequired"), 1).'</td>';
193print '<td class="right">'.$langs->trans("Color").'</td>';
194print "</tr>";
195
196$i = 1;
197while ($i <= $MAXAGENDA)
198{
199	$key = $i;
200	$name = 'AGENDA_EXT_NAME'.$key;
201	$src = 'AGENDA_EXT_SRC'.$key;
202	$offsettz = 'AGENDA_EXT_OFFSETTZ'.$key;
203	$color = 'AGENDA_EXT_COLOR'.$key;
204	$enabled = 'AGENDA_EXT_ENABLED'.$key;
205
206
207	print '<tr class="oddeven">';
208	// Nb
209	print '<td width="180" class="nowrap">'.$langs->trans("AgendaExtNb", $key)."</td>";
210	// Name
211	print '<td><input type="text" class="flat hideifnotset" name="AGENDA_EXT_NAME'.$key.'" value="'.(GETPOST('AGENDA_EXT_NAME'.$key) ?GETPOST('AGENDA_EXT_NAME'.$key, 'alpha') : $conf->global->$name).'" size="28"></td>';
212	// URL
213	print '<td><input type="url" class="flat hideifnotset" name="AGENDA_EXT_SRC'.$key.'" value="'.(GETPOST('AGENDA_EXT_SRC'.$key) ?GETPOST('AGENDA_EXT_SRC'.$key, 'alpha') : $conf->global->$src).'" size="60"></td>';
214	// Offset TZ
215	print '<td><input type="text" class="flat hideifnotset" name="AGENDA_EXT_OFFSETTZ'.$key.'" value="'.(GETPOST('AGENDA_EXT_OFFSETTZ'.$key) ?GETPOST('AGENDA_EXT_OFFSETTZ'.$key) : $conf->global->$offsettz).'" size="2"></td>';
216	// Color (Possible colors are limited by Google)
217	print '<td class="nowrap right">';
218	//print $formadmin->selectColor($conf->global->$color, "google_agenda_color".$key, $colorlist);
219	print $formother->selectColor((GETPOST("AGENDA_EXT_COLOR".$key) ?GETPOST("AGENDA_EXT_COLOR".$key) : $conf->global->$color), "AGENDA_EXT_COLOR".$key, 'extsitesconfig', 1, '', 'hideifnotset');
220	print '</td>';
221	print "</tr>";
222	$i++;
223}
224
225print '</table>';
226
227print dol_get_fiche_end();
228
229print '<div class="center">';
230print '<input type="submit" id="save" name="save" class="button hideifnotset button-save" value="'.$langs->trans("Save").'">';
231print '</div>';
232
233print "</form>\n";
234
235// End of page
236llxFooter();
237$db->close();
238