const.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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', 'alpha');
  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', 'none'); // We shoul dbe 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', 'alpha');
  44. $sortorder = GETPOST('sortorder', 'alpha');
  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. }
  78. else
  79. {
  80. dol_print_error($db);
  81. }
  82. }
  83. }
  84. // Mass update
  85. if (!empty($consts) && $action == 'update')
  86. {
  87. $nbmodified = 0;
  88. foreach ($consts as $const)
  89. {
  90. if (!empty($const["check"]))
  91. {
  92. if (dolibarr_set_const($db, $const["name"], $const["value"], $const["type"], 1, $const["note"], $const["entity"]) >= 0)
  93. {
  94. $nbmodified++;
  95. }
  96. else
  97. {
  98. dol_print_error($db);
  99. }
  100. }
  101. }
  102. if ($nbmodified > 0) setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  103. $action = '';
  104. }
  105. // Mass delete
  106. if (!empty($consts) && $action == 'delete')
  107. {
  108. $nbdeleted = 0;
  109. foreach ($consts as $const)
  110. {
  111. if (!empty($const["check"])) // Is checkbox checked
  112. {
  113. if (dolibarr_del_const($db, $const["rowid"], -1) >= 0)
  114. {
  115. $nbdeleted++;
  116. }
  117. else
  118. {
  119. dol_print_error($db);
  120. }
  121. }
  122. }
  123. if ($nbdeleted > 0) setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  124. $action = '';
  125. }
  126. // Delete line from delete picto
  127. if ($action == 'delete')
  128. {
  129. if (dolibarr_del_const($db, $rowid, $entity) >= 0)
  130. {
  131. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  132. }
  133. else
  134. {
  135. dol_print_error($db);
  136. }
  137. }
  138. /*
  139. * View
  140. */
  141. $form = new Form($db);
  142. $wikihelp = 'EN:Setup_Other|FR:Paramétrage_Divers|ES:Configuración_Varios';
  143. llxHeader('', $langs->trans("Setup"), $wikihelp);
  144. // Add logic to show/hide buttons
  145. if ($conf->use_javascript_ajax)
  146. {
  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. jQuery("#action").val('delete');
  155. });
  156. jQuery(".inputforupdate").keyup(function() { // keypress does not support back
  157. var field_id = jQuery(this).attr("id");
  158. var row_num = field_id.split("_");
  159. jQuery("#updateconst").show();
  160. jQuery("#action").val('update');
  161. jQuery("#check_" + row_num[1]).prop("checked",true);
  162. });
  163. });
  164. </script>
  165. <?php
  166. }
  167. print load_fiche_titre($langs->trans("OtherSetup"), '', 'title_setup');
  168. print '<span class="opacitymedium">'.$langs->trans("ConstDesc")."</span><br>\n";
  169. print "<br>\n";
  170. $param = '';
  171. print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug) ? '?debug=1' : '').'" method="POST">';
  172. print '<input type="hidden" name="token" value="'.newToken().'">';
  173. print '<input type="hidden" id="action" name="action" value="">';
  174. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  175. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  176. print '<div class="div-table-responsive-no-min">';
  177. print '<table class="noborder centpercent">';
  178. print '<tr class="liste_titre">';
  179. print getTitleFieldOfList('Name', 0, $_SERVER['PHP_SELF'], 'name', '', $param, '', $sortfield, $sortorder, '')."\n";
  180. print getTitleFieldOfList("Value", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  181. print getTitleFieldOfList("Comment", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
  182. print getTitleFieldOfList('DateModificationShort', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
  183. if (!empty($conf->multicompany->enabled) && !$user->entity)
  184. {
  185. print getTitleFieldOfList('Entity', 0, $_SERVER['PHP_SELF'], 'tms', '', $param, '', $sortfield, $sortorder, 'center ')."\n";
  186. }
  187. print getTitleFieldOfList("Action", 0, $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'center ');
  188. print "</tr>\n";
  189. // Line to add new record
  190. print "\n";
  191. print '<tr class="oddeven"><td><input type="text" class="flat" size="24" name="constname" value="'.$constname.'"></td>'."\n";
  192. print '<td>';
  193. print '<input type="text" class="flat" size="30" name="constvalue" value="'.$constvalue.'">';
  194. print '</td><td>';
  195. print '<input type="text" class="flat" size="40" name="constnote" value="'.$constnote.'">';
  196. print '</td>';
  197. print '<td>';
  198. print '</td>';
  199. // Limit to superadmin
  200. if (!empty($conf->multicompany->enabled) && !$user->entity)
  201. {
  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. }
  207. else
  208. {
  209. print '<td class="center">';
  210. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  211. }
  212. print '<input type="submit" class="button" value="'.$langs->trans("Add").'" name="add">';
  213. print "</td>\n";
  214. print '</tr>';
  215. // Show constants
  216. $sql = "SELECT";
  217. $sql .= " rowid";
  218. $sql .= ", ".$db->decrypt('name')." as name";
  219. $sql .= ", ".$db->decrypt('value')." as value";
  220. $sql .= ", type";
  221. $sql .= ", note";
  222. $sql .= ", tms";
  223. $sql .= ", entity";
  224. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  225. $sql .= " WHERE entity IN (".$user->entity.",".$conf->entity.")";
  226. if ((empty($user->entity) || $user->admin) && $debug) {} // to force for superadmin to debug
  227. 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
  228. if (GETPOST('name')) $sql .= natural_search("name", GETPOST('name'));
  229. $sql .= $db->order($sortfield, $sortorder);
  230. dol_syslog("Const::listConstant", LOG_DEBUG);
  231. $result = $db->query($sql);
  232. if ($result)
  233. {
  234. $num = $db->num_rows($result);
  235. $i = 0;
  236. while ($i < $num)
  237. {
  238. $obj = $db->fetch_object($result);
  239. print "\n";
  240. print '<tr class="oddeven"><td>'.$obj->name.'</td>'."\n";
  241. // Value
  242. print '<td>';
  243. print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  244. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->name.'">';
  245. print '<input type="hidden" name="const['.$i.'][type]" value="'.$obj->type.'">';
  246. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.htmlspecialchars($obj->value).'">';
  247. print '</td>';
  248. // Note
  249. print '<td>';
  250. print '<input type="text" id="note_'.$i.'" class="flat inputforupdate" size="40" name="const['.$i.'][note]" value="'.htmlspecialchars($obj->note, 1).'">';
  251. print '</td>';
  252. // Date last change
  253. print '<td>';
  254. print dol_print_date($db->jdate($obj->tms), 'dayhour');
  255. print '</td>';
  256. // Entity limit to superadmin
  257. if (!empty($conf->multicompany->enabled) && !$user->entity)
  258. {
  259. print '<td>';
  260. print '<input type="text" class="flat" size="1" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
  261. print '</td>';
  262. print '<td class="center">';
  263. }
  264. else
  265. {
  266. print '<td class="center">';
  267. print '<input type="hidden" name="const['.$i.'][entity]" value="'.$obj->entity.'">';
  268. }
  269. if ($conf->use_javascript_ajax)
  270. {
  271. print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
  272. }
  273. else
  274. {
  275. print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&action=delete'.((empty($user->entity) && $debug) ? '&debug=1' : '').'">'.img_delete().'</a>';
  276. }
  277. print "</td></tr>\n";
  278. print "\n";
  279. $i++;
  280. }
  281. }
  282. print '</table>';
  283. print '</div>';
  284. if ($conf->use_javascript_ajax)
  285. {
  286. print '<br>';
  287. print '<div id="updateconst" class="right">';
  288. print '<input type="submit" name="update" class="button" value="'.$langs->trans("Modify").'">';
  289. print '</div>';
  290. print '<div id="delconst" class="right">';
  291. print '<input type="submit" name="delete" class="button" value="'.$langs->trans("Delete").'">';
  292. print '</div>';
  293. }
  294. print "</form>\n";
  295. // End of page
  296. llxFooter();
  297. $db->close();