const.php 8.7 KB

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