actions_extrafields.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. if (strpos($param_ligne, ',')!==false) {
  159. list($key, $value) = explode(',', $param_ligne);
  160. if (!array_key_exists('options', $params)) {
  161. $params['options'] = array();
  162. }
  163. } else {
  164. $key=$param_ligne;
  165. $value=null;
  166. }
  167. $params['options'][$key] = $value;
  168. }
  169. }
  170. // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
  171. $visibility = GETPOST('list', 'alpha');
  172. if ($type == 'separate') {
  173. $visibility = 3;
  174. }
  175. $result = $extrafields->addExtraField(
  176. GETPOST('attrname', 'aZ09'),
  177. GETPOST('label', 'alpha'),
  178. $type,
  179. GETPOST('pos', 'int'),
  180. $extrasize,
  181. $elementtype,
  182. (GETPOST('unique', 'alpha') ? 1 : 0),
  183. (GETPOST('required', 'alpha') ? 1 : 0),
  184. $default_value,
  185. $params,
  186. (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
  187. (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
  188. $visibility,
  189. GETPOST('help', 'alpha'),
  190. GETPOST('computed_value', 'alpha'),
  191. (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
  192. GETPOST('langfile', 'alpha'),
  193. 1,
  194. (GETPOST('totalizable', 'alpha') ? 1 : 0),
  195. GETPOST('printable', 'alpha'),
  196. array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist)
  197. );
  198. if ($result > 0) {
  199. setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
  200. header("Location: ".$_SERVER["PHP_SELF"]);
  201. exit;
  202. } else {
  203. $error++;
  204. $mesg = $extrafields->error;
  205. setEventMessages($mesg, null, 'errors');
  206. }
  207. } else {
  208. $error++;
  209. $langs->load("errors");
  210. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
  211. setEventMessages($mesg, null, 'errors');
  212. $action = 'create';
  213. }
  214. } else {
  215. setEventMessages($mesg, null, 'errors');
  216. }
  217. }
  218. }
  219. // Rename field
  220. if ($action == 'update') {
  221. if (GETPOST("button") != $langs->trans("Cancel")) {
  222. // Check values
  223. if (!$type) {
  224. $error++;
  225. $langs->load("errors");
  226. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
  227. $action = 'edit';
  228. }
  229. if ($type == 'varchar' && $extrasize <= 0) {
  230. $error++;
  231. $langs->load("errors");
  232. $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
  233. $action = 'edit';
  234. }
  235. if ($type == 'varchar' && $extrasize > $maxsizestring) {
  236. $error++;
  237. $langs->load("errors");
  238. $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
  239. $action = 'edit';
  240. }
  241. if ($type == 'int' && $extrasize > $maxsizeint) {
  242. $error++;
  243. $langs->load("errors");
  244. $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
  245. $action = 'edit';
  246. }
  247. if ($type == 'select' && !$param) {
  248. $error++;
  249. $langs->load("errors");
  250. $mesg[] = $langs->trans("ErrorNoValueForSelectType");
  251. $action = 'edit';
  252. }
  253. if ($type == 'sellist' && !$param) {
  254. $error++;
  255. $langs->load("errors");
  256. $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
  257. $action = 'edit';
  258. }
  259. if ($type == 'checkbox' && !$param) {
  260. $error++;
  261. $langs->load("errors");
  262. $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
  263. $action = 'edit';
  264. }
  265. if ($type == 'radio' && !$param) {
  266. $error++;
  267. $langs->load("errors");
  268. $mesg[] = $langs->trans("ErrorNoValueForRadioType");
  269. $action = 'edit';
  270. }
  271. if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
  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. if (!empty($param_ligne)) {
  277. if (preg_match_all('/,/', $param_ligne, $matches)) {
  278. if (count($matches[0]) > 1) {
  279. $error++;
  280. $langs->load("errors");
  281. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  282. $action = 'edit';
  283. }
  284. } else {
  285. $error++;
  286. $langs->load("errors");
  287. $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
  288. $action = 'edit';
  289. }
  290. }
  291. }
  292. }
  293. if (!$error) {
  294. if (strlen(GETPOST('attrname', 'aZ09')) < 3 && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
  295. $error++;
  296. $langs->load("errors");
  297. $mesg[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
  298. $action = 'edit';
  299. }
  300. }
  301. // Check reserved keyword with more than 3 characters
  302. if (!$error) {
  303. if (in_array(GETPOST('attrname', 'aZ09'), array('and', 'keyword', 'table', 'index', 'integer', 'float', 'double', 'position')) && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
  304. $error++;
  305. $langs->load("errors");
  306. $mesg[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
  307. $action = 'edit';
  308. }
  309. }
  310. if (!$error) {
  311. if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
  312. $pos = GETPOST('pos', 'int');
  313. // Construct array for parameter (value of select list)
  314. $parameters = $param;
  315. $parameters_array = explode("\r\n", $parameters);
  316. $params = array();
  317. //In sellist we have only one line and it can have come to do SQL expression
  318. if ($type == 'sellist' || $type == 'chkbxlst') {
  319. foreach ($parameters_array as $param_ligne) {
  320. $params['options'] = array($parameters=>null);
  321. }
  322. } else {
  323. //Esle it's separated key/value and coma list
  324. foreach ($parameters_array as $param_ligne) {
  325. list($key, $value) = explode(',', $param_ligne);
  326. if (!array_key_exists('options', $params)) {
  327. $params['options'] = array();
  328. }
  329. $params['options'][$key] = $value;
  330. }
  331. }
  332. // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
  333. $visibility = GETPOST('list', 'alpha');
  334. if ($type == 'separate') {
  335. $visibility = 3;
  336. }
  337. // Example: is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : 'objnotdefined'
  338. $computedvalue = GETPOST('computed_value', 'nohtml');
  339. $result = $extrafields->update(
  340. GETPOST('attrname', 'aZ09'),
  341. GETPOST('label', 'alpha'),
  342. $type,
  343. $extrasize,
  344. $elementtype,
  345. (GETPOST('unique', 'alpha') ? 1 : 0),
  346. (GETPOST('required', 'alpha') ? 1 : 0),
  347. $pos,
  348. $params,
  349. (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
  350. (GETPOST('perms', 'alpha') ?GETPOST('perms', 'alpha') : ''),
  351. $visibility,
  352. GETPOST('help', 'alpha'),
  353. GETPOST('default_value', 'alpha'),
  354. $computedvalue,
  355. (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
  356. GETPOST('langfile'),
  357. GETPOST('enabled', 'nohtml'),
  358. (GETPOST('totalizable', 'alpha') ? 1 : 0),
  359. GETPOST('printable', 'alpha'),
  360. array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist)
  361. );
  362. if ($result > 0) {
  363. setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
  364. header("Location: ".$_SERVER["PHP_SELF"]);
  365. exit;
  366. } else {
  367. $error++;
  368. $mesg = $extrafields->error;
  369. setEventMessages($mesg, null, 'errors');
  370. }
  371. } else {
  372. $error++;
  373. $langs->load("errors");
  374. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
  375. setEventMessages($mesg, null, 'errors');
  376. }
  377. } else {
  378. setEventMessages($mesg, null, 'errors');
  379. }
  380. }
  381. }
  382. // Delete attribute
  383. if ($action == 'delete') {
  384. if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST("attrname", 'aZ09'))) {
  385. $result = $extrafields->delete(GETPOST("attrname", 'aZ09'), $elementtype);
  386. if ($result >= 0) {
  387. header("Location: ".$_SERVER["PHP_SELF"]);
  388. exit;
  389. } else {
  390. $mesg = $extrafields->error;
  391. }
  392. } else {
  393. $error++;
  394. $langs->load("errors");
  395. $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
  396. }
  397. }