actions_extrafields.inc.php 13 KB

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