upload_file_form.class.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace CourseDescription;
  3. use Chamilo;
  4. /**
  5. * Form to upload a file.
  6. *
  7. * @license /licence.txt
  8. * @author Laurent Opprecht <laurent@opprecht.info>
  9. */
  10. class UploadFileForm extends \FormValidator
  11. {
  12. function __construct($form_name = 'upload_file', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
  13. {
  14. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  15. }
  16. /**
  17. *
  18. *
  19. */
  20. function init()
  21. {
  22. $form_name = get_lang('UploadFile');
  23. $this->add_header($form_name);
  24. $label = get_lang('File');
  25. $this->add_file('file', $label);
  26. $this->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  27. //$this->add_checkbox('replace', '', get_lang('ReplaceExistingEntries'));
  28. $this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
  29. // $label = get_lang('CSVMustLookLike');
  30. // $label = "<h4>$label</h4>";
  31. // $help = '<pre>
  32. // <strong>"url"</strong>;"title";"description";"target";"category_title";"category_description"
  33. // "http://chamilo.org";"Chamilo";"";"_self";"";""
  34. // "http://google.com";"Google";"";"_self";"Google";""
  35. // "http://mail.google.com";"Google";"";"_self";"Google";""
  36. // </pre>';
  37. //
  38. // $this->add_html($label . $help);
  39. }
  40. /**
  41. *
  42. * @return object
  43. */
  44. public function get_file()
  45. {
  46. $result = Request::file('file', array());
  47. if (empty($result)) {
  48. return null;
  49. }
  50. $error = isset($result['error']) ? (bool) $result['error'] : false;
  51. if ($error) {
  52. return array();
  53. }
  54. return (object)$result;
  55. }
  56. public function validate()
  57. {
  58. $result = (bool) parent::validate();
  59. if ($result == false) {
  60. return false;
  61. }
  62. $file = $this->get_file();
  63. if (empty($file)) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. }