1<?php
2/* Copyright (C) 2008-2015	Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2011		Regis Houssin		<regis.houssin@inodbox.com>
4 * Copyright (C) 2011-2012  Juanjo Menent		<jmenent@2byte.es>
5 * Copyright (C) 2015		Jean-François Ferry <jfefe@aternatik.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.php
23 *      \ingroup    agenda
24 *      \brief      Autocreate actions for agenda module setup page
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
30
31if (!$user->admin)
32	accessforbidden();
33
34// Load translation files required by the page
35$langs->loadLangs(array('admin', 'other', 'agenda'));
36
37$action = GETPOST('action', 'aZ09');
38$cancel = GETPOST('cancel', 'alpha');
39
40$search_event = GETPOST('search_event', 'alpha');
41
42// Get list of triggers available
43$triggers = array();
44$sql = "SELECT a.rowid, a.code, a.label, a.elementtype, a.rang as position";
45$sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as a";
46$sql .= " ORDER BY a.rang ASC";
47$resql = $db->query($sql);
48if ($resql)
49{
50	$num = $db->num_rows($resql);
51	$i = 0;
52	while ($i < $num)
53	{
54		$obj = $db->fetch_object($resql);
55		$triggers[$i]['rowid'] = $obj->rowid;
56		$triggers[$i]['code'] 		= $obj->code;
57		$triggers[$i]['element'] = $obj->elementtype;
58		$triggers[$i]['label']		= ($langs->trans("Notify_".$obj->code) != "Notify_".$obj->code ? $langs->trans("Notify_".$obj->code) : $obj->label);
59		$triggers[$i]['position'] = $obj->position;
60
61		$i++;
62	}
63	$db->free($resql);
64} else {
65	dol_print_error($db);
66}
67
68//$triggers = dol_sort_array($triggers, 'code', 'asc', 0, 0, 1);
69
70
71/*
72 *	Actions
73 */
74
75// Purge search criteria
76if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) // All tests are required to be compatible with all browsers
77{
78	$search_event = '';
79	$action = '';
80}
81
82if (GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha'))	// To avoid the save when we click on search
83{
84	$action = '';
85}
86
87if ($action == "save" && empty($cancel))
88{
89	$i = 0;
90
91	$db->begin();
92
93	foreach ($triggers as $trigger)
94	{
95		$keyparam = 'MAIN_AGENDA_ACTIONAUTO_'.$trigger['code'];
96		//print "param=".$param." - ".$_POST[$param];
97		if ($search_event === '' || preg_match('/'.preg_quote($search_event, '/').'/i', $keyparam))
98		{
99			$res = dolibarr_set_const($db, $keyparam, (GETPOST($keyparam, 'alpha') ?GETPOST($keyparam, 'alpha') : ''), 'chaine', 0, '', $conf->entity);
100			if (!($res > 0)) $error++;
101		}
102	}
103
104 	if (!$error)
105	{
106		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
107		$db->commit();
108	} else {
109		setEventMessages($langs->trans("Error"), null, 'errors');
110		$db->rollback();
111	}
112}
113
114
115
116/**
117 * View
118 */
119
120$wikihelp = 'EN:Module_Agenda_En|FR:Module_Agenda|ES:Módulo_Agenda';
121llxHeader('', $langs->trans("AgendaSetup"), $wikihelp);
122
123$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
124print load_fiche_titre($langs->trans("AgendaSetup"), $linkback, 'title_setup');
125
126print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
127print '<input type="hidden" name="token" value="'.newToken().'">';
128print '<input type="hidden" name="action" value="save">';
129
130$param = '';
131$param .= '&search_event='.urlencode($search_event);
132
133$head = agenda_prepare_head();
134
135print dol_get_fiche_head($head, 'autoactions', $langs->trans("Agenda"), -1, 'action');
136
137print '<span class="opacitymedium">'.$langs->trans("AgendaAutoActionDesc")." ".$langs->trans("OnlyActiveElementsAreShown", 'modules.php').'</span><br>';
138print "<br>\n";
139
140print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
141print '<table class="noborder centpercent">';
142print '<tr class="liste_titre">';
143print '<td class="liste_titre"><input type="text" name="search_event" value="'.dol_escape_htmltag($search_event).'"></td>';
144print '<td class="liste_titre"></td>';
145// Action column
146print '<td class="liste_titre maxwidthsearch">';
147$searchpicto = $form->showFilterButtons();
148print $searchpicto;
149print '</td>';
150print '</tr>';
151print '</tr>'."\n";
152
153print '<tr class="liste_titre">';
154print '<th class="liste_titre" colspan="2">'.$langs->trans("ActionsEvents").'</th>';
155print '<th class="liste_titre"><a href="'.$_SERVER["PHP_SELF"].'?action=selectall'.($param ? $param : '').'">'.$langs->trans("All").'</a>/<a href="'.$_SERVER["PHP_SELF"].'?action=selectnone'.($param ? $param : '').'">'.$langs->trans("None").'</a></th>';
156print '</tr>'."\n";
157// Show each trigger (list is in c_action_trigger)
158if (!empty($triggers))
159{
160	foreach ($triggers as $trigger)
161	{
162		$module = $trigger['element'];
163		if ($module == 'order_supplier' || $module == 'invoice_supplier') $module = 'fournisseur';
164		if ($module == 'shipping') $module = 'expedition_bon';
165		if ($module == 'member') $module = 'adherent';
166		if ($module == 'project') $module = 'projet';
167		if ($module == 'proposal_supplier') $module = 'supplier_proposal';
168		if ($module == 'contact') $module = 'societe';
169
170		// If 'element' value is myobject@mymodule instead of mymodule
171		$tmparray = explode('@', $module);
172		if (!empty($tmparray[1])) {
173			$module = $tmparray[1];
174		}
175
176		//print 'module='.$module.' code='.$trigger['code'].'<br>';
177		if (!empty($conf->$module->enabled))
178		{
179			// Discard special case: If option FICHINTER_CLASSIFY_BILLED is not set, we discard both trigger FICHINTER_CLASSIFY_BILLED and FICHINTER_CLASSIFY_UNBILLED
180			if ($trigger['code'] == 'FICHINTER_CLASSIFY_BILLED' && empty($conf->global->FICHINTER_CLASSIFY_BILLED)) continue;
181			if ($trigger['code'] == 'FICHINTER_CLASSIFY_UNBILLED' && empty($conf->global->FICHINTER_CLASSIFY_BILLED)) continue;
182
183			if ($search_event === '' || preg_match('/'.preg_quote($search_event, '/').'/i', $trigger['code']))
184			{
185				print '<tr class="oddeven">';
186				print '<td>'.$trigger['code'].'</td>';
187				print '<td>'.$trigger['label'].'</td>';
188				print '<td class="right" width="40">';
189				$key = 'MAIN_AGENDA_ACTIONAUTO_'.$trigger['code'];
190				$value = $conf->global->$key;
191				print '<input class="oddeven" type="checkbox" name="'.$key.'" value="1"'.((($action == 'selectall' || $value) && $action != "selectnone") ? ' checked' : '').'>';
192				print '</td></tr>'."\n";
193			}
194		}
195	}
196}
197print '</table>';
198print '</div>';
199
200print dol_get_fiche_end();
201
202print '<div class="center">';
203print '<input type="submit" name="save" class="button button-save" value="'.$langs->trans("Save").'">';
204print "</div>";
205
206print "</form>\n";
207
208
209print "<br>";
210
211// End of page
212llxFooter();
213$db->close();
214