const.php 10 KB

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