const.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. // Load translation files required by the page
  28. $langs->load("admin");
  29. if (!$user->admin) {
  30. accessforbidden();
  31. }
  32. $rowid = GETPOST('rowid', 'int');
  33. $entity = GETPOST('entity', 'int');
  34. $action = GETPOST('action', 'aZ09');
  35. $debug = GETPOST('debug', 'int');
  36. $consts = GETPOST('const', 'array');
  37. $constname = GETPOST('constname', 'alphanohtml');
  38. $constvalue = GETPOST('constvalue', 'restricthtml'); // We should be able to send everything here
  39. $constnote = GETPOST('constnote', 'alpha');
  40. // Load variable for pagination
  41. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  42. $sortfield = GETPOST('sortfield', 'aZ09comma');
  43. $sortorder = GETPOST('sortorder', 'aZ09comma');
  44. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  45. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  46. $page = 0;
  47. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  48. $offset = $limit * $page;
  49. $pageprev = $page - 1;
  50. $pagenext = $page + 1;
  51. if (empty($sortfield)) {
  52. $sortfield = 'entity,name';
  53. }
  54. if (empty($sortorder)) {
  55. $sortorder = 'ASC';
  56. }
  57. /*
  58. * Actions
  59. */
  60. if ($action == 'add' || (GETPOST('add') && $action != 'update')) {
  61. $error = 0;
  62. if (empty($constname)) {
  63. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
  64. $error++;
  65. }
  66. if ($constvalue == '') {
  67. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Value")), null, 'errors');
  68. $error++;
  69. }
  70. if (!$error) {
  71. if (dolibarr_set_const($db, $constname, $constvalue, 'chaine', 1, $constnote, $entity) >= 0) {
  72. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  73. $action = "";
  74. $constname = "";
  75. $constvalue = "";
  76. $constnote = "";
  77. } else {
  78. dol_print_error($db);
  79. }
  80. }
  81. }
  82. // Mass update
  83. if (!empty($consts) && $action == 'update') {
  84. $nbmodified = 0;
  85. foreach ($consts as $const) {
  86. if (!empty($const["check"])) {
  87. if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0) {
  88. $nbmodified++;
  89. } else {
  90. dol_print_error($db);
  91. }
  92. }
  93. }
  94. if ($nbmodified > 0) {
  95. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  96. }
  97. $action = '';
  98. }
  99. // Mass delete
  100. if (!empty($consts) && $action == 'delete') {
  101. $nbdeleted = 0;
  102. foreach ($consts as $const) {
  103. if (!empty($const["check"])) { // Is checkbox checked
  104. if (dolibarr_del_const($db, $const["rowid"], -1) >= 0) {
  105. $nbdeleted++;
  106. } else {
  107. dol_print_error($db);
  108. }
  109. }
  110. }
  111. if ($nbdeleted > 0) {
  112. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  113. }
  114. $action = '';
  115. }
  116. // Delete line from delete picto
  117. if ($action == 'delete') {
  118. if (dolibarr_del_const($db, $rowid, $entity) >= 0) {
  119. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  120. } else {
  121. dol_print_error($db);
  122. }
  123. }
  124. /*
  125. * View
  126. */
  127. $form = new Form($db);
  128. $wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
  129. llxHeader('', $langs->trans("Setup"), $wikihelp);
  130. // Add logic to show/hide buttons
  131. if ($conf->use_javascript_ajax) {
  132. ?>
  133. <script type="text/javascript">
  134. jQuery(document).ready(function() {
  135. jQuery("#updateconst").hide();
  136. jQuery("#delconst").hide();
  137. jQuery(".checkboxfordelete").click(function() {
  138. jQuery("#delconst").show();
  139. jQuery("#action").val('delete');
  140. });
  141. jQuery(".inputforupdate").keyup(function() { // keypress does not support back
  142. var field_id = jQuery(this).attr("id");
  143. var row_num = field_id.split("_");
  144. jQuery("#updateconst").show();
  145. jQuery("#action").val('update');
  146. jQuery("#check_" + row_num[1]).prop("checked",true);
  147. });
  148. });
  149. </script>
  150. <?php
  151. }
  152. print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
  153. print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
  154. print "<br>\n";
  155. $param = '';
  156. print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
  157. print '<input type="hidden" name="token" value="'.newToken().'">';
  158. print '<input type="hidden" id="action" name="action" value="">';
  159. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  160. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  161. print '<div class="div-table-responsive-no-min">';
  162. print '<table class="noborder centpercent">';
  163. print '<tr class="liste_titre">';
  164. print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '')."\n";
  165. print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  166. print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  167. print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
  168. if (!empty($conf->multicompany->enabled) && !$user->entity) {
  169. print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
  170. }
  171. print getTitleFieldOfList("", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
  172. print "</tr>\n";
  173. // Line to add new record
  174. print "\n";
  175. print '<tr class="oddeven nohover"><td>';
  176. print '<input type="text" class="flat minwidth300" name="constname" value="'.$constname.'">';
  177. print '</td>'."\n";
  178. print '<td>';
  179. print '<input type="text" class="flat minwidth100" name="constvalue" value="'.$constvalue.'">';
  180. print '</td>';
  181. print '<td>';
  182. print '<input type="text" class="flat minwidth100" name="constnote" value="'.$constnote.'">';
  183. print '</td>';
  184. print '<td>';
  185. print '</td>';
  186. // Limit to superadmin
  187. if (!empty($conf->multicompany->enabled) && !$user->entity) {
  188. print '<td>';
  189. print '<input type="text" class="flat" size="1" name="entity" value="'.$conf->entity.'">';
  190. print '</td>';
  191. print '<td class="center">';
  192. } else {
  193. print '<td class="center">';
  194. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  195. }
  196. print '<input type="submit" class="button button-add small" name="add" value="'.$langs->trans("Add").'">';
  197. print "</td>\n";
  198. print '</tr>';
  199. // Show constants
  200. $sql = "SELECT";
  201. $sql .= " rowid";
  202. $sql .= ", ".$db->decrypt('name')." as name";
  203. $sql .= ", ".$db->decrypt('value')." as value";
  204. $sql .= ", type";
  205. $sql .= ", note";
  206. $sql .= ", tms";
  207. $sql .= ", entity";
  208. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  209. $sql .= " WHERE entity IN (".$db->sanitize($user->entity.",".$conf->entity).")";
  210. if ((empty($user->entity) || $user->admin) && $debug) {
  211. } elseif (!GETPOST('visible') || GETPOST('visible') != 'all') {
  212. // to force for superadmin to debug
  213. $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
  214. }
  215. if (GETPOST('name')) {
  216. $sql .= natural_search("name", GETPOST('name'));
  217. }
  218. $sql .= $db->order($sortfield, $sortorder);
  219. dol_syslog("Const::listConstant", LOG_DEBUG);
  220. $result = $db->query($sql);
  221. if ($result) {
  222. $num = $db->num_rows($result);
  223. $i = 0;
  224. while ($i < $num) {
  225. $obj = $db->fetch_object($result);
  226. print "\n";
  227. print '<tr class="oddeven" data-checkbox-id="check_'.$i.'"><td>'.$obj->name.'</td>'."\n";
  228. // Value
  229. print '<td>';
  230. print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  231. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
  232. print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
  233. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate minwidth150" name="const['.$i.'][value]" value="'.htmlspecialchars($obj->value).'">';
  234. print '</td>';
  235. // Note
  236. print '<td>';
  237. print '<input type="text" id="note_'.$i.'" class="flat inputforupdate minwidth200" name="const['.$i.'][note]" value="'.htmlspecialchars($obj->note, 1).'">';
  238. print '</td>';
  239. // Date last change
  240. print '<td class="nowraponall center">';
  241. print dol_print_date($db->jdate($obj->tms), 'dayhour');
  242. print '</td>';
  243. // Entity limit to superadmin
  244. if (!empty($conf->multicompany->enabled) && !$user->entity) {
  245. print '<td>';
  246. print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
  247. print '</td>';
  248. print '<td class="center">';
  249. } else {
  250. print '<td class="center">';
  251. print '<input type="hidden" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
  252. }
  253. if ($conf->use_javascript_ajax) {
  254. print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
  255. } else {
  256. 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>';
  257. }
  258. print "</td></tr>\n";
  259. print "\n";
  260. $i++;
  261. }
  262. }
  263. print '</table>';
  264. print '</div>';
  265. if ($conf->use_javascript_ajax) {
  266. print '<br>';
  267. print '<div id="updateconst" class="right">';
  268. print '<input type="submit" class="button button-edit marginbottomonly" name="update" value="'.$langs->trans("Modify").'">';
  269. print '</div>';
  270. print '<div id="delconst" class="right">';
  271. print '<input type="submit" class="button button-cancel marginbottomonly" name="delete" value="'.$langs->trans("Delete").'">';
  272. print '</div>';
  273. }
  274. print "</form>\n";
  275. // End of page
  276. llxFooter();
  277. $db->close();