upload_file_form.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Glossary;
  3. use Chamilo;
  4. /**
  5. * Form to upload a CSV file.
  6. *
  7. * @license /licence.txt
  8. * @author Laurent Opprecht <laurent@opprecht.info>
  9. */
  10. class UploadFileForm extends \FormValidator
  11. {
  12. /**
  13. *
  14. * @param string $action
  15. * @return \Glossary\UploadFileForm
  16. */
  17. public static function create($action)
  18. {
  19. return new self('upload_file', 'post', $action);
  20. }
  21. function __construct($form_name = 'upload_file', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
  22. {
  23. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  24. }
  25. /**
  26. *
  27. *
  28. */
  29. function init()
  30. {
  31. $form_name = get_lang('ImportGlossary');
  32. $this->add_header($form_name);
  33. $this->add_hidden(Request::PARAM_SEC_TOKEN, Access::instance()->get_token());
  34. $label = get_lang('ImportCSVFileLocation');
  35. $this->add_file('file', $label);
  36. $this->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  37. $this->add_checkbox('deleteall', '', get_lang('DeleteAllGlossaryTerms'));
  38. $this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
  39. $label = get_lang('CSVMustLookLike');
  40. $label = "$label";
  41. $help = '<pre>
  42. <b>term</b>;<b>definition</b>;
  43. "Hello";"Hola";
  44. "Good";"Bueno";
  45. </pre>';
  46. $this->add_html($label . $help);
  47. }
  48. public function get_delete_all()
  49. {
  50. return (bool) $this->exportValue('deleteall');
  51. }
  52. /**
  53. *
  54. * @return object
  55. */
  56. public function get_file()
  57. {
  58. $result = Request::file('file', array());
  59. if (empty($result)) {
  60. return null;
  61. }
  62. $error = isset($result['error']) ? (bool) $result['error'] : false;
  63. if ($error) {
  64. return array();
  65. }
  66. return (object) $result;
  67. }
  68. public function validate()
  69. {
  70. $result = (bool) parent::validate();
  71. if ($result == false) {
  72. return false;
  73. }
  74. $file = $this->get_file();
  75. if (empty($file)) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. }