PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/form/filepicker.php

https://github.com/hit-moodle/moodle
PHP | 141 lines | 105 code | 22 blank | 14 comment | 13 complexity | 8bbb3ced96edd5c653010affdf059fec MD5 | raw file
  1. <?php
  2. global $CFG;
  3. require_once("HTML/QuickForm/button.php");
  4. require_once($CFG->dirroot.'/repository/lib.php');
  5. /**
  6. * HTML class for a single filepicker element (based on button)
  7. *
  8. * @author Moodle.com
  9. * @version 1.0
  10. * @since Moodle 2.0
  11. * @access public
  12. */
  13. class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
  14. public $_helpbutton = '';
  15. protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
  16. function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  17. global $CFG;
  18. $options = (array)$options;
  19. foreach ($options as $name=>$value) {
  20. if (array_key_exists($name, $this->_options)) {
  21. $this->_options[$name] = $value;
  22. }
  23. }
  24. if (!empty($options['maxbytes'])) {
  25. $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
  26. }
  27. $this->_type = 'filepicker';
  28. parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  29. }
  30. function setHelpButton($helpbuttonargs, $function='helpbutton') {
  31. debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
  32. }
  33. function getHelpButton() {
  34. return $this->_helpbutton;
  35. }
  36. function getElementTemplateType() {
  37. if ($this->_flagFrozen){
  38. return 'nodisplay';
  39. } else {
  40. return 'default';
  41. }
  42. }
  43. function toHtml() {
  44. global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
  45. $id = $this->_attributes['id'];
  46. $elname = $this->_attributes['name'];
  47. if ($this->_flagFrozen) {
  48. return $this->getFrozenHtml();
  49. }
  50. if (!$draftitemid = (int)$this->getValue()) {
  51. // no existing area info provided - let's use fresh new draft area
  52. $draftitemid = file_get_unused_draft_itemid();
  53. $this->setValue($draftitemid);
  54. }
  55. if ($COURSE->id == SITEID) {
  56. $context = get_context_instance(CONTEXT_SYSTEM);
  57. } else {
  58. $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
  59. }
  60. $client_id = uniqid();
  61. $args = new stdClass();
  62. // need these three to filter repositories list
  63. $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
  64. $args->return_types = FILE_INTERNAL;
  65. $args->itemid = $draftitemid;
  66. $args->maxbytes = $this->_options['maxbytes'];
  67. $args->context = $PAGE->context;
  68. $args->buttonname = $elname.'choose';
  69. $args->elementname = $elname;
  70. $html = $this->_getTabs();
  71. $fp = new file_picker($args);
  72. $options = $fp->options;
  73. $options->context = $PAGE->context;
  74. $html .= $OUTPUT->render($fp);
  75. $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
  76. $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate'));
  77. $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
  78. $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
  79. 'env'=>'filepicker',
  80. 'action'=>'browse',
  81. 'itemid'=>$draftitemid,
  82. 'subdirs'=>0,
  83. 'maxbytes'=>$options->maxbytes,
  84. 'maxfiles'=>1,
  85. 'ctx_id'=>$PAGE->context->id,
  86. 'course'=>$PAGE->course->id,
  87. 'sesskey'=>sesskey(),
  88. ));
  89. // non js file picker
  90. $html .= '<noscript>';
  91. $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
  92. $html .= '</noscript>';
  93. return $html;
  94. }
  95. function exportValue(&$submitValues, $assoc = false) {
  96. global $USER;
  97. $draftitemid = $this->_findValue($submitValues);
  98. if (null === $draftitemid) {
  99. $draftitemid = $this->getValue();
  100. }
  101. // make sure max one file is present and it is not too big
  102. if (!is_null($draftitemid)) {
  103. $fs = get_file_storage();
  104. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  105. if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
  106. $file = array_shift($files);
  107. if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
  108. // bad luck, somebody tries to sneak in oversized file
  109. $file->delete();
  110. }
  111. foreach ($files as $file) {
  112. // only one file expected
  113. $file->delete();
  114. }
  115. }
  116. }
  117. return $this->_prepareValue($draftitemid, true);
  118. }
  119. }