defaultvalues.php 15 KB

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