actions_extrafields.inc.php 12 KB

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