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