MDL-16493, database module use filepicker to choose file/pciture, url field use file...
authorDongsheng Cai <unoter@gmail.com>
Mon, 24 May 2010 09:05:40 +0000 (09:05 +0000)
committerDongsheng Cai <unoter@gmail.com>
Mon, 24 May 2010 09:05:40 +0000 (09:05 +0000)
lib/outputcomponents.php
mod/data/data.js
mod/data/field/file/field.class.php
mod/data/field/picture/field.class.php
mod/data/field/url/field.class.php

index ab4c422..bc34153 100644 (file)
@@ -68,13 +68,18 @@ class file_picker implements renderable {
         if (!empty($options->itemid)) {
             $fs = get_file_storage();
             $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
-            if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $options->itemid, 'id DESC', false)) {
-                $file = reset($files);
+            if (empty($options->filename)) {
+                if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $options->itemid, 'id DESC', false)) {
+                    $file = reset($files);
+                }
+            } else {
+                $file = $fs->get_file($usercontext->id, 'user_draft', $options->itemid, $options->filepath, $options->filename);
+            }
+            if (!empty($file)) {
                 $options->currentfile = html_writer::link(file_encode_url($CFG->wwwroot.'/draftfile.php/', $usercontext->id.'/user_draft/'.$file->get_itemid().'/'.$file->get_filename()), $file->get_filename());
             }
         }
 
-
         // initilise options, getting files in root path
         $this->options = initialise_filepicker($options);
 
index 68fa0eb..b7c3db3 100644 (file)
@@ -39,6 +39,15 @@ function showHideAdvSearch(checked) {
 
 M.data_filepicker = {};
 
+
+M.data_filepicker.callback = function(params) {
+    var html = '<a href="'+params['url']+'">'+params['file']+'</a>';
+    document.getElementById('file_info_'+params['client_id']).innerHTML = html;
+};
+
+/**
+ * This fucntion is called for each file picker on page.
+ */
 M.data_filepicker.init = function(Y, options) {
     options.formcallback = M.data_filepicker.callback;
     if (!M.core_filepicker.instances[options.client_id]) {
@@ -49,11 +58,62 @@ M.data_filepicker.init = function(Y, options) {
         M.core_filepicker.instances[client_id].show();
     }, '#filepicker-button-'+options.client_id, null, options.client_id);
 
+    var item = document.getElementById('nonjs-filepicker-'+options.client_id);
+    if (item) {
+        item.parentNode.removeChild(item);
+    }
+    item = document.getElementById('filepicker-wrapper-'+options.client_id);
+    if (item) {
+        item.style.display = '';
+    }
 };
 
-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;
+M.data_urlpicker = {};
+
+M.data_urlpicker.init = function(Y, options) {
+    this.formelementid = options.formelementid;
+    options.formcallback = M.data_urlpicker.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_urlpicker.callback = function (params) {
+    document.getElementById(M.data_urlpicker.formelementid).value = params.url;
 }
+
+M.data_imagepicker = {};
+
+
+M.data_imagepicker.callback = function(params) {
+    var html = '<a href="'+params['url']+'"><img src="'+params['url']+'" /> '+params['file']+'</a>';
+    document.getElementById('file_info_'+params['client_id']).innerHTML = html;
+};
+
+/**
+ * This fucntion is called for each file picker on page.
+ */
+M.data_imagepicker.init = function(Y, options) {
+    options.formcallback = M.data_imagepicker.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);
+
+    var item = document.getElementById('nonjs-filepicker-'+options.client_id);
+    if (item) {
+        item.parentNode.removeChild(item);
+    }
+    item = document.getElementById('filepicker-wrapper-'+options.client_id);
+    if (item) {
+        item.style.display = '';
+    }
+};
index f1671bd..0a7f38a 100755 (executable)
@@ -43,22 +43,16 @@ class data_field_file extends data_field_base {
 
                 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)) {
                             // 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 = 'no file added';
                         }
