defaultvalues.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. /**
  19. * \file htdocs/admin/defaultvalues.php
  20. * \brief Page to set default values used used in a create form
  21. * Default values are stored into $user->default_values[url]['createform']['querystring'|'_noquery_'][paramkey]=paramvalue
  22. * Default filters are stored into $user->default_values[url]['filters']['querystring'|'_noquery_'][paramkey]=paramvalue
  23. * Default sort order are stored into $user->default_values[url]['sortorder']['querystring'|'_noquery_'][paramkey]=paramvalue
  24. * Default focus are stored into $user->default_values[url]['focus']['querystring'|'_noquery_'][paramkey]=paramvalue
  25. * Mandatory fields are stored into $user->default_values[url]['mandatory']['querystring'|'_noquery_'][paramkey]=paramvalue
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('companies', 'products', 'admin', 'sms', 'other', 'errors'));
  33. if (!$user->admin) accessforbidden();
  34. $id=GETPOST('rowid','int');
  35. $action=GETPOST('action','alpha');
  36. $mode = GETPOST('mode','aZ09')?GETPOST('mode','aZ09'):'createform'; // 'createform', 'filters', 'sortorder', 'focus'
  37. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  38. $sortfield = GETPOST("sortfield",'alpha');
  39. $sortorder = GETPOST("sortorder",'alpha');
  40. $page = GETPOST("page",'int');
  41. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  42. $offset = $limit * $page;
  43. $pageprev = $page - 1;
  44. $pagenext = $page + 1;
  45. if (! $sortfield) $sortfield='page,param';
  46. if (! $sortorder) $sortorder='ASC';
  47. $defaulturl = GETPOST('defaulturl');
  48. $defaultkey = GETPOST('defaultkey','alpha');
  49. $defaultvalue = GETPOST('defaultvalue');
  50. $defaulturl=preg_replace('/^\//', '', $defaulturl);
  51. $urlpage = GETPOST('urlpage');
  52. $key = GETPOST('key');
  53. $value = GETPOST('value');
  54. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  55. $hookmanager->initHooks(array('admindefaultvalues','globaladmin'));
  56. /*
  57. * Actions
  58. */
  59. if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
  60. if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  61. $parameters=array('socid'=>$socid);
  62. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  63. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  64. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  65. // Purge search criteria
  66. 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
  67. {
  68. $defaulturl='';
  69. $defaultkey='';
  70. $defaultvalue='';
  71. $toselect='';
  72. $search_array_options=array();
  73. }
  74. if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES')
  75. {
  76. if (GETPOST('value')) dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
  77. else dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
  78. }
  79. if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify'))
  80. {
  81. $error=0;
  82. if (($action == 'add' || (GETPOST('add') && $action != 'update')))
  83. {
  84. if (empty($defaulturl))
  85. {
  86. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
  87. $error++;
  88. }
  89. if (empty($defaultkey))
  90. {
  91. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
  92. $error++;
  93. }
  94. }
  95. if (GETPOST('actionmodify'))
  96. {
  97. if (empty($urlpage))
  98. {
  99. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
  100. $error++;
  101. }
  102. if (empty($key))
  103. {
  104. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
  105. $error++;
  106. }
  107. }
  108. if (! $error)
  109. {
  110. $db->begin();
  111. if ($action == 'add' || (GETPOST('add') && $action != 'update'))
  112. {
  113. $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).")";
  114. }
  115. if (GETPOST('actionmodify'))
  116. {
  117. $sql = "UPDATE ".MAIN_DB_PREFIX."default_values SET page = '".$db->escape($urlpage)."', param = '".$db->escape($key)."', value = '".$db->escape($value)."'";
  118. $sql.= " WHERE rowid = ".$id;
  119. }
  120. $result = $db->query($sql);
  121. if ($result > 0)
  122. {
  123. $db->commit();
  124. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  125. $action="";
  126. $defaulturl='';
  127. $defaultkey='';
  128. $defaultvalue='';
  129. }
  130. else
  131. {
  132. $db->rollback();
  133. setEventMessages($db->lasterror(), null, 'errors');
  134. $action='';
  135. }
  136. }
  137. }
  138. // Delete line from delete picto
  139. if ($action == 'delete')
  140. {
  141. $sql = "DELETE FROM ".MAIN_DB_PREFIX."default_values WHERE rowid = ".$db->escape($id);
  142. // Delete const
  143. $result = $db->query($sql);
  144. if ($result >= 0)
  145. {
  146. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  147. }
  148. else
  149. {
  150. dol_print_error($db);
  151. }
  152. }
  153. /*
  154. * View
  155. */
  156. $form=new Form($db);
  157. $formadmin = new FormAdmin($db);
  158. $wikihelp='EN:Setup|FR:Paramétrage|ES:Configuración';
  159. llxHeader('',$langs->trans("Setup"),$wikihelp);
  160. $param='&mode='.$mode;
  161. $enabledisablehtml.= $langs->trans("EnableDefaultValues").' ';
  162. if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES))
  163. {
  164. // Button off, click to enable
  165. $enabledisablehtml.= '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&value=1'.$param.'">';
  166. $enabledisablehtml.= img_picto($langs->trans("Disabled"),'switch_off');
  167. $enabledisablehtml.= '</a>';
  168. }
  169. else
  170. {
  171. // Button on, click to disable
  172. $enabledisablehtml.= '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&value=0'.$param.'">';
  173. $enabledisablehtml.= img_picto($langs->trans("Activated"),'switch_on');
  174. $enabledisablehtml.= '</a>';
  175. }
  176. print load_fiche_titre($langs->trans("DefaultValues"), $enabledisablehtml, 'title_setup');
  177. print $langs->trans("DefaultValuesDesc")."<br>\n";
  178. print "<br>\n";
  179. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  180. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  181. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  182. if ($defaulturl) $param.='&defaulturl='.urlencode($defaulturl);
  183. if ($defaultkey) $param.='&defaultkey='.urlencode($defaultkey);
  184. if ($defaultvalue) $param.='&defaultvalue='.urlencode($defaultvalue);
  185. print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug)?'?debug=1':'').'" method="POST">';
  186. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  187. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  188. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  189. print '<input type="hidden" name="action" value="list">';
  190. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  191. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  192. print '<input type="hidden" name="page" value="'.$page.'">';
  193. $head=defaultvalues_prepare_head();
  194. dol_fiche_head($head, $mode, '', -1, '');
  195. if ($mode == 'sortorder')
  196. {
  197. print info_admin($langs->trans("WarningSettingSortOrder")).'<br>';
  198. }
  199. if ($mode == 'mandatory')
  200. {
  201. print info_admin($langs->trans("FeatureSupportedOnTextFieldsOnly")).'<br>';
  202. }
  203. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  204. print '<input type="hidden" id="action" name="action" value="">';
  205. print '<input type="hidden" id="mode" name="mode" value="'.dol_escape_htmltag($mode).'">';
  206. print '<div class="div-table-responsive-no-min">';
  207. print '<table class="noborder" width="100%">';
  208. print '<tr class="liste_titre">';
  209. // Page
  210. $texthelp=$langs->trans("PageUrlForDefaultValues");
  211. if ($mode == 'createform') $texthelp.=$langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php', 'societe/card.php?abc=val1&def=val2');
  212. else $texthelp.=$langs->trans("PageUrlForDefaultValuesList", 'societe/list.php', 'societe/list.php?abc=val1&def=val2');
  213. $texturl=$form->textwithpicto($langs->trans("Url"), $texthelp);
  214. print_liste_field_titre($texturl,$_SERVER["PHP_SELF"],'page,param','',$param,'',$sortfield,$sortorder);
  215. // Field
  216. $texthelp=$langs->trans("TheKeyIsTheNameOfHtmlField");
  217. if ($mode != 'sortorder')
  218. {
  219. $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
  220. }
  221. else
  222. {
  223. $texthelp='field or alias.field';
  224. $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
  225. }
  226. print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'param','',$param,'',$sortfield,$sortorder);
  227. // Value
  228. if ($mode != 'focus' && $mode != 'mandatory')
  229. {
  230. if ($mode != 'sortorder')
  231. {
  232. $substitutionarray=getCommonSubstitutionArray($langs, 2, array('object','objectamount')); // Must match list into GETPOST
  233. unset($substitutionarray['__USER_SIGNATURE__']);
  234. $texthelp=$langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
  235. foreach($substitutionarray as $key => $val)
  236. {
  237. $texthelp.=$key.' -> '.$val.'<br>';
  238. }
  239. $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, 'subsitutiontooltip');
  240. }
  241. else
  242. {
  243. $texthelp='ASC or DESC';
  244. $textvalue=$form->textwithpicto($langs->trans("SortOrder"), $texthelp);
  245. }
  246. print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
  247. }
  248. // Entity
  249. if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre("Entity",$_SERVER["PHP_SELF"],'entity,page','',$param,'',$sortfield,$sortorder);
  250. else print_liste_field_titre("",$_SERVER["PHP_SELF"],'','',$param,'',$sortfield,$sortorder);
  251. // Actions
  252. print_liste_field_titre("",$_SERVER["PHP_SELF"],'','',$param,'',$sortfield,$sortorder);
  253. print "</tr>\n";
  254. // Line to add new record
  255. print "\n";
  256. print '<tr class="oddeven">';
  257. // Page
  258. print '<td>';
  259. print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="'.dol_escape_htmltag(GETPOST('defaulturl','alphanohtml')).'">';
  260. print '</td>'."\n";
  261. // Field
  262. print '<td>';
  263. print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultkey" value="'.dol_escape_htmltag(GETPOST('defaultkey','alphanohtml')).'">';
  264. print '</td>';
  265. // Value
  266. if ($mode != 'focus' && $mode != 'mandatory')
  267. {
  268. print '<td>';
  269. print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultvalue" value="">';
  270. print '</td>';
  271. }
  272. // Limit to superadmin
  273. if (! empty($conf->multicompany->enabled) && !$user->entity)
  274. {
  275. print '<td>';
  276. 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
  277. print '</td>';
  278. }
  279. else
  280. {
  281. print '<td align="center">';
  282. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  283. print '</td>';
  284. }
  285. print '<td align="center">';
  286. $disabled='';
  287. if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) $disabled=' disabled="disabled"';
  288. print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
  289. print "</td>\n";
  290. print '</tr>';
  291. // Show constants
  292. $sql = "SELECT rowid, entity, type, page, param, value";
  293. $sql.= " FROM ".MAIN_DB_PREFIX."default_values";
  294. $sql.= " WHERE type = '".$db->escape($mode)."'";
  295. $sql.= " AND entity IN (".$user->entity.",".$conf->entity.")";
  296. $sql.= $db->order($sortfield, $sortorder);
  297. dol_syslog("translation::select from table", LOG_DEBUG);
  298. $result = $db->query($sql);
  299. if ($result)
  300. {
  301. $num = $db->num_rows($result);
  302. $i = 0;
  303. while ($i < $num)
  304. {
  305. $obj = $db->fetch_object($result);
  306. print "\n";
  307. print '<tr class="oddeven">';
  308. // Page
  309. print '<td>';
  310. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->page;
  311. else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($obj->page).'">';
  312. print '</td>'."\n";
  313. // Field
  314. print '<td>';
  315. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->param;
  316. else print '<input type="text" name="key" value="'.dol_escape_htmltag($obj->param).'">';
  317. print '</td>'."\n";
  318. // Value
  319. if ($mode != 'focus' && $mode != 'mandatory')
  320. {
  321. print '<td>';
  322. /*print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  323. print '<input type="hidden" name="const['.$i.'][lang]" value="'.$obj->lang.'">';
  324. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->transkey.'">';
  325. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.dol_escape_htmltag($obj->transvalue).'">';
  326. */
  327. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->value;
  328. else print '<input type="text" name="value" value="'.dol_escape_htmltag($obj->value).'">';
  329. print '</td>';
  330. }
  331. if (! empty($conf->multicompany->enabled) && !$user->entity)
  332. {
  333. print '<td></td>';
  334. }
  335. // Actions
  336. print '<td align="center">';
  337. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid)
  338. {
  339. 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>';
  340. print ' &nbsp; ';
  341. 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>';
  342. }
  343. else
  344. {
  345. print '<input type="hidden" name="page" value="'.$page.'">';
  346. print '<input type="hidden" name="rowid" value="'.$id.'">';
  347. print '<div name="'.(! empty($obj->rowid)?$obj->rowid:'none').'"></div>';
  348. print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
  349. print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  350. }
  351. print '</td>';
  352. print "</tr>\n";
  353. print "\n";
  354. $i++;
  355. }
  356. }
  357. else
  358. {
  359. dol_print_error($db);
  360. }
  361. print '</table>';
  362. print '</div>';
  363. dol_fiche_end();
  364. print "</form>\n";
  365. // End of page
  366. llxFooter();
  367. $db->close();