[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/form/ -> filepicker.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  
  18  /**
  19   * Filepicker form element
  20   *
  21   * Contains HTML class for a single filepicker form element
  22   *
  23   * @package   core_form
  24   * @copyright 2009 Dongsheng Cai <[email protected]>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  
  30  require_once("HTML/QuickForm/button.php");
  31  require_once($CFG->dirroot.'/repository/lib.php');
  32  
  33  /**
  34   * Filepicker form element
  35   *
  36   * HTML class for a single filepicker element (based on button)
  37   *
  38   * @package   core_form
  39   * @category  form
  40   * @copyright 2009 Dongsheng Cai <[email protected]>
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
  44      /** @var string html for help button, if empty then no help will icon will be dispalyed. */
  45      public $_helpbutton = '';
  46  
  47      /** @var array options provided to initalize filemanager */
  48      // PHP doesn't support 'key' => $value1 | $value2 in class definition
  49      // We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
  50      // So I have to set null here, and do it in constructor
  51      protected $_options    = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>null);
  52  
  53      /**
  54       * Constructor
  55       *
  56       * @param string $elementName (optional) name of the filepicker
  57       * @param string $elementLabel (optional) filepicker label
  58       * @param array $attributes (optional) Either a typical HTML attribute string
  59       *              or an associative array
  60       * @param array $options set of options to initalize filepicker
  61       */
  62      function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  63          global $CFG, $PAGE;
  64  
  65          $options = (array)$options;
  66          foreach ($options as $name=>$value) {
  67              if (array_key_exists($name, $this->_options)) {
  68                  $this->_options[$name] = $value;
  69              }
  70          }
  71          if (empty($options['return_types'])) {
  72              $this->_options['return_types'] = FILE_INTERNAL;
  73          }
  74          $fpmaxbytes = 0;
  75          if (!empty($options['maxbytes'])) {
  76              $fpmaxbytes = $options['maxbytes'];
  77          }
  78          $coursemaxbytes = 0;
  79          if (!empty($PAGE->course->maxbytes)) {
  80              $coursemaxbytes = $PAGE->course->maxbytes;
  81          }
  82          $this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $coursemaxbytes, $fpmaxbytes);
  83          $this->_type = 'filepicker';
  84          parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  85      }
  86  
  87      /**
  88       * Returns html for help button.
  89       *
  90       * @return string html for help button
  91       */
  92      function getHelpButton() {
  93          return $this->_helpbutton;
  94      }
  95  
  96      /**
  97       * Returns type of filepicker element
  98       *
  99       * @return string
 100       */
 101      function getElementTemplateType() {
 102          if ($this->_flagFrozen){
 103              return 'nodisplay';
 104          } else {
 105              return 'default';
 106          }
 107      }
 108  
 109      /**
 110       * Returns HTML for filepicker form element.
 111       *
 112       * @return string
 113       */
 114      function toHtml() {
 115          global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
 116          $id     = $this->_attributes['id'];
 117          $elname = $this->_attributes['name'];
 118  
 119          if ($this->_flagFrozen) {
 120              return $this->getFrozenHtml();
 121          }
 122          if (!$draftitemid = (int)$this->getValue()) {
 123              // no existing area info provided - let's use fresh new draft area
 124              $draftitemid = file_get_unused_draft_itemid();
 125              $this->setValue($draftitemid);
 126          }
 127  
 128          if ($COURSE->id == SITEID) {
 129              $context = context_system::instance();
 130          } else {
 131              $context = context_course::instance($COURSE->id);
 132          }
 133  
 134          $client_id = uniqid();
 135  
 136          $args = new stdClass();
 137          // need these three to filter repositories list
 138          $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
 139          $args->return_types = $this->_options['return_types'];
 140          $args->itemid = $draftitemid;
 141          $args->maxbytes = $this->_options['maxbytes'];
 142          $args->context = $PAGE->context;
 143          $args->buttonname = $elname.'choose';
 144          $args->elementname = $elname;
 145  
 146          $html = $this->_getTabs();
 147          $fp = new file_picker($args);
 148          $options = $fp->options;
 149          $options->context = $PAGE->context;
 150          $html .= $OUTPUT->render($fp);
 151          $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
 152  
 153          $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload'));
 154          $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
 155  
 156          $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
 157              'env'=>'filepicker',
 158              'action'=>'browse',
 159              'itemid'=>$draftitemid,
 160              'subdirs'=>0,
 161              'maxbytes'=>$options->maxbytes,
 162              'maxfiles'=>1,
 163              'ctx_id'=>$PAGE->context->id,
 164              'course'=>$PAGE->course->id,
 165              'sesskey'=>sesskey(),
 166              ));
 167  
 168          // non js file picker
 169          $html .= '<noscript>';
 170          $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
 171          $html .= '</noscript>';
 172  
 173          return $html;
 174      }
 175  
 176      /**
 177       * export uploaded file
 178       *
 179       * @param array $submitValues values submitted.
 180       * @param bool $assoc specifies if returned array is associative
 181       * @return array
 182       */
 183      function exportValue(&$submitValues, $assoc = false) {
 184          global $USER;
 185  
 186          $draftitemid = $this->_findValue($submitValues);
 187          if (null === $draftitemid) {
 188              $draftitemid = $this->getValue();
 189          }
 190  
 191          // make sure max one file is present and it is not too big
 192          if (!is_null($draftitemid)) {
 193              $fs = get_file_storage();
 194              $usercontext = context_user::instance($USER->id);
 195              if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
 196                  $file = array_shift($files);
 197                  if ($this->_options['maxbytes']
 198                      and $this->_options['maxbytes'] !== USER_CAN_IGNORE_FILE_SIZE_LIMITS
 199                      and $file->get_filesize() > $this->_options['maxbytes']) {
 200  
 201                      // bad luck, somebody tries to sneak in oversized file
 202                      $file->delete();
 203                  }
 204                  foreach ($files as $file) {
 205                      // only one file expected
 206                      $file->delete();
 207                  }
 208              }
 209          }
 210  
 211          return $this->_prepareValue($draftitemid, true);
 212      }
 213  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1