extra_fields.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $extraFieldType = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script();
  11. //Add the JS needed to use the jqgrid
  12. $htmlHeadXtra[] = api_get_jqgrid_js();
  13. // setting breadcrumbs
  14. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  15. $tool_name = null;
  16. $action = isset($_GET['action']) ? $_GET['action'] : null;
  17. if (!in_array($extraFieldType, ExtraField::getValidExtraFieldTypes())) {
  18. api_not_allowed(true);
  19. }
  20. $check = Security::check_token('request');
  21. $token = Security::get_token();
  22. $obj = new ExtraField($extraFieldType);
  23. $obj->setupBreadcrumb($interbreadcrumb, $action);
  24. //jqgrid will use this URL to do the selects
  25. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_extra_fields&type='.$extraFieldType;
  26. //The order is important you need to check the the $column variable in the model.ajax.php file
  27. $columns = $obj->getJqgridColumnNames();
  28. //Column config
  29. $column_model = $obj->getJqgridColumnModel();
  30. //Autowidth
  31. $extra_params['autowidth'] = 'true';
  32. //height auto
  33. $extra_params['height'] = 'auto';
  34. $extra_params['sortname'] = 'field_order';
  35. $action_links = $obj->getJqgridActionLinks($token);
  36. $htmlHeadXtra[] = '<script>
  37. $(function() {
  38. // grid definition see the $obj->display() function
  39. '.Display::grid_js(
  40. $obj->type.'_fields',
  41. $url,
  42. $columns,
  43. $column_model,
  44. $extra_params,
  45. [],
  46. $action_links,
  47. true
  48. ).'
  49. $("#field_type").on("change", function() {
  50. id = $(this).val();
  51. switch(id) {
  52. case "1":
  53. $("#example").html("'.addslashes(Display::return_icon('userfield_text.png')).'");
  54. break;
  55. case "2":
  56. $("#example").html("'.addslashes(Display::return_icon('userfield_text_area.png')).'");
  57. break;
  58. case "3":
  59. $("#example").html("'.addslashes(Display::return_icon('add_user_field_howto.png')).'");
  60. break;
  61. case "4":
  62. $("#example").html("'.addslashes(Display::return_icon('userfield_drop_down.png')).'");
  63. break;
  64. case "5":
  65. $("#example").html("'.addslashes(Display::return_icon('userfield_multidropdown.png')).'");
  66. break;
  67. case "6":
  68. $("#example").html("'.addslashes(Display::return_icon('userfield_data.png')).'");
  69. break;
  70. case "7":
  71. $("#example").html("'.addslashes(Display::return_icon('userfield_date_time.png')).'");
  72. break;
  73. case "8":
  74. $("#example").html("'.addslashes(Display::return_icon('userfield_doubleselect.png')).'");
  75. break;
  76. case "9":
  77. $("#example").html("'.addslashes(Display::return_icon('userfield_divider.png')).'");
  78. break;
  79. case "10":
  80. $("#example").html("'.addslashes(Display::return_icon('userfield_user_tag.png')).'");
  81. break;
  82. case "11":
  83. $("#example").html("'.addslashes(Display::return_icon('userfield_data.png')).'");
  84. break;
  85. }
  86. });
  87. });
  88. </script>';
  89. // The header.
  90. Display::display_header($tool_name);
  91. // Action handling: Add
  92. switch ($action) {
  93. case 'add':
  94. if (api_get_session_id() != 0 &&
  95. !api_is_allowed_to_session_edit(false, true)
  96. ) {
  97. api_not_allowed();
  98. }
  99. $url = api_get_self().'?type='.$obj->type.'&action='.Security::remove_XSS($_GET['action']);
  100. $form = $obj->return_form($url, 'add');
  101. // The validation or display
  102. if ($form->validate()) {
  103. $values = $form->exportValues();
  104. unset($values['id']);
  105. $res = $obj->save($values);
  106. if ($res) {
  107. echo Display::return_message(get_lang('Item added'), 'confirmation');
  108. }
  109. $obj->display();
  110. } else {
  111. echo '<div class="actions">';
  112. echo '<a href="'.api_get_self().'?type='.$obj->type.'">'.
  113. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  114. echo '</div>';
  115. $form->addElement('hidden', 'sec_token');
  116. $form->setConstants(['sec_token' => $token]);
  117. $form->display();
  118. }
  119. break;
  120. case 'edit':
  121. // Action handling: Editing
  122. $url = api_get_self().'?type='.$obj->type.'&action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']);
  123. $form = $obj->return_form($url, 'edit');
  124. // The validation or display
  125. if ($form->validate()) {
  126. $values = $form->exportValues();
  127. $res = $obj->update($values);
  128. echo Display::return_message(
  129. sprintf(get_lang('Item updated'), $values['variable']),
  130. 'confirmation',
  131. false
  132. );
  133. $obj->display();
  134. } else {
  135. echo '<div class="actions">';
  136. echo '<a href="'.api_get_self().'?type='.$obj->type.'">'.
  137. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  138. echo '</div>';
  139. $form->addElement('hidden', 'sec_token');
  140. $form->setConstants(['sec_token' => $token]);
  141. $form->display();
  142. }
  143. break;
  144. case 'delete':
  145. // Action handling: delete
  146. $res = $obj->delete($_GET['id']);
  147. if ($res) {
  148. echo Display::return_message(get_lang('Item deleted'), 'confirmation');
  149. }
  150. $obj->display();
  151. break;
  152. default:
  153. $obj->display();
  154. break;
  155. }
  156. Display::display_footer();