PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/admin/defaultvalues.php

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