dolibarr 25.0.0-alpha
defaultvalues.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2017-2018 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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
31// Load Dolibarr environment
32require '../main.inc.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/defaultvalues.class.php';
44
45// Load translation files required by the page
46$langs->loadLangs(array('companies', 'products', 'admin', 'sms', 'other'));
47
48if (!$user->admin) {
50}
51
52$id = GETPOSTINT('rowid');
53$action = GETPOST('action', 'aZ09');
54$contextpage = GETPOST('contextpage', 'aZ09');
55$optioncss = GETPOST('optionscss', 'alphanohtml');
56
57$mode = GETPOST('mode', 'aZ09') ? GETPOST('mode', 'aZ09') : 'createform'; // 'createform', 'filters', 'sortorder', 'focus'
58
59$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
60$sortfield = GETPOST('sortfield', 'aZ09comma');
61$sortorder = GETPOST('sortorder', 'aZ09comma');
62$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
63if (empty($page) || $page == -1) {
64 $page = 0;
65} // If $page is not defined, or '' or -1
66$offset = $limit * $page;
67$pageprev = $page - 1;
68$pagenext = $page + 1;
69if (!$sortfield) {
70 $sortfield = 'page,param';
71}
72if (!$sortorder) {
73 $sortorder = 'ASC';
74}
75
76$defaulturl = GETPOST('defaulturl', 'alphanohtml');
77$defaultkey = GETPOST('defaultkey', 'alphanohtml');
78$defaultvalue = GETPOST('defaultvalue', 'restricthtml');
79
80$defaulturl = preg_replace('/^\//', '', $defaulturl);
81
82$urlpage = GETPOST('urlpage', 'alphanohtml');
83$key = GETPOST('key', 'alphanohtml');
84$value = GETPOST('value', 'restricthtml');
85
86// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
87$hookmanager->initHooks(array('admindefaultvalues', 'globaladmin'));
88
89
90$object = new DefaultValues($db);
91/*
92 * Actions
93 */
94
95if (GETPOST('cancel', 'alpha')) {
96 $action = 'list';
97 $massaction = '';
98}
99if (!GETPOST('confirmmassaction', 'alpha') && !empty($massaction) && $massaction != 'presend' && $massaction != 'confirm_presend') {
100 $massaction = '';
101}
102
103$parameters = array();
104$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
105if ($reshook < 0) {
106 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
107}
108
109include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
110
111// Purge search criteria
112if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
113 $defaulturl = '';
114 $defaultkey = '';
115 $defaultvalue = '';
116 $toselect = array();
117 $search_array_options = array();
118}
119
120if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES') {
121 if (GETPOST('value')) {
122 dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
123 } else {
124 dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
125 }
126}
127
128if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify')) {
129 $error = 0;
130
131 if (($action == 'add' || (GETPOST('add') && $action != 'update'))) {
132 if (empty($defaulturl)) {
133 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
134 $error++;
135 }
136 if (empty($defaultkey)) {
137 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
138 $error++;
139 }
140 }
141 if (GETPOST('actionmodify')) {
142 if (empty($urlpage)) {
143 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
144 $error++;
145 }
146 if (empty($key)) {
147 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
148 $error++;
149 }
150 }
151
152 if (!$error) {
153 if ($action == 'add' || (GETPOST('add') && $action != 'update')) {
154 $object->type = $mode;
155 $object->user_id = 0;
156 $object->page = $defaulturl;
157 $object->param = $defaultkey;
158 $object->value = $defaultvalue;
159 $object->entity = $conf->entity;
160 $result = $object->create($user);
161 if ($result < 0) {
162 $action = '';
163 setEventMessages($object->error, $object->errors, 'errors');
164 } else {
165 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
166 $action = '';
167 $defaulturl = '';
168 $defaultkey = '';
169 $defaultvalue = '';
170 }
171 }
172 if (GETPOST('actionmodify')) {
173 $object->id = $id;
174 $object->type = $mode;
175 $object->page = $urlpage;
176 $object->param = $key;
177 $object->value = $value;
178 $object->entity = $conf->entity;
179 $result = $object->update($user);
180 if ($result < 0) {
181 $action = '';
182 setEventMessages($object->error, $object->errors, 'errors');
183 } else {
184 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
185 $action = "";
186 $defaulturl = '';
187 $defaultkey = '';
188 $defaultvalue = '';
189 }
190 }
191 }
192}
193
194// Delete line from delete picto
195if ($action == 'delete') {
196 $object->id = $id;
197 $result = $object->delete($user);
198 if ($result < 0) {
199 $action = '';
200 setEventMessages($object->error, $object->errors, 'errors');
201 }
202}
203
204
205
206/*
207 * View
208 */
209
210$form = new Form($db);
211$formadmin = new FormAdmin($db);
212
213$wikihelp = 'EN:First_setup|FR:Premiers_paramétrages|ES:Primeras_configuraciones';
214llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-defaultvalues');
215
216$param = '&mode='.$mode;
217
218$enabledisablehtml = '<span class="divfilteralone">';
219if (!getDolGlobalString('MAIN_ENABLE_DEFAULT_VALUES')) {
220 // Button off, click to enable
221 $enabledisablehtml .= '<a class="reposition valignmiddle nounderlineimp unsetcolor small" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&token='.newToken().'&value=1'.$param.'">';
222} else {
223 // Button on, click to disable
224 $enabledisablehtml .= '<a class="reposition valignmiddle nounderlineimp unsetcolor small" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&token='.newToken().'&value=0'.$param.'">';
225}
226$enabledisablehtml .= '<span class="hideonsmartphone">'.$langs->trans("EnableDefaultValues").'</span>';
227if (!getDolGlobalString('MAIN_ENABLE_DEFAULT_VALUES')) {
228 $enabledisablehtml .= img_picto($langs->trans("Disabled"), 'switch_off', 'class="paddingleft valignmiddle"');
229} else {
230 $enabledisablehtml .= img_picto($langs->trans("Activated"), 'switch_on', 'class="paddingleft valignmiddle"');
231}
232$enabledisablehtml .= '</a>';
233$enabledisablehtml .= '</span>';
234
235print load_fiche_titre($langs->trans("DefaultValues"), $enabledisablehtml, 'title_setup');
236
237print '<div class="info hideonsmartphone">'.$langs->trans("DefaultValuesDesc")."</div>\n";
238
239if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
240 $param .= '&contextpage='.urlencode($contextpage);
241}
242if ($limit > 0 && $limit != $conf->liste_limit) {
243 $param .= '&limit='.((int) $limit);
244}
245if ($optioncss != '') {
246 $param .= '&optioncss='.urlencode($optioncss);
247}
248if ($defaulturl) {
249 $param .= '&defaulturl='.urlencode($defaulturl);
250}
251if ($defaultkey) {
252 $param .= '&defaultkey='.urlencode($defaultkey);
253}
254if ($defaultvalue) {
255 $param .= '&defaultvalue='.urlencode($defaultvalue);
256}
257
258
259print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" spellcheck="false">';
260if ($optioncss != '') {
261 print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
262}
263print '<input type="hidden" name="token" value="'.newToken().'">';
264print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
265print '<input type="hidden" name="action" value="list">';
266print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
267print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
268print '<input type="hidden" name="page" value="'.$page.'">';
269
271
272print dol_get_fiche_head($head, $mode, '', -1, '');
273
274if ($mode == 'sortorder') {
275 print info_admin($langs->trans("WarningSettingSortOrder"), 0, 0, 'warning').'<br>';
276}
277if ($mode == 'mandatory') {
278 print info_admin($langs->trans("FeatureSupportedOnTextFieldsOnly"), 0, 0, 'warning').'<br>';
279}
280
281print '<input type="hidden" name="token" value="'.newToken().'">';
282print '<input type="hidden" id="action" name="action" value="">';
283print '<input type="hidden" id="mode" name="mode" value="'.dol_escape_htmltag($mode).'">';
284
285print '<div class="div-table-responsive-no-min">';
286print '<table class="noborder centpercent">';
287print '<tr class="liste_titre">';
288// Page
289$texthelp = $langs->trans("PageUrlForDefaultValues");
290if ($mode == 'createform') {
291 $texthelp .= $langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php', 'societe/card.php?abc=val1&def=val2');
292} else {
293 $texthelp .= $langs->trans("PageUrlForDefaultValuesList", 'societe/list.php', 'societe/list.php?abc=val1&def=val2');
294}
295$texthelp .= '<br><br>'.$langs->trans("AlsoDefaultValuesAreEffectiveForActionCreate");
296$texturl = $form->textwithpicto($langs->trans("RelativeURL"), $texthelp);
297print_liste_field_titre($texturl, $_SERVER["PHP_SELF"], 'page,param', '', $param, '', $sortfield, $sortorder);
298// Field
299$texthelp = $langs->trans("TheKeyIsTheNameOfHtmlField");
300if ($mode != 'sortorder') {
301 $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
302} else {
303 $texthelp = 'field or alias.field';
304 $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
305}
306print_liste_field_titre($textkey, $_SERVER["PHP_SELF"], 'param', '', $param, '', $sortfield, $sortorder);
307// Value
308if ($mode != 'focus' && $mode != 'mandatory') {
309 if ($mode != 'sortorder') {
310 $substitutionarray = getCommonSubstitutionArray($langs, 2, array('object', 'objectamount')); // Must match list into GETPOST
311 unset($substitutionarray['__USER_SIGNATURE__']);
312 unset($substitutionarray['__SENDEREMAIL_SIGNATURE__']);
313 $texthelp = $langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
314 foreach ($substitutionarray as $key => $val) {
315 $texthelp .= $key.' -> '.$val.'<br>';
316 }
317 $textvalue = $form->textwithpicto($langs->trans("Value"), $langs->trans("DefaultValuesHelpText"));
318 } else {
319 $texthelp = 'ASC or DESC';
320 $textvalue = $form->textwithpicto($langs->trans("SortOrder"), $texthelp);
321 }
322 print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
323}
324// Entity
325if (isModEnabled('multicompany') && !$user->entity) {
326 print_liste_field_titre("Entity", $_SERVER["PHP_SELF"], 'entity,page', '', $param, '', $sortfield, $sortorder);
327} else {
328 print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
329}
330// Actions
331print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
332print "</tr>\n";
333
334
335// Line to add new record
336print "\n";
337
338print '<tr class="oddeven">';
339// Page
340print '<td>';
341print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="'.dol_escape_htmltag($defaulturl).'">';
342print '</td>'."\n";
343// Field
344print '<td>';
345print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultkey" value="'.dol_escape_htmltag($defaultkey).'">';
346print '</td>';
347// Value
348if ($mode != 'focus' && $mode != 'mandatory') {
349 print '<td>';
350 print '<span class="nowraponall"><input type="text" class="flat maxwidth100onsmartphone" name="defaultvalue" value="'.dol_escape_htmltag($defaultvalue).'">';
351 if ($mode != 'sortorder') {
352 print $form->textwithpicto('', $texthelp, 1, 'list-alt', 'paddingleftimp cursorpointer', 0, 2, 'subsitutiontooltip');
353 }
354 print '</span></td>';
355}
356// Limit to superadmin
357if (isModEnabled('multicompany') && !$user->entity) {
358 print '<td>';
359 print '<input type="text" class="flat" size="1" disabled name="entity" value="' . $conf->entity . '">'; // We see environment, but to change it we must switch on other entity
360 print '</td>';
361} else {
362 print '<td class="center">';
363 print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
364 print '</td>';
365}
366print '<td class="center">';
367$disabled = '';
368if (!getDolGlobalString('MAIN_ENABLE_DEFAULT_VALUES')) {
369 $disabled = ' disabled="disabled"';
370}
371print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
372print '</td>'."\n";
373print '</tr>'."\n";
374//"(t.type:=:".$mode.") AND (t.entity:in:("((int)$user->entity).", ".((int)$conf->entity)."))"
375$result = $object->fetchAll($sortorder, $sortfield, 0, 0, "(t.type:=:'".$mode."') AND (t.entity:in:".((int) $user->entity).", ".((int) $conf->entity).")");
376
377if (!is_array($result) && $result < 0) {
378 setEventMessages($object->error, $object->errors, 'errors');
379} elseif (is_array($result) && count($result) > 0) {
380 foreach ($result as $key => $defaultvalue) {
381 print '<tr class="oddeven">';
382
383 // Page
384 print '<td>';
385 if ($action != 'edit' || GETPOSTINT('rowid') != $defaultvalue->id) {
386 print $defaultvalue->page;
387 } else {
388 print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($defaultvalue->page).'">';
389 }
390 print '</td>'."\n";
391
392 // Field
393 print '<td>';
394 if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
395 print $defaultvalue->param;
396 } else {
397 print '<input type="text" name="key" value="'.dol_escape_htmltag($defaultvalue->param).'">';
398 }
399 print '</td>'."\n";
400
401 // Value
402 if ($mode != 'focus' && $mode != 'mandatory') {
403 print '<td>';
404 if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
405 print dol_escape_htmltag($defaultvalue->value);
406 } else {
407 print '<input type="text" name="value" value="'.dol_escape_htmltag($defaultvalue->value).'">';
408 }
409 print '</td>';
410 }
411
412 // Multicompany
413 print '<td>';
414 if (isModEnabled('multicompany')) {
415 print dol_escape_htmltag((string) $defaultvalue->entity);
416 }
417 print '</td>';
418
419 // Actions
420 print '<td class="center">';
421 if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
422 print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?rowid='.$defaultvalue->id.'&entity='.$defaultvalue->entity.'&mode='.$mode.'&action=edit&token='.newToken().'">'.img_edit().'</a>';
423 print '<a class="marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?rowid='.$defaultvalue->id.'&entity='.$defaultvalue->entity.'&mode='.$mode.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
424 } else {
425 print '<input type="hidden" name="page" value="'.$page.'">';
426 print '<input type="hidden" name="rowid" value="'.$id.'">';
427 print '<div name="'.(!empty($defaultvalue->id) ? $defaultvalue->id : 'none').'"></div>';
428 print '<input type="submit" class="button button-edit" name="actionmodify" value="'.$langs->trans("Modify").'">';
429 print '<input type="submit" class="button button-cancel" name="actioncancel" value="'.$langs->trans("Cancel").'">';
430 }
431 print '</td>';
432
433 print "</tr>\n";
434 }
435}
436
437print '</table>';
438print '</div>';
439
440print dol_get_fiche_end();
441
442print "</form>\n";
443
444// End of page
445llxFooter();
446$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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).
defaultvalues_prepare_head()
Prepare array with list of tabs.
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 for MyObject.
Class to generate html code for admin pages.
Class to manage generation of HTML components Only common components must be here.
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.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:740
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='', $cssfordropdown='info_admin')
Show information in HTML for admin users or standard users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.