Skip to content

Commit

Permalink
MDL-15906 filepicker support in all file functions in formslib
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 7, 2008
1 parent 96fef79 commit 4287fc0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 55 deletions.
28 changes: 16 additions & 12 deletions lib/form/filepicker.php
Expand Up @@ -12,16 +12,14 @@
* @since Moodle 2.0
* @access public
*/
class MoodleQuickForm_filepicker extends HTML_QuickForm_button
{
var $_filearea;
class MoodleQuickForm_filepicker extends HTML_QuickForm_button {
var $_helpbutton='';
function MoodleQuickForm_filepicker($elementName=null, $value=null, $filearea=null, $attributes=null) {

function MoodleQuickForm_filepicker($elementName=null, $value=null, $attributes=null) {
HTML_QuickForm_input::HTML_QuickForm_input($elementName, $value, $attributes); // Set label cause button doesn't
parent::HTML_QuickForm_button($elementName, $value, $attributes);
$this->_filearea = $filearea;
}

function setHelpButton($helpbuttonargs, $function='helpbutton'){
if (!is_array($helpbuttonargs)){
$helpbuttonargs=array($helpbuttonargs);
Expand All @@ -36,33 +34,39 @@ function setHelpButton($helpbuttonargs, $function='helpbutton'){
}
$this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
}

function getHelpButton(){
return $this->_helpbutton;
}

function getElementTemplateType(){
if ($this->_flagFrozen){
return 'nodisplay';
} else {
return 'default';
}
}

function toHtml() {
global $CFG, $COURSE;
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
} else {
$strsaved = get_string('filesaved', 'repository');
if(empty($COURSE->context)) {

if (empty($COURSE->context)) {
$ctx = get_context_instance(CONTEXT_SYSTEM);
} else {
$ctx = $COURSE->context;
}
$ret = repository_get_client($ctx);

$suffix = $ret['suffix'];
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];

$str = $this->_getTabs();
$str .= '<input type="hidden" value="" name="'.$this->_attributes['name'].'" id="'.$this->_attributes['id'].'_'.$suffix.'" />';
$id = $this->_attributes['id'];
$filearea = $this->_filearea;
$str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'_'.$suffix.'" value="" />';
$str .= <<<EOD
<script type="text/javascript">
function updatefile_$suffix(str){
Expand All @@ -74,16 +78,16 @@ function callpicker_$suffix(){
picker.id = 'file-picker-$suffix';
document.body.appendChild(picker);
var el=document.getElementById('${id}_${suffix}');
openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix, 'filearea':"${filearea}"})
openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix})
}
</script>
EOD;
// $this->_getAttrString($this->_attributes);
$str .= "<input name=\"filearea\" value=\"$filearea\" type=\"hidden\">\n";
$str .= '<input value ="'.get_string('openpicker', 'repository').'" type="button" onclick=\'callpicker_'.$suffix.'()\' />'.'<span id="repo_info_'.$suffix.'" style="color:green"></span>'.$ret['css'].$ret['js'];
return $str;
}
}

function exportValue(&$submitValues, $assoc = false) {
return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
}
Expand Down
152 changes: 109 additions & 43 deletions lib/formslib.php
Expand Up @@ -187,7 +187,7 @@ function _process_submission($method) {
}

