defaultvalues.php 15 KB

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