dolibarr 25.0.0-alpha
const.php
Go to the documentation of this file.
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Load Dolibarr environment
30require '../main.inc.php';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
40
41// Load translation files required by the page
42$langs->load("admin");
43
44$rowid = GETPOSTINT('rowid');
45$entity = GETPOSTINT('entity');
46$action = GETPOST('action', 'aZ09');
47$massaction = GETPOST('massaction', 'aZ09');
48
49$debug = GETPOSTINT('debug');
50$consts = GETPOST('const', 'array');
51$constname = GETPOST('constname', 'alphanohtml');
52$constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
53$constnote = GETPOST('constnote', 'alpha');
54
55// Load variable for pagination
56$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
57$sortfield = GETPOST('sortfield', 'aZ09comma');
58$sortorder = GETPOST('sortorder', 'aZ09comma');
59$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
60if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
61 $page = 0;
62} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
63$offset = $limit * $page;
64$pageprev = $page - 1;
65$pagenext = $page + 1;
66if (empty($sortfield)) {
67 $sortfield = 'entity,name';
68}
69if (empty($sortorder)) {
70 $sortorder = 'ASC';
71}
72
73if ($action == 'add' && GETPOST('update')) { // Click on button update must be used in priority before param $action
74 $action = 'update';
75}
76if ($action == 'add' && GETPOST('delete')) { // Click on button update must be used in priority before param $action
77 $action = 'delete';
78}
79/*if ($action == 'update' && GETPOST('add')) { // 'add' button is always clicked as it is the first in form.
80 $action = 'add';
81}*/
82if ($action == 'delete' && GETPOST('add')) { // Click on button add must be used in priority before param $action
83 $action = 'add';
84}
85
86if (!$user->admin) {
88}
89
90
91/*
92 * Actions
93 */
94
95// Add a new record
96if ($action == 'add') {
97 $error = 0;
98
99 if (empty($constname)) {
100 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
101 $error++;
102 }
103 if ($constvalue == '') {
104 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
105 $error++;
106 }
107
108 if (!$error) {
109 if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
110 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
111 $action = "";
112 $constname = "";
113 $constvalue = "";
114 $constnote = "";
115 } else {
117 }
118 }
119}
120
121// Mass update
122if (!empty($consts) && $action == 'update') {
123 $nbmodified = 0;
124 foreach ($consts as $const) {
125 if (!empty($const["check"])) {
126 if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
127 $nbmodified++;
128 } else {
130 }
131 }
132 }
133 if ($nbmodified > 0) {
134 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
135 }
136 $action = '';
137}
138
139// Mass delete
140if (!empty($consts) && $action == 'delete') {
141 $nbdeleted = 0;
142 foreach ($consts as $const) {
143 if (!empty($const["check"])) { // Is checkbox checked
144 if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
145 $nbdeleted++;
146 } else {
148 }
149 }
150 }
151 if ($nbdeleted > 0) {
152 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
153 }
154 $action = '';
155}
156
157// Delete line from delete picto
158if ($action == 'delete') {
159 if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
160 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
161 } else {
163 }
164}
165
166
167/*
168 * View
169 */
170
171$form = new Form($db);
172
173$wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
174llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-const');
175
176// Add logic to show/hide buttons
177if ($conf->use_javascript_ajax) {
178 ?>
179<script type="text/javascript">
180jQuery(document).ready(function() {
181 jQuery("#updateconst").hide();
182 jQuery("#delconst").hide();
183 jQuery(".checkboxfordelete").click(function() {
184 jQuery("#delconst").show();
185 });
186 jQuery(".inputforupdate").keyup(function() { // keypress does not support back
187 var field_id = jQuery(this).attr("id");
188 var row_num = field_id.split("_");
189 jQuery("#updateconst").show();
190 jQuery("#action").val('update'); // so default action if we type enter will be update, but correct action is also detected correctly without that when clicking on "Update" button.
191 jQuery("#check_" + row_num[1]).prop("checked",true);
192 });
193});
194</script>
195 <?php
196}
197
198print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
199
200print '<div class="info">'.$langs->trans("ConstDesc")."</div><br>\n";
201
202$param = '';
203
204print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
205print '<input type="hidden" name="token" value="'.newToken().'">';
206print '<input type="hidden" id="action" name="action" value="add">';
207print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
208print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
209
210print '<div class="div-table-responsive-no-min">';
211print '<table class="noborder centpercent">';
212print '<tr class="liste_titre">';
213print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '') . "\n";
214print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
215print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
216print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
217if (isModEnabled('multicompany') && !$user->entity) {
218 print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
219}
220print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
221print "</tr>\n";
222
223
224// Line to add new record
225print "\n";
226
227print '<tr class="oddeven nohover"><td>';
228print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'" spellcheck="false">';
229print '</td>'."\n";
230print '<td>';
231print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'" spellcheck="false">';
232print '</td>';
233print '<td>';
234print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
235print '</td>';
236print '<td>';
237print '</td>';
238// Limit to superadmin
239if (isModEnabled('multicompany') && !$user->entity) {
240 print '<td>';
241 print '<input type="text" class="flat" size="1" name="entity" value="' . $conf->entity . '">';
242 print '</td>';
243 print '<td class="center">';
244} else {
245 print '<td class="center">';
246 print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
247}
248print '<input type="submit" class="button button-add small" id="add" name="add" value="'.$langs->trans("Add").'">';
249print "</td>\n";
250print '</tr>';
251
252
253// Show constants
254$sql = "SELECT";
255$sql .= " rowid";
256$sql .= ", ".$db->decrypt('name')." as name";
257$sql .= ", ".$db->decrypt('value')." as value";
258$sql .= ", type";
259$sql .= ", note";
260$sql .= ", tms";
261$sql .= ", entity";
262$sql .= " FROM ".MAIN_DB_PREFIX."const";
263$sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".((int) $conf->entity)).")";
264if ((empty($user->entity)/* || $user->admin */) && $debug) {
265 // empty
266} elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
267 // to force for superadmin to debug
268 $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
269}
270if (GETPOST('name')) {
271 $sql .= natural_search("name", GETPOST('name'));
272}
273$sql .= $db->order($sortfield, $sortorder);
274
275dol_syslog("Const::listConstant", LOG_DEBUG);
276$result = $db->query($sql);
277if ($result) {
278 $num = $db->num_rows($result);
279 $i = 0;
280
281 while ($i < $num) {
282 $obj = $db->fetch_object($result);
283
284 $value = dolDecrypt($obj->value);
285
286 print "\n";
287
288 print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
289
290 // Value
291 print '<td>';
292 print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
293 print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
294 print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
295 print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.(isset($value) ? htmlspecialchars($value) : '').'">';
296 print '</td>';
297
298 // Note
299 print '<td>';
300 print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.(empty($obj->note) ? '' : htmlspecialchars($obj->note, 1)).'">';
301 print '</td>';
302
303 // Date last change
304 print '<td class="nowraponall center">';
305 print dol_print_date($db->jdate($obj->tms), 'dayhour');
306 print '</td>';
307
308 // Entity limit to superadmin
309 if (isModEnabled('multicompany') && empty($user->entity)) {
310 print '<td>';
311 print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
312 print '</td>';
313 print '<td class="center">';
314 } else {
315 print '<td class="center">';
316 print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
317 }
318
319 if (!empty($conf->use_javascript_ajax)) {
320 print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
321 } else {
322 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>';
323 }
324
325 print "</td></tr>\n";
326
327 print "\n";
328 $i++;
329 }
330}
331
332
333print '</table>';
334print '</div>';
335
336if ($conf->use_javascript_ajax) {
337 print '<br>';
338 print '<div id="updateconst" class="right">';
339 print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
340 print '</div>';
341 print '<div id="delconst" class="right">';
342 print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
343 print '</div>';
344}
345
346print "</form>\n";
347
348// End of page
349llxFooter();
350$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $disablesortlink=0, $tooltip='', $forcenowrapcolumntitle=0)
Get title line of an array.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.