defaultvalues.php 16 KB

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