actions_extrafields.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /* Copyright (C) 2011-2015 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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. *
  18. * $elementype must be defined.
  19. */
  20. /**
  21. * \file htdocs/core/actions_extrafields.inc.php
  22. * \brief Code for actions on extrafields admin pages
  23. */
  24. $maxsizestring = 255;
  25. $maxsizeint = 10;
  26. $mesg = array();
  27. $extrasize = GETPOST('size', 'intcomma');
  28. $type = GETPOST('type', 'alpha');
  29. $param = GETPOST('param', 'alpha');
  30. if ($type == 'double' && strpos($extrasize, ',') === false) $extrasize = '24,8';
  31. if ($type == 'date') $extrasize = '';
  32. if ($type == 'datetime') $extrasize = '';
  33. if ($type == 'select') $extrasize = '';
  34. // Add attribute
  35. if ($action == 'add')
  36. {
  37. if ($_POST["button"] != $langs->trans("Cancel"))
  38. {
  39. // Check values
  40. if (!$type)
  41. {
  42. $error++;
  43. $langs->load("errors");
  44. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
  45. $action = 'create';
  46. }
  47. if ($type == 'varchar' && $extrasize <= 0)
  48. {
  49. $error++;
  50. $langs->load("errors");
  51. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
  52. $action = 'edit';
  53. }
  54. if ($type == 'varchar' && $extrasize > $maxsizestring)
  55. {
  56. $error++;
  57. $langs->load("errors");
  58. $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
  59. $action = 'create';
  60. }
  61. if ($type == 'int' && $extrasize > $maxsizeint)
  62. {
  63. $error++;
  64. $langs->load("errors");
  65. $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
  66. $action = 'create';
  67. }
  68. if ($type == 'select' && !$param)
  69. {
  70. $error++;
  71. $langs->load("errors");
  72. $mesg[] = $langs->trans("ErrorNoValueForSelectType");
  73. $action = 'create';
  74. }
  75. if ($type == 'sellist' && !$param)
  76. {
  77. $error++;
  78. $langs->load("errors");
  79. $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
  80. $action = 'create';
  81. }
  82. if ($type == 'checkbox' && !$param)
  83. {
  84. $error++;
  85. $langs->load("errors");
  86. $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
  87. $action = 'create';
  88. }
  89. if ($type == 'link' && !$param)
  90. {
  91. $error++;
  92. $langs->load("errors");
  93. $mesg[] = $langs->trans("ErrorNoValueForLinkType");
  94. $action = 'create';
  95. }
  96. if ($type == 'radio' && !$param)
  97. {
  98. $error++;
  99. $langs->load("errors");
  100. $mesg[] = $langs->trans("ErrorNoValueForRadioType");
  101. $action = 'create';
  102. }
  103. if ((($type == 'radio') || ($type == 'checkbox')) && $param)
  104. {
  105. // Construct array for parameter (value of select list)
  106. $parameters = $param;
  107. $parameters_array = explode("\r\n", $parameters);
  108. foreach ($parameters_array as $param_ligne)
  109. {
  110. if (!empty($param_ligne)) {
  111. if (preg_match_all('/,/', $param_ligne, $matches))
  112. {
  113. if (count($matches[0]) > 1) {
  114. $error++;
  115. $langs->load("errors");
  116. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  117. $action = 'create';
  118. }
  119. }
  120. else
  121. {
  122. $error++;
  123. $langs->load("errors");
  124. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  125. $action = 'create';
  126. }
  127. }
  128. }
  129. }
  130. if (!$error)
  131. {
  132. // attrname must be alphabetical and lower case only
  133. if (isset($_POST["attrname"]) && preg_match("/^[a-z0-9-_]+$/", $_POST['attrname']) && !is_numeric($_POST["attrname"]))
  134. {
  135. // Construct array for parameter (value of select list)
  136. $default_value = GETPOST('default_value', 'alpha');
  137. $parameters = $param;
  138. $parameters_array = explode("\r\n", $parameters);
  139. //In sellist we have only one line and it can have come to do SQL expression
  140. if ($type == 'sellist') {
  141. foreach ($parameters_array as $param_ligne)
  142. {
  143. $params['options'] = array($parameters=>null);
  144. }
  145. }
  146. else
  147. {
  148. //Esle it's separated key/value and coma list
  149. foreach ($parameters_array as $param_ligne)
  150. {
  151. list($key, $value) = explode(',', $param_ligne);
  152. $params['options'][$key] = $value;
  153. }
  154. }
  155. // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
  156. $visibility = GETPOST('list', 'alpha');
  157. if ($type == 'separate') $visibility = 3;
  158. $result = $extrafields->addExtraField(
  159. GETPOST('attrname', 'alpha'),
  160. GETPOST('label', 'alpha'),
  161. $type,
  162. GETPOST('pos', 'int'),
  163. $extrasize,
  164. $elementtype,
  165. (GETPOST('unique', 'alpha') ? 1 : 0),
  166. (GETPOST('required', 'alpha') ? 1 : 0),
  167. $default_value,
  168. $params,
  169. (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
  170. (GETPOST('perms', 'alpha') ?GETPOST('perms', 'alpha') : ''),
  171. $visibility,
  172. GETPOST('help', 'alpha'),
  173. GETPOST('computed_value', 'alpha'),
  174. (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
  175. GETPOST('langfile', 'alpha'),
  176. 1,
  177. (GETPOST('totalizable', 'alpha') ? 1 : 0),
  178. GETPOST('printable', 'alpha')
  179. );
  180. if ($result > 0)
  181. {
  182. setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
  183. header("Location: ".$_SERVER["PHP_SELF"]);
  184. exit;
  185. }
  186. else
  187. {
  188. $error++;
  189. $mesg = $extrafields->error;
  190. setEventMessages($mesg, null, 'errors');
  191. }
  192. }
  193. else
  194. {
  195. $error++;
  196. $langs->load("errors");
  197. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
  198. setEventMessages($mesg, null, 'errors');
  199. $action = 'create';
  200. }
  201. }
  202. else
  203. {
  204. setEventMessages($mesg, null, 'errors');
  205. }
  206. }
  207. }
  208. // Rename field
  209. if ($action == 'update')
  210. {
  211. if ($_POST["button"] != $langs->trans("Cancel"))
  212. {
  213. // Check values
  214. if (!$type)
  215. {
  216. $error++;
  217. $langs->load("errors");
  218. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
  219. $action = 'edit';
  220. }
  221. if ($type == 'varchar' && $extrasize <= 0)
  222. {
  223. $error++;
  224. $langs->load("errors");
  225. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
  226. $action = 'edit';
  227. }
  228. if ($type == 'varchar' && $extrasize > $maxsizestring)
  229. {
  230. $error++;
  231. $langs->load("errors");
  232. $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
  233. $action = 'edit';
  234. }
  235. if ($type == 'int' && $extrasize > $maxsizeint)
  236. {
  237. $error++;
  238. $langs->load("errors");
  239. $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
  240. $action = 'edit';
  241. }
  242. if ($type == 'select' && !$param)
  243. {
  244. $error++;
  245. $langs->load("errors");
  246. $mesg[] = $langs->trans("ErrorNoValueForSelectType");
  247. $action = 'edit';
  248. }
  249. if ($type == 'sellist' && !$param)
  250. {
  251. $error++;
  252. $langs->load("errors");
  253. $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
  254. $action = 'edit';
  255. }
  256. if ($type == 'checkbox' && !$param)
  257. {
  258. $error++;
  259. $langs->load("errors");
  260. $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
  261. $action = 'edit';
  262. }
  263. if ($type == 'radio' && !$param)
  264. {
  265. $error++;
  266. $langs->load("errors");
  267. $mesg[] = $langs->trans("ErrorNoValueForRadioType");
  268. $action = 'edit';
  269. }
  270. if ((($type == 'radio') || ($type == 'checkbox')) && $param)
  271. {
  272. // Construct array for parameter (value of select list)
  273. $parameters = $param;
  274. $parameters_array = explode("\r\n", $parameters);
  275. foreach ($parameters_array as $param_ligne)
  276. {
  277. if (!empty($param_ligne)) {
  278. if (preg_match_all('/,/', $param_ligne, $matches))
  279. {
  280. if (count($matches[0]) > 1) {
  281. $error++;
  282. $langs->load("errors");
  283. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  284. $action = 'edit';
  285. }
  286. }
  287. else
  288. {
  289. $error++;
  290. $langs->load("errors");
  291. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  292. $action = 'edit';
  293. }
  294. }
  295. }
  296. }
  297. if (!$error)
  298. {
  299. if (isset($_POST["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $_POST['attrname']))
  300. {
  301. $pos = GETPOST('pos', 'int');
  302. // Construct array for parameter (value of select list)
  303. $parameters = $param;
  304. $parameters_array = explode("\r\n", $parameters);
  305. //In sellist we have only one line and it can have come to do SQL expression
  306. if ($type == 'sellist') {
  307. foreach ($parameters_array as $param_ligne)
  308. {
  309. $params['options'] = array($parameters=>null);
  310. }
  311. }
  312. else
  313. {
  314. //Esle it's separated key/value and coma list
  315. foreach ($parameters_array as $param_ligne)
  316. {
  317. list($key, $value) = explode(',', $param_ligne);
  318. $params['options'][$key] = $value;
  319. }
  320. }
  321. // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
  322. $visibility = GETPOST('list', 'alpha');
  323. if ($type == 'separate') $visibility = 3;
  324. $result = $extrafields->update(
  325. GETPOST('attrname', 'alpha'),
  326. GETPOST('label', 'alpha'),
  327. $type,
  328. $extrasize,
  329. $elementtype,
  330. (GETPOST('unique', 'alpha') ? 1 : 0),
  331. (GETPOST('required', 'alpha') ? 1 : 0),
  332. $pos,
  333. $params,
  334. (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
  335. (GETPOST('perms', 'alpha') ?GETPOST('perms', 'alpha') : ''),
  336. $visibility,
  337. GETPOST('help', 'alpha'),
  338. GETPOST('default_value', 'alpha'),
  339. GETPOST('computed_value', 'alpha'),
  340. (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
  341. GETPOST('langfile'),
  342. 1,
  343. (GETPOST('totalizable', 'alpha') ? 1 : 0),
  344. GETPOST('printable', 'alpha')
  345. );
  346. if ($result > 0)
  347. {
  348. setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
  349. header("Location: ".$_SERVER["PHP_SELF"]);
  350. exit;
  351. }
  352. else
  353. {
  354. $error++;
  355. $mesg = $extrafields->error;
  356. setEventMessages($mesg, null, 'errors');
  357. }
  358. }
  359. else
  360. {
  361. $error++;
  362. $langs->load("errors");
  363. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
  364. setEventMessages($mesg, null, 'errors');
  365. }
  366. }
  367. else
  368. {
  369. setEventMessages($mesg, null, 'errors');
  370. }
  371. }
  372. }
  373. // Delete attribute
  374. if ($action == 'delete')
  375. {
  376. if (isset($_GET["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $_GET["attrname"]))
  377. {
  378. $result = $extrafields->delete($_GET["attrname"], $elementtype);
  379. if ($result >= 0)
  380. {
  381. header("Location: ".$_SERVER["PHP_SELF"]);
  382. exit;
  383. }
  384. else $mesg = $extrafields->error;
  385. }
  386. else
  387. {
  388. $error++;
  389. $langs->load("errors");
  390. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
  391. }
  392. }