adherent_extrafields.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/adherents/admin/adherent_extrafields.php
  21. * \ingroup member
  22. * \brief Page to setup extra fields of members
  23. */
  24. require("../../main.inc.php");
  25. require_once(DOL_DOCUMENT_ROOT."/lib/member.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/core/class/extrafields.class.php");
  27. $langs->load("members");
  28. $langs->load("admin");
  29. $extrafields = new ExtraFields($db);
  30. $form = new Form($db);
  31. // List of supported format
  32. $type2label=array(
  33. 'varchar'=>$langs->trans('String'),
  34. 'text'=>$langs->trans('Text'),
  35. 'int'=>$langs->trans('Int'),
  36. 'date'=>$langs->trans('Date'),
  37. 'datetime'=>$langs->trans('DateAndTime')
  38. );
  39. $action=GETPOST("action");
  40. $elementtype='member';
  41. if (!$user->admin)
  42. accessforbidden();
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'add')
  47. {
  48. if ($_POST["button"] != $langs->trans("Cancel"))
  49. {
  50. // Check values
  51. if (GETPOST('type')=='varchar' && GETPOST('size') > 255)
  52. {
  53. $error++;
  54. $langs->load("errors");
  55. $mesg=$langs->trans("ErrorSizeTooLongForVarcharType");
  56. $action = 'create';
  57. }
  58. if (! $error)
  59. {
  60. // Type et taille non encore pris en compte => varchar(255)
  61. if (isset($_POST["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_POST['attrname']))
  62. {
  63. $result=$extrafields->addExtraField($_POST['attrname'],$_POST['label'],$_POST['type'],$_POST['pos'],$_POST['size'],$elementtype);
  64. if ($result > 0)
  65. {
  66. Header("Location: ".$_SERVER["PHP_SELF"]);
  67. exit;
  68. }
  69. else
  70. {
  71. $error++;
  72. $mesg=$extrafields->error;
  73. }
  74. }
  75. else
  76. {
  77. $error++;
  78. $langs->load("errors");
  79. $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
  80. $action = 'create';
  81. }
  82. }
  83. }
  84. }
  85. // Rename field
  86. if ($action == 'update')
  87. {
  88. if ($_POST["button"] != $langs->trans("Cancel"))
  89. {
  90. // Check values
  91. if (GETPOST('type')=='varchar' && GETPOST('size') > 255)
  92. {
  93. $error++;
  94. $langs->load("errors");
  95. $mesg=$langs->trans("ErrorSizeTooLongForVarcharType");
  96. $action = 'edit';
  97. }
  98. if (! $error)
  99. {
  100. if (isset($_POST["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_POST['attrname']))
  101. {
  102. $result=$extrafields->update($_POST['attrname'],$_POST['type'],$_POST['size'],$elementtype);
  103. if ($result > 0)
  104. {
  105. if (isset($_POST['label']))
  106. {
  107. $extrafields->update_label($_POST['attrname'],$_POST['label'],$_POST['type'],$_POST['size'],$elementtype);
  108. }
  109. Header("Location: ".$_SERVER["PHP_SELF"]);
  110. exit;
  111. }
  112. else
  113. {
  114. $error++;
  115. $mesg=$extrafields->error;
  116. }
  117. }
  118. else
  119. {
  120. $error++;
  121. $langs->load("errors");
  122. $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
  123. }
  124. }
  125. }
  126. }
  127. # Suppression attribut
  128. if ($action == 'delete')
  129. {
  130. if(isset($_GET["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_GET["attrname"]))
  131. {
  132. $result=$extrafields->delete($_GET["attrname"],$elementtype);
  133. if ($result >= 0)
  134. {
  135. Header("Location: ".$_SERVER["PHP_SELF"]);
  136. exit;
  137. }
  138. else $mesg=$extrafields->error;
  139. }
  140. else
  141. {
  142. $error++;
  143. $langs->load("errors");
  144. $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
  145. }
  146. }
  147. /*
  148. * View
  149. */
  150. $textobject=$langs->transnoentitiesnoconv("Members");
  151. $help_url='EN:Module_Foundations|FR:Module_Adh&eacute;rents|ES:M&oacute;dulo_Miembros';
  152. llxHeader('',$langs->trans("MembersSetup"),$help_url);
  153. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  154. print_fiche_titre($langs->trans("MembersSetup"),$linkback,'setup');
  155. $head = member_admin_prepare_head($adh);
  156. dol_fiche_head($head, 'attributes', $langs->trans("Member"), 0, 'user');
  157. print $langs->trans("DefineHereComplementaryAttributes",$textobject).'<br>'."\n";
  158. print '<br>';
  159. dol_htmloutput_errors($mesg);
  160. // Load attribute_label
  161. $extrafields->fetch_name_optionals_label($elementtype);
  162. print "<table summary=\"listofattributes\" class=\"noborder\" width=\"100%\">";
  163. print '<tr class="liste_titre">';
  164. print '<td>'.$langs->trans("Label").'</td>';
  165. print '<td>'.$langs->trans("AttributeCode").'</td>';
  166. print '<td>'.$langs->trans("Type").'</td>';
  167. print '<td align="right">'.$langs->trans("Size").'</td>';
  168. print '<td width="80">&nbsp;</td>';
  169. print "</tr>\n";
  170. $var=True;
  171. foreach($extrafields->attribute_type as $key => $value)
  172. {
  173. $var=!$var;
  174. print "<tr ".$bc[$var].">";
  175. print "<td>".$extrafields->attribute_label[$key]."</td>\n";
  176. print "<td>".$key."</td>\n";
  177. print "<td>".$type2label[$extrafields->attribute_type[$key]]."</td>\n";
  178. print '<td align="right">'.$extrafields->attribute_size[$key]."</td>\n";
  179. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=edit&attrname='.$key.'">'.img_edit().'</a>';
  180. print "&nbsp; <a href=\"".$_SERVER["PHP_SELF"]."?action=delete&attrname=".$key."\">".img_delete()."</a></td>\n";
  181. print "</tr>";
  182. // $i++;
  183. }
  184. print "</table>";
  185. dol_fiche_end();
  186. /*
  187. * Barre d'actions
  188. *
  189. */
  190. if ($action != 'create' && $action != 'edit')
  191. {
  192. print '<div class="tabsAction">';
  193. print "<a class=\"butAction\" href=\"".$_SERVER["PHP_SELF"]."?action=create\">".$langs->trans("NewAttribute")."</a>";
  194. print "</div>";
  195. }
  196. /* ************************************************************************** */
  197. /* */
  198. /* Creation d'un champ optionnel
  199. /* */
  200. /* ************************************************************************** */
  201. if ($action == 'create')
  202. {
  203. print "<br>";
  204. print_titre($langs->trans('NewAttribute'));
  205. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  206. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  207. print '<table summary="listofattributes" class="border" width="100%">';
  208. print '<input type="hidden" name="action" value="add">';
  209. // Label
  210. print '<tr><td class="fieldrequired" required>'.$langs->trans("Label").'</td><td class="valeur"><input type="text" name="label" size="40" value="'.GETPOST('label').'"></td></tr>';
  211. // Code
  212. print '<tr><td class="fieldrequired" required>'.$langs->trans("AttributeCode").' ('.$langs->trans("AlphaNumOnlyCharsAndNoSpace").')</td><td class="valeur"><input type="text" name="attrname" size="10" value"'.GETPOST('attrname').'"></td></tr>';
  213. // Type
  214. print '<tr><td class="fieldrequired" required>'.$langs->trans("Type").'</td><td class="valeur">';
  215. print $form->selectarray('type',$type2label,GETPOST('type'));
  216. print '</td></tr>';
  217. // Size
  218. print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'255').'"></td></tr>';
  219. print '<tr><td colspan="2" align="center"><input type="submit" name="button" class="button" value="'.$langs->trans("Save").'"> &nbsp; ';
  220. print '<input type="submit" name="button" class="button" value="'.$langs->trans("Cancel").'"></td></tr>';
  221. print "</form>\n";
  222. print "</table>\n";
  223. }
  224. /* ************************************************************************** */
  225. /* */
  226. /* Edition d'un champ optionnel */
  227. /* */
  228. /* ************************************************************************** */
  229. if ($_GET["attrname"] && $action == 'edit')
  230. {
  231. print "<br>";
  232. print_titre($langs->trans("FieldEdition",$_GET["attrname"]));
  233. /*
  234. * formulaire d'edition
  235. */
  236. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?attrname='.$_GET["attrname"].'">';
  237. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  238. print '<input type="hidden" name="attrname" value="'.$_GET["attrname"].'">';
  239. print '<input type="hidden" name="action" value="update">';
  240. print '<table summary="listofattributes" class="border" width="100%">';
  241. // Label
  242. print '<tr>';
  243. print '<td class="fieldrequired" required>'.$langs->trans("Label").'</td><td class="valeur"><input type="text" name="label" size="40" value="'.$extrafields->attribute_label[$_GET["attrname"]].'"></td>';
  244. print '</tr>';
  245. // Code
  246. print '<tr>';
  247. print '<td class="fieldrequired" required>'.$langs->trans("AttributeCode").'</td>';
  248. print '<td class="valeur">'.$_GET["attrname"].'&nbsp;</td>';
  249. print '</tr>';
  250. // Type
  251. $type=$extrafields->attribute_type[$_GET["attrname"]];
  252. $size=$extrafields->attribute_size[$_GET["attrname"]];
  253. print '<tr><td class="fieldrequired" required>'.$langs->trans("Type").'</td>';
  254. print '<td class="valeur">';
  255. print $type2label[$type];
  256. print '<input type="hidden" name="type" value="'.$type.'">';
  257. print '</td></tr>';
  258. // Size
  259. print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td class="valeur"><input type="text" name="size" size="5" value="'.$size.'"></td></tr>';
  260. print '<tr><td colspan="2" align="center"><input type="submit" class="button" value="'.$langs->trans("Save").'"> &nbsp; ';
  261. print '<input type="submit" name="button" class="button" value="'.$langs->trans("Cancel").'"></td></tr>';
  262. print '</table>';
  263. print "</form>";
  264. }
  265. $db->close();
  266. llxFooter();
  267. ?>