specific_fields_add.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Add form.
  5. *
  6. * @package chamilo.admin
  7. */
  8. // Resetting the course id.
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $libpath = api_get_path(LIBRARY_PATH);
  12. require_once $libpath.'specific_fields_manager.lib.php';
  13. // section for the tabs
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. // user permissions
  16. api_protect_admin_script();
  17. $fieldId = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : 0;
  18. $interbreadcrumb[] = [
  19. 'url' => 'index.php',
  20. 'name' => get_lang('PlatformAdmin'),
  21. ];
  22. $interbreadcrumb[] = [
  23. 'url' => 'settings.php?category=Search',
  24. 'name' => get_lang('PlatformConfigSettings'),
  25. ];
  26. $interbreadcrumb[] = [
  27. 'url' => 'specific_fields.php',
  28. 'name' => get_lang('SpecificSearchFields'),
  29. ];
  30. $tool_name = get_lang('AddSpecificSearchField');
  31. if (isset($_GET['action']) && $_GET['action'] === 'edit') {
  32. $tool_name = get_lang('EditSpecificSearchField');
  33. }
  34. // Create the form
  35. $form = new FormValidator('specific_fields_add');
  36. // Field variable name
  37. $form->addElement('hidden', 'field_id', $fieldId);
  38. $form->addElement('text', 'field_name', get_lang('FieldName'));
  39. $form->applyFilter('field_name', 'html_filter');
  40. $form->applyFilter('field_name', 'trim');
  41. $form->addRule('field_name', get_lang('ThisFieldIsRequired'), 'required');
  42. $form->addRule('field_name', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  43. $form->addRule('field_name', '', 'maxlength', 20);
  44. // Set default values (only not empty when editing)
  45. $defaults = [];
  46. if ($fieldId) {
  47. $form_information = get_specific_field_list(['id' => $fieldId]);
  48. $defaults['field_name'] = $form_information[0]['name'];
  49. }
  50. $form->setDefaults($defaults);
  51. // Submit button
  52. $form->addButtonCreate(get_lang('Add'), 'submit');
  53. // Validate form
  54. if ($form->validate()) {
  55. $field = $form->exportValues();
  56. $field_name = $field['field_name'];
  57. if (is_numeric($field['field_id']) && $field['field_id'] != 0 && !empty($field['field_id'])) {
  58. edit_specific_field($field['field_id'], $field['field_name']);
  59. $message = get_lang('FieldEdited');
  60. } else {
  61. $field_id = add_specific_field($field_name);
  62. $message = get_lang('FieldAdded');
  63. }
  64. header('Location: specific_fields.php?message='.$message);
  65. //exit ();
  66. }
  67. // Display form
  68. Display::display_header($tool_name);
  69. $form->display();
  70. Display::display_footer();