dolibarr 21.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 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';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
32
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$debug = GETPOSTINT('debug');
48$consts = GETPOST('const', 'array');
49$constname = GETPOST('constname', 'alphanohtml');
50$constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
51$constnote = GETPOST('constnote', 'alpha');
52// Load variable for pagination
53$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
54$sortfield = GETPOST('sortfield', 'aZ09comma');
55$sortorder = GETPOST('sortorder', 'aZ09comma');
56$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
57if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
58 $page = 0;
59} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
60$offset = $limit * $page;
61$pageprev = $page - 1;
62$pagenext = $page + 1;
63if (empty($sortfield)) {
64 $sortfield = 'entity,name';
65}
66if (empty($sortorder)) {
67 $sortorder = 'ASC';
68}
69
70if ($action == 'add' && GETPOST('update')) { // Click on button update must be used in priority before param $action
71 $action = 'update';
72}
73if ($action == 'add' && GETPOST('delete')) { // Click on button update must be used in priority before param $action
74 $action = 'delete';
75}
76/*if ($action == 'update' && GETPOST('add')) { // 'add' button is always clicked as it is the first in form.
77 $action = 'add';
78}*/
79if ($action == 'delete' && GETPOST('add')) { // Click on button add must be used in priority before param $action
80 $action = 'add';
81}
82
83if (!$user->admin) {
85}
86
87
88/*
89 * Actions
90 */
91
92// Add a new record
93if ($action == 'add') {
94 $error = 0;
95
96 if (empty($constname)) {
97 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
98 $error++;
99 }
100 if ($constvalue == '') {
101 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
102 $error++;
103 }
104
105 if (!$error) {
106 if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
107 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
108 $action = "";
109 $constname = "";
110 $constvalue = "";
111 $constnote = "";
112 } else {
113 dol_print_error($db);
114 }
115 }
116}
117
118// Mass update
119if (!empty($consts) && $action == 'update') {
120 $nbmodified = 0;
121 foreach ($consts as $const) {
122 if (!empty($const["check"])) {
123 if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
124 $nbmodified++;
125 } else {
126 dol_print_error($db);
127 }
128 }
129 }
130 if ($nbmodified > 0) {
131 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
132 }
133 $action = '';
134}
135
136// Mass delete
137if (!empty($consts) && $action == 'delete') {
138 $nbdeleted = 0;
139 foreach ($consts as $const) {
140 if (!empty($const["check"])) { // Is checkbox checked
141 if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
142 $nbdeleted++;
143 } else {
144 dol_print_error($db);
145 }
146 }
147 }
148 if ($nbdeleted > 0) {
149 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
150 }
151 $action = '';
152}
153
154// Delete line from delete picto
155if ($action == 'delete') {
156 if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
157 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
158 } else {
159 dol_print_error($db);
160 }
161}
162
163
164/*
165 * View
166 */
167
168$form = new Form($db);
169
170$wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
171llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-const');
172
173// Add logic to show/hide buttons
174if ($conf->use_javascript_ajax) {
175 ?>
176<script type="text/javascript">
177jQuery(document).ready(function() {
178 jQuery("#updateconst").hide();
179 jQuery("#delconst").hide();
180 jQuery(".checkboxfordelete").click(function() {
181 jQuery("#delconst").show();
182 });
183 jQuery(".inputforupdate").keyup(function() { // keypress does not support back
184 var field_id = jQuery(this).attr("id");
185 var row_num = field_id.split("_");
186 jQuery("#updateconst").show();
187 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.
188 jQuery("#check_" + row_num[1]).prop("checked",true);
189 });
190});
191</script>
192 <?php
193}
194
195print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
196
197print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
198print "<br>\n";
199print "<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.'">';
228print '</td>'."\n";
229print '<td>';
230print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'">';
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} elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
265 // to force for superadmin to debug
266 $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
267}
268if (GETPOST('name')) {
269 $sql .= natural_search("name", GETPOST('name'));
270}
271$sql .= $db->order($sortfield, $sortorder);
272
273dol_syslog("Const::listConstant", LOG_DEBUG);
274$result = $db->query($sql);
275if ($result) {
276 $num = $db->num_rows($result);
277 $i = 0;
278
279 while ($i < $num) {
280 $obj = $db->fetch_object($result);
281
282 $value = dolDecrypt($obj->value);
283
284 print "\n";
285
286 print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
287
288 // Value
289 print '<td>';
290 print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
291 print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
292 print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
293 print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.(isset($value) ? htmlspecialchars($value) : '').'">';
294 print '</td>';
295
296 // Note
297 print '<td>';
298 print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.(empty($obj->note) ? '' : htmlspecialchars($obj->note, 1)).'">';
299 print '</td>';
300
301 // Date last change
302 print '<td class="nowraponall center">';
303 print dol_print_date($db->jdate($obj->tms), 'dayhour');
304 print '</td>';
305
306 // Entity limit to superadmin
307 if (isModEnabled('multicompany') && empty($user->entity)) {
308 print '<td>';
309 print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
310 print '</td>';
311 print '<td class="center">';
312 } else {
313 print '<td class="center">';
314 print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
315 }
316
317 if (!empty($conf->use_javascript_ajax)) {
318 print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
319 } else {
320 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>';
321 }
322
323 print "</td></tr>\n";
324
325 print "\n";
326 $i++;
327 }
328}
329
330
331print '</table>';
332print '</div>';
333
334if ($conf->use_javascript_ajax) {
335 print '<br>';
336 print '<div id="updateconst" class="right">';
337 print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
338 print '</div>';
339 print '<div id="delconst" class="right">';
340 print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
341 print '</div>';
342}
343
344print "</form>\n";
345
346// End of page
347llxFooter();
348$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.
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:71
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.