[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> filemanager.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   * FileManager form element
  20   *
  21   * Contains HTML class for a filemanager form element
  22   *
  23   * @package   core_form
  24   * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
  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/element.php');
  31  require_once($CFG->dirroot.'/lib/filelib.php');
  32  require_once($CFG->dirroot.'/repository/lib.php');
  33  
  34  /**
  35   * Filemanager form element
  36   *
  37   * FilemaneManager lets user to upload/manage multiple files
  38   * @package   core_form
  39   * @category  form
  40   * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
  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('mainfile' => '', 'subdirs' => 1, 'maxbytes' => -1, 'maxfiles' => -1,
  52              'accepted_types' => '*', 'return_types' =>  null, 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED);
  53  
  54      /**
  55       * Constructor
  56       *
  57       * @param string $elementName (optional) name of the filemanager
  58       * @param string $elementLabel (optional) filemanager label
  59       * @param array $attributes (optional) Either a typical HTML attribute string
  60       *              or an associative array
  61       * @param array $options set of options to initalize filemanager
  62       */
  63      public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  64          global $CFG, $PAGE;
  65  
  66          $options = (array)$options;
  67          foreach ($options as $name=>$value) {
  68              if (array_key_exists($name, $this->_options)) {
  69                  $this->_options[$name] = $value;
  70              }
  71          }
  72          if (!empty($options['maxbytes'])) {
  73              $this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $options['maxbytes']);
  74          }
  75          if (empty($options['return_types'])) {
  76              $this->_options['return_types'] = (FILE_INTERNAL | FILE_REFERENCE);
  77          }
  78          $this->_type = 'filemanager';
  79          parent::__construct($elementName, $elementLabel, $attributes);
  80      }
  81  
  82      /**
  83       * Old syntax of class constructor. Deprecated in PHP7.
  84       *
  85       * @deprecated since Moodle 3.1
  86       */
  87      public function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  88          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  89          self::__construct($elementName, $elementLabel, $attributes, $options);
  90      }
  91  
  92      /**
  93       * Called by HTML_QuickForm whenever form event is made on this element
  94       *
  95       * @param string $event Name of event
  96       * @param mixed $arg event arguments
  97       * @param object $caller calling object
  98       * @return bool
  99       */
 100      function onQuickFormEvent($event, $arg, &$caller)
 101      {
 102          switch ($event) {
 103              case 'createElement':
 104                  $caller->setType($arg[0], PARAM_INT);
 105                  break;
 106          }
 107          return parent::onQuickFormEvent($event, $arg, $caller);
 108      }
 109  
 110      /**
 111       * Sets name of filemanager
 112       *
 113       * @param string $name name of the filemanager
 114       */
 115      function setName($name) {
 116          $this->updateAttributes(array('name'=>$name));
 117      }
 118  
 119      /**
 120       * Returns name of filemanager
 121       *
 122       * @return string
 123       */
 124      function getName() {
 125          return $this->getAttribute('name');
 126      }
 127  
 128      /**
 129       * Updates filemanager attribute value
 130       *
 131       * @param string $value value to set
 132       */
 133      function setValue($value) {
 134          $this->updateAttributes(array('value'=>$value));
 135      }
 136  
 137      /**
 138       * Returns filemanager attribute value
 139       *
 140       * @return string
 141       */
 142      function getValue() {
 143          return $this->getAttribute('value');
 144      }
 145  
 146      /**
 147       * Returns maximum file size which can be uploaded
 148       *
 149       * @return int
 150       */
 151      function getMaxbytes() {
 152          return $this->_options['maxbytes'];
 153      }
 154  
 155      /**
 156       * Sets maximum file size which can be uploaded
 157       *
 158       * @param int $maxbytes file size
 159       */
 160      function setMaxbytes($maxbytes) {
 161          global $CFG, $PAGE;
 162          $this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $maxbytes);
 163      }
 164  
 165      /**
 166       * Returns the maximum size of the area.
 167       *
 168       * @return int
 169       */
 170      function getAreamaxbytes() {
 171          return $this->_options['areamaxbytes'];
 172      }
 173  
 174      /**
 175       * Sets the maximum size of the area.
 176       *
 177       * @param int $areamaxbytes size limit
 178       */
 179      function setAreamaxbytes($areamaxbytes) {
 180          $this->_options['areamaxbytes'] = $areamaxbytes;
 181      }
 182  
 183      /**
 184       * Returns true if subdirectoy can be created, else false
 185       *
 186       * @return bool
 187       */
 188      function getSubdirs() {
 189          return $this->_options['subdirs'];
 190      }
 191  
 192      /**
 193       * Set option to create sub directory, while uploading  file
 194       *
 195       * @param bool $allow true if sub directory can be created.
 196       */
 197      function setSubdirs($allow) {
 198          $this->_options['subdirs'] = $allow;
 199      }
 200  
 201      /**
 202       * Returns maximum number of files which can be uploaded
 203       *
 204       * @return int
 205       */
 206      function getMaxfiles() {
 207          return $this->_options['maxfiles'];
 208      }
 209  
 210      /**
 211       * Sets maximum number of files which can be uploaded.
 212       *
 213       * @param int $num number of files
 214       */
 215      function setMaxfiles($num) {
 216          $this->_options['maxfiles'] = $num;
 217      }
 218  
 219      /**
 220       * Returns html for help button.
 221       *
 222       * @return string html for help button
 223       */
 224      function getHelpButton() {
 225          return $this->_helpbutton;
 226      }
 227  
 228      /**
 229       * Returns type of filemanager element
 230       *
 231       * @return string
 232       */
 233      function getElementTemplateType() {
 234          if ($this->_flagFrozen){
 235              return 'nodisplay';
 236          } else {
 237              return 'default';
 238          }
 239      }
 240  
 241      /**
 242       * Returns HTML for filemanager form element.
 243       *
 244       * @return string
 245       */
 246      function toHtml() {
 247          global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
 248          require_once("$CFG->dirroot/repository/lib.php");
 249  
 250          // security - never ever allow guest/not logged in user to upload anything or use this element!
 251          if (isguestuser() or !isloggedin()) {
 252              print_error('noguest');
 253          }
 254  
 255          if ($this->_flagFrozen) {
 256              return $this->getFrozenHtml();
 257          }
 258  
 259          $id          = $this->_attributes['id'];
 260          $elname      = $this->_attributes['name'];
 261          $subdirs     = $this->_options['subdirs'];
 262          $maxbytes    = $this->_options['maxbytes'];
 263          $draftitemid = $this->getValue();
 264          $accepted_types = $this->_options['accepted_types'];
 265  
 266          if (empty($draftitemid)) {
 267              // no existing area info provided - let's use fresh new draft area
 268              require_once("$CFG->libdir/filelib.php");
 269              $this->setValue(file_get_unused_draft_itemid());
 270              $draftitemid = $this->getValue();
 271          }
 272  
 273          $client_id = uniqid();
 274  
 275          // filemanager options
 276          $options = new stdClass();
 277          $options->mainfile  = $this->_options['mainfile'];
 278          $options->maxbytes  = $this->_options['maxbytes'];
 279          $options->maxfiles  = $this->getMaxfiles();
 280          $options->client_id = $client_id;
 281          $options->itemid    = $draftitemid;
 282          $options->subdirs   = $this->_options['subdirs'];
 283          $options->target    = $id;
 284          $options->accepted_types = $accepted_types;
 285          $options->return_types = $this->_options['return_types'];
 286          $options->context = $PAGE->context;
 287          $options->areamaxbytes = $this->_options['areamaxbytes'];
 288  
 289          $html = $this->_getTabs();
 290          $fm = new form_filemanager($options);
 291          $output = $PAGE->get_renderer('core', 'files');
 292          $html .= $output->render($fm);
 293  
 294          $html .= html_writer::empty_tag('input', array('value' => $draftitemid, 'name' => $elname, 'type' => 'hidden'));
 295          // label element needs 'for' attribute work
 296          $html .= html_writer::empty_tag('input', array('value' => '', 'id' => 'id_'.$elname, 'type' => 'hidden'));
 297  
 298          return $html;
 299      }
 300  }
 301  
 302  /**
 303   * Data structure representing a file manager.
 304   *
 305   * This class defines the data structure for file mnager
 306   *
 307   * @package   core_form
 308   * @copyright 2010 Dongsheng Cai
 309   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 310   * @todo      do not use this abstraction (skodak)
 311   */
 312  class form_filemanager implements renderable {
 313      /** @var stdClass $options options for filemanager */
 314      public $options;
 315  
 316      /**
 317       * Constructor
 318       *
 319       * @param stdClass $options options for filemanager
 320       *   default options are:
 321       *       maxbytes=>-1,
 322       *       areamaxbytes => FILE_AREA_MAX_BYTES_UNLIMITED,
 323       *       maxfiles=>-1,
 324       *       itemid=>0,
 325       *       subdirs=>false,
 326       *       client_id=>uniqid(),
 327       *       acepted_types=>'*',
 328       *       return_types=>FILE_INTERNAL,
 329       *       context=>$PAGE->context,
 330       *       author=>fullname($USER),
 331       *       licenses=>array build from $CFG->licenses,
 332       *       defaultlicense=>$CFG->sitedefaultlicense
 333       */
 334      public function __construct(stdClass $options) {
 335          global $CFG, $USER, $PAGE;
 336          require_once($CFG->dirroot. '/repository/lib.php');
 337          $defaults = array(
 338              'maxbytes'=>-1,
 339              'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED,
 340              'maxfiles'=>-1,
 341              'itemid'=>0,
 342              'subdirs'=>0,
 343              'client_id'=>uniqid(),
 344              'accepted_types'=>'*',
 345              'return_types'=>FILE_INTERNAL,
 346              'context'=>$PAGE->context,
 347              'author'=>fullname($USER),
 348              'licenses'=>array()
 349              );
 350          if (!empty($CFG->licenses)) {
 351              $array = explode(',', $CFG->licenses);
 352              foreach ($array as $license) {
 353                  $l = new stdClass();
 354                  $l->shortname = $license;
 355                  $l->fullname = get_string($license, 'license');
 356                  $defaults['licenses'][] = $l;
 357              }
 358          }
 359          if (!empty($CFG->sitedefaultlicense)) {
 360              $defaults['defaultlicense'] = $CFG->sitedefaultlicense;
 361          }
 362          foreach ($defaults as $key=>$value) {
 363              // Using !isset() prevents us from overwriting falsey values with defaults (as empty() did).
 364              if (!isset($options->$key)) {
 365                  $options->$key = $value;
 366              }
 367          }
 368  
 369          $fs = get_file_storage();
 370  
 371          // initilise options, getting files in root path
 372          $this->options = file_get_drafarea_files($options->itemid, '/');
 373  
 374          // calculate file count
 375          $usercontext = context_user::instance($USER->id);
 376          $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
 377          $filecount = count($files);
 378          $this->options->filecount = $filecount;
 379  
 380          // copying other options
 381          foreach ($options as $name=>$value) {
 382              $this->options->$name = $value;
 383          }
 384  
 385          // calculate the maximum file size as minimum from what is specified in filepicker options,
 386          // course options, global configuration and php settings
 387          $coursebytes = $maxbytes = 0;
 388          list($context, $course, $cm) = get_context_info_array($this->options->context->id);
 389          if (is_object($course)) {
 390              $coursebytes = $course->maxbytes;
 391          }
 392          if (!empty($this->options->maxbytes) && $this->options->maxbytes > 0) {
 393              $maxbytes = $this->options->maxbytes;
 394          }
 395          $this->options->maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursebytes, $maxbytes);
 396  
 397          // building file picker options
 398          $params = new stdClass();
 399          $params->accepted_types = $options->accepted_types;
 400          $params->return_types = $options->return_types;
 401          $params->context = $options->context;
 402          $params->env = 'filemanager';
 403          $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
 404          $filepicker_options = initialise_filepicker($params);
 405          $this->options->filepicker = $filepicker_options;
 406      }
 407  
 408      public function get_nonjsurl() {
 409          global $PAGE;
 410          return new moodle_url('/repository/draftfiles_manager.php', array(
 411              'env'=>'filemanager',
 412              'action'=>'browse',
 413              'itemid'=>$this->options->itemid,
 414              'subdirs'=>$this->options->subdirs,
 415              'maxbytes'=>$this->options->maxbytes,
 416              'areamaxbytes' => $this->options->areamaxbytes,
 417              'maxfiles'=>$this->options->maxfiles,
 418              'ctx_id'=>$PAGE->context->id, // TODO ?
 419              'course'=>$PAGE->course->id, // TODO ?
 420              'sesskey'=>sesskey(),
 421              ));
 422      }
 423  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1