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$entity = GETPOSTINT('entity');
45$action = GETPOST('action', 'aZ09');
46$massaction = GETPOST('massaction', 'aZ09');
47$toselect = GETPOST('toselect', 'array:int');
48$confirm = GETPOST('confirm', 'alpha');
49
50$debug = GETPOSTINT('debug');
51$consts = GETPOST('const', 'array');
52$constname = GETPOST('constname', 'aZ09');
53$constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
54$constnote = GETPOST('constnote', 'alphanohtml');
55
56// Search criteria
57$search_name = GETPOST('search_name', 'alpha');
58$search_value = GETPOST('search_value', 'alpha');
59
60// Purge search criteria
61if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
62 $search_name = '';
63 $search_value = '';
64}
65
66// True if the search or clear search button of the list of records was submitted
67$filterbuttonclicked = (bool) (GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha')
68 || GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha'));
69
70// Load variable for pagination
71$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
72$sortfield = GETPOST('sortfield', 'aZ09comma');
73$sortorder = GETPOST('sortorder', 'aZ09comma');
74$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
75if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
76 $page = 0;
77} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
78$offset = $limit * $page;
79$pageprev = $page - 1;
80$pagenext = $page + 1;
81if (empty($sortfield)) {
82 $sortfield = 'entity,name';
83}
84if (empty($sortorder)) {
85 $sortorder = 'ASC';
86}
87
88if ($action == 'add' && GETPOST('update')) { // Click on button update must be used in priority before param $action
89 $action = 'update';
90}
91
92if (!$user->admin) {
94}
95
96
97/*
98 * Actions
99 */
100
101// Mass action
102if (!GETPOST('confirmmassaction', 'alpha')) {
103 $massaction = '';
104}
105
106// Add a new record (only when the zone to add a new record was submitted and no mass action is in progress)
107if ($action == 'add' && empty($massaction) && GETPOSTISSET('constname') && empty($filterbuttonclicked)) {
108 $error = 0;
109
110 if (empty($constname)) {
111 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
112 $error++;
113 }
114 if ($constvalue == '') {
115 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
116 $error++;
117 }
118
119 if (!$error) {
120 if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
121 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
122 $action = "";
123 $constname = "";
124 $constvalue = "";
125 $constnote = "";
126 } else {
127 dol_print_error($db);
128 }
129 }
130}
131
132// Mass update
133if (!empty($consts) && $action == 'update') {
134 $nbmodified = 0;
135 foreach ($consts as $const) {
136 if (!empty($const["rowid"]) && in_array((int) $const["rowid"], $toselect)) { // Only records checked
137 if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
138 $nbmodified++;
139 } else {
140 dol_print_error($db);
141 }
142 }
143 }
144 if ($nbmodified > 0) {
145 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
146 }
147 $action = '';
148}
149
150// Mass delete (massaction = 'delete', or action = 'delete' after confirmation of massaction = 'predelete')
151if ($massaction == 'delete' || ($action == 'delete' && $confirm == 'yes')) {
152 $nbdeleted = 0;
153 foreach ($toselect as $constrowid) {
154 if (dolibarr_del_const($db, $constrowid, -1) >= 0) {
155 $nbdeleted++;
156 } else {
157 dol_print_error($db);
158 }
159 }
160 if ($nbdeleted > 1) {
161 setEventMessages($langs->trans("RecordsDeleted", $nbdeleted), null, 'mesgs');
162 } elseif ($nbdeleted > 0) {
163 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
164 } else {
165 setEventMessages($langs->trans("NoRecordDeleted"), null, 'mesgs');
166 }
167 $action = '';
168 $massaction = '';
169}
170
171
172/*
173 * View
174 */
175
176$form = new Form($db);
177
178$wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
179llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-const');
180
181// Add logic to show/hide buttons
182if ($conf->use_javascript_ajax) {
183 ?>
184<script type="text/javascript">
185jQuery(document).ready(function() {
186 jQuery("#updateconst").hide();
187 jQuery(".checkforselect").change(function() {
188 jQuery("#updateconst").show();
189 });
190 jQuery(".inputforupdate").keyup(function() { // keypress does not support back
191 jQuery("#updateconst").show();
192 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.
193 jQuery(this).closest("tr").find(".checkforselect").prop("checked", true);
194 });
195 jQuery("#add").click(function() {
196 jQuery("#action").val('add'); // so the add is forced even if we typed before into a field of a record
197 });
198});
199</script>
200 <?php
201}
202
203// List of mass actions available
204$arrayofmassactions = array(
205 'predelete' => img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete"),
206);
207if (GETPOSTINT('nomassaction') || $massaction == 'predelete') {
208 $arrayofmassactions = array();
209}
210$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
211
212// Button to check all into the action column of title of list
213$selectedfields = (count($arrayofmassactions) ? $form->showCheckAddButtons('checkforselect', 1) : '');
214
215$arrayofselected = is_array($toselect) ? $toselect : array();
216
217// Button + to show/hide the zone to add a new record
218$addformvisible = (($action == 'create' || ($action == 'add' && GETPOSTISSET('constname'))) && empty($massaction) && empty($filterbuttonclicked)); // $action == 'add' keeps the zone visible if a field is missing
219$urlbuttonplus = $_SERVER['PHP_SELF'].($addformvisible ? '' : '?action=create');
220if (empty($user->entity) && $debug) {
221 $urlbuttonplus .= $addformvisible ? '?debug=1' : '&debug=1';
222}
223$newcardbutton = dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', $urlbuttonplus, '', ($addformvisible ? 2 : 1));
224
225$param = '';
226if ($search_name !== '') {
227 $param .= '&search_name='.urlencode($search_name);
228}
229if ($search_value !== '') {
230 $param .= '&search_value='.urlencode($search_value);
231}
232
233// Unique form, so the combo of mass actions into the line of title can submit the selection of records
234print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST" spellcheck="false">';
235print '<input type="hidden" name="token" value="'.newToken().'">';
236print '<input type="hidden" id="action" name="action" value="add">';
237print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
238print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
239
240print load_fiche_titre($langs->trans("OtherSetup"), $newcardbutton, 'title_setup', 0, '', '', $massactionbutton);
241
242print '<div class="info">'.$langs->trans("ConstDesc")."</div><br>\n";
243
244// Zone to add a new record (visible only when we click on the button +)
245if ($addformvisible) {
246 dol_set_focus('input[name="constname"]');
247 print '<div class="div-table-responsive-no-min">';
248 print '<table class="noborder centpercent">';
249
250 // Line of title
251 print '<tr class="liste_titre">';
252 print '<th>'.$langs->trans("Name").'</th>';
253 print '<th>'.$langs->trans("Value").'</th>';
254 print '<th>'.$langs->trans("Comment").'</th>';
255 print '<th class="center">'.$langs->trans("DateModificationShort").'</th>';
256 if (isModEnabled('multicompany') && !$user->entity) {
257 print '<th class="center">'.$langs->trans("Entity").'</th>';
258 }
259 print '<th class="center"></th>';
260 print '</tr>'."\n";
261
262 // Line to add new record
263 print "\n";
264
265 print '<tr class="oddeven nohover"><td>';
266 print '<input type="text" class="flat minwidth300" name="constname" value="'.dolPrintHTMLForAttribute($constname).'" spellcheck="false">';
267 print '</td>'."\n";
268 print '<td>';
269 print '<input type="text" class="flat minwidth100" name="constvalue" value="'.dolPrintHTMLForAttribute($constvalue, 1).'" spellcheck="false">';
270 print '</td>';
271 print '<td>';
272 print '<input type="text" class="flat minwidth100" name="constnote" value="'.dolPrintHTMLForAttribute($constnote).'">';
273 print '</td>';
274 print '<td>';
275 print '</td>';
276 // Limit to superadmin
277 if (isModEnabled('multicompany') && !$user->entity) {
278 print '<td class="center">';
279 print '<input type="text" class="width50" name="entity" value="' . $conf->entity . '">';
280 print '</td>';
281 print '<td class="center">';
282 } else {
283 print '<td class="center">';
284 print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
285 }
286 print '<input type="submit" class="button button-add small" id="add" name="add" value="'.$langs->trans("Add").'">';
287 print "</td>\n";
288 print '</tr>';
289
290 print '</table>';
291 print '</div>';
292 print '<br>';
293}
294
295// Code for pre mass action (confirmation of mass deletion)
296if ($massaction == 'predelete') {
297 print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDeletion"), $langs->trans("ConfirmMassDeletionQuestion", count($toselect)), "delete", null, '', 0, 200, 500, 1);
298}
299
300// List of records
301print '<div class="div-table-responsive-no-min">';
302print '<table class="noborder centpercent">';
303
304// Line with search filters
305print '<tr class="liste_titre_filter">';
306// Action column
307if ($conf->main_checkbox_left_column) {
308 print '<td class="liste_titre center maxwidthsearch">';
309 print $form->showFilterButtons();
310 print '</td>';
311}
312// Name
313print '<td class="liste_titre">';
314print '<input type="text" class="flat maxwidth100" name="search_name" value="'.dol_escape_htmltag($search_name).'">';
315print '</td>';
316// Value
317print '<td class="liste_titre">';
318print '<input type="text" class="flat maxwidth100" name="search_value" value="'.dol_escape_htmltag($search_value).'">';
319print '</td>';
320// Comment
321print '<td class="liste_titre"></td>';
322// Date last change
323print '<td class="liste_titre"></td>';
324// Entity limit to superadmin
325if (isModEnabled('multicompany') && !$user->entity) {
326 print '<td class="liste_titre"></td>';
327}
328// Action column
329if (!$conf->main_checkbox_left_column) {
330 print '<td class="liste_titre center maxwidthsearch">';
331 print $form->showFilterButtons();
332 print '</td>';
333}
334print '</tr>'."\n";
335
336print '<tr class="liste_titre">';
337// Action column
338if ($conf->main_checkbox_left_column) {
339 print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center maxwidthsearch ');
340}
341print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '') . "\n";
342print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
343print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
344print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
345if (isModEnabled('multicompany') && !$user->entity) {
346 print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
347}
348// Action column
349if (!$conf->main_checkbox_left_column) {
350 print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center maxwidthsearch ');
351}
352print "</tr>\n";
353
354
355// Show constants
356$sql = "SELECT";
357$sql .= " rowid";
358$sql .= ", ".$db->decrypt('name')." as name";
359$sql .= ", ".$db->decrypt('value')." as value";
360$sql .= ", type";
361$sql .= ", note";
362$sql .= ", tms";
363$sql .= ", entity";
364$sql .= " FROM ".MAIN_DB_PREFIX."const";
365$sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".((int) $conf->entity)).")";
366if ((empty($user->entity)/* || $user->admin */) && $debug) {
367 // empty
368} elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
369 // to force for superadmin to debug
370 $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
371}
372if ($search_name !== '') {
373 $sql .= natural_search($db->decrypt('name'), $search_name);
374}
375if ($search_value !== '') {
376 $sql .= natural_search($db->decrypt('value'), $search_value);
377}
378$sql .= $db->order($sortfield, $sortorder);
379
380dol_syslog("Const::listConstant", LOG_DEBUG);
381$result = $db->query($sql);
382if ($result) {
383 $num = $db->num_rows($result);
384 $i = 0;
385
386 while ($i < $num) {
387 $obj = $db->fetch_object($result);
388
389 $value = dolDecrypt($obj->value);
390
391 // Content of the action column (checkbox to select record for mass actions)
392 $selected = 0;
393 if (in_array((int) $obj->rowid, $arrayofselected)) {
394 $selected = 1;
395 }
396 $actioncolumncontent = '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected ? ' checked="checked"' : '').'>';
397
398 print "\n";
399
400 print '<tr class="oddeven">';
401
402 // Action column
403 if ($conf->main_checkbox_left_column) {
404 print '<td class="center">'.$actioncolumncontent.'</td>';
405 }
406
407 print '<td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
408
409 // Value
410 print '<td>';
411 print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
412 print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
413 print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
414 if (!isModEnabled('multicompany') || !empty($user->entity)) { // If there is no input for entity, we save it into a hidden input
415 print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
416 }
417 print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.(isset($value) ? htmlspecialchars($value) : '').'">';
418 print '</td>';
419
420 // Note
421 print '<td>';
422 print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.(empty($obj->note) ? '' : htmlspecialchars($obj->note, 1)).'">';
423 print '</td>';
424
425 // Date last change
426 print '<td class="nowraponall center">';
427 print dol_print_date($db->jdate($obj->tms), 'dayhour');
428 print '</td>';
429
430 // Entity limit to superadmin
431 if (isModEnabled('multicompany') && empty($user->entity)) {
432 print '<td class="center">';
433 print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
434 print '</td>';
435 }
436
437 // Action column
438 if (!$conf->main_checkbox_left_column) {
439 print '<td class="center">'.$actioncolumncontent.'</td>';
440 }
441
442 print "</tr>\n";
443
444 print "\n";
445 $i++;
446 }
447
448 // Show a line when there is no record found
449 if ($num == 0) {
450 $colspan = 5; // 4 fields + action column
451 if (isModEnabled('multicompany') && !$user->entity) {
452 $colspan++;
453 }
454 print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
455 }
456}
457
458
459print '</table>';
460print '</div>';
461
462if ($conf->use_javascript_ajax) {
463 print '<br>';
464 print '<div id="updateconst" class="right">';
465 print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
466 print '</div>';
467}
468
469print "</form>\n";
470
471// End of page
472llxFooter();
473$db->close();
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage generation of HTML components Only common components must be here.
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...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
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).
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.
dolGetButtonTitle($label, $helpText='', $iconClass='fa fa-file', $url='', $id='', $status=1, $params=array())
Function dolGetButtonTitle : this kind of buttons are used in title in list.
getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $disablesortlink=0, $tooltip='', $forcenowrapcolumntitle=0)
Get title line of an array.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
dol_set_focus($selector, $nooutput=0)
Set focus onto field with selector (similar behaviour of 'autofocus' HTML5 tag)
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.