dolibarr  19.0.0-dev
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
29 // Load Dolibarr environment
30 require '../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/class/defaultvalues.class.php';
35 
36 // Load translation files required by the page
37 $langs->loadLangs(array('companies', 'products', 'admin', 'sms', 'other', 'errors'));
38 
39 if (!$user->admin) {
41 }
42 
43 $id = GETPOST('rowid', 'int');
44 $action = GETPOST('action', 'aZ09');
45 $optioncss = GETPOST('optionscss', 'alphanohtml');
46 
47 $mode = GETPOST('mode', 'aZ09') ? GETPOST('mode', 'aZ09') : 'createform'; // 'createform', 'filters', 'sortorder', 'focus'
48 
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) {
54  $page = 0;
55 } // If $page is not defined, or '' or -1
56 $offset = $limit * $page;
57 $pageprev = $page - 1;
58 $pagenext = $page + 1;
59 if (!$sortfield) {
60  $sortfield = 'page,param';
61 }
62 if (!$sortorder) {
63  $sortorder = 'ASC';
64 }
65 
66 $defaulturl = GETPOST('defaulturl', 'alphanohtml');
67 $defaultkey = GETPOST('defaultkey', 'alphanohtml');
68 $defaultvalue = GETPOST('defaultvalue', 'restricthtml');
69 
70 $defaulturl = preg_replace('/^\//', '', $defaulturl);
71 
72 $urlpage = GETPOST('urlpage', 'alphanohtml');
73 $key = GETPOST('key', 'alphanohtml');
74 $value = GETPOST('value', 'restricthtml');
75 
76 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
77 $hookmanager->initHooks(array('admindefaultvalues', 'globaladmin'));
78 
79 
80 $object = new DefaultValues($db);
81 /*
82  * Actions
83  */
84 
85 if (GETPOST('cancel', 'alpha')) {
86  $action = 'list'; $massaction = '';
87 }
88 if (!GETPOST('confirmmassaction', 'alpha') && !empty($massaction) && $massaction != 'presend' && $massaction != 'confirm_presend') {
89  $massaction = '';
90 }
91 
92 $parameters = array();
93 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
94 if ($reshook < 0) {
95  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
96 }
97 
98 include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
99 
100 // Purge search criteria
101 if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
102  $defaulturl = '';
103  $defaultkey = '';
104  $defaultvalue = '';
105  $toselect = array();
106  $search_array_options = array();
107 }
108 
109 if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES') {
110  if (GETPOST('value')) {
111  dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
112  } else {
113  dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
114  }
115 }
116 
117 if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify')) {
118  $error = 0;
119 
120  if (($action == 'add' || (GETPOST('add') && $action != 'update'))) {
121  if (empty($defaulturl)) {
122  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
123  $error++;
124  }
125  if (empty($defaultkey)) {
126  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
127  $error++;
128  }
129  }
130  if (GETPOST('actionmodify')) {
131  if (empty($urlpage)) {
132  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
133  $error++;
134  }
135  if (empty($key)) {
136  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
137  $error++;
138  }
139  }
140 
141  if (!$error) {
142  if ($action == 'add' || (GETPOST('add') && $action != 'update')) {
143  $object->type=$mode;
144  $object->user_id=0;
145  $object->page=$defaulturl;
146  $object->param=$defaultkey;
147  $object->value=$defaultvalue;
148  $object->entity=$conf->entity;
149  $result=$object->create($user);
150  if ($result < 0) {
151  $action = '';
152  setEventMessages($object->error, $object->errors, 'errors');
153  } else {
154  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
155  $action = '';
156  $defaulturl = '';
157  $defaultkey = '';
158  $defaultvalue = '';
159  }
160  }
161  if (GETPOST('actionmodify')) {
162  $object->id=$id;
163  $object->type=$mode;
164  $object->page=$urlpage;
165  $object->param=$key;
166  $object->value=$value;
167  $object->entity=$conf->entity;
168  $result=$object->update($user);
169  if ($result<0) {
170  $action = '';
171  setEventMessages($object->error, $object->errors, 'errors');
172  } else {
173  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
174  $action = "";
175  $defaulturl = '';
176  $defaultkey = '';
177  $defaultvalue = '';
178  }
179  }
180  }
181 }
182 
183 // Delete line from delete picto
184 if ($action == 'delete') {
185  $object->id=$id;
186  $result=$object->delete($user);
187  if ($result<0) {
188  $action = '';
189  setEventMessages($object->error, $object->errors, 'errors');
190  }
191 }
192 
193 
194 
195 /*
196  * View
197  */
198 
199 $form = new Form($db);
200 $formadmin = new FormAdmin($db);
201 
202 $wikihelp = 'EN:First_setup|FR:Premiers_paramétrages|ES:Primeras_configuraciones';
203 llxHeader('', $langs->trans("Setup"), $wikihelp);
204 
205 $param = '&mode='.$mode;
206 
207 $enabledisablehtml = $langs->trans("EnableDefaultValues").' ';
208 if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) {
209  // Button off, click to enable
210  $enabledisablehtml .= '<a class="reposition valignmiddle" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&token='.newToken().'&value=1'.$param.'">';
211  $enabledisablehtml .= img_picto($langs->trans("Disabled"), 'switch_off');
212  $enabledisablehtml .= '</a>';
213 } else {
214  // Button on, click to disable
215  $enabledisablehtml .= '<a class="reposition valignmiddle" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&token='.newToken().'&value=0'.$param.'">';
216  $enabledisablehtml .= img_picto($langs->trans("Activated"), 'switch_on');
217  $enabledisablehtml .= '</a>';
218 }
219 
220 print load_fiche_titre($langs->trans("DefaultValues"), $enabledisablehtml, 'title_setup');
221 
222 print '<span class="opacitymedium">'.$langs->trans("DefaultValuesDesc")."</span><br>\n";
223 print "<br>\n";
224 
225 if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
226  $param .= '&contextpage='.urlencode($contextpage);
227 }
228 if ($limit > 0 && $limit != $conf->liste_limit) {
229  $param .= '&limit='.((int) $limit);
230 }
231 if ($optioncss != '') {
232  $param .= '&optioncss='.urlencode($optioncss);
233 }
234 if ($defaulturl) {
235  $param .= '&defaulturl='.urlencode($defaulturl);
236 }
237 if ($defaultkey) {
238  $param .= '&defaultkey='.urlencode($defaultkey);
239 }
240 if ($defaultvalue) {
241  $param .= '&defaultvalue='.urlencode($defaultvalue);
242 }
243 
244 
245 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
246 if ($optioncss != '') {
247  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
248 }
249 print '<input type="hidden" name="token" value="'.newToken().'">';
250 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
251 print '<input type="hidden" name="action" value="list">';
252 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
253 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
254 print '<input type="hidden" name="page" value="'.$page.'">';
255 
257 
258 print dol_get_fiche_head($head, $mode, '', -1, '');
259 
260 if ($mode == 'sortorder') {
261  print info_admin($langs->trans("WarningSettingSortOrder")).'<br>';
262 }
263 if ($mode == 'mandatory') {
264  print info_admin($langs->trans("FeatureSupportedOnTextFieldsOnly")).'<br>';
265 }
266 
267 print '<input type="hidden" name="token" value="'.newToken().'">';
268 print '<input type="hidden" id="action" name="action" value="">';
269 print '<input type="hidden" id="mode" name="mode" value="'.dol_escape_htmltag($mode).'">';
270 
271 print '<div class="div-table-responsive-no-min">';
272 print '<table class="noborder centpercent">';
273 print '<tr class="liste_titre">';
274 // Page
275 $texthelp = $langs->trans("PageUrlForDefaultValues");
276 if ($mode == 'createform') {
277  $texthelp .= $langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php', 'societe/card.php?abc=val1&def=val2');
278 } else {
279  $texthelp .= $langs->trans("PageUrlForDefaultValuesList", 'societe/list.php', 'societe/list.php?abc=val1&def=val2');
280 }
281 $texthelp .= '<br><br>'.$langs->trans("AlsoDefaultValuesAreEffectiveForActionCreate");
282 $texturl = $form->textwithpicto($langs->trans("RelativeURL"), $texthelp);
283 print_liste_field_titre($texturl, $_SERVER["PHP_SELF"], 'page,param', '', $param, '', $sortfield, $sortorder);
284 // Field
285 $texthelp = $langs->trans("TheKeyIsTheNameOfHtmlField");
286 if ($mode != 'sortorder') {
287  $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
288 } else {
289  $texthelp = 'field or alias.field';
290  $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
291 }
292 print_liste_field_titre($textkey, $_SERVER["PHP_SELF"], 'param', '', $param, '', $sortfield, $sortorder);
293 // Value
294 if ($mode != 'focus' && $mode != 'mandatory') {
295  if ($mode != 'sortorder') {
296  $substitutionarray = getCommonSubstitutionArray($langs, 2, array('object', 'objectamount')); // Must match list into GETPOST
297  unset($substitutionarray['__USER_SIGNATURE__']);
298  unset($substitutionarray['__SENDEREMAIL_SIGNATURE__']);
299  $texthelp = $langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
300  foreach ($substitutionarray as $key => $val) {
301  $texthelp .= $key.' -> '.$val.'<br>';
302  }
303  $textvalue = $form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, 'subsitutiontooltip');
304  } else {
305  $texthelp = 'ASC or DESC';
306  $textvalue = $form->textwithpicto($langs->trans("SortOrder"), $texthelp);
307  }
308  print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
309 }
310 // Entity
311 if (isModEnabled('multicompany') && !$user->entity) {
312  print_liste_field_titre("Entity", $_SERVER["PHP_SELF"], 'entity,page', '', $param, '', $sortfield, $sortorder);
313 } else {
314  print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
315 }
316 // Actions
317 print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
318 print "</tr>\n";
319 
320 
321 // Line to add new record
322 print "\n";
323 
324 print '<tr class="oddeven">';
325 // Page
326 print '<td>';
327 print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="'.dol_escape_htmltag($defaulturl).'">';
328 print '</td>'."\n";
329 // Field
330 print '<td>';
331 print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultkey" value="'.dol_escape_htmltag($defaultkey).'">';
332 print '</td>';
333 // Value
334 if ($mode != 'focus' && $mode != 'mandatory') {
335  print '<td>';
336  print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultvalue" value="'.dol_escape_htmltag($defaultvalue).'">';
337  print '</td>';
338 }
339 // Limit to superadmin
340 if (isModEnabled('multicompany') && !$user->entity) {
341  print '<td>';
342  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
343  print '</td>';
344 } else {
345  print '<td class="center">';
346  print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
347  print '</td>';
348 }
349 print '<td class="center">';
350 $disabled = '';
351 if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) {
352  $disabled = ' disabled="disabled"';
353 }
354 print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
355 print '</td>'."\n";
356 print '</tr>'."\n";
357 
358 $result = $object->fetchAll($sortorder, $sortfield, 0, 0, array('t.type'=>$mode,'t.entity'=>array($user->entity,$conf->entity)));
359 
360 if (!is_array($result) && $result < 0) {
361  setEventMessages($object->error, $object->errors, 'errors');
362 } elseif (is_array($result) && count($result) > 0) {
363  foreach ($result as $key => $defaultvalue) {
364  print '<tr class="oddeven">';
365 
366  // Page
367  print '<td>';
368  if ($action != 'edit' || GETPOST('rowid', 'int') != $defaultvalue->id) print $defaultvalue->page;
369  else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($defaultvalue->page).'">';
370  print '</td>'."\n";
371 
372  // Field
373  print '<td>';
374  if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) print $defaultvalue->param;
375  else print '<input type="text" name="key" value="'.dol_escape_htmltag($defaultvalue->param).'">';
376  print '</td>'."\n";
377 
378  // Value
379  if ($mode != 'focus' && $mode != 'mandatory') {
380  print '<td>';
381  if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) print dol_escape_htmltag($defaultvalue->value);
382  else print '<input type="text" name="value" value="'.dol_escape_htmltag($defaultvalue->value).'">';
383  print '</td>';
384  }
385 
386  // Multicompany
387  print '<td>';
388  if (isModEnabled('multicompany')) {
389  print dol_escape_htmltag($defaultvalue->entity);
390  }
391  print '</td>';
392 
393  // Actions
394  print '<td class="center">';
395  if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
396  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>';
397  print '<a class="marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?rowid='.$defaultvalue->id.'&entity='.$defaultvalue->entity.'&mode='.$mode.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
398  } else {
399  print '<input type="hidden" name="page" value="'.$page.'">';
400  print '<input type="hidden" name="rowid" value="'.$id.'">';
401  print '<div name="'.(!empty($defaultvalue->id) ? $defaultvalue->id : 'none').'"></div>';
402  print '<input type="submit" class="button button-edit" name="actionmodify" value="'.$langs->trans("Modify").'">';
403  print '<input type="submit" class="button button-cancel" name="actioncancel" value="'.$langs->trans("Cancel").'">';
404  }
405  print '</td>';
406 
407  print "</tr>\n";
408  }
409 }
410 
411 print '</table>';
412 print '</div>';
413 
414 print dol_get_fiche_end();
415 
416 print "</form>\n";
417 
418 // End of page
419 llxFooter();
420 $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:638
defaultvalues_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:943
if(GETPOSTISSET('MAIN_AGENDA_XCAL_EXPORTKEY')) if(GETPOSTISSET('MAIN_AGENDA_EXPORT_PAST_DELAY')) if(GETPOSTISSET('MAIN_AGENDA_EXPORT_CACHE')) if(GETPOSTISSET('AGENDA_EXPORT_FIX_TZ')) if($actionsave) if(!isset($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) $wikihelp
View.
Definition: agenda_xcal.php:90
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 for MyObject.
Class to generate html code for admin pages.
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_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show 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.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
isModEnabled($module)
Is Dolibarr module enabled.
img_edit($titlealt='default', $float=0, $other='')
Show logo editer/modifier fiche.
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...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.