dolibarr  x.y.z
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  *
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 
27 // Load Dolibarr environment
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
31 
32 // Load translation files required by the page
33 $langs->load("admin");
34 
35 if (!$user->admin) {
37 }
38 
39 $rowid = GETPOST('rowid', 'int');
40 $entity = GETPOST('entity', 'int');
41 $action = GETPOST('action', 'aZ09');
42 $debug = GETPOST('debug', 'int');
43 $consts = GETPOST('const', 'array');
44 $constname = GETPOST('constname', 'alphanohtml');
45 $constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
46 $constnote = GETPOST('constnote', 'alpha');
47 
48 // Load variable for pagination
49 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
50 $sortfield = GETPOST('sortfield', 'aZ09comma');
51 $sortorder = GETPOST('sortorder', 'aZ09comma');
52 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
53 if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
54  $page = 0;
55 } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
56 $offset = $limit * $page;
57 $pageprev = $page - 1;
58 $pagenext = $page + 1;
59 if (empty($sortfield)) {
60  $sortfield = 'entity,name';
61 }
62 if (empty($sortorder)) {
63  $sortorder = 'ASC';
64 }
65 
66 
67 /*
68  * Actions
69  */
70 
71 if ($action == 'add' || (GETPOST('add') && $action != 'update')) {
72  $error = 0;
73 
74  if (empty($constname)) {
75  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
76  $error++;
77  }
78  if ($constvalue == '') {
79  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
80  $error++;
81  }
82 
83  if (!$error) {
84  if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
85  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
86  $action = "";
87  $constname = "";
88  $constvalue = "";
89  $constnote = "";
90  } else {
91  dol_print_error($db);
92  }
93  }
94 }
95 
96 // Mass update
97 if (!empty($consts) && $action == 'update') {
98  $nbmodified = 0;
99  foreach ($consts as $const) {
100  if (!empty($const["check"])) {
101  if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
102  $nbmodified++;
103  } else {
104  dol_print_error($db);
105  }
106  }
107  }
108  if ($nbmodified > 0) {
109  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
110  }
111  $action = '';
112 }
113 
114 // Mass delete
115 if (!empty($consts) && $action == 'delete') {
116  $nbdeleted = 0;
117  foreach ($consts as $const) {
118  if (!empty($const["check"])) { // Is checkbox checked
119  if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
120  $nbdeleted++;
121  } else {
122  dol_print_error($db);
123  }
124  }
125  }
126  if ($nbdeleted > 0) {
127  setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
128  }
129  $action = '';
130 }
131 
132 // Delete line from delete picto
133 if ($action == 'delete') {
134  if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
135  setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
136  } else {
137  dol_print_error($db);
138  }
139 }
140 
141 
142 /*
143  * View
144  */
145 
146 $form = new Form($db);
147 
148 $wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
149 llxHeader('', $langs->trans("Setup"), $wikihelp);
150 
151 // Add logic to show/hide buttons
152 if ($conf->use_javascript_ajax) {
153  ?>
154 <script type="text/javascript">
155 jQuery(document).ready(function() {
156  jQuery("#updateconst").hide();
157  jQuery("#delconst").hide();
158  jQuery(".checkboxfordelete").click(function() {
159  jQuery("#delconst").show();
160  jQuery("#action").val('delete');
161  });
162  jQuery(".inputforupdate").keyup(function() { // keypress does not support back
163  var field_id = jQuery(this).attr("id");
164  var row_num = field_id.split("_");
165  jQuery("#updateconst").show();
166  jQuery("#action").val('update');
167  jQuery("#check_" + row_num[1]).prop("checked",true);
168  });
169 });
170 </script>
171  <?php
172 }
173 
174 print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
175 
176 print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
177 print "<br>\n";
178 
179 $param = '';
180 
181 print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
182 print '<input type="hidden" name="token" value="'.newToken().'">';
183 print '<input type="hidden" id="action" name="action" value="">';
184 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
185 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
186 
187 print '<div class="div-table-responsive-no-min">';
188 print '<table class="noborder centpercent">';
189 print '<tr class="liste_titre">';
190 print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '') . "\n";
191 print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
192 print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
193 print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
194 if (isModEnabled('multicompany') && !$user->entity) {
195  print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
196 }
197 print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
198 print "</tr>\n";
199 
200 
201 // Line to add new record
202 print "\n";
203 
204 print '<tr class="oddeven nohover"><td>';
205 print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'">';
206 print '</td>'."\n";
207 print '<td>';
208 print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'">';
209 print '</td>';
210 print '<td>';
211 print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
212 print '</td>';
213 print '<td>';
214 print '</td>';
215 // Limit to superadmin
216 if (isModEnabled('multicompany') && !$user->entity) {
217  print '<td>';
218  print '<input type="text" class="flat" size="1" name="entity" value="' . $conf->entity . '">';
219  print '</td>';
220  print '<td class="center">';
221 } else {
222  print '<td class="center">';
223  print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
224 }
225 print '<input type="submit" class="button button-add small" name="add" value="'.$langs->trans("Add").'">';
226 print "</td>\n";
227 print '</tr>';
228 
229 
230 // Show constants
231 $sql = "SELECT";
232 $sql .= " rowid";
233 $sql .= ", ".$db->decrypt('name')." as name";
234 $sql .= ", ".$db->decrypt('value')." as value";
235 $sql .= ", type";
236 $sql .= ", note";
237 $sql .= ", tms";
238 $sql .= ", entity";
239 $sql .= " FROM ".MAIN_DB_PREFIX."const";
240 $sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".$conf->entity).")";
241 if ((empty($user->entity) || $user->admin) && $debug) {
242 } elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
243  // to force for superadmin to debug
244  $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
245 }
246 if (GETPOST('name')) {
247  $sql .= natural_search("name", GETPOST('name'));
248 }
249 $sql .= $db->order($sortfield, $sortorder);
250 
251 dol_syslog("Const::listConstant", LOG_DEBUG);
252 $result = $db->query($sql);
253 if ($result) {
254  $num = $db->num_rows($result);
255  $i = 0;
256 
257  while ($i < $num) {
258  $obj = $db->fetch_object($result);
259 
260  $value = dolDecrypt($obj->value);
261 
262  print "\n";
263 
264  print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
265 
266  // Value
267  print '<td>';
268  print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
269  print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
270  print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
271  print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.htmlspecialchars($value).'">';
272  print '</td>';
273 
274  // Note
275  print '<td>';
276  print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.htmlspecialchars($obj->note, 1).'">';
277  print '</td>';
278 
279  // Date last change
280  print '<td class="nowraponall center">';
281  print dol_print_date($db->jdate($obj->tms), 'dayhour');
282  print '</td>';
283 
284  // Entity limit to superadmin
285  if (isModEnabled('multicompany') && !$user->entity) {
286  print '<td>';
287  print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
288  print '</td>';
289  print '<td class="center">';
290  } else {
291  print '<td class="center">';
292  print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
293  }
294 
295  if ($conf->use_javascript_ajax) {
296  print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
297  } else {
298  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>';
299  }
300 
301  print "</td></tr>\n";
302 
303  print "\n";
304  $i++;
305  }
306 }
307 
308 
309 print '</table>';
310 print '</div>';
311 
312 if ($conf->use_javascript_ajax) {
313  print '<br>';
314  print '<div id="updateconst" class="right">';
315  print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
316  print '</div>';
317  print '<div id="delconst" class="right">';
318  print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
319  print '</div>';
320 }
321 
322 print "</form>\n";
323 
324 // End of page
325 llxFooter();
326 $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).
Definition: admin.lib.php:632
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:556
if($actionsave) if(!isset($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) $wikihelp
View.
Definition: agenda_xcal.php:73
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $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.
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.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
dolDecrypt($chain, $key='')
Decode a string with a symetric encryption.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.