defaultvalues.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/admin/defaultvalues.php
  19. * \brief Page to set default values used used in a create form
  20. */
  21. require '../main.inc.php';
  22. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  25. $langs->load("companies");
  26. $langs->load("products");
  27. $langs->load("admin");
  28. $langs->load("sms");
  29. $langs->load("other");
  30. $langs->load("errors");
  31. if (!$user->admin) accessforbidden();
  32. $id=GETPOST('rowid','int');
  33. $action=GETPOST('action','alpha');
  34. $mode = GETPOST('mode')?GETPOST('mode'):'createform'; // 'createform', 'filters', 'sortorder', 'focus'
  35. $limit = GETPOST("limit")?GETPOST("limit","int"):$conf->liste_limit;
  36. $sortfield = GETPOST("sortfield",'alpha');
  37. $sortorder = GETPOST("sortorder",'alpha');
  38. $page = GETPOST("page",'int');
  39. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  40. $offset = $limit * $page;
  41. $pageprev = $page - 1;
  42. $pagenext = $page + 1;
  43. if (! $sortfield) $sortfield='page,param';
  44. if (! $sortorder) $sortorder='ASC';
  45. $defaulturl = GETPOST('defaulturl');
  46. $defaultkey = GETPOST('defaultkey','alpha');
  47. $defaultvalue = GETPOST('defaultvalue');
  48. $defaulturl=preg_replace('/^\//', '', $defaulturl);
  49. $urlpage = GETPOST('urlpage');
  50. $key = GETPOST('key');
  51. $value = GETPOST('value');
  52. /*
  53. * Actions
  54. */
  55. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  56. if (! GETPOST('confirmmassaction') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  57. $parameters=array('socid'=>$socid);
  58. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  59. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  60. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  61. // Purge search criteria
  62. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All tests are required to be compatible with all browsers
  63. {
  64. $defaulturl='';
  65. $defaultkey='';
  66. $defaultvalue='';
  67. $toselect='';
  68. $search_array_options=array();
  69. }
  70. if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES')
  71. {
  72. if (GETPOST('value')) dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
  73. else dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
  74. }
  75. if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify'))
  76. {
  77. $error=0;
  78. if (($action == 'add' || (GETPOST('add') && $action != 'update')))
  79. {
  80. if (empty($defaulturl))
  81. {
  82. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
  83. $error++;
  84. }
  85. if (empty($defaultkey))
  86. {
  87. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
  88. $error++;
  89. }
  90. }
  91. if (GETPOST('actionmodify'))
  92. {
  93. if (empty($urlpage))
  94. {
  95. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
  96. $error++;
  97. }
  98. if (empty($key))
  99. {
  100. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
  101. $error++;
  102. }
  103. }
  104. if (! $error)
  105. {
  106. $db->begin();
  107. if ($action == 'add' || (GETPOST('add') && $action != 'update'))
  108. {
  109. $sql = "INSERT INTO ".MAIN_DB_PREFIX."default_values(type, user_id, page, param, value, entity) VALUES ('".$db->escape($mode)."', 0, '".$db->escape($defaulturl)."','".$db->escape($defaultkey)."','".$db->escape($defaultvalue)."', ".$db->escape($conf->entity).")";
  110. }
  111. if (GETPOST('actionmodify'))
  112. {
  113. $sql = "UPDATE ".MAIN_DB_PREFIX."default_values SET page = '".$db->escape($urlpage)."', param = '".$db->escape($key)."', value = '".$db->escape($value)."'";
  114. $sql.= " WHERE rowid = ".$id;
  115. }
  116. $result = $db->query($sql);
  117. if ($result > 0)
  118. {
  119. $db->commit();
  120. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  121. $action="";
  122. $defaulturl='';
  123. $defaultkey='';
  124. $defaultvalue='';
  125. }
  126. else
  127. {
  128. $db->rollback();
  129. setEventMessages($db->lasterror(), null, 'errors');
  130. $action='';
  131. }
  132. }
  133. }
  134. // Delete line from delete picto
  135. if ($action == 'delete')
  136. {
  137. $sql = "DELETE FROM ".MAIN_DB_PREFIX."default_values WHERE rowid = ".$db->escape($id);
  138. // Delete const
  139. $result = $db->query($sql);
  140. if ($result >= 0)
  141. {
  142. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  143. }
  144. else
  145. {
  146. dol_print_error($db);
  147. }
  148. }
  149. /*
  150. * View
  151. */
  152. $form=new Form($db);
  153. $formadmin = new FormAdmin($db);
  154. $wikihelp='EN:Setup|FR:Paramétrage|ES:Configuración';
  155. llxHeader('',$langs->trans("Setup"),$wikihelp);
  156. print load_fiche_titre($langs->trans("DefaultValues"),'','title_setup');
  157. print $langs->trans("DefaultValuesDesc")."<br>\n";
  158. print "<br>\n";
  159. print $langs->trans("EnableDefaultValues").' ';
  160. if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES))
  161. {
  162. // Button off, click to enable
  163. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&amp;value=1">';
  164. print img_picto($langs->trans("Disabled"),'switch_off');
  165. print '</a>';
  166. }
  167. else
  168. {
  169. // Button on, click to disable
  170. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setMAIN_ENABLE_DEFAULT_VALUES&amp;value=0">';
  171. print img_picto($langs->trans("Activated"),'switch_on');
  172. print '</a>';
  173. }
  174. print "<br><br>\n";
  175. $param='&mode='.$mode;
  176. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  177. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  178. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  179. if (defaulturl) $param.='&defaulturl='.urlencode(defaulturl);
  180. if (defaultkey) $param.='&defaultkey='.urlencode(defaultkey);
  181. if (defaultvalue) $param.='&defaultvalue='.urlencode(defaultvalue);
  182. print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug)?'?debug=1':'').'" method="POST">';
  183. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  184. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  185. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  186. print '<input type="hidden" name="action" value="list">';
  187. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  188. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  189. print '<input type="hidden" name="page" value="'.$page.'">';
  190. $head=defaultvalues_prepare_head();
  191. dol_fiche_head($head, $mode, '', -1, '');
  192. if ($mode == 'sortorder')
  193. {
  194. print info_admin($langs->trans("WarningSettingSortOrder")).'<br>';
  195. }
  196. if ($mode == 'focus')
  197. {
  198. print info_admin($langs->trans("FeatureNotYetAvailable")).'<br>';
  199. }
  200. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  201. print '<input type="hidden" id="action" name="action" value="">';
  202. print '<input type="hidden" id="mode" name="mode" value="'.dol_escape_htmltag($mode).'">';
  203. print '<table class="noborder" width="100%">';
  204. print '<tr class="liste_titre">';
  205. // Page
  206. $texthelp=$langs->trans("PageUrlForDefaultValues");
  207. if ($mode == 'createform') $texthelp.=$langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php');
  208. else $texthelp.=$langs->trans("PageUrlForDefaultValuesList", 'societe/list.php');
  209. $texturl=$form->textwithpicto($langs->trans("Url"), $texthelp);
  210. print_liste_field_titre($texturl,$_SERVER["PHP_SELF"],'page,param','',$param,'',$sortfield,$sortorder);
  211. // Field
  212. $texthelp=$langs->trans("TheKeyIsTheNameOfHtmlField");
  213. if ($mode != 'sortorder')
  214. {
  215. $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
  216. }
  217. else
  218. {
  219. $texthelp='field or alias.field';
  220. $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp);
  221. }
  222. print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'param','',$param,'',$sortfield,$sortorder);
  223. // Value
  224. if ($mode != 'focus')
  225. {
  226. if ($mode != 'sortorder')
  227. {
  228. $substitutionarray=getCommonSubstitutionArray($langs, 0, array('object')); // Must match list into GETPOST
  229. $substitutionarray['__(AnyTranslationKey)__']=$langs->trans("Translation");
  230. $texthelp=$langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
  231. foreach($substitutionarray as $key => $val)
  232. {
  233. $texthelp.=$key.'<br>';
  234. }
  235. $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, ''); // No tooltip on click, this also triggers the sort click
  236. }
  237. else
  238. {
  239. $texthelp='ASC or DESC';
  240. $textvalue=$form->textwithpicto($langs->trans("SortOrder"), $texthelp);
  241. }
  242. print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
  243. }
  244. // Entity
  245. if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre($langs->trans("Entity"),$_SERVER["PHP_SELF"],'entity,page','',$param,'',$sortfield,$sortorder);
  246. // Actions
  247. print '<td align="center"></td>';
  248. print "</tr>\n";
  249. // Line to add new record
  250. print "\n";
  251. print '<tr class="oddeven">';
  252. // Page
  253. print '<td>';
  254. print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="">';
  255. print '</td>'."\n";
  256. // Field
  257. print '<td>';
  258. print '<input type="text" class="flat maxwidth100" name="defaultkey" value="">';
  259. print '</td>';
  260. // Value
  261. if ($mode != 'focus')
  262. {
  263. print '<td>';
  264. print '<input type="text" class="flat maxwidthonsmartphone" name="defaultvalue" value="">';
  265. print '</td>';
  266. }
  267. // Limit to superadmin
  268. if (! empty($conf->multicompany->enabled) && !$user->entity)
  269. {
  270. print '<td>';
  271. print '<input type="text" class="flat" size="1" name="entity" value="'.$conf->entity.'">';
  272. print '</td>';
  273. print '<td align="center">';
  274. }
  275. else
  276. {
  277. print '<td align="center">';
  278. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  279. }
  280. $disabled='';
  281. if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) $disabled=' disabled="disabled"';
  282. print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
  283. print "</td>\n";
  284. print '</tr>';
  285. // Show constants
  286. $sql = "SELECT rowid, entity, type, page, param, value";
  287. $sql.= " FROM ".MAIN_DB_PREFIX."default_values";
  288. $sql.= " WHERE type = '".$db->escape($mode)."'";
  289. $sql.= " AND entity IN (".$user->entity.",".$conf->entity.")";
  290. $sql.= $db->order($sortfield, $sortorder);
  291. dol_syslog("translation::select from table", LOG_DEBUG);
  292. $result = $db->query($sql);
  293. if ($result)
  294. {
  295. $num = $db->num_rows($result);
  296. $i = 0;
  297. while ($i < $num)
  298. {
  299. $obj = $db->fetch_object($result);
  300. print "\n";
  301. print '<tr class="oddeven">';
  302. // Page
  303. print '<td>';
  304. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->page;
  305. else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($obj->page).'">';
  306. print '</td>'."\n";
  307. // Field
  308. print '<td>';
  309. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->param;
  310. else print '<input type="text" name="key" value="'.dol_escape_htmltag($obj->param).'">';
  311. print '</td>'."\n";
  312. // Value
  313. if ($mode != 'focus')
  314. {
  315. print '<td>';
  316. /*print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  317. print '<input type="hidden" name="const['.$i.'][lang]" value="'.$obj->lang.'">';
  318. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->transkey.'">';
  319. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.dol_escape_htmltag($obj->transvalue).'">';
  320. */
  321. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->value;
  322. else print '<input type="text" name="value" value="'.dol_escape_htmltag($obj->value).'">';
  323. print '</td>';
  324. }
  325. // Actions
  326. print '<td align="center">';
  327. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid)
  328. {
  329. print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=edit'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_edit().'</a>';
  330. print ' &nbsp; ';
  331. print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=delete'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_delete().'</a>';
  332. }
  333. else
  334. {
  335. print '<input type="hidden" name="page" value="'.$page.'">';
  336. print '<input type="hidden" name="rowid" value="'.$id.'">';
  337. print '<div name="'.(! empty($obj->rowid)?$obj->rowid:'none').'"></div>';
  338. print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
  339. print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  340. }
  341. print '</td>';
  342. print "</tr>\n";
  343. print "\n";
  344. $i++;
  345. }
  346. }
  347. else
  348. {
  349. dol_print_error($db);
  350. }
  351. print '</table>';
  352. dol_fiche_end();
  353. print "</form>\n";
  354. llxFooter();
  355. $db->close();