extra_field_options.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $this_section = SECTION_PLATFORM_ADMIN;
  9. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  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('PlatformAdmin')];
  15. $tool_name = null;
  16. $action = isset($_GET['action']) ? $_GET['action'] : null;
  17. $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null;
  18. if (empty($field_id)) {
  19. api_not_allowed();
  20. }
  21. if (!in_array($type, ExtraField::getValidExtraFieldTypes())) {
  22. api_not_allowed();
  23. }
  24. $extra_field = new ExtraField($type);
  25. $extra_field_info = $extra_field->get($field_id);
  26. $check = Security::check_token('request');
  27. $token = Security::get_token();
  28. if ($action == 'add') {
  29. $interbreadcrumb[] = ['url' => 'extra_fields.php?type='.$extra_field->type, 'name' => $extra_field->pageName];
  30. $interbreadcrumb[] = [
  31. 'url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],
  32. 'name' => $extra_field_info['display_text'],
  33. ];
  34. $interbreadcrumb[] = [
  35. 'url' => 'extra_field_options.php?type='.$extra_field->type.'&field_id='.$extra_field_info['id'],
  36. 'name' => get_lang('EditExtraFieldOptions'),
  37. ];
  38. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
  39. } elseif ($action == 'edit') {
  40. $interbreadcrumb[] = ['url' => 'extra_fields.php?type='.$extra_field->type, 'name' => $extra_field->pageName];
  41. $interbreadcrumb[] = [
  42. 'url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],
  43. 'name' => $extra_field_info['display_text'],
  44. ];
  45. $interbreadcrumb[] = [
  46. 'url' => 'extra_field_options.php?type='.$extra_field->type.'&field_id='.$extra_field_info['id'],
  47. 'name' => get_lang('EditExtraFieldOptions'),
  48. ];
  49. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
  50. } else {
  51. $interbreadcrumb[] = ['url' => 'extra_fields.php?type='.$extra_field->type, 'name' => $extra_field->pageName];
  52. $interbreadcrumb[] = [
  53. 'url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],
  54. 'name' => $extra_field_info['display_text'],
  55. ];
  56. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('EditExtraFieldOptions')];
  57. }
  58. //jqgrid will use this URL to do the selects
  59. $params = 'field_id='.$field_id.'&type='.$extra_field->type;
  60. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_extra_field_options&'.$params;
  61. //The order is important you need to check the the $column variable in the model.ajax.php file
  62. $columns = [
  63. get_lang('Name'),
  64. get_lang('Value'),
  65. get_lang('Order'),
  66. get_lang('Actions'),
  67. ];
  68. //Column config
  69. $column_model = [
  70. [
  71. 'name' => 'display_text',
  72. 'index' => 'display_text',
  73. 'width' => '180',
  74. 'align' => 'left',
  75. ],
  76. [
  77. 'name' => 'option_value',
  78. 'index' => 'option_value',
  79. 'width' => '',
  80. 'align' => 'left',
  81. 'sortable' => 'false',
  82. ],
  83. [
  84. 'name' => 'option_order',
  85. 'index' => 'option_order',
  86. 'width' => '',
  87. 'align' => 'left',
  88. 'sortable' => 'false',
  89. ],
  90. [
  91. 'name' => 'actions',
  92. 'index' => 'actions',
  93. 'width' => '100',
  94. 'align' => 'left',
  95. 'formatter' => 'action_formatter',
  96. 'sortable' => 'false',
  97. ],
  98. ];
  99. //Autowidth
  100. $extra_params['autowidth'] = 'true';
  101. //height auto
  102. $extra_params['height'] = 'auto';
  103. //With this function we can add actions to the jgrid (edit, delete, etc)
  104. $action_links = 'function action_formatter(cellvalue, options, rowObject) {
  105. return \'<a href="?action=edit&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>'.
  106. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(get_lang("ConfirmYourChoice"))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>'.
  107. '\';
  108. }';
  109. $htmlHeadXtra[] = '<script>
  110. $(function() {
  111. // grid definition see the $obj->display() function
  112. '.Display::grid_js(
  113. 'extra_field_options',
  114. $url,
  115. $columns,
  116. $column_model,
  117. $extra_params,
  118. [],
  119. $action_links,
  120. true
  121. ).'
  122. });
  123. </script>';
  124. Display::display_header($tool_name);
  125. echo Display::page_header($extra_field_info['display_text'], $extra_field_info['variable'], 'h1');
  126. $obj = new ExtraFieldOption($extra_field->type);
  127. $obj->fieldId = $field_id;
  128. // Action handling: Add
  129. switch ($action) {
  130. case 'add':
  131. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  132. api_not_allowed();
  133. }
  134. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&'.$params;
  135. $form = $obj->return_form($url, 'add');
  136. // The validation or display
  137. if ($form->validate()) {
  138. if ($check) {
  139. $values = $form->exportValues();
  140. $res = $obj->save_one_item($values);
  141. if ($res) {
  142. echo Display::return_message(get_lang('ItemAdded'), 'confirmation');
  143. }
  144. }
  145. $obj->display();
  146. } else {
  147. $form->addElement('hidden', 'sec_token');
  148. $form->setConstants(['sec_token' => $token]);
  149. $form->display();
  150. }
  151. break;
  152. case 'edit':
  153. // Action handling: Editing
  154. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']).'&'.$params;
  155. $form = $obj->return_form($url, 'edit');
  156. // The validation or display
  157. if ($form->validate()) {
  158. if ($check) {
  159. $values = $form->exportValues();
  160. $res = $obj->update($values);
  161. echo Display::return_message(
  162. sprintf(get_lang('ItemUpdated'), $values['display_text']),
  163. 'confirmation',
  164. false
  165. );
  166. }
  167. $obj->display();
  168. } else {
  169. $form->addElement('hidden', 'sec_token');
  170. $form->setConstants(['sec_token' => $token]);
  171. $form->display();
  172. }
  173. break;
  174. case 'delete':
  175. // Action handling: delete
  176. if ($check) {
  177. $res = $obj->delete($_GET['id']);
  178. if ($res) {
  179. echo Display::return_message(get_lang('ItemDeleted'), 'confirmation');
  180. }
  181. }
  182. $obj->display();
  183. break;
  184. default:
  185. $obj->display();
  186. break;
  187. }
  188. Display::display_footer();