Commit 923c4606 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Change filedownload block config to use filebrowser form element

parent 87348619
......@@ -103,11 +103,22 @@ class PluginBlocktypeFiledownload extends PluginBlocktype {
public static function instance_config_form($instance, $istemplate) {
$configdata = $instance->get('configdata');
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null, $istemplate),
// self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null, $istemplate),
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactids'])) ? $configdata['artefactids'] : null, $istemplate),
);
}
public static function instance_config_save($values) {
if (isset($values['filebrowser']['selected'])) {
$values['artefactids'] = $values['filebrowser']['selected'];
}
unset($values['filebrowser']);
return $values;
}
public static function artefactchooser_element($default=null, $istemplate=false) {
$element = array(
'name' => 'artefactids',
......@@ -137,6 +148,38 @@ class PluginBlocktypeFiledownload extends PluginBlocktype {
return ArtefactTypeFileBase::artefactchooser_get_file_data($artefact);
}
public static function filebrowser_element(&$instance, $default=null, $istemplate=false) {
$element = array(
'name' => 'filebrowser',
'type' => 'filebrowser',
'title' => get_string('Files', 'blocktype.file/filedownload'),
'folder' => param_integer('folder', 0),
'highlight' => null,
'browse' => true,
'page' => View::make_base_url(),
// 'page' => get_config('wwwroot') . 'view/blocks.php?id=' . $instance->get('view') . '&block=' . $instance->get('id'),
'config' => array(
'upload' => true,
'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
'createfolder' => false,
'edit' => false,
'select' => true,
'alwaysopen' => true,
),
'selectlistcallback' => array(
'name' => 'artefact_get_records_by_id',
'args' => array(empty($default) ? array() : $default),
),
);
if (!$istemplate) {
// You don't have to choose a file if this view is a template
$element['rules'] = array(
'required' => true,
);
}
return $element;
}
public static function default_copy_type() {
return 'full';
}
......
......@@ -34,7 +34,7 @@ defined('INTERNAL') || die();
* @return string The HTML for the element
*/
function pieform_element_filebrowser(Pieform $form, $element) {
global $USER;
global $USER, $_PIEFORM_FILEBROWSERS;
$smarty = smarty_core();
$group = $form->get_property('group');
$institution = $form->get_property('institution');
......@@ -56,7 +56,12 @@ function pieform_element_filebrowser(Pieform $form, $element) {
}
$config = array_map('intval', $element['config']);
if ($config['select']) {
$selected = $element['selectlistcallback']();
if (is_array($element['selectlistcallback'])) {
$selected = call_user_func_array($element['selectlistcallback']['name'], $element['selectlistcallback']['args']);
}
else {
$selected = $element['selectlistcallback']();
}
$smarty->assign('selectedlist', $selected);
$selectedliststr = json_encode($selected);
}
......@@ -86,13 +91,15 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$smarty->assign('prefix', $prefix);
$initjs = "var {$prefix} = new FileBrowser('{$prefix}', {$folder}, {$configstr}, config);
{$prefix}.formname = '{$formid}';
$initjs = "{$prefix} = new FileBrowser('{$prefix}', {$folder}, {$configstr}, config);
{$prefix}.filedata = {$fileliststr};";
if ($config['select']) {
$initjs .= "{$prefix}.selecteddata = {$selectedliststr};";
$initjs .= "forEach(getElementsByTagAndClassName('button', 'unselect', '{$prefix}_selectlist'), function (elem) { removeElementClass(elem, 'hidden'); });";
}
$_PIEFORM_FILEBROWSERS[$prefix]['views_js'] = $initjs;
$initjs .= "addLoadEvent({$prefix}.init);";
$smarty->assign('initjs', $initjs);
......@@ -135,13 +142,14 @@ function pieform_element_filebrowser_build_path($form, $element, $folder) {
if (!$form->submitted_by_js()) {
return;
}
$querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&');
$path = pieform_element_filebrowser_get_path($folder);
$foldername = $path[0]->title;
$smarty = smarty_core();
$smarty->assign('path', array_reverse($path));
$smarty->assign('querybase', $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'));
$smarty->assign('querybase', $querybase);
return array('html' => $smarty->fetch('artefact:file:form/folderpath.tpl'), 'foldername' => $foldername);
}
......@@ -156,16 +164,21 @@ function pieform_element_filebrowser_build_filelist($form, $element, $folder, $h
$group = $form->get_property('group');
$institution = $form->get_property('institution');
$userid = ($group || $institution) ? null : $USER->get('id');
$editable = (int) $element['config']['edit'];
$selectable = (int) $element['config']['select'];
$querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&');
$prefix = $form->get_name() . '_' . $element['name'];
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution);
$smarty = smarty_core();
$smarty->assign('edit', -1);
$smarty->assign('highlight', $highlight);
$smarty->assign('editable', (int) $element['config']['edit']);
$smarty->assign('selectable', (int) $element['config']['select']);
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution);
$smarty->assign('editable', $editable);
$smarty->assign('selectable', $selectable);
$smarty->assign('filelist', $filedata);
$smarty->assign('querybase', $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'));
$smarty->assign('prefix', $form->get_name() . '_' . $element['name']);
$smarty->assign('querybase', $querybase);
$smarty->assign('prefix', $prefix);
return array(
'data' => $filedata,
......@@ -180,6 +193,8 @@ function pieform_element_filebrowser_get_value(Pieform $form, $element) {
$result = pieform_element_filebrowser_doupdate($form, $element);
$prefix = $form->get_name() . '_' . $element['name'];
if (is_array($result)) {
// We did something. If js, replace the filebrowser now and
// don't continue form submission.
......@@ -188,6 +203,8 @@ function pieform_element_filebrowser_get_value(Pieform $form, $element) {
}
if ($form->submitted_by_js()) {
$replacehtml = false; // Don't replace the entire form when replying with json data.
$result['formelement'] = $prefix;
$result['formelementsuccess'] = $prefix . '.success';
$form->json_reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result, $replacehtml);
}
// Not js. Remember this change and submit it with the
......@@ -195,24 +212,38 @@ function pieform_element_filebrowser_get_value(Pieform $form, $element) {
return $result;
}
$prefix = $form->get_name() . '_' . $element['name'];
$selected = param_variable($prefix . '_selected', null);
if (is_array($selected)) {
$selected = array_keys($selected);
}
// We only care about the following when js is off:
if (!$form->submitted_by_js()) {
$select = param_variable($prefix . '_select', null);
$unselect = param_variable($prefix . '_unselect', null);
$edit = param_variable($prefix . '_edit', null);
if (is_array($select) && is_callable($element['selectcallback'])) {
if (is_array($select)) {
$keys = array_keys($select);
// try
$element['selectcallback']((int) $keys[0]);
$add = (int) $keys[0];
if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) {
$element['selectcallback']($add);
}
else {
$result['selected'] = is_array($selected) ? $selected : array();
$result['selected'][] = $add;
}
$result['message'] = get_string('fileadded', 'artefact.file');
$result['browse'] = 1;
}
else if (is_array($unselect) && is_callable($element['unselectcallback'])) {
else if (is_array($unselect)) {
$keys = array_keys($unselect);
// try
$element['unselectcallback']((int) $keys[0]);
$del = (int) $keys[0];
if (isset($element['unselectcallback']) && is_callable($element['unselectcallback'])) {
$element['unselectcallback']($del);
}
else {
$result['selected'] = is_array($selected) ? array_diff($selected, array($del)) : array();
}
$result['message'] = get_string('fileremoved', 'artefact.file');
}
else if (is_array($edit)) {
......@@ -225,12 +256,9 @@ function pieform_element_filebrowser_get_value(Pieform $form, $element) {
}
}
if (!is_array($result)) {
$selected = param_variable($prefix . '_selected', null);
if (is_array($selected)) {
// When files are being selected, this element has a real value
$result['selected'] = array_keys($selected);
}
if (!is_array($result) && is_array($selected)) {
// When files are being selected, this element has a real value
$result['selected'] = $selected;
}
$result['folder'] = $element['folder'];
......@@ -346,8 +374,15 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
));
// If it's a non-js upload, automatically select the newly uploaded file.
$result['browse'] = 1;
if (!$form->submitted_by_js() && !$result['error'] && !empty($element['config']['select']) && is_callable($element['selectcallback'])) {
$element['selectcallback']($result['highlight']);
if (!$form->submitted_by_js() && !$result['error'] && !empty($element['config']['select'])) {
if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) {
$element['selectcallback']($result['highlight']);
}
else {
$selected = param_variable($prefix . '_selected', array());
$result['selected'] = array_keys($selected);
$result['selected'][] = $result['highlight'];
}
}
return $result;
}
......@@ -367,22 +402,42 @@ function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
global $USER;
$parentfolder = $data['uploadfolder'] ? (int) $data['uploadfolder'] : null;
$parentfoldername = $data['uploadfoldername'];
$institution = $form->get_property('institution');
$group = $form->get_property('group');
$uploadnumber = (int) $data['uploadnumber'];
$editable = (int) $element['config']['edit'];
$selectable = (int) $element['config']['select'];
$querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&');
$prefix = $form->get_name() . '_' . $element['name'];
$result = array('error' => false, 'uploadnumber' => $uploadnumber);
$result = array('error' => false, 'uploadnumber' => (int) $data['uploadnumber']);
if ($parentfolder == 0) {
$parentfolder = null;
}
$data = new StdClass;
$data->parent = $parentfolder;
$data->owner = null;
if ($parentfolder && !$USER->can_edit_artefact(artefact_instance_from_id($parentfolder))) {
$result['error'] = true;
$result['message'] = get_string('cannoteditfolder', 'artefact.file');
return $result;
if ($parentfolder) {
$parentartefact = artefact_instance_from_id($parentfolder);
if (!$USER->can_edit_artefact($parentartefact)) {
$result['error'] = true;
$result['message'] = get_string('cannoteditfolder', 'artefact.file');
return $result;
}
$parentfoldername = $parentartefact->get('title');
}
else {
$parentfoldername = null;
}
if ($institution) {
if (!$USER->can_edit_institution($institution)) {
$result['error'] = true;
$result['message'] = get_string('notadminforinstitution', 'admin');
return $result;
}
$data->institution = $institution;
} else if ($group) {
require_once(get_config('libroot') . 'group.php');
......@@ -657,24 +712,12 @@ function pieform_element_filebrowser_changefolder(Pieform $form, $element, $fold
);
}
/* function pieform_element_filebrowser_views_js(Pieform $form, $element) {
$prefix = $form->get_name() . '_' . $element['name'];
$parentfolder = json_encode($element['parentfolder']);
log_debug($prefix);
$js = "var {$prefix} = new Uploader('{$prefix}', {$parentfolder}); {$prefix}.init();";
$js .= "{$prefix}.uploadscript = '" . get_config('wwwroot') . "artefact/file/upload2.php';";
$js .= "window.{$prefix} = {$prefix};";
if (get_config_plugin('artefact', 'file', 'uploadagreement')) {
$js .= <<<JAVASCRIPT
connect('{$prefix}_openbutton', 'onclick', function () {
addElementClass('{$prefix}_openbutton', 'hidden');
removeElementClass('{$prefix}_elements', 'hidden');
});
JAVASCRIPT;
}
log_debug($js);
return $js;
}*/
function pieform_element_filebrowser_views_js(Pieform $form, $element) {
global $_PIEFORM_FILEBROWSERS;
$formname = $form->get_name();
$prefix = $formname . '_' . $element['name'];
return $_PIEFORM_FILEBROWSERS[$prefix]['views_js'] . " {$prefix}.init();";
}
/**
......@@ -688,31 +731,7 @@ JAVASCRIPT;
function pieform_element_filebrowser_get_headdata($element) {
$headdata = array('<script type="text/javascript" src="' . get_config('wwwroot') . 'artefact/file/js/filebrowser.js"></script>');
$strings = array();
if ($element['config']['edit']) {
$strings = array(
'artefact.file' => array(
'editfile',
'editfolder',
'filewithnameexists',
'nametoolong',
'namefieldisrequired',
'detachfilewarning',
),
);
}
if ($element['config']['upload']) {
$strings['artefact.file'][] = 'uploadingfiletofolder';
if ($element['config']['uploadagreement']) {
$strings['artefact.file'][] = 'youmustagreetothecopyrightnotice';
}
}
if ($element['config']['createfolder']) {
$strings['artefact.file'][] = 'foldernamerequired';
}
if ($element['config']['select']) {
$strings['mahara'][] = 'remove';
}
$strings = PluginArtefactFile::jsstrings('filebrowser');
$jsstrings = '';
foreach ($strings as $section => $sectionstrings) {
foreach ($sectionstrings as $s) {
......
......@@ -5,16 +5,17 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
this.folderid = folderid;
this.config = config;
this.config.wwwroot = globalconfig.wwwroot;
this.config.sesskey = globalconfig.sesskey;
this.config.theme = globalconfig.theme;
this.nextupload = 0;
this.init = function () {
self.form = $(self.formname);
self.form = getFirstParentByTagAndClassName(self.id + '_filelist_container', 'form', 'pieform');
if (!self.form) {
alert('Filebrowser error 1');
}
if (self.config.select && typeof(self.form.submit) != 'function') {
alert('Filebrowser error 2'); // Rename your submit element to something other than "submit".
// logWarn('Filebrowser error 2'); // Rename your submit element to something other than "submit".
}
self.foldername = $(self.id + '_foldername').value;
if (self.config.select) {
......@@ -29,12 +30,17 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
}
}
this.submitform = function () {
signal(self.form, 'onsubmit');
self.form.submit();
};
this.upload_init = function () {
if ($(self.id + '_notice')) {
setNodeAttribute(self.id + '_userfile', 'disabled', true);
}
if (!$(self.id + '_upload')) {
appendChildNodes(self.form, INPUT({
insertSiblingNodesAfter(self.id + '_uploadnumber', INPUT({
'type': 'hidden',
'name': self.id + '_upload',
'id' : self.id + '_upload',
......@@ -89,8 +95,8 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
self.upload_presubmit();
$(self.id + '_upload').value = 1;
signal(self.form, 'onsubmit');
self.form.submit();
self.submitform();
// $(self.id + '_userfile').value = ''; // Won't work in IE
replaceChildNodes(self.id + '_userfile_container', INPUT({
'type':'file',
......@@ -246,8 +252,7 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
var href = getNodeAttribute(this, 'href');
var params = parseQueryString(href.substring(href.indexOf('?')+1));
$(self.id + '_changefolder').value = params.folder;
signal(self.form, 'onsubmit');
self.form.submit();
self.submitform();
$(self.id + '_changefolder').value = '';
e.stop();
return false;
......@@ -273,8 +278,7 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
}
$(self.id + '_move').value = dragid;
$(self.id + '_moveto').value = dropid;
signal(self.form, 'onsubmit');
self.form.submit();
self.submitform();
$(self.id + '_move').value = '';
$(self.id + '_moveto').value = '';
}
......@@ -334,18 +338,22 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
};
this.select_init = function () {
connect(self.id + '_open_upload_browse', 'onclick', function (e) {
e.stop();
removeElementClass(self.id + '_upload_browse', 'hidden');
addElementClass(self.id + '_open_upload_browse_container', 'hidden');
return false;
});
connect(self.id + '_close_upload_browse', 'onclick', function (e) {
e.stop();
addElementClass(self.id + '_upload_browse', 'hidden');
removeElementClass(self.id + '_open_upload_browse_container', 'hidden');
return false;
});
if ($(self.id + '_open_upload_browse')) {
connect(self.id + '_open_upload_browse', 'onclick', function (e) {
e.stop();
removeElementClass(self.id + '_upload_browse', 'hidden');
addElementClass(self.id + '_open_upload_browse_container', 'hidden');
return false;
});
}
if ($(self.id + '_close_upload_browse')) {
connect(self.id + '_close_upload_browse', 'onclick', function (e) {
e.stop();
addElementClass(self.id + '_upload_browse', 'hidden');
removeElementClass(self.id + '_open_upload_browse_container', 'hidden');
return false;
});
}
forEach(getElementsByTagAndClassName('button', 'unselect', self.id + '_selectlist'), function (elem) {
connect(elem, 'onclick', self.unselect);
});
......@@ -431,6 +439,7 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
}
this.success = function (form, data) {
self.form = form; // ????
if (data.uploaded) {
self.upload_success(data); // Remove uploading message
}
......
......@@ -112,7 +112,7 @@ class PluginArtefactFile extends PluginArtefact {
public static function themepaths($type) {
static $themepaths = array(
'file' => array(
'filebrowser' => array(
'images/file.gif',
'images/folder.gif',
'images/image.gif',
......@@ -166,13 +166,29 @@ class PluginArtefactFile extends PluginArtefact {
'Role',
),
),
'filebrowser' => array(
'mahara' => array(
'remove',
),
'artefact.file' => array(
'detachfilewarning',
'editfile',
'editfolder',
'filewithnameexists',
'foldernamerequired',
'nametoolong',
'namefieldisrequired',
'uploadingfiletofolder',
'youmustagreetothecopyrightnotice',
),
),
);
return $jsstrings[$type];
}
public static function jshelp($type) {
static $jshelp = array(
'file' => array(
'filebrowser' => array(
'artefact.file' => array(
'notice',
'quota_message',
......
......@@ -18,7 +18,7 @@
<div id="{$prefix}_upload_browse" class="upload_browse{if $config.select} select{if !$browse} hidden{/if}{/if}">
{if $config.select}
{if $config.select && !$config.alwaysopen}
<input type="submit" class="buttondk" name="{$prefix}_cancelbrowse" id="{$prefix}_close_upload_browse" value="{str tag=Close}" />
{/if}
......
......@@ -1200,4 +1200,13 @@ function artefact_get_references_in_html($html) {
return $artefacts;
}
function artefact_get_records_by_id($ids) {
if (!empty($ids)) {
if ($records = get_records_select_assoc('artefact', 'id IN (' . join(',', $ids) . ')')) {
return $records;
}
}
return array();
}
?>
......@@ -116,7 +116,8 @@ $extraconfig = array(
'stylesheets' => array('style/views.css'),
);
$smarty = smarty(array('views', 'tinytinymce', 'paginator', 'tablerenderer'), array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">'), false, $extraconfig);
// $smarty = smarty(array('views', 'tinytinymce', 'paginator', 'tablerenderer'), array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">'), false, $extraconfig);
$smarty = smarty(array('views', 'tinytinymce', 'paginator', 'tablerenderer', 'artefact/file/js/filebrowser.js'), array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">'), false, $extraconfig);
// The list of categories for the tabbed interface
$smarty->assign('category_list', $view->build_category_list($category, $new));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment