1<?php
2/* Copyright (C) 2003		Rodolphe Quiedeville	<rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2013	Laurent Destailleur		<eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@inodbox.com>
5 * Copyright (C) 2013		Juanjo Menent			<jmenent@2byte.es>
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/const.php
23 *	\ingroup    setup
24 *	\brief      Admin page to define miscellaneous constants
25 */
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29
30// Load translation files required by the page
31$langs->load("admin");
32
33if (!$user->admin)
34	accessforbidden();
35
36$rowid = GETPOST('rowid', 'int');
37$entity = GETPOST('entity', 'int');
38$action = GETPOST('action', 'aZ09');
39$update = GETPOST('update', 'alpha');
40$delete = GETPOST('delete', 'none'); // Do not use alpha here
41$debug = GETPOST('debug', 'int');
42$consts = GETPOST('const', 'array');
43$constname = GETPOST('constname', 'alphanohtml');
44$constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
45$constnote = GETPOST('constnote', 'alpha');
46
47// Load variable for pagination
48$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
49$sortfield = GETPOST('sortfield', 'aZ09comma');
50$sortorder = GETPOST('sortorder', 'aZ09comma');
51$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
52if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) { $page = 0; }     // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
53$offset = $limit * $page;
54$pageprev = $page - 1;
55$pagenext = $page + 1;
56if (empty($sortfield)) $sortfield = 'entity,name';
57if (empty($sortorder)) $sortorder = 'ASC';
58
59
60/*
61 * Actions
62 */
63
64if ($action == 'add' || (GETPOST('add') && $action != 'update'))
65{
66	$error = 0;
67
68	if (empty($constname))
69	{
70		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
71		$error++;
72	}
73	if ($constvalue == '')
74	{
75		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
76		$error++;
77	}
78
79	if (!$error)
80	{
81		if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0)
82		{
83			setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
84			$action = "";
85			$constname = "";
86			$constvalue = "";
87			$constnote = "";
88		} else {
89			dol_print_error($db);
90		}
91	}
92}
93
94// Mass update
95if (!empty($consts) && $action == 'update')
96{
97	$nbmodified = 0;
98	foreach ($consts as $const)
99	{
100		if (!empty($const["check"]))
101		{
102			if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0)
103			{
104				$nbmodified++;
105			} else {
106				dol_print_error($db);
107			}
108		}
109	}
110	if ($nbmodified > 0) setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
111	$action = '';
112}
113
114// Mass delete
115if (!empty($consts) && $action == 'delete')
116{
117	$nbdeleted = 0;
118	foreach ($consts as $const)
119	{
120		if (!empty($const["check"]))	// Is checkbox checked
121		{
122			if (dolibarr_del_const($db, $const["rowid"], -1) >= 0)
123			{
124				$nbdeleted++;
125			} else {
126				dol_print_error($db);
127			}
128		}
129	}
130	if ($nbdeleted > 0) setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
131	$action = '';
132}
133
134// Delete line from delete picto
135if ($action == 'delete')
136{
137	if (dolibarr_del_const($db, $rowid, $entity) >= 0)
138	{
139		setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
140	} else {
141		dol_print_error($db);
142	}
143}
144
145
146/*
147 * View
148 */
149
150$form = new Form($db);
151
152$wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
153llxHeader('', $langs->trans("Setup"), $wikihelp);
154
155// Add logic to show/hide buttons
156if ($conf->use_javascript_ajax)
157{
158	?>
159<script type="text/javascript">
160jQuery(document).ready(function() {
161	jQuery("#updateconst").hide();
162	jQuery("#delconst").hide();
163	jQuery(".checkboxfordelete").click(function() {
164		jQuery("#delconst").show();
165		jQuery("#action").val('delete');
166	});
167	jQuery(".inputforupdate").keyup(function() {	// keypress does not support back
168		var field_id = jQuery(this).attr("id");
169		var row_num = field_id.split("_");
170		jQuery("#updateconst").show();
171		jQuery("#action").val('update');
172		jQuery("#check_" + row_num[1]).prop("checked",true);
173	});
174});
175</script>
176    <?php
177}
178
179print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
180
181print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
182print "<br>\n";
183
184$param = '';
185
186print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
187print '<input type="hidden" name="token" value="'.newToken().'">';
188print '<input type="hidden" id="action" name="action" value="">';
189print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
190print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
191
192print '<div class="div-table-responsive-no-min">';
193print '<table class="noborder centpercent">';
194print '<tr class="liste_titre">';
195print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '')."\n";
196print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
197print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
198print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
199if (!empty($conf->multicompany->enabled) && !$user->entity)
200{
201	print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
202}
203print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
204print "</tr>\n";
205
206
207// Line to add new record
208print "\n";
209
210print '<tr class="oddeven nohover"><td>';
211print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'">';
212print '</td>'."\n";
213print '<td>';
214print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'">';
215print '</td>';
216print '<td>';
217print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
218print '</td>';
219print '<td>';
220print '</td>';
221// Limit to superadmin
222if (!empty($conf->multicompany->enabled) && !$user->entity)
223{
224	print '<td>';
225	print '<input type="text" class="flat" size="1" name="entity" value="'.$conf->entity.'">';
226	print '</td>';
227	print '<td class="center">';
228} else {
229	print '<td class="center">';
230	print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
231}
232print '<input type="submit" class="button" value="'.$langs->trans("Add").'" name="add">';
233print "</td>\n";
234print '</tr>';
235
236
237// Show constants
238$sql = "SELECT";
239$sql .= " rowid";
240$sql .= ", ".$db->decrypt('name')." as name";
241$sql .= ", ".$db->decrypt('value')." as value";
242$sql .= ", type";
243$sql .= ", note";
244$sql .= ", tms";
245$sql .= ", entity";
246$sql .= " FROM ".MAIN_DB_PREFIX."const";
247$sql .= " WHERE entity IN (".$user->entity.",".$conf->entity.")";
248if ((empty($user->entity) || $user->admin) && $debug) {} 										// to force for superadmin to debug
249elseif (!GETPOST('visible') || GETPOST('visible') != 'all') $sql .= " AND visible = 1"; // We must always have this. Otherwise, array is too large and submitting data fails due to apache POST or GET limits
250if (GETPOST('name')) $sql .= natural_search("name", GETPOST('name'));
251$sql .= $db->order($sortfield, $sortorder);
252
253dol_syslog("Const::listConstant", LOG_DEBUG);
254$result = $db->query($sql);
255if ($result)
256{
257	$num = $db->num_rows($result);
258	$i = 0;
259
260	while ($i < $num)
261	{
262		$obj = $db->fetch_object($result);
263
264		print "\n";
265
266		print '<tr class="oddeven"><td>'.$obj->name.'</td>'."\n";
267
268		// Value
269		print '<td>';
270		print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
271		print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
272		print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
273		print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.htmlspecialchars($obj->value).'">';
274		print '</td>';
275
276		// Note
277		print '<td>';
278		print '<input type="text" id="note_'.$i.'" class="flat inputforupdate" size="40" name="const['.$i.'][note]" value="'.htmlspecialchars($obj->note, 1).'">';
279		print '</td>';
280
281		// Date last change
282		print '<td>';
283		print dol_print_date($db->jdate($obj->tms), 'dayhour');
284		print '</td>';
285
286		// Entity limit to superadmin
287		if (!empty($conf->multicompany->enabled) && !$user->entity)
288		{
289			print '<td>';
290			print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
291			print '</td>';
292			print '<td class="center">';
293		} else {
294			print '<td class="center">';
295			print '<input type="hidden" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
296		}
297
298		if ($conf->use_javascript_ajax)
299		{
300			print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
301		} else {
302			print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&action=delete&token='.newToken().((empty($user->entity) && $debug) ? '&debug=1' : '').'">'.img_delete().'</a>';
303		}
304
305		print "</td></tr>\n";
306
307		print "\n";
308		$i++;
309	}
310}
311
312
313print '</table>';
314print '</div>';
315
316if ($conf->use_javascript_ajax)
317{
318	print '<br>';
319	print '<div id="updateconst" class="right">';
320	print '<input type="submit" name="update" class="button marginbottomonly" value="'.$langs->trans("Modify").'">';
321	print '</div>';
322	print '<div id="delconst" class="right">';
323	print '<input type="submit" name="delete" class="button marginbottomonly" value="'.$langs->trans("Delete").'">';
324	print '</div>';
325}
326
327print "</form>\n";
328
329// End of page
330llxFooter();
331$db->close();
332