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