"MDL-15405, data module to use filemanager"
authorDongsheng Cai <unoter@gmail.com>
Fri, 5 Mar 2010 05:37:09 +0000 (05:37 +0000)
committerDongsheng Cai <unoter@gmail.com>
Fri, 5 Mar 2010 05:37:09 +0000 (05:37 +0000)
lib/form/filemanager.js
mod/data/data.js
mod/data/edit.php
mod/data/field/file/field.class.php

index c45b6dd..fb5a115 100644 (file)
@@ -479,6 +479,15 @@ M.form_filemanager.init = function(Y, options) {
                             if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) {
                                 var button_addfile  = Y.one("#btnadd-"+scope.client_id);
                                 button_addfile.setStyle('display', 'inline');
+                                button_addfile.on('click', function(e) {
+                                    var options = scope.filepicker_options;
+                                    options.formcallback = scope.filepicker_callback;
+                                    // XXX: magic here, to let filepicker use filemanager scope
+                                    options.magicscope = scope;
+                                    options.savepath = scope.currentpath;
+                                    console.info(options);
+                                    M.core_filepicker.show(Y, options);
+                                }, this);
                             }
                         }
                     });
index f66c8a8..68fa0eb 100644 (file)
@@ -35,4 +35,25 @@ function showHideAdvSearch(checked) {
             }
         }
     }
-}
\ No newline at end of file
+}
+
+M.data_filepicker = {};
+
+M.data_filepicker.init = function(Y, options) {
+    options.formcallback = M.data_filepicker.callback;
+    if (!M.core_filepicker.instances[options.client_id]) {
+        M.core_filepicker.init(Y, options); 
+    }
+    Y.on('click', function(e, client_id) {
+        e.preventDefault();
+        M.core_filepicker.instances[client_id].show();
+    }, '#filepicker-button-'+options.client_id, null, options.client_id);
+
+};
+
+M.data_filepicker.callback = function (params) {
+    console.info(params);
+    var html = '<a href="'+params['url']+'">'+params['file']+'</a>';
+    // TODO: support delete the draft file
+    document.getElementById('file_info_'+params['client_id']).innerHTML = html;
+}
index 381857e..95e41ae 100755 (executable)
@@ -120,6 +120,14 @@ if ($data->jstemplate) {
     $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
 }
 
+$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
+
+foreach ($possiblefields as $field) {
+    if ($field->type == 'file') {
+        require_once($CFG->dirroot.'/repository/lib.php');
+        break;
+    }
+}
 
 /// Print the page header
 $strdata = get_string('modulenameplural','data');
index 8719052..bca9ba5 100755 (executable)
@@ -26,43 +26,149 @@ class data_field_file extends data_field_base {
     var $type = 'file';
 
     function display_add_field($recordid=0) {
-        global $CFG, $DB, $OUTPUT;
+        global $CFG, $DB, $OUTPUT, $PAGE, $USER;
 
         $file        = false;
         $content     = false;
         $displayname = '';
         $fs = get_file_storage();
+        $context = $PAGE->context;
+        $itemid = null;
+
+        // editing an existing database entry
         if ($recordid){
             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
+
+                file_prepare_draft_area($itemid, $this->context->id, 'data_content', $content->id);
+
                 if (!empty($content->content)) {
                     if ($file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) {
+                        // move to draft
+
+
+                        $fs = get_file_storage();
+                        $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+                        if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $itemid, 'id DESC', false)) {
+                            return false;
+                        }
+                        $file = reset($files);
                         if (empty($content->content1)) {
-                            $displayname = $file->get_filename();
+                            // Print icon if file already exists
+                            $browser = get_file_browser();
+                            $src     = file_encode_url($CFG->wwwroot.'/draftfile.php/', $usercontext->id.'/user_draft/'.$itemid.'/'.$file->get_filename());
+                            $displayname = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" class="icon" alt="'.$file->get_mimetype().'" />'. '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
+
+                            $fs->delete_area_files($this->context->id, 'data_content', $content->id);
                         } else {
-                            $displayname = $content->content1;
+                            $displayname = 'no file added';
                         }
                     }
                 }
             }
