actions_extrafields.inc.php 11 KB

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