@@ -77,20 +71,23 @@ class data_field_file extends data_field_base {
         // itemid element
         $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
 
-        $filemanager_options = new stdclass;
-        $filemanager_options->maxbytes = $this->field->param3;
-        $filemanager_options->maxfiles = 1;
-        $filemanager_options->filearea = 'user_draft';
-        $filemanager_options->itemid   = $itemid;
-        $filemanager_options->subdirs  = 0;
-        $filemanager_options->accepted_types = '*';
-        $filemanager_options->return_types = FILE_INTERNAL;
-        $filemanager_options->context  = $PAGE->context;
-        $html .= $OUTPUT->file_manager($filemanager_options);
+        $options = new stdclass;
+        $options->maxbytes  = $this->field->param3;
+        $options->itemid    = $itemid;
+        $options->accepted_types = '*';
+        $options->return_types = FILE_INTERNAL;
+        $options->context = $PAGE->context;
+
+        $fp = new file_picker($options);
+        // print out file picker
+        $html .= $OUTPUT->render($fp);
 
         $html .= '</fieldset>';
         $html .= '</div>';
 
+        $module = array('name'=>'data_filepicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
+        $PAGE->requires->js_init_call('M.data_filepicker.init', array($fp->options), true, $module);
+
         return $html;
     }
 
@@ -158,8 +155,10 @@ 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, $USER;
+        $fs = get_file_storage();
 
         if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
+
         // Quickly make one now!
             $content = new object();
             $content->fieldid  = $this->field->id;
@@ -168,8 +167,10 @@ class data_field_file extends data_field_base {
             $content = $DB->get_record('data_content', array('id'=>$id));
         }
 
+        // delete existing files
+        $fs->delete_area_files($this->context->id, 'data_content', $content->id);
+
         $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) {
@@ -196,16 +197,6 @@ class data_field_file extends data_field_base {
         }
     }
 
-    function notemptyfield($value, $name) {
-        $names = explode('_',$name);
-        if ($names[2] == 'file') {
-            $filename = $_FILES[$names[0].'_'.$names[1]];
-            return !empty($filename['name']);
-            // if there's a file in $_FILES, not empty
-        }
-        return false;
-    }
-
     function text_export_supported() {
         return false;
     }
index d515d0d..db820c6 100755 (executable)
@@ -28,43 +28,76 @@ class data_field_picture extends data_field_base {
     var $previewheight = 50;
 
     function display_add_field($recordid=0) {
-        global $CFG, $DB;
+        global $CFG, $DB, $OUTPUT, $USER, $PAGE;
 
         $file        = false;
         $content     = false;
         $displayname = '';
         $alttext     = '';
+        $itemid = null;
         $fs = get_file_storage();
-        if ($recordid){
+
+        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)) {
+                        $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+                        if (!$files = $fs->get_area_files($usercontext->id, 'user_draft', $itemid, 'id DESC', false)) {
+                            return false;
+                        }
+                        if ($thumbfile = $fs->get_file($usercontext->id, 'user_draft', $itemid, '/', 'thumb_'.$content->content)) {
+                            $thumbfile->delete();
+                        }
                         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>';
+
                         } else {
-                            $displayname = $content->content1;
+                            $displayname = get_string('nofilesattached', 'repository');
                         }
                     }
                 }
                 $alttext = $content->content1;
             }
+        } else {
+            $itemid = file_get_unused_draft_itemid();
         }
 
         $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" id="field_'.$this->field->id.'_file"  value="fakevalue" />';
