PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/form/filemanager.php

https://github.com/kpike/moodle
PHP | 340 lines | 230 code | 52 blank | 58 comment | 16 complexity | 98b9d38c94ad31eb50bba8164afcb0f4 MD5 | raw file
  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. * File manager
  18. *
  19. * @package moodlecore
  20. * @subpackage file
  21. * @copyright 1999 onwards Dongsheng Cai <dongsheng@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. global $CFG;
  25. require_once('HTML/QuickForm/element.php');
  26. require_once($CFG->dirroot.'/lib/filelib.php');
  27. require_once($CFG->dirroot.'/repository/lib.php');
  28. class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
  29. public $_helpbutton = '';
  30. protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
  31. function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  32. global $CFG, $PAGE;
  33. $options = (array)$options;
  34. foreach ($options as $name=>$value) {
  35. if (array_key_exists($name, $this->_options)) {
  36. $this->_options[$name] = $value;
  37. }
  38. }
  39. if (!empty($options['maxbytes'])) {
  40. $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
  41. }
  42. parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  43. }
  44. function setName($name) {
  45. $this->updateAttributes(array('name'=>$name));
  46. }
  47. function getName() {
  48. return $this->getAttribute('name');
  49. }
  50. function setValue($value) {
  51. $this->updateAttributes(array('value'=>$value));
  52. }
  53. function getValue() {
  54. return $this->getAttribute('value');
  55. }
  56. function getMaxbytes() {
  57. return $this->_options['maxbytes'];
  58. }
  59. function setMaxbytes($maxbytes) {
  60. global $CFG;
  61. $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
  62. }
  63. function getSubdirs() {
  64. return $this->_options['subdirs'];
  65. }
  66. function setSubdirs($allow) {
  67. $this->_options['subdirs'] = $allow;
  68. }
  69. function getMaxfiles() {
  70. return $this->_options['maxfiles'];
  71. }
  72. function setMaxfiles($num) {
  73. $this->_options['maxfiles'] = $num;
  74. }
  75. function setHelpButton($helpbuttonargs, $function='helpbutton'){
  76. debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
  77. }
  78. function getHelpButton() {
  79. return $this->_helpbutton;
  80. }
  81. function getElementTemplateType() {
  82. if ($this->_flagFrozen){
  83. return 'nodisplay';
  84. } else {
  85. return 'default';
  86. }
  87. }
  88. function toHtml() {
  89. global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
  90. require_once("$CFG->dirroot/repository/lib.php");
  91. // security - never ever allow guest/not logged in user to upload anything or use this element!
  92. if (isguestuser() or !isloggedin()) {
  93. print_error('noguest');
  94. }
  95. if ($this->_flagFrozen) {
  96. return $this->getFrozenHtml();
  97. }
  98. $id = $this->_attributes['id'];
  99. $elname = $this->_attributes['name'];
  100. $subdirs = $this->_options['subdirs'];
  101. $maxbytes = $this->_options['maxbytes'];
  102. $draftitemid = $this->getValue();
  103. $accepted_types = $this->_options['accepted_types'];
  104. if (empty($draftitemid)) {
  105. // no existing area info provided - let's use fresh new draft area
  106. require_once("$CFG->libdir/filelib.php");
  107. $this->setValue(file_get_unused_draft_itemid());
  108. $draftitemid = $this->getValue();
  109. }
  110. $client_id = uniqid();
  111. // filemanager options
  112. $options = new stdClass();
  113. $options->mainfile = $this->_options['mainfile'];
  114. $options->maxbytes = $this->_options['maxbytes'];
  115. $options->maxfiles = $this->getMaxfiles();
  116. $options->client_id = $client_id;
  117. $options->itemid = $draftitemid;
  118. $options->subdirs = $this->_options['subdirs'];
  119. $options->target = $id;
  120. $options->accepted_types = $accepted_types;
  121. $options->return_types = FILE_INTERNAL;
  122. $options->context = $PAGE->context;
  123. $html = $this->_getTabs();
  124. $html .= form_filemanager_render($options);
  125. $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
  126. // label element needs 'for' attribute work
  127. $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
  128. return $html;
  129. }
  130. }
  131. /**
  132. * Data structure representing a file manager.
  133. *
  134. * @copyright 2010 Dongsheng Cai
  135. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  136. * @since Moodle 2.0
  137. */
  138. class form_filemanaer_x {
  139. //TODO: do not use this abstraction (skodak)
  140. public $options;
  141. public function __construct(stdClass $options) {
  142. global $CFG, $USER, $PAGE;
  143. require_once($CFG->dirroot. '/repository/lib.php');
  144. $defaults = array(
  145. 'maxbytes'=>-1,
  146. 'maxfiles'=>-1,
  147. 'itemid'=>0,
  148. 'subdirs'=>0,
  149. 'client_id'=>uniqid(),
  150. 'accepted_types'=>'*',
  151. 'return_types'=>FILE_INTERNAL,
  152. 'context'=>$PAGE->context
  153. );
  154. foreach ($defaults as $key=>$value) {
  155. if (empty($options->$key)) {
  156. $options->$key = $value;
  157. }
  158. }
  159. $fs = get_file_storage();
  160. // initilise options, getting files in root path
  161. $this->options = file_get_drafarea_files($options->itemid, '/');
  162. // calculate file count
  163. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  164. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
  165. $filecount = count($files);
  166. $this->options->filecount = $filecount;
  167. // copying other options
  168. foreach ($options as $name=>$value) {
  169. $this->options->$name = $value;
  170. }
  171. // building file picker options
  172. $params = new stdClass();
  173. $params->accepted_types = $options->accepted_types;
  174. $params->return_types = $options->return_types;
  175. $params->context = $options->context;
  176. $params->env = 'filemanager';
  177. $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
  178. $filepicker_options = initialise_filepicker($params);
  179. $this->options->filepicker = $filepicker_options;
  180. }
  181. }
  182. /**
  183. * Print the file manager
  184. *
  185. * <pre>
  186. * $OUTPUT->file_manager($options);
  187. * </pre>
  188. *
  189. * @param array $options associative array with file manager options
  190. * options are:
  191. * maxbytes=>-1,
  192. * maxfiles=>-1,
  193. * itemid=>0,
  194. * subdirs=>false,
  195. * client_id=>uniqid(),
  196. * acepted_types=>'*',
  197. * return_types=>FILE_INTERNAL,
  198. * context=>$PAGE->context
  199. * @return string HTML fragment
  200. */
  201. function form_filemanager_render($options) {
  202. global $CFG, $OUTPUT, $PAGE;
  203. $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex
  204. static $filemanagertemplateloaded;
  205. $html = '';
  206. $options = $fm->options;
  207. $straddfile = get_string('addfile', 'repository');
  208. $strmakedir = get_string('makeafolder', 'moodle');
  209. $strdownload = get_string('downloadfolder', 'repository');
  210. $strloading = get_string('loading', 'repository');
  211. $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
  212. $client_id = $options->client_id;
  213. $itemid = $options->itemid;
  214. list($context, $course, $cm) = get_context_info_array($options->context->id);
  215. if (is_object($course)) {
  216. $course_maxbytes = $course->maxbytes;
  217. } else {
  218. $course_maxbytes = $CFG->maxbytes;
  219. }
  220. if ($options->maxbytes == -1 || empty($options->maxbytes)) {
  221. $options->maxbytes = $CFG->maxbytes;
  222. }
  223. if (empty($options->filecount)) {
  224. $extra = ' style="display:none"';
  225. } else {
  226. $extra = '';
  227. }
  228. $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
  229. $html .= <<<FMHTML
  230. <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
  231. $icon_progress
  232. </div>
  233. <div id="filemanager-wrapper-{$client_id}" style="display:none">
  234. <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
  235. <div class="filemanager-toolbar">
  236. <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" />
  237. <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" />
  238. <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" />
  239. <span> $maxsize </span>
  240. </div>
  241. <div class="filemanager-container" id="filemanager-{$client_id}">
  242. <ul id="draftfiles-{$client_id}" class="fm-filelist">
  243. <li>Loading...</li>
  244. </ul>
  245. </div>
  246. </div>
  247. <div class='clearer'></div>
  248. FMHTML;
  249. if (empty($filemanagertemplateloaded)) {
  250. $filemanagertemplateloaded = true;
  251. $html .= <<<FMHTML
  252. <div id="fm-template" style="display:none">___fullname___ ___action___</div>
  253. FMHTML;
  254. }
  255. $module = array(
  256. 'name'=>'form_filemanager',
  257. 'fullpath'=>'/lib/form/filemanager.js',
  258. 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
  259. 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
  260. array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
  261. array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
  262. array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
  263. array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
  264. array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
  265. array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
  266. array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
  267. array('moving', 'repository'), array('files', 'moodle')
  268. )
  269. );
  270. $PAGE->requires->js_module($module);
  271. $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
  272. // non javascript file manager
  273. $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
  274. 'env'=>'filemanager',
  275. 'action'=>'browse',
  276. 'itemid'=>$itemid,
  277. 'subdirs'=>$options->subdirs,
  278. 'maxbytes'=>$options->maxbytes,
  279. 'maxfiles'=>$options->maxfiles,
  280. 'ctx_id'=>$PAGE->context->id,
  281. 'course'=>$PAGE->course->id,
  282. 'sesskey'=>sesskey(),
  283. ));
  284. $html .= '<noscript>';
  285. $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
  286. $html .= '</noscript>';
  287. return $html;
  288. }