const.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/const.php
  22. * \ingroup setup
  23. * \brief Admin page to define miscellaneous constants
  24. */
  25. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  29. // Load translation files required by the page
  30. $langs->load("admin");
  31. $rowid = GETPOST('rowid', 'int');
  32. $entity = GETPOST('entity', 'int');
  33. $action = GETPOST('action', 'aZ09');
  34. $debug = GETPOST('debug', 'int');
  35. $consts = GETPOST('const', 'array');
  36. $constname = GETPOST('constname', 'alphanohtml');
  37. $constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
  38. $constnote = GETPOST('constnote', 'alpha');
  39. // Load variable for pagination
  40. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  45. $page = 0;
  46. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  47. $offset = $limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. if (empty($sortfield)) {
  51. $sortfield = 'entity,name';
  52. }
  53. if (empty($sortorder)) {
  54. $sortorder = 'ASC';
  55. }
  56. if ($action == 'add' && GETPOST('update')) { // Click on button update must be used in priority before param $action
  57. $action = 'update';
  58. }
  59. if ($action == 'add' && GETPOST('delete')) { // Click on button update must be used in priority before param $action
  60. $action = 'delete';
  61. }
  62. /*if ($action == 'update' && GETPOST('add')) { // 'add' button is always clicked as it is the first in form.
  63. $action = 'add';
  64. }*/
  65. if ($action == 'delete' && GETPOST('add')) { // Click on button add must be used in priority before param $action
  66. $action = 'add';
  67. }
  68. if (!$user->admin) {
  69. accessforbidden();
  70. }
  71. /*
  72. * Actions
  73. */
  74. // Add a new record
  75. if ($action == 'add') {
  76. $error = 0;
  77. if (empty($constname)) {
  78. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
  79. $error++;
  80. }
  81. if ($constvalue == '') {
  82. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
  83. $error++;
  84. }
  85. if (!$error) {
  86. if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
  87. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  88. $action = "";
  89. $constname = "";
  90. $constvalue = "";
  91. $constnote = "";
  92. } else {
  93. dol_print_error($db);
  94. }
  95. }
  96. }
  97. // Mass update
  98. if (!empty($consts) && $action == 'update') {
  99. $nbmodified = 0;
  100. foreach ($consts as $const) {
  101. if (!empty($const["check"])) {
  102. if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
  103. $nbmodified++;
  104. } else {
  105. dol_print_error($db);
  106. }
  107. }
  108. }
  109. if ($nbmodified > 0) {
  110. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  111. }
  112. $action = '';
  113. }
  114. // Mass delete
  115. if (!empty($consts) && $action == 'delete') {
  116. $nbdeleted = 0;
  117. foreach ($consts as $const) {
  118. if (!empty($const["check"])) { // Is checkbox checked
  119. if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
  120. $nbdeleted++;
  121. } else {
  122. dol_print_error($db);
  123. }
  124. }
  125. }
  126. if ($nbdeleted > 0) {
  127. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  128. }
  129. $action = '';
  130. }
  131. // Delete line from delete picto
  132. if ($action == 'delete') {
  133. if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
  134. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  135. } else {
  136. dol_print_error($db);
  137. }
  138. }
  139. /*
  140. * View
  141. */
  142. $form = new Form($db);
  143. $wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
  144. llxHeader('', $langs->trans("Setup"), $wikihelp);
  145. // Add logic to show/hide buttons
  146. if ($conf->use_javascript_ajax) {
  147. ?>
  148. <script type="text/javascript">
  149. jQuery(document).ready(function() {
  150. jQuery("#updateconst").hide();
  151. jQuery("#delconst").hide();
  152. jQuery(".checkboxfordelete").click(function() {
  153. jQuery("#delconst").show();
  154. });
  155. jQuery(".inputforupdate").keyup(function() { // keypress does not support back
  156. var field_id = jQuery(this).attr("id");
  157. var row_num = field_id.split("_");
  158. jQuery("#updateconst").show();
  159. jQuery("#action").val('update'); // so default action if we type enter will be update, but correct action is also detected correctly without that when clicking on "Update" button.
  160. jQuery("#check_" + row_num[1]).prop("checked",true);
  161. });
  162. });
  163. </script>
  164. <?php
  165. }
  166. print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
  167. print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
  168. print "<br>\n";
  169. $param = '';
  170. print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
  171. print '<input type="hidden" name="token" value="'.newToken().'">';
  172. print '<input type="hidden" id="action" name="action" value="add">';
  173. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  174. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  175. print '<div class="div-table-responsive-no-min">';
  176. print '<table class="noborder centpercent">';
  177. print '<tr class="liste_titre">';
  178. print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '') . "\n";
  179. print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  180. print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  181. print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
  182. if (isModEnabled('multicompany') && !$user->entity) {
  183. print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ') . "\n";
  184. }
  185. print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
  186. print "</tr>\n";
  187. // Line to add new record
  188. print "\n";
  189. print '<tr class="oddeven nohover"><td>';
  190. print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'">';
  191. print '</td>'."\n";
  192. print '<td>';
  193. print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'">';
  194. print '</td>';
  195. print '<td>';
  196. print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
  197. print '</td>';
  198. print '<td>';
  199. print '</td>';
  200. // Limit to superadmin
  201. if (isModEnabled('multicompany') && !$user->entity) {
  202. print '<td>';
  203. print '<input type="text" class="flat" size="1" name="entity" value="' . $conf->entity . '">';
  204. print '</td>';
  205. print '<td class="center">';
  206. } else {
  207. print '<td class="center">';
  208. print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
  209. }
  210. print '<input type="submit" class="button button-add small" id="add" name="add" value="'.$langs->trans("Add").'">';
  211. print "</td>\n";
  212. print '</tr>';
  213. // Show constants
  214. $sql = "SELECT";
  215. $sql .= " rowid";
  216. $sql .= ", ".$db->decrypt('name')." as name";
  217. $sql .= ", ".$db->decrypt('value')." as value";
  218. $sql .= ", type";
  219. $sql .= ", note";
  220. $sql .= ", tms";
  221. $sql .= ", entity";
  222. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  223. $sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".$conf->entity).")";
  224. if ((empty($user->entity) || $user->admin) && $debug) {
  225. } elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
  226. // to force for superadmin to debug
  227. $sql .= " AND visible = 1"; // We must always have this. Otherwise, array is too large and submitting data fails due to apache POST or GET limits
  228. }
  229. if (GETPOST('name')) {
  230. $sql .= natural_search("name", GETPOST('name'));
  231. }
  232. $sql .= $db->order($sortfield, $sortorder);
  233. dol_syslog("Const::listConstant", LOG_DEBUG);
  234. $result = $db->query($sql);
  235. if ($result) {
  236. $num = $db->num_rows($result);
  237. $i = 0;
  238. while ($i < $num) {
  239. $obj = $db->fetch_object($result);
  240. $value = dolDecrypt($obj->value);
  241. print "\n";
  242. print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.dol_escape_htmltag($obj->name).'</td>'."\n";
  243. // Value
  244. print '<td>';
  245. print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  246. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
  247. print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
  248. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.(isset($value) ? htmlspecialchars($value) : '').'">';
  249. print '</td>';
  250. // Note
  251. print '<td>';
  252. print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.(empty($obj->note) ? '' : htmlspecialchars($obj->note, 1)).'">';
  253. print '</td>';
  254. // Date last change
  255. print '<td class="nowraponall center">';
  256. print dol_print_date($db->jdate($obj->tms), 'dayhour');
  257. print '</td>';
  258. // Entity limit to superadmin
  259. if (isModEnabled('multicompany') && empty($user->entity)) {
  260. print '<td>';
  261. print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
  262. print '</td>';
  263. print '<td class="center">';
  264. } else {
  265. print '<td class="center">';
  266. print '<input type="hidden" name="const['.$i.'][entity]" value="'.((int) $obj->entity).'">';
  267. }
  268. if (!empty($conf->use_javascript_ajax)) {
  269. print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
  270. } else {
  271. print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&action=delete&token='.newToken().((empty($user->entity) && $debug) ? '&debug=1' : '').'">'.img_delete().'</a>';
  272. }
  273. print "</td></tr>\n";
  274. print "\n";
  275. $i++;
  276. }
  277. }
  278. print '</table>';
  279. print '</div>';
  280. if ($conf->use_javascript_ajax) {
  281. print '<br>';
  282. print '<div id="updateconst" class="right">';
  283. print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
  284. print '</div>';
  285. print '<div id="delconst" class="right">';
  286. print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
  287. print '</div>';
  288. }
  289. print "</form>\n";
  290. // End of page
  291. llxFooter();
  292. $db->close();