extra_field_options.php 6.7 KB

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