-        $str .= '<label for="field_'.$this->field->id.'">'.get_string('picture','data'). '</label>&nbsp;<input type="file" name ="field_'.$this->field->id.'" id="field_'.$this->field->id.'" /><br />';
-        $str .= '<label for="field_'.$this->field->id.'_alttext">'.get_string('alttext','data') .'</label>&nbsp;<input type="text" name="field_'
-                .$this->field->id.'_alttext" id="field_'.$this->field->id.'_alttext" value="'.s($alttext).'" /><br />';
-        //$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />';
         if ($file) {
-            $browser = get_file_browser();
-            $src     = file_encode_url($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
+            $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
             $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />';
         }
+
+        $options = new stdclass;
+        $options->maxbytes  = $this->field->param3;
+        $options->itemid    = $itemid;
+        $options->accepted_types = array('image');
+        $options->return_types = FILE_INTERNAL;
+        $options->context = $PAGE->context;
+        if (!empty($file)) {
+            $options->filename = $file->get_filename();
+            $options->filepath = '/';
+        }
+        $fp = new file_picker($options);
+        $str .= $OUTPUT->render($fp);
+
+
+        $str .= '<div class="mdl-left">';
+        $str .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
+        $str .= '<label for="field_'.$this->field->id.'_alttext">'.get_string('alttext','data') .'</label>&nbsp;<input type="text" name="field_'
+                .$this->field->id.'_alttext" id="field_'.$this->field->id.'_alttext" value="'.s($alttext).'" />';
+        $str .= '</div>';
+
         $str .= '</fieldset>';
         $str .= '</div>';
 
+        $module = array('name'=>'data_imagepicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
+        $PAGE->requires->js_init_call('M.data_imagepicker.init', array($fp->options), true, $module);
         return $str;
     }
 
@@ -170,7 +203,7 @@ class data_field_picture extends data_field_base {
     }
 
     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!
@@ -184,22 +217,34 @@ class data_field_picture extends data_field_base {
         $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);
-                        // Regenerate the thumbnail
-                        $this->update_thumbnail($content, $file);
+                $fs = get_file_storage();
+                $fs->delete_area_files($this->context->id, 'data_content', $content->id);
+                $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+                $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();
+
+                            $content->content = $draftfile->get_filename();
+
+                            $file = $fs->create_file_from_storedfile($file_record, $draftfile);
+                            $DB->update_record('data_content', $content);
+                            $this->update_thumbnail($content, $file);
+
+                            if ($count > 0) {
+                                break;
+                            } else {
+                                $count++;
+                            }
+                        }
                     }
                 }
+
                 break;
 
             case 'alttext':
@@ -230,16 +275,6 @@ class data_field_picture extends data_field_base {
         }
     }
 
-    function notemptyfield($value, $name) {
-        $names = explode('_',$name);
-        if ($names[2] == 'file') {
-            $filename = $_FILES[$names[0].'_'.$names[1]];
-            return !empty($filename['name']);
-            // if there's a file in $_FILES, not empty
-        }
-        return false;
-    }
-
     function text_export_supported() {
         return false;
     }
index 01542a4..686c9df 100755 (executable)
@@ -26,8 +26,9 @@ class data_field_url extends data_field_base {
     var $type = 'url';
 
     function display_add_field($recordid=0) {
-        global $CFG, $DB;
+        global $CFG, $DB, $OUTPUT, $PAGE;
 
+        $straddlink = get_string('choosealink', 'repository');
         $url = '';
         $text = '';
         if ($recordid) {
@@ -47,6 +48,24 @@ class data_field_url extends data_field_base {
             // Just the URL field
             $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="field_'.$this->field->id.'_0" value="'.s($url).'" size="60" />';
         }
+        $args = new stdclass;
+        $args->accepted_types = '*';
+        $args->return_types = FILE_EXTERNAL;
+        $args->context = $this->context;
+        $args->env = 'url';
+        $fp = new file_picker($args);
+        $options = $fp->options;
+        $options->formelementid = 'field_'.$this->field->id.'_0';
+
+        $str .= '<button id="filepicker-button-'.$options->client_id.'" style="display:none">'.$straddlink.'</button>';
+
+        // print out file picker
+        $str .= $OUTPUT->render($fp);
+
+        $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
+        $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module);
+        $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$options->client_id));
+
         $str .= '</div>';
         return $str;
     }