user_fields_add.php 12 KB

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