upload_file_form.class.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Notebook;
  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('Import');
  32. $this->add_header($form_name);
  33. $this->add_hidden(Request::PARAM_SEC_TOKEN, Access::instance()->get_token());
  34. $label = get_lang('File');
  35. $this->add_file('file', $label);
  36. $this->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  37. $this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
  38. $label = get_lang('CSVMustLookLike');
  39. $label = "$label";
  40. $help = '<pre>
  41. <b>title</b>;<b>description</b>;
  42. "Hello";"Hola";
  43. "Good";"Bueno";
  44. </pre>';
  45. $this->add_html($label . $help);
  46. }
  47. /**
  48. *
  49. * @return object
  50. */
  51. public function get_file()
  52. {
  53. $result = Request::file('file', array());
  54. if (empty($result)) {
  55. return null;
  56. }
  57. $error = isset($result['error']) ? (bool) $result['error'] : false;
  58. if ($error) {
  59. return array();
  60. }
  61. return (object) $result;
  62. }
  63. public function validate()
  64. {
  65. $result = (bool) parent::validate();
  66. if ($result == false) {
  67. return false;
  68. }
  69. $file = $this->get_file();
  70. if (empty($file)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. }