"MDL-15405, perform filemanager init tasks in one function"
authorDongsheng Cai <unoter@gmail.com>
Mon, 8 Mar 2010 03:28:22 +0000 (03:28 +0000)
committerDongsheng Cai <unoter@gmail.com>
Mon, 8 Mar 2010 03:28:22 +0000 (03:28 +0000)
lib/weblib.php
mod/data/field/file/field.class.php

index 267e951..36af768 100644 (file)
@@ -2707,6 +2707,90 @@ function print_maintenance_message() {
     die;
 }
 
+/**
+ * this function is used to print a filemanager
+ *
+ * @param $options array options to setup filemanager
+ * @param $return bool If true output is returned rather than printed out
+ * @return string
+ */
+function print_filemanager($options, $return = false) {
+    global $PAGE, $CFG;
+    // start to setup filemanager
+    // loading filemanager language string
+    $PAGE->requires->string_for_js('loading', 'repository');
+    $PAGE->requires->string_for_js('nomorefiles', 'repository');
+    $PAGE->requires->string_for_js('confirmdeletefile', 'repository');
+    $PAGE->requires->string_for_js('add', 'repository');
+    $PAGE->requires->string_for_js('accessiblefilepicker', 'repository');
+    $PAGE->requires->string_for_js('move', 'moodle');
+    $PAGE->requires->string_for_js('cancel', 'moodle');
+    $PAGE->requires->string_for_js('download', 'moodle');
+    $PAGE->requires->string_for_js('ok', 'moodle');
+    $PAGE->requires->string_for_js('emptylist', 'repository');
+    $PAGE->requires->string_for_js('entername', 'repository');
+    $PAGE->requires->string_for_js('enternewname', 'repository');
+    $PAGE->requires->string_for_js('zip', 'editor');
+    $PAGE->requires->string_for_js('unzip', 'moodle');
+    $PAGE->requires->string_for_js('rename', 'moodle');
+    $PAGE->requires->string_for_js('delete', 'moodle');
+    $PAGE->requires->string_for_js('setmainfile', 'resource');
+    $PAGE->requires->string_for_js('cannotdeletefile', 'error');
+    $PAGE->requires->string_for_js('confirmdeletefile', 'repository');
+    $PAGE->requires->string_for_js('nopathselected', 'repository');
+    $PAGE->requires->string_for_js('popupblockeddownload', 'repository');
+    $PAGE->requires->string_for_js('path', 'moodle');
+    // language strings
+    $straddfile  = get_string('add', 'repository') . '...';
+    $strmakedir  = get_string('makeafolder', 'moodle');
+    $strdownload  = get_string('downloadfolder', 'repository');
+
+    $client_id = $options->client_id;
+    $itemid = $options->itemid;
+
+    $html = '';
+
+    $html .= <<<FMHTML
+<div id="filemanager-wrapper-{$client_id}" style="display:none">
+    <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
+    <div class="filemanager-toolbar">
+        <button id="btnadd-{$client_id}" onclick="return false">{$straddfile}</button>
+        <button id="btncrt-{$client_id}" onclick="return false">{$strmakedir}</button>
+        <button id="btndwn-{$client_id}" onclick="return false">{$strdownload}</button>
+    </div>
+    <div class="filemanager-container" id="filemanager-{$client_id}">
+        <ul id="draftfiles-{$client_id}">
+            <li>Loading...</li>
+        </ul>
+    </div>
+</div>
+FMHTML;
+        // print out file entry template only one time
+    if (empty($CFG->filemanagertemplateloaded)) {
+        $CFG->filemanagertemplateloaded = true;
+        $html .= <<<FMHTML
+<div id="fm-template" style="display:none"><div class="fm-file-menu">___action___</div> <div class="fm-file-name">___fullname___</div></div>
+FMHTML;
+    }
+        $filemanagerurl = "$CFG->wwwroot/repository/filepicker.php?env=filemanager&amp;action=embedded&amp;itemid=$itemid&amp;subdirs=/&amp;maxbytes=$options->maxbytes&amp;ctx_id=".$PAGE->context->id.'&amp;course='.$PAGE->course->id;
+
+        $html .= <<<NONJS
+<div id="nonjs-filemanager-$client_id">
+<object type="text/html" data="$filemanagerurl" height="160" width="600" style="border:1px solid #000">Error</object>
+</div>
+NONJS;
+
+    $module = array('name'=>'form_filemanager', 'fullpath'=>'/lib/form/filemanager.js', 'requires' => array('core_filepicker', 'base', 'io', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'));
+    $PAGE->requires->js_module($module);
+    $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
+
+    if ($return) {
+        return $html;
+    } else {
+        echo $html;
+    }
+}
+
 /**
  * Adjust the list of allowed tags based on $CFG->allowobjectembed and user roles (admin)
  *
@@ -3398,4 +3482,4 @@ function create_ufo_inline($id, $args) {
     $jsoutput = html_writer::script('', $CFG->wwwroot.'/lib/ufo.js');
     $jsoutput .= html_writer::script(js_writer::function_call('M.util.create_UFO_object', array($id, $args)));
     return $jsoutput;
-}
\ No newline at end of file
+}
index ade44e6..18ca52f 100755 (executable)
@@ -72,111 +72,47 @@ class data_field_file extends data_field_base {
         $draftareainfo = file_get_draft_area_info($itemid);
         $filecount = $draftareainfo['filecount'];
 
-        // start to setup filemanager
-        // loading filemanager language string
-        $PAGE->requires->string_for_js('loading', 'repository');
-        $PAGE->requires->string_for_js('nomorefiles', 'repository');
-        $PAGE->requires->string_for_js('confirmdeletefile', 'repository');
-        $PAGE->requires->string_for_js('add', 'repository');
-        $PAGE->requires->string_for_js('accessiblefilepicker', 'repository');
-        $PAGE->requires->string_for_js('move', 'moodle');
-        $PAGE->requires->string_for_js('cancel', 'moodle');
-        $PAGE->requires->string_for_js('download', 'moodle');
-        $PAGE->requires->string_for_js('ok', 'moodle');
-        $PAGE->requires->string_for_js('emptylist', 'repository');
-        $PAGE->requires->string_for_js('entername', 'repository');
-        $PAGE->requires->string_for_js('enternewname', 'repository');
-        $PAGE->requires->string_for_js('zip', 'editor');
-        $PAGE->requires->string_for_js('unzip', 'moodle');
-        $PAGE->requires->string_for_js('rename', 'moodle');
-        $PAGE->requires->string_for_js('delete', 'moodle');
-        $PAGE->requires->string_for_js('setmainfile', 'resource');
-        $PAGE->requires->string_for_js('cannotdeletefile', 'error');
-        $PAGE->requires->string_for_js('confirmdeletefile', 'repository');
-        $PAGE->requires->string_for_js('nopathselected', 'repository');
-        $PAGE->requires->string_for_js('popupblockeddownload', 'repository');
-        $PAGE->requires->string_for_js('path', 'moodle');
 
         // create a unique id for filemanager
         $client_id = uniqid();
 
-        $str = '';
-        // print out file entry template only one time
-        if (empty($CFG->filemanagertemplateloaded)) {
-            $CFG->filemanagertemplateloaded = true;
-            $str .= <<<FMHTML
-<div id="fm-template" style="display:none"><div class="fm-file-menu">___action___</div> <div class="fm-file-name">___fullname___</div></div>
-FMHTML;
-        }
-        // database entry label
-        $str .= '<div title="'.s($this->field->description).'">';
-        $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
-        // itemid element
-        $str .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
-
-        // language strings
-        $straddfile  = get_string('add', 'repository') . '...';
-        $strmakedir  = get_string('makeafolder', 'moodle');
-        $strdownload  = get_string('downloadfolder', 'repository');
-
-        $str .= <<<FMHTML
-<div id="filemanager-wrapper-{$client_id}" style="display:none">
-    <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
-    <div class="filemanager-toolbar">
-        <button id="btnadd-{$client_id}" onclick="return false">{$straddfile}</button>
-        <button id="btncrt-{$client_id}" onclick="return false">{$strmakedir}</button>
-        <button id="btndwn-{$client_id}" onclick="return false">{$strdownload}</button>
-    </div>
-    <div class="filemanager-container" id="filemanager-{$client_id}">
-        <ul id="draftfiles-{$client_id}">
-            <li>Loading...</li>
-        </ul>
-    </div>
-</div>
-FMHTML;
-        $editorurl = "$CFG->wwwroot/repository/filepicker.php?env=filemanager&amp;action=embedded&amp;itemid=$itemid&amp;subdirs=/&amp;maxbytes=$options->maxbytes&amp;ctx_id=".$PAGE->context->id.'&amp;course='.$PAGE->course->id;
-
-        $str .= <<<NONJS
-<div id="nonjs-filemanager-$client_id">
-<object type="text/html" data="$editorurl" height="160" width="600" style="border:1px solid #000">Error</object>
-</div>
-NONJS;
-
-        $str .= '</fieldset>';
-        $str .= '</div>';
-
-        // need these arguments to filter repositories list
-        $args = new stdclass;
-        $args->accepted_types = '*';
-        $args->return_types = FILE_INTERNAL;
-        $args->context = $PAGE->context;
-
-        $filepicker_options = initialise_filepicker($args);
+        // parameters to filter repositories
+        $params = new stdclass;
+        $params->accepted_types = '*';
+        $params->return_types = FILE_INTERNAL;
+        $params->context = $PAGE->context;
+
+        $filepicker_options = initialise_filepicker($params);
 
         $filepicker_options->client_id = $client_id;
         $filepicker_options->env      = 'filemanager';
         $filepicker_options->itemid   = $itemid;
 
-        $options = file_get_draft_area_files($itemid);
-        $options->client_id = $client_id;
-        $options->filecount = $filecount;
-        $options->itemid    = $itemid;
-        $options->subdirs   = 0;
-        $options->maxbytes = -1;
-        $options->maxfiles = 1;
+        // get existing files
+        $filemanager_options = file_get_draft_area_files($itemid);
+        $filemanager_options->client_id = $client_id;
+        $filemanager_options->filecount = $filecount;
+        $filemanager_options->itemid    = $itemid;
+        $filemanager_options->subdirs   = 0;
+        $filemanager_options->maxbytes = $this->field->param3;
+        $filemanager_options->maxfiles = 1;
         // store filepicker options
-        $options->filepicker = $filepicker_options;
+        $filemanager_options->filepicker = $filepicker_options;
 
-        $module = array('name'=>'form_filemanager', 'fullpath'=>'/lib/form/filemanager.js', 'requires' => array('core_filepicker', 'base', 'io', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'));
-        $PAGE->requires->js_module($module);
-        $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
+        $html = '';
+        // database entry label
+        $html .= '<div title="'.s($this->field->description).'">';
+        $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
 
-        // destroy iframe, it is needed in non javascript only
-        $str .= $PAGE->requires->js_function_call('destroy_item', array("nonjs-filepicker-{$client_id}"));
-        // make filemanager buttons visible
-        $str .= $PAGE->requires->js_function_call('show_item', array("filepicker-wrapper-{$client_id}"));
+        // itemid element
+        $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
 
-        return $str;
+        $html .= print_filemanager($filemanager_options, true);
+
+        $html .= '</fieldset>';
+        $html .= '</div>';
+
+        return $html;
     }
 
     function display_search_field($value = '') {