actions_extrafields.inc.php 11 KB

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