dolibarr  9.0.0
defaultvalues.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017-2018 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 <http://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 
34 // Load translation files required by the page
35 $langs->loadLangs(array('companies', 'products', 'admin', 'sms', 'other', 'errors'));
36 
37 if (!$user->admin) accessforbidden();
38 
39 $id=GETPOST('rowid','int');
40 $action=GETPOST('action','alpha');
41 
42 $mode = GETPOST('mode','aZ09')?GETPOST('mode','aZ09'):'createform'; // 'createform', 'filters', 'sortorder', 'focus'
43 
44 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
45 $sortfield = GETPOST("sortfield",'alpha');
46 $sortorder = GETPOST("sortorder",'alpha');
47 $page = GETPOST("page",'int');
48 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
49 $offset = $limit * $page;
50 $pageprev = $page - 1;
51 $pagenext = $page + 1;
52 if (! $sortfield) $sortfield='page,param';
53 if (! $sortorder) $sortorder='ASC';
54 
55 $defaulturl = GETPOST('defaulturl');
56 $defaultkey = GETPOST('defaultkey','alpha');
57 $defaultvalue = GETPOST('defaultvalue');
58 
59 $defaulturl=preg_replace('/^\//', '', $defaulturl);
60 
61 $urlpage = GETPOST('urlpage');
62 $key = GETPOST('key');
63 $value = GETPOST('value');
64 
65 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
66 $hookmanager->initHooks(array('admindefaultvalues','globaladmin'));
67 
68 
69 /*
70  * Actions
71  */
72 
73 if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
74 if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
75 
76 $parameters=array('socid'=>$socid);
77 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
78 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
79 
80 include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
81 
82 // Purge search criteria
83 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
84 {
85  $defaulturl='';
86  $defaultkey='';
87  $defaultvalue='';
88  $toselect='';
89  $search_array_options=array();
90 }
91 
92 if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES')
93 {
94  if (GETPOST('value')) dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
95  else dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
96 }
97 
98 if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify'))
99 {
100  $error=0;
101 
102  if (($action == 'add' || (GETPOST('add') && $action != 'update')))
103  {
104  if (empty($defaulturl))
105  {
106  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
107  $error++;
108  }
109  if (empty($defaultkey))
110  {
111  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
112  $error++;
113  }
114  }
115  if (GETPOST('actionmodify'))
116  {
117  if (empty($urlpage))
118  {
119  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
120  $error++;
121  }
122  if (empty($key))
123  {
124  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
125  $error++;
126  }
127  }
128 
129  if (! $error)
130  {
131  $db->begin();
132 
133  if ($action == 'add' || (GETPOST('add') && $action != 'update'))
134  {
135  $sql = "INSERT INTO ".MAIN_DB_PREFIX."default_values(type, user_id, page, param, value, entity) VALUES ('".$db->escape($mode)."', 0, '".$db->escape($defaulturl)."','".$db->escape($defaultkey)."','".$db->escape($defaultvalue)."', ".$db->escape($conf->entity).")";
136  }
137  if (GETPOST('actionmodify'))
138  {
139  $sql = "UPDATE ".MAIN_DB_PREFIX."default_values SET page = '".$db->escape($urlpage)."', param = '".$db->escape($key)."', value = '".$db->escape($value)."'";
140  $sql.= " WHERE rowid = ".$id;
141  }
142 
143  $result = $db->query($sql);
144  if ($result > 0)
145  {
146  $db->commit();
147  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
148  $action="";
149  $defaulturl='';
150  $defaultkey='';
151  $defaultvalue='';
152  }
153  else
154  {
155  $db->rollback();
156  setEventMessages($db->lasterror(), null, 'errors');
157  $action='';
158  }
159  }
160 }
161 
162 // Delete line from delete picto
163 if ($action == 'delete')
164 {
165  $sql = "DELETE FROM ".MAIN_DB_PREFIX."default_values WHERE rowid = ".$db->escape($id);
166  // Delete const
167  $result = $db->query($sql);
168  if ($result >= 0)
169  {
170  setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
171  }
172  else
173  {
174  dol_print_error($db);
175  }
176 }
177 
178 
179 
180 /*
181  * View
182  */
183 
184 $form=new Form($db);
185 $formadmin = new FormAdmin($db);
186 
187 $wikihelp='EN:Setup|FR:Paramétrage|ES:Configuración';
188 llxHeader('',$langs->trans("Setup"),$wikihelp);
189 
190 $param='&mode='.$mode;
191 
192 $enabledisablehtml.= $langs->trans("EnableDefaultValues").' ';
193 if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES))
194 {
195  // Button off, click to enable
196  $enabledisablehtml.= '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&value=1'.$param.'">';
197  $enabledisablehtml.= img_picto($langs->trans("Disabled"),'switch_off');
198  $enabledisablehtml.= '</a>';
199 }
200 else
201 {
202  // Button on, click to disable
203  $enabledisablehtml.= '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&value=0'.$param.'">';
204  $enabledisablehtml.= img_picto($langs->trans("Activated"),'switch_on');
205  $enabledisablehtml.= '</a>';
206 }
207 
208 print load_fiche_titre($langs->trans("DefaultValues"), $enabledisablehtml, 'title_setup');
209 
210 print $langs->trans("DefaultValuesDesc")."<br>\n";
211 print "<br>\n";
212 
213 if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
214 if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
215 if ($optioncss != '') $param.='&optioncss='.$optioncss;
216 if ($defaulturl) $param.='&defaulturl='.urlencode($defaulturl);
217 if ($defaultkey) $param.='&defaultkey='.urlencode($defaultkey);
218 if ($defaultvalue) $param.='&defaultvalue='.urlencode($defaultvalue);
219 
220 
221 print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug)?'?debug=1':'').'" method="POST">';
222 if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
223 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
224 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
225 print '<input type="hidden" name="action" value="list">';
226 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
227 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
228 print '<input type="hidden" name="page" value="'.$page.'">';
229 
231 
232 dol_fiche_head($head, $mode, '', -1, '');
233 
234 if ($mode == 'sortorder')
235 {
236  print info_admin($langs->trans("WarningSettingSortOrder")).'<br>';
237 }
238 if ($mode == 'mandatory')
239 {
240  print info_admin($langs->trans("FeatureSupportedOnTextFieldsOnly")).'<br>';
241 }
242 
243 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
244 print '<input type="hidden" id="action" name="action" value="">';
245 print '<input type="hidden" id="mode" name="mode" value="'.dol_escape_htmltag($mode).'">';
246 
247 print '<div class="div-table-responsive-no-min">';
248 print '<table class="noborder" width="100%">';
249 print '<tr class="liste_titre">';
250 // Page
251 $texthelp=$langs->trans("PageUrlForDefaultValues");
252 if ($mode == 'createform') $texthelp.=$langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php', 'societe/card.php?abc=val1&def=val2');
253 else $texthelp.=$langs->trans("PageUrlForDefaultValuesList", 'societe/list.php', 'societe/list.php?abc=val1&def=val2');
254 $texturl=$form->textwithpicto($langs->trans("Url"), $texthelp);
255 print_liste_field_titre($texturl,$_SERVER["PHP_SELF"],'page,param','',$param,'',$sortfield,$sortorder);
256 // Field
257 $texthelp=$langs->trans("TheKeyIsTheNameOfHtmlField");
258 if ($mode != 'sortorder')
259 {
260  $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
261 }
262 else
263 {
264  $texthelp='field or alias.field';
265  $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
266 }
267 print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'param','',$param,'',$sortfield,$sortorder);
268 // Value
269 if ($mode != 'focus' && $mode != 'mandatory')
270 {
271  if ($mode != 'sortorder')
272  {
273  $substitutionarray=getCommonSubstitutionArray($langs, 2, array('object','objectamount')); // Must match list into GETPOST
274  unset($substitutionarray['__USER_SIGNATURE__']);
275  $texthelp=$langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
276  foreach($substitutionarray as $key => $val)
277  {
278  $texthelp.=$key.' -> '.$val.'<br>';
279  }
280  $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, 'subsitutiontooltip');
281  }
282  else
283  {
284  $texthelp='ASC or DESC';
285  $textvalue=$form->textwithpicto($langs->trans("SortOrder"), $texthelp);
286  }
287  print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
288 }
289 // Entity
290 if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre("Entity",$_SERVER["PHP_SELF"],'entity,page','',$param,'',$sortfield,$sortorder);
291 else print_liste_field_titre("",$_SERVER["PHP_SELF"],'','',$param,'',$sortfield,$sortorder);
292 // Actions
293 print_liste_field_titre("",$_SERVER["PHP_SELF"],'','',$param,'',$sortfield,$sortorder);
294 print "</tr>\n";
295 
296 
297 // Line to add new record
298 print "\n";
299 
300 print '<tr class="oddeven">';
301 // Page
302 print '<td>';
303 print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="'.dol_escape_htmltag(GETPOST('defaulturl','alphanohtml')).'">';
304 print '</td>'."\n";
305 // Field
306 print '<td>';
307 print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultkey" value="'.dol_escape_htmltag(GETPOST('defaultkey','alphanohtml')).'">';
308 print '</td>';
309 // Value
310 if ($mode != 'focus' && $mode != 'mandatory')
311 {
312  print '<td>';
313  print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultvalue" value="">';
314  print '</td>';
315 }
316 // Limit to superadmin
317 if (! empty($conf->multicompany->enabled) && !$user->entity)
318 {
319  print '<td>';
320  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
321  print '</td>';
322 }
323 else
324 {
325  print '<td align="center">';
326  print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
327  print '</td>';
328 }
329 print '<td align="center">';
330 $disabled='';
331 if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) $disabled=' disabled="disabled"';
332 print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
333 print "</td>\n";
334 print '</tr>';
335 
336 
337 // Show constants
338 $sql = "SELECT rowid, entity, type, page, param, value";
339 $sql.= " FROM ".MAIN_DB_PREFIX."default_values";
340 $sql.= " WHERE type = '".$db->escape($mode)."'";
341 $sql.= " AND entity IN (".$user->entity.",".$conf->entity.")";
342 $sql.= $db->order($sortfield, $sortorder);
343 
344 dol_syslog("translation::select from table", LOG_DEBUG);
345 $result = $db->query($sql);
346 if ($result)
347 {
348  $num = $db->num_rows($result);
349  $i = 0;
350 
351  while ($i < $num)
352  {
353  $obj = $db->fetch_object($result);
354 
355 
356  print "\n";
357 
358  print '<tr class="oddeven">';
359 
360  // Page
361  print '<td>';
362  if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->page;
363  else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($obj->page).'">';
364  print '</td>'."\n";
365 
366  // Field
367  print '<td>';
368  if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->param;
369  else print '<input type="text" name="key" value="'.dol_escape_htmltag($obj->param).'">';
370  print '</td>'."\n";
371 
372  // Value
373  if ($mode != 'focus' && $mode != 'mandatory')
374  {
375  print '<td>';
376  /*print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
377  print '<input type="hidden" name="const['.$i.'][lang]" value="'.$obj->lang.'">';
378  print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->transkey.'">';
379  print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.dol_escape_htmltag($obj->transvalue).'">';
380  */
381  if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->value;
382  else print '<input type="text" name="value" value="'.dol_escape_htmltag($obj->value).'">';
383  print '</td>';
384  }
385 
386  if (! empty($conf->multicompany->enabled) && !$user->entity)
387  {
388  print '<td></td>';
389  }
390 
391  // Actions
392  print '<td align="center">';
393  if ($action != 'edit' || GETPOST('rowid') != $obj->rowid)
394  {
395  print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=edit'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_edit().'</a>';
396  print ' &nbsp; ';
397  print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=delete'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_delete().'</a>';
398  }
399  else
400  {
401  print '<input type="hidden" name="page" value="'.$page.'">';
402  print '<input type="hidden" name="rowid" value="'.$id.'">';
403  print '<div name="'.(! empty($obj->rowid)?$obj->rowid:'none').'"></div>';
404  print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
405  print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">';
406  }
407  print '</td>';
408 
409  print "</tr>\n";
410  print "\n";
411  $i++;
412  }
413 }
414 else
415 {
416  dol_print_error($db);
417 }
418 
419 
420 print '</table>';
421 print '</div>';
422 
423 dol_fiche_end();
424 
425 print "</form>\n";
426 
427 // End of page
428 llxFooter();
429 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
img_edit($titlealt='default', $float=0, $other='class="pictoedit"')
Show logo editer/modifier fiche.
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:510
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
img_delete($titlealt='default', $other='class="pictodelete"')
Show delete logo.
Class to generate html code for admin pages.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='')
Show information for admin users or standard users.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
defaultvalues_prepare_head()
Prepare array with list of tabs.
Definition: admin.lib.php:706
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $wikihelp
View.
Definition: agenda.php:120