dolibarr 24.0.0-beta
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
39
40// Load translation files required by the page
41$langs->load("admin");
42
43$rowid = GETPOSTINT('rowid');
44$entity = GETPOSTINT('entity');
45$action = GETPOST('action', 'aZ09');
46$massaction = GETPOST('massaction', 'aZ09');
47
48$debug = GETPOSTINT('debug');
49$consts = GETPOST('const', 'array');
50$constname = GETPOST('constname', 'alphanohtml');
51$constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
52$constnote = GETPOST('constnote', 'alpha');
53
54// Load variable for pagination
55$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
56$sortfield = GETPOST('sortfield', 'aZ09comma');
57$sortorder = GETPOST('sortorder', 'aZ09comma');
58$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
59if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
60 $page = 0;
61} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
62$offset = $limit * $page;
63$pageprev = $page - 1;
64$pagenext = $page + 1;
65if (empty($sortfield)) {
66 $sortfield = 'entity,name';
67}
68if (empty($sortorder)) {
69 $sortorder = 'ASC';
70}
71
72if ($action == 'add' && GETPOST('update')) { // Click on button update must be used in priority before param $action
73 $action = 'update';
74}
75if ($action == 'add' && GETPOST('delete')) { // Click on button update must be used in priority before param $action
76 $action = 'delete';
77}
78/*if ($action == 'update' && GETPOST('add')) { // 'add' button is always clicked as it is the first in form.
79 $action = 'add';
80}*/
81if ($action == 'delete' && GETPOST('add')) { // Click on button add must be used in priority before param $action
82 $action = 'add';
83}
84
85if (!$user->admin) {
87}
88
89
90/*
91 * Actions
92 */
93
94// Add a new record
95if ($action == 'add') {
96 $error = 0;
97
98 if (empty($constname)) {
99 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
100 $error++;
101 }
102 if ($constvalue == '') {
103 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
104 $error++;
105 }
106
107 if (!$error) {
108 if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
109 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
110 $action = "";
111 $constname = "";
112 $constvalue = "";
113 $constnote = "";
114 } else {
116 }
117 }
118}
119
120// Mass update
121if (!empty($consts) && $action == 'update') {
122 $nbmodified = 0;
123 foreach ($consts as $const) {
124 if (!empty($const["check"])) {
125 if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
126 $nbmodified++;
127 } else {
129 }
130 }
131 }
132 if ($nbmodified > 0) {
133 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
134 }
135 $action = '';
136}
137
138// Mass delete
139if (!empty($consts) && $action == 'delete') {
140 $nbdeleted = 0;
141 foreach ($consts as $const) {
142 if (!empty($const["check"])) { // Is checkbox checked
143 if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
144 $nbdeleted++;
145 } else {
147 }
148 }
149 }
150 if ($nbdeleted > 0) {
151 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
152 }
153 $action = '';
154}
155
156// Delete line from delete picto
157if ($action == 'delete') {
158 if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
159 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
160 } else {
162 }
163}
164
165
166/*
167 * View
168 */
169
170$form = new Form($db);
171
172$wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
173llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-const');
174
175// Add logic to show/hide buttons
176if ($conf->use_javascript_ajax) {
177 ?>
178<script type="text/javascript">
179jQuery(document).ready(function() {
180 jQuery("#updateconst").hide();
181 jQuery("#delconst").hide();
182 jQuery(".checkboxfordelete").click(function() {
183 jQuery("#delconst").show();
184 });
185 jQuery(".inputforupdate").keyup(function() { // keypress does not support back
186 var field_id = jQuery(this).attr("id");
187 var row_num = field_id.split("_");
188 jQuery("#updateconst").show();
189 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.
190 jQuery("#check_" + row_num[1]).prop("checked",true);
191 });
192});
193</script>
194 <?php
195}
196
197print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
198
199print '<div class="info">'.$langs->trans("ConstDesc")."</div><br>\n";
200
201$param = '';
202
203print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
204print '<input type="hidden" name="token" value="'.newToken().'">';
205print '<input type="hidden" id="action" name="action" value="add">';
206print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
207print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
208
209print '<div class="div-table-responsive-no-min">';
210print '<table class="noborder centpercent">';
211print '<tr class="liste_titre">';
212print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '') . "\n";
213print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
214print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
215print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
216if (isModEnabled('multicompany') && !$user->entity) {
217 print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
218}
219print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
220print "</tr>\n";
221
222
223// Line to add new record
224print "\n";
225
226print '<tr class="oddeven nohover"><td>';
227print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'" spellcheck="false">';
228print '</td>'."\n";
229print '<td>';
230print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'" spellcheck="false">';
231print '</td>';
232print '<td>';
233print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
234print '</td>';
235print '<td>';
236print '</td>';
237// Limit to superadmin
238if (isModEnabled('multicompany') && !$user->entity) {
239 print '<td>';
240 print '<input type="text" class="flat" size="1" name="entity" value="' . $conf->entity . '">';
241 print '</td>';
242 print '<td class="center">';
243} else {
244 print '<td class="center">';
245 print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
246}
247print '<input type="submit" class="button button-add small" id="add" name="add" value="'.$langs->trans("Add").'">';
248print "</td>\n";
249print '</tr>';
250
251
252// Show constants
253$sql = "SELECT";
254$sql .= " rowid";
255$sql .= ", ".$db->decrypt('name')." as name";
256$sql .= ", ".$db->decrypt('value')." as value";
257$sql .= ", type";
258$sql .= ", note";
259$sql .= ", tms";
260$sql .= ", entity";
261$sql .= " FROM ".MAIN_DB_PREFIX."const";
262$sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".$conf->entity).")";
263if ((empty($user->entity)/* || $user->admin */) && $debug) {
264 // empty
265} elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
266 // to force for superadmin to debug
267 $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
268}
269if (GETPOST('name')) {
270 $sql .= natural_search("name", GETPOST('name'));
271}
272$sql .= $db->order($sortfield, $sortorder);
273
274dol_syslog("Const::listConstant", LOG_DEBUG);
275$result = $db->query($sql);
276if ($result) {
277 $num = $db->num_rows($result);
278 $i = 0;
279
280 while ($i < $num) {
281 $obj = $db->fetch_object($result);
282
283 $value = dolDecrypt($obj->value);
284
285 print "\n";
286
287 print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
288
289 // Value
290 print '<td>';
291 print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
292 print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
293 print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
294 print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.(isset($value) ? htmlspecialchars($value) : '').'">';
295 print '</td>';
296
297 // Note
298 print '<td>';
299 print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.(empty($obj->note) ? '' : htmlspecialchars($obj->note, 1)).'">';
300 print '</td>';
301
302 // Date last change
303 print '<td class="nowraponall center">';
304 print dol_print_date($db->jdate($obj->tms), 'dayhour');
305 print '</td>';
306
307 // Entity limit to superadmin
308 if (isModEnabled('multicompany') && empty($user->entity)) {
309 print '<td>';
310 print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
311 print '</td>';
312 print '<td class="center">';
313 } else {
314 print '<td class="center">';
315 print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
316 }
317
318 if (!empty($conf->use_javascript_ajax)) {
319 print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
320 } else {
321 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>';
322 }
323
324 print "</td></tr>\n";
325
326 print "\n";
327 $i++;
328 }
329}
330
331
332print '</table>';
333print '</div>';
334
335if ($conf->use_javascript_ajax) {
336 print '<br>';
337 print '<div id="updateconst" class="right">';
338 print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
339 print '</div>';
340 print '<div id="delconst" class="right">';
341 print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
342 print '</div>';
343}
344
345print "</form>\n";
346
347// End of page
348llxFooter();
349$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.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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...
getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $disablesortlink=0, $tooltip='', $forcenowrapcolumntitle=0)
Get title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
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.