upload_file_form.class.php 2.2 KB

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