upload_file_form.class.php 2.4 KB

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