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