+        } else {
+            $itemid = file_get_unused_draft_itemid();
         }
 
-        $str = '<div title="'.s($this->field->description).'">';
+        $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>';
-        $str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" value="fakevalue" />';
-        $str .= get_string('file','data').' <input type="file" name ="field_'.$this->field->id.'" id="field_'.
-                            $this->field->id.'" title="'.s($this->field->description).'" /><br />';
-        $str .= get_string('optionalfilename','data').' <input type="text" name="field_'.$this->field->id.'_filename"
-                            id="field_'.$this->field->id.'_filename" value="'.s($displayname).'" /><br />';
-        //$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />';
+        // 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;
+
         $str .= '</fieldset>';
         $str .= '</div>';
-        if ($file) {
-            // Print icon if file already exists
-            $browser = get_file_browser();
-            $src     = file_encode_url($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
-            $str .= '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" class="icon" alt="'.$file->get_mimetype().'" />'.
-                    '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
-        }
+
+        // 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);
+
+        $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;
+        // store filepicker options
+        $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);
+
+        // 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}"));
+
         return $str;
     }
 
@@ -101,7 +207,7 @@ class data_field_file extends data_field_base {
     }
 
     function display_browse_field($recordid, $template) {
-        global $CFG, $DB;
+        global $CFG, $DB, $OUTPUT;
 
         if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
             return '';
@@ -129,7 +235,7 @@ class data_field_file extends data_field_base {
 
     // content: "a##b" where a is the file name, b is the display name
     function update_content($recordid, $value, $name) {
-        global $CFG, $DB;
+        global $CFG, $DB, $USER;
 
         if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
         // Quickly make one now!
@@ -140,33 +246,31 @@ class data_field_file extends data_field_base {
             $content = $DB->get_record('data_content', array('id'=>$id));
         }
 
-        $names = explode('_', $name);
-        switch ($names[2]) {
-            case 'file':
-                // file just uploaded
-                $tmpfile = $_FILES[$names[0].'_'.$names[1]];
-                $filename = $tmpfile['name'];
-                $pathanme = $tmpfile['tmp_name'];
-                if ($filename){
-                    $fs = get_file_storage();
-                    // TODO: uploaded file processing will be in file picker ;-)
-                    $fs->delete_area_files($this->context->id, 'data_content', $content->id);
-                    $file_record = array('contextid'=>$this->context->id, 'filearea'=>'data_content', 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename);
-                    if ($file = $fs->create_file_from_pathname($file_record, $pathanme)) {
-                        $content->content = $file->get_filename();
-                        $DB->update_record('data_content', $content);
-                    }
-                }
-                break;
+        $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+        $fs = get_file_storage();
+        $files = $fs->get_area_files($usercontext->id, 'user_draft', $value);
+
+        if (count($files)<2) {
+            // no file
+        } else {
+            $count = 0;
+            foreach ($files as $draftfile) {
+                $file_record = array('contextid'=>$this->context->id, 'itemid'=>$content->id, 'filepath'=>'/', 'filearea'=>'data_content');
+                if (!$draftfile->is_directory()) {
+                    $file_record['filename'] = $draftfile->get_filename();
 
-            case 'filename':
-                // only changing alt tag
-                $content->content1 = clean_param($value, PARAM_NOTAGS);
-                $DB->update_record('data_content', $content);
-                break;
+                    $content->content = $draftfile->get_filename();
 
-            default:
-                break;
+                    $fs->create_file_from_storedfile($file_record, $draftfile);
+                    $DB->update_record('data_content', $content);
+
+                    if ($count > 0) {
+                        break;
+                    } else {
+                        $count++;
+                    }
+                }
+            }
         }
     }