/**
* Internal method. Validates all uploaded files.
* Internal method. Validates all old-style uploaded files.
*/
function _validate_files(&$files) {
global $CFG, $COURSE;
Expand Down Expand Up @@ -388,7 +388,7 @@ function is_validated() {
$file_val = false;
}

$data = $mform->exportValues(null, false);
$data = $mform->exportValues();
$moodle_val = $this->validation($data, $files);
if ((is_array($moodle_val) && count($moodle_val)!==0)) {
// non-empty array means errors
Expand Down Expand Up @@ -487,6 +487,8 @@ function save_files($destination) {
* @return mixed false in case of failure, string if ok
*/
function get_new_filename($elname=null) {
global $USER;

if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}
Expand All @@ -498,6 +500,28 @@ function get_new_filename($elname=null) {
reset($_FILES);
$elname = key($_FILES);
}

if (empty($elname)) {
return false;
}

$element = $this->_form->getElement($elname);

if ($element instanceof MoodleQuickForm_filepicker) {
$values = $this->_form->exportValues($elname);
if (empty($values[$elname])) {
return false;
}
$draftid = $values[$elname];
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_USER, $USER->id);
if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
return false;
}
$file = reset($files);
return $file->get_filename();
}

if (!isset($_FILES[$elname])) {
return false;
}
Expand All @@ -513,11 +537,9 @@ function get_new_filename($elname=null) {
* @return bool success
*/
function save_file($elname, $pathname, $override=false) {
if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}
global $USER;

if (!isset($_FILES[$elname])) {
if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}

Expand All @@ -530,11 +552,29 @@ function save_file($elname, $pathname, $override=false) {
return false;
}
}
if (!copy($_FILES[$elname]['tmp_name'], $pathname)) {
return false;

$element = $this->_form->getElement($elname);

if ($element instanceof MoodleQuickForm_filepicker) {
$values = $this->_form->exportValues($elname);
if (empty($values[$elname])) {
return false;
}
$draftid = $values[$elname];
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_USER, $USER->id);
if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
return false;
}
$file = reset($files);

return $file->copy_content_to($pathname);

} else if (isset($_FILES[$elname])) {
return copy($_FILES[$elname]['tmp_name'], $pathname);
}

return true;
return false;
}

/**
Expand All @@ -550,7 +590,6 @@ function save_file($elname, $pathname, $override=false) {
*/
function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $newfilepath='/',
$newfilename=null, $overwrite=false, $newuserid=null) {

global $USER;

if (!$this->is_submitted() or !$this->is_validated()) {
Expand All @@ -561,43 +600,50 @@ function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $new
$newuserid = $USER->id;
}

if (isset($_FILES[$elname])) {
$filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;
$element = $this->_form->getElement($elname);
$fs = get_file_storage();

$fs = get_file_storage();
if ($element instanceof MoodleQuickForm_filepicker) {
$values = $this->_form->exportValues($elname);
if (empty($values[$elname])) {
return false;
}
$draftid = $values[$elname];
$context = get_context_instance(CONTEXT_USER, $USER->id);
if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
return false;
}
$file = reset($files);
if (is_null($newfilename)) {
$newfilename = $file->get_filename();
}

if ($file = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
if ($overwrite) {
$file->delete();
} else {
return false;
if ($overwrite) {
if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
if (!$oldfile->delete()) {
return false;
}
}
}

$file_record = new object();
$file_record->contextid = $newcontextid;
$file_record->filearea = $newfilearea;
$file_record->itemid = $newitemid;
$file_record->filepath = $newfilepath;
$file_record->filename = $newfilename;
$file_record->userid = $newuserid;
$file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
return $fs->create_file_from_storedfile($file_record, $file);

return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);

} else { // We check if the file has been uploaded already into the user's draft area

$values = $this->get_data();

if (!empty($values->$elname)) {

$itemid = $values->$elname;

$fs = get_file_storage();

$newfiles = $fs->move_draft_to_final($itemid, $newcontextid, $newfilearea, $newitemid, $newfilepath, $overwrite);
} else if (isset($_FILES[$elname])) {
$filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;

return array_pop($newfiles);
if ($overwrite) {
if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
if (!$oldfile->delete()) {
return false;
}
}
}

$file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
}

return false;
Expand All @@ -609,15 +655,34 @@ function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $new
* @return mixed false in case of failure, string if ok
*/
function get_file_content($elname) {
global $USER;

if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}

if (!isset($_FILES[$elname])) {
return false;
$element = $this->_form->getElement($elname);

if ($element instanceof MoodleQuickForm_filepicker) {
$values = $this->_form->exportValues($elname);
if (empty($values[$elname])) {
return false;
}
$draftid = $values[$elname];
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_USER, $USER->id);
if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
return false;
}
$file = reset($files);

return $file->get_content();

} else if (isset($_FILES[$elname])) {
return file_get_contents($_FILES[$elname]['tmp_name']);
}

return file_get_contents($_FILES[$elname]['tmp_name']);
return false;
}

/**
Expand Down Expand Up @@ -1229,7 +1294,8 @@ function exportValues($elementList = null){
if (PEAR::isError($value)) {
return $value;
}
$unfiltered[$elementName] = $value;
//oh, stock QuickFOrm was returning array of arrays!
$unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
}
}

Expand Down

0 comments on commit 4287fc0

Please sign in to comment.