user_fields_add.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // name of the language file that needs to be included
  7. $language_file = array('admin','registration');
  8. $cidReset = true;
  9. // including necessary libraries
  10. require_once '../inc/global.inc.php';
  11. // section for the tabs
  12. $this_section=SECTION_PLATFORM_ADMIN;
  13. // user permissions
  14. api_protect_admin_script();
  15. $htmlHeadXtra[] = '<script>
  16. function change_image_user_field (image_value) {
  17. if (image_value==1) {
  18. document.getElementById(\'options\').style.display = \'none\';
  19. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  20. $("div#id_image_user_field").html("&nbsp;");
  21. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_text.png', get_lang('AddUserFields'))."'".');
  22. } else if (image_value==2) {
  23. document.getElementById(\'options\').style.display = \'none\';
  24. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  25. $("div#id_image_user_field").html("&nbsp;");
  26. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_text_area.png', get_lang('AddUserFields'))."'".');
  27. } else if (image_value==3) {
  28. document.getElementById(\'options\').style.display = \'block\';
  29. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  30. $("div#id_image_user_field").html("&nbsp;");
  31. $("div#id_image_user_field").html('."'<br />".Display::return_icon('add_user_field_howto.png', get_lang('AddUserFields'))."'".');
  32. } else if (image_value==4) {
  33. document.getElementById(\'options\').style.display = \'block\';
  34. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  35. $("div#id_image_user_field").html("&nbsp;");
  36. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_drop_down.png', get_lang('AddUserFields'))."'".');
  37. } else if (image_value==5) {
  38. document.getElementById(\'options\').style.display = \'block\';
  39. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  40. $("div#id_image_user_field").html("&nbsp;");
  41. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_multidropdown.png', get_lang('AddUserFields'))."'".');
  42. } else if (image_value==6) {
  43. document.getElementById(\'options\').style.display = \'none\';
  44. $("div#id_image_user_field").html("&nbsp;");
  45. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_data.png', get_lang('AddUserFields'))."'".');
  46. } else if (image_value==7) {
  47. document.getElementById(\'options\').style.display = \'none\';
  48. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  49. $("div#id_image_user_field").html("&nbsp;");
  50. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_date_time.png', get_lang('AddUserFields'))."'".');
  51. } else if (image_value==8) {
  52. document.getElementById(\'options\').style.display = \'block\';
  53. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  54. $("div#id_image_user_field").html("&nbsp;");
  55. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_doubleselect.png', get_lang('AddUserFields'))."'".');
  56. } else if (image_value==9) {
  57. document.getElementById(\'options\').style.display = \'none\';
  58. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  59. $("div#id_image_user_field").html("&nbsp;");
  60. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_divider.png', get_lang('AddUserFields'))."'".');
  61. } else if (image_value==10) {
  62. document.getElementById(\'options\').style.display = \'none\';
  63. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  64. $("div#id_image_user_field").html("&nbsp;");
  65. $("div#id_image_user_field").html('."'<br />".Display::return_icon('userfield_user_tag.png', get_lang('UserTag'))."'".');
  66. }
  67. }
  68. function advanced_parameters() {
  69. if(document.getElementById(\'options\').style.display == \'none\') {
  70. document.getElementById(\'options\').style.display = \'block\';
  71. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  72. } else {
  73. document.getElementById(\'options\').style.display = \'none\';
  74. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  75. }
  76. }
  77. </script>';
  78. // Database table definitions
  79. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  80. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  81. $table_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  82. $table_uf_opt = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  83. $table_uf_val = Database :: get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  84. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  85. $interbreadcrumb[] = array ('url' => 'user_fields.php', 'name' => get_lang('UserFields'));
  86. if ($_GET['action']<>'edit') {
  87. $tool_name = get_lang('AddUserFields');
  88. } else {
  89. $tool_name = get_lang('EditUserFields');
  90. }
  91. // Create the form
  92. $form = new FormValidator('user_fields_add');
  93. $form->addElement('header', '', $tool_name);
  94. // Field display name
  95. $form->addElement('text','fieldtitle',get_lang('FieldTitle'));
  96. $form->applyFilter('fieldtitle','html_filter');
  97. $form->applyFilter('fieldtitle','trim');
  98. $form->addRule('fieldtitle', get_lang('ThisFieldIsRequired'), 'required');
  99. // Field type
  100. $types = UserManager::get_user_field_types();
  101. $form->addElement('select','fieldtype',get_lang('FieldType'), $types, array('onchange'=>'change_image_user_field(this.value)'));
  102. $form->addRule('fieldtype', get_lang('ThisFieldIsRequired'), 'required');
  103. //Advanced parameters
  104. $form -> addElement('advanced_settings','<a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><div style="vertical-align:top;" ><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</div></span></a>');
  105. //When edit, the combobox displey the field type displeyed else none
  106. if ( (isset($_GET['action']) && $_GET['action'] == 'edit') && in_array($_GET['field_type'],array(3,4,5,8))) {
  107. $form -> addElement('html','<div id="options" style="display:block">');
  108. } else {
  109. $form -> addElement('html','<div id="options" style="display:none">');
  110. }
  111. //field label
  112. $form->addElement('hidden','fieldid',Security::remove_XSS($_GET['field_id']));
  113. $form->addElement('text','fieldlabel',get_lang('FieldLabel'));
  114. $form->applyFilter('fieldlabel','html_filter');
  115. $form->addRule('fieldlabel', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  116. $form->addRule('fieldlabel', '', 'maxlength',60);
  117. //$form->addRule('fieldlabel', get_lang('FieldTaken'), 'fieldlabel_available');
  118. // Field options possible
  119. $form->addElement('text','fieldoptions',get_lang('FieldPossibleValues').Display::return_icon('info3.gif', get_lang('FieldPossibleValuesComment'), array('align' => 'absmiddle', 'hspace' => '3px')));
  120. $form->applyFilter('fieldoptions','trim');
  121. if (is_numeric($_GET['field_id'])) {
  122. $form->addElement('static', 'option_reorder', '', '<a href="user_fields_options.php?field_id='.Security::remove_XSS($_GET['field_id']).'">'.get_lang('ReorderOptions').'</a>');
  123. }
  124. // Field default value
  125. $form->addElement('text','fielddefaultvalue',get_lang('FieldDefaultValue'));
  126. $form->applyFilter('fielddefaultvalue','trim');
  127. // Set default values (only not empty when editing)
  128. $defaults = array();
  129. if (is_numeric($_GET['field_id'])) {
  130. $form_information = UserManager::get_extra_field_information((int)$_GET['field_id']);
  131. $defaults['fieldtitle'] = $form_information['field_display_text'];
  132. $defaults['fieldlabel'] = $form_information['field_variable'];
  133. $defaults['fieldtype'] = $form_information['field_type'];
  134. $defaults['fielddefaultvalue'] = $form_information['field_default_value'];
  135. $count = 0;
  136. // we have to concatenate the options
  137. if (count($form_information['options'])>0) {
  138. foreach ($form_information['options'] as $option_id=>$option) {
  139. if ($count<>0) {
  140. $defaults['fieldoptions'] = $defaults['fieldoptions'].'; '.$option['option_display_text'];
  141. } else {
  142. $defaults['fieldoptions'] = $option['option_display_text'];
  143. }
  144. $count++;
  145. }
  146. }
  147. }
  148. $form->setDefaults($defaults);
  149. if(isset($_GET['field_id']) && !empty($_GET['field_id'])) {
  150. $class="save";
  151. $text=get_lang('buttonEditUserField');
  152. } else {
  153. $class="add";
  154. $text=get_lang('buttonAddUserField');
  155. }
  156. $form->addElement('html','</div>');
  157. // Submit button
  158. $form->addElement('style_submit_button', 'submit',$text, 'class='.$class.'');
  159. // Validate form
  160. if( $form->validate()) {
  161. $check = Security::check_token('post');
  162. if($check) {
  163. $field = $form->exportValues();
  164. $fieldlabel = empty($field['fieldlabel'])?$field['fieldtitle']:$field['fieldlabel'];
  165. $fieldlabel = trim(strtolower(str_replace(" ","_",$fieldlabel)));
  166. $fieldtype = $field['fieldtype'];
  167. $fieldtitle = $field['fieldtitle'];
  168. $fielddefault = $field['fielddefaultvalue'];
  169. $fieldoptions = $field['fieldoptions']; //comma-separated list of options
  170. if (is_numeric($field['fieldid']) AND !empty($field['fieldid'])) {
  171. UserManager:: save_extra_field_changes($field['fieldid'],$fieldlabel,$fieldtype,$fieldtitle,$fielddefault,$fieldoptions);
  172. $message = get_lang('FieldEdited');
  173. } else {
  174. $field_id = UserManager::create_extra_field($fieldlabel,$fieldtype,$fieldtitle,$fielddefault,$fieldoptions);
  175. $message = get_lang('FieldAdded');
  176. }
  177. Security::clear_token();
  178. header('Location: user_fields.php?action=show_message&message='.urlencode(get_lang('FieldAdded')));
  179. exit ();
  180. }
  181. } else {
  182. if(isset($_POST['submit'])){
  183. Security::clear_token();
  184. }
  185. $token = Security::get_token();
  186. $form->addElement('hidden','sec_token');
  187. $form->setConstants(array('sec_token' => $token));
  188. }
  189. // Display form
  190. Display::display_header($tool_name);
  191. //api_display_tool_title($tool_name);
  192. if(!empty($_GET['message'])) {
  193. Display::display_normal_message($_GET['message']);
  194. }
  195. //else
  196. //{
  197. //Display::display_normal_message(get_lang('UserFieldsAddHelp'),false);
  198. //}
  199. $form->display();
  200. echo '<div id="id_image_user_field">';
  201. if(!empty($defaults['fieldtype'])) {
  202. $image_value = $defaults['fieldtype'];
  203. if ($image_value==1) {
  204. echo '<br />'.Display::return_icon('userfield_text.png', get_lang('AddUserFields'));
  205. } else if ($image_value==2) {
  206. echo '<br />'.Display::return_icon('userfield_text_area.png', get_lang('AddUserFields'));
  207. } else if ($image_value==3) {
  208. echo '<br />'.Display::return_icon('add_user_field_howto.png', get_lang('AddUserFields'));
  209. } else if ($image_value==4) {
  210. echo '<br />'.Display::return_icon('userfield_drop_down.png', get_lang('AddUserFields'));
  211. } else if ($image_value==5) {
  212. echo '<br />'.Display::return_icon('userfield_multidropdown.png', get_lang('AddUserFields'));
  213. } else if ($image_value==6) {
  214. echo '<br />'.Display::return_icon('userfield_data.png', get_lang('AddUserFields'));
  215. } else if ($image_value==7) {
  216. echo '<br />'.Display::return_icon('userfield_date_time.png', get_lang('AddUserFields'));
  217. } else if ($image_value==8) {
  218. echo '<br />'.Display::return_icon('userfield_doubleselect.png', get_lang('AddUserFields'));
  219. } else if ($image_value==9) {
  220. echo '<br />'.Display::return_icon('userfield_divider.png', get_lang('AddUserFields'));
  221. } else if ($image_value==10) {
  222. echo '<br />'.Display::return_icon('userfield_user_tag.png', get_lang('UserTag'));
  223. }
  224. } else {
  225. echo '<br />'.Display::return_icon('userfield_text.png', get_lang('AddUserFields'));
  226. }
  227. echo '</div>';
  228. // footer
  229. Display::display_footer();