defaultvalues.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 ($page == -1) { $page = 0; }
  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. $texthelp=$langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
  229. // See list into GETPOST
  230. $texthelp.='__USERID__<br>';
  231. $texthelp.='__SUPERVISORID__<br>';
  232. $texthelp.='__MYCOUNTRYID__<br>';
  233. $texthelp.='__DAY__<br>';
  234. $texthelp.='__MONTH__<br>';
  235. $texthelp.='__YEAR__<br>';
  236. $texthelp.='__PREVIOUS_DAY__<br>';
  237. $texthelp.='__PREVIOUS_MONTH__<br>';
  238. $texthelp.='__PREVIOUS_YEAR__<br>';
  239. $texthelp.='__NEXT_DAY__<br>';
  240. $texthelp.='__NEXT_MONTH__<br>';
  241. $texthelp.='__NEXT_YEAR__<br>';
  242. if (! empty($conf->multicompany->enabled)) $texthelp.='__ENTITYID__<br>';
  243. $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, '');
  244. }
  245. else
  246. {
  247. $texthelp='ASC or DESC';
  248. $textvalue=$form->textwithpicto($langs->trans("SortOrder"), $texthelp);
  249. }
  250. print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
  251. }
  252. // Entity
  253. if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre($langs->trans("Entity"),$_SERVER["PHP_SELF"],'entity,page','',$param,'',$sortfield,$sortorder);
  254. // Actions
  255. print '<td align="center"></td>';
  256. print "</tr>\n";
  257. // Line to add new record
  258. print "\n";
  259. print '<tr class="oddeven">';
  260. // Page
  261. print '<td>';
  262. print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="">';
  263. print '</td>'."\n";
  264. // Field
  265. print '<td>';
  266. print '<input type="text" class="flat maxwidth100" name="defaultkey" value="">';
  267. print '</td>';
  268. // Value
  269. if ($mode != 'focus')
  270. {
  271. print '<td>';
  272. print '<input type="text" class="flat maxwidthonsmartphone" name="defaultvalue" value="">';
  273. print '</td>';
  274. }
  275. // Limit to superadmin
  276. if (! empty($conf->multicompany->enabled) && !$user->entity)
  277. {
  278. print '<td>';
  279. print '<input type="text" class="flat" size="1" name="entity" value="'.$conf->entity.'">';
  280. print '</td>';
  281. print '<td align="center">';
  282. }
  283. else
  284. {
  285. print '<td align="center">';
  286. print '<input type="hidden" name="entity" value="'.$conf->entity.'">';
  287. }
  288. $disabled='';
  289. if (empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) $disabled=' disabled="disabled"';
  290. print '<input type="submit" class="button"'.$disabled.' value="'.$langs->trans("Add").'" name="add">';
  291. print "</td>\n";
  292. print '</tr>';
  293. // Show constants
  294. $sql = "SELECT rowid, entity, type, page, param, value";
  295. $sql.= " FROM ".MAIN_DB_PREFIX."default_values";
  296. $sql.= " WHERE type = '".$db->escape($mode)."'";
  297. $sql.= " AND entity IN (".$user->entity.",".$conf->entity.")";
  298. $sql.= $db->order($sortfield, $sortorder);
  299. dol_syslog("translation::select from table", LOG_DEBUG);
  300. $result = $db->query($sql);
  301. if ($result)
  302. {
  303. $num = $db->num_rows($result);
  304. $i = 0;
  305. while ($i < $num)
  306. {
  307. $obj = $db->fetch_object($result);
  308. print "\n";
  309. print '<tr class="oddeven">';
  310. // Page
  311. print '<td>';
  312. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->page;
  313. else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($obj->page).'">';
  314. print '</td>'."\n";
  315. // Field
  316. print '<td>';
  317. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->param;
  318. else print '<input type="text" name="key" value="'.dol_escape_htmltag($obj->param).'">';
  319. print '</td>'."\n";
  320. // Value
  321. if ($mode != 'focus')
  322. {
  323. print '<td>';
  324. /*print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">';
  325. print '<input type="hidden" name="const['.$i.'][lang]" value="'.$obj->lang.'">';
  326. print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->transkey.'">';
  327. print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.dol_escape_htmltag($obj->transvalue).'">';
  328. */
  329. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->value;
  330. else print '<input type="text" name="value" value="'.dol_escape_htmltag($obj->value).'">';
  331. print '</td>';
  332. }
  333. // Actions
  334. print '<td align="center">';
  335. if ($action != 'edit' || GETPOST('rowid') != $obj->rowid)
  336. {
  337. 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>';
  338. print ' &nbsp; ';
  339. 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>';
  340. }
  341. else
  342. {
  343. print '<input type="hidden" name="page" value="'.$page.'">';
  344. print '<input type="hidden" name="rowid" value="'.$id.'">';
  345. print '<div name="'.(! empty($obj->rowid)?$obj->rowid:'none').'"></div>';
  346. print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
  347. print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  348. }
  349. print '</td>';
  350. print "</tr>\n";
  351. print "\n";
  352. $i++;
  353. }
  354. }
  355. else
  356. {
  357. dol_print_error($db);
  358. }
  359. print '</table>';
  360. dol_fiche_end();
  361. print "</form>\n";
  362. llxFooter();
  363. $db->close();