Commit f3f514a2 authored by Martyn Smith's avatar Martyn Smith Committed by Martyn Smith
Browse files
parents 13c4d247 a5d02229
......@@ -175,6 +175,8 @@ function uploadcsv_submit($values) {
foreach (ArtefactTypeProfile::get_mandatory_fields() as $field => $type) {
set_profile_field($id, $field, $record[$i++]);
}
handle_event('createuser', $user);
}
log_info('Inserted ' . count($CSVDATA) . ' records');
......
......@@ -61,6 +61,7 @@ else {
$pagetitle = 'editblogpost';
}
// This form just has the main text inputs and no submit button. The
// submit and cancel buttons are in their own form at the bottom of
// the page.
......@@ -107,59 +108,130 @@ $form = pieform(array(
)
));
// Strings used in the javascript
$getstring = quotestrings(array(
'mahara' => array(
),
'artefact.file' => array(
'myfiles',
),
'artefact.blog' => array(
'attach',
'blogpost',
'browsemyfiles',
'nofilesattachedtothispost',
'remove',
)));
// Insert this automatically sometime.
$copyright = get_field('site_content', 'content', 'name', 'uploadcopyright');
$wwwroot = get_config('wwwroot');
$javascript = <<< EOF
// The file uploader uploads files to the list of blog post attachments
var copyrightnotice = '{$copyright}';
var uploader = new FileUploader('uploader', 'upload.php', {$getstring['blogpost']}, false,
attachtopost, fileexists);
uploader.createid = {$createid};
// File browser instance allows users to attach files from the my files area
var browser = null;
var browsebutton = INPUT({'id':'browsebutton', 'type':'button', 'value':{$getstring['browsemyfiles']},
'onclick':browsemyfiles});
function browsemyfiles() {
hideElement('browsebutton');
insertSiblingNodesAfter('browsebutton', H3(null, {$getstring['myfiles']}));
showElement('filebrowser');
browser = new FileBrowser('filebrowser', '{$wwwroot}artefact/file/myfiles.json.php',
function () {}, {$getstring['attach']}, attachtopost);
browser.init();
}
addLoadEvent(function () {insertSiblingNodesBefore('filebrowser', browsebutton);});
// List of attachments to the blog post
var attached = new TableRenderer(
'attachedfiles',
'attachedfiles.json.php',
[
'title',
'description',
function () { return TD(null); }
function (r) {
return TD(null, INPUT({'type':'button', 'value':{$getstring['remove']},
'onclick':"removefrompost('attached_old:"+r.id+"')"}));
}
]
);
attached.emptycontent = {$getstring['nofilesattachedtothispost']};
attached.paginate = false;
attached.blogpost = {$blogpost};
attached.statevars.push('blogpost');
attached.rowfunction = function (r) { return TR({'id':'attached_old_' + r.id}); };
attached.rowfunction = function (r) { return TR({'id':'attached_old:' + r.id}); };
attached.updateOnLoad();
// This function adds a newly uploaded file to the attached files list.
// Show/hide the 'no attachments' message if there are no/some attachments
function checknoattachments() {
if (attached.tbody.hasChildNodes()) {
hideElement(attached.table.previousSibling);
showElement(attached.table);
}
else {
showElement(attached.table.previousSibling);
hideElement(attached.table);
}
}
// Add a newly uploaded file to the attached files list.
function attachtopost(data) {
return true;
var rowid = data.uploadnumber ? 'uploaded:' + data.uploadnumber : 'existing:' + data.id;
appendChildNodes(attached.tbody,
TR({'id':rowid},
map(partial(TD,null),
[data.title, data.description,
INPUT({'type':'button', 'value':{$getstring['remove']},
'onclick':"removefrompost('"+rowid+"')"})])));
checknoattachments();
}
// Remove a row from the attached files list.
function removefrompost(rowid) {
removeElement(rowid);
checknoattachments();
}
// This function checks if there's a file attached to the post with the given name
// Check if there's already a file attached to the post with the given name
function fileexists(name) {
return false;
}
attached.updateOnLoad();
EOF;
$smarty = smarty(array('tablerenderer', 'artefact/file/js/uploader.js'));
$smarty = smarty(array('tablerenderer', 'artefact/file/js/file.js'));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign_by_ref('textinputform', $form);
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('artefact:blog:editpost.tpl');
/**
* This function gets called to create a new blog post, and publish it
* simultaneously.
......@@ -179,6 +251,8 @@ function editpost_submit(array $values) {
redirect(get_config('wwwroot') . 'artefact/blog/list/');
}
/**
* This function get called to cancel the form submission. It returns to the
* blog list.
......
......@@ -32,9 +32,12 @@ $string['description'] = 'description';
$string['addblog'] = 'Add Blog';
$string['newblog'] = 'New Blog';
$string['attach'] = 'Attach';
$string['attachedfiles'] = 'Attached files';
$string['nofilesattachedtothispost'] = 'No attached files';
$string['blogpost'] = 'post';
$string['browsemyfiles'] = 'Browse my files';
$string['remove'] = 'Remove';
$string['blogtitle'] = 'Title';
$string['blogtitledesc'] = 'e.g., ‘Jill’s Travel Blog’.';
......
......@@ -17,9 +17,26 @@
<div class="maincontent">
<h2>{str section="artefact.blog" tag=$pagetitle}</h2>
{$textinputform}
<h3>{str section=artefact.blog tag=attachedfiles}</h3>
<div id='uploader'></div>
<table id='attachedfiles'><tbody><tr><td></td></tr></tbody></table>
<table id='filebrowser' style='display: none;'>
<thead><tr>
<th>{str section=artefact.file tag=name}</th>
<th>{str section=artefact.file tag=description}</th>
<th>{str section=artefact.file tag=size}</th>
<th>{str section=mahara tag=date}</th>
<th></th>
</tr></thead>
<tbody><tr><td></td></tr></tbody>
</table>
<h3>{str section=artefact.blog tag=attachedfiles}</h3>
<table id='attachedfiles'>
<thead><tr>
<th>{str section=artefact.file tag=name}</th>
<th>{str section=artefact.file tag=description}</th>
<th></th>
</tr></thead>
<tbody><tr><td></td></tr></tbody>
</table>
</div>
</span></span></span></span>
</div>
......
......@@ -34,15 +34,14 @@ $copyright = get_field('site_content', 'content', 'name', 'uploadcopyright');
$javascript = <<<JAVASCRIPT
var copyrightnotice = '{$copyright}';
var browser = new FileBrowser('filelist');
var browser = new FileBrowser('filelist', 'myfiles.json.php');
var uploader = new FileUploader('uploader', 'upload.php', null, null, browser.refresh, browser.fileexists);
browser.changedircallback = uploader.updatedestination;
JAVASCRIPT;
$smarty = smarty(array('tablerenderer',
'artefact/file/js/filebrowser.js',
'artefact/file/js/uploader.js'));
'artefact/file/js/file.js'));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->display('artefact:file:index.tpl');
......
// file.js
// The file browser part needs to be kept relatively separated from
// the file uploader because they are used slightly differently in the
// my files screen and the edit blog post screen
var changedir = function () {};
function FileBrowser(element, changedircallback) {
function FileBrowser(element, source, changedircallback, actionname, actioncallback) {
var self = this;
this.element = element;
this.source = source;
this.pathids = {'/':null};
this.cwd = '/';
if (typeof(changedircallback) == 'function') {
this.changedircallback = changedircallback;
this.changedircallback = (typeof(changedircallback) == 'function') ? changedircallback : function () {};
this.actioncallback = (typeof(actioncallback) == 'function') ? actioncallback : function () {};
this.actionname = actionname;
this.canmodify = !actionname;
this.filenames = {};
if (this.actionname) {
this.lastcolumnfunc = function(r) {
if (r.artefacttype != 'folder') {
var button = INPUT({'type':'button', 'value':self.actionname});
button.onclick = function () { self.actioncallback(r) };
return TD(null, button);
}
return TD(null);
}
}
else {
this.changedircallback = function () {};
this.lastcolumnfunc = function (r) {
var editb = INPUT({'type':'button', 'value':get_string('edit')});
editb.onclick = function () { self.openeditform(r); };
if (r.emptyfolder) {
return TD(null, editb);
}
var deleteb = INPUT({'type':'button', 'value':get_string('delete')});
deleteb.onclick = function () {
if (confirm(get_string(r.artefacttype == 'folder' ? 'deletefolder?' : 'deletefile?'))) {
sendjsonrequest('delete.json.php', {'id': r.id}, self.refresh);
}
};
return TD(null, editb, deleteb);
}
}
this.filenames = {};
this.init = function() {
// Create the button which opens up the create folder form
var button = INPUT({'type':'button','value':get_string('createfolder'), 'onclick':function () {
hideElement(self.createfolderbutton);
showElement(self.createfolderform);
}});
self.createfolderbutton = button;
self.createfolderform = self.initcreatefolderform();
insertSiblingNodesBefore(self.element, self.createfolderbutton, self.createfolderform);
if (self.canmodify) {
// Create the button which opens up the create folder form
var button = INPUT({'type':'button','value':get_string('createfolder'), 'onclick':function () {
hideElement(self.createfolderbutton);
showElement(self.createfolderform);
}});
self.createfolderbutton = button;
self.createfolderform = self.initcreatefolderform();
insertSiblingNodesBefore(self.element, self.createfolderbutton, self.createfolderform);
}
// Folder navigation links
insertSiblingNodesBefore(self.element, DIV({'id':'foldernav'}));
self.filelist = new TableRenderer(
self.element,
'myfiles.json.php',
self.source,
[
self.formatname,
'description',
......@@ -37,7 +71,7 @@ function FileBrowser(element, changedircallback) {
'mtime',
// @todo this function should be changed for when we
// are using the browser to attach files
self.editdelete
self.lastcolumnfunc
]
);
self.filelist.emptycontent = get_string('nofilesfound');
......@@ -50,21 +84,6 @@ function FileBrowser(element, changedircallback) {
}
this.refresh = function () { self.changedir(self.cwd); };
this.editdelete = function(r) {
var editb = INPUT({'type':'button', 'value':get_string('edit')});
editb.onclick = function () { self.openeditform(r); };
if (r.emptyfolder) {
return TD(null, editb);
}
var deleteb = INPUT({'type':'button', 'value':get_string('delete')});
deleteb.onclick = function () {
if (confirm(get_string(r.artefacttype == 'folder' ? 'deletefolder?' : 'deletefile?'))) {
sendjsonrequest('delete.json.php', {'id': r.id}, self.refresh);
}
};
return TD(null, editb, deleteb);
}
this.savemetadata = function (fileid, formid, replacefile, originalname) {
var name = $(formid).name.value;
......@@ -185,6 +204,9 @@ function FileBrowser(element, changedircallback) {
r.title);
return TD(null, link);
}
if (self.actionname) {
return TD(null, r.title);
}
return TD(null, A({'href':'download.php?file=' + r.id}, r.title));
}
......@@ -229,3 +251,174 @@ function FileBrowser(element, changedircallback) {
addLoadEvent(this.init);
}
function FileUploader(element, uploadscript, foldername, folderid, uploadcallback, fileexists) {
var self = this;
this.element = element;
this.uploadscript = uploadscript;
this.folderid = folderid;
this.foldername = foldername ? foldername : get_string('home');
this.uploadcallback = uploadcallback;
if (typeof(fileexists) == 'function') {
this.fileexists = fileexists;
}
else {
this.fileexists = function (filename) { alert(filename); };
}
this.init = function() {
self.nextupload = 1;
// Create the upload form
self.form = self.initform();
// Create the button which opens up the upload form
var button = INPUT({'type':'button','value':get_string('uploadfile'), 'onclick':function () {
hideElement(self.openbutton);
showElement(self.form);
}});
self.openbutton = button;
appendChildNodes(self.element, self.form, self.openbutton);
}
this.filepart = function (path) {
if (path.indexOf('/') > -1) {
var separator = '/';
}
else {
var separator = '\\';
}
return path.substring(path.lastIndexOf(separator)+1, path.length);
}
this.initform = function () {
var form = FORM({'method':'post', 'id':'uploadform',
'enctype':'multipart/form-data', 'encoding':'multipart/form-data',
'action':self.uploadscript, 'target':''});
var cancelform = function () {
if ($('uploadformmessage')) {
$('uploadformmessage').innerHTML = '';
}
self.form.notice.checked = '';
self.form.userfile.value = '';
self.form.title.value = '';
self.form.description.value = '';
hideElement(self.form.replace);
hideElement(self.form);
showElement(self.openbutton);
};
var notice = SPAN(null);
notice.innerHTML = copyrightnotice;
var destinationattributes = (self.folderid === false) ? {'style':'display: none;'} : null;
appendChildNodes(form,
TABLE(null,
TBODY(null,
TR(null, TH({'colSpan':2}, LABEL(null, get_string('uploadfile')))),
TR(destinationattributes, TH(null, LABEL(null, get_string('destination'))),
TD(null, SPAN({'id':'uploaddest'},self.foldername))),
TR(null, TH(null,LABEL(null,get_string('copyrightnotice'))),
TD(null,INPUT({'type':'checkbox','name':'notice'}),notice)),
TR(null, TH(null, LABEL(null, get_string('file'))),
TD(null, INPUT({'type':'file','name':'userfile','size':40,'onchange':function () {
self.form.title.value = self.filepart(self.form.userfile.value);
}}))),
TR(null, TH(null, LABEL(null, get_string('title'))),
TD(null, INPUT({'type':'text', 'name':'title', 'size':40}))),
TR(null, TH(null, LABEL(null, get_string('description'))),
TD(null, INPUT({'type':'text', 'name':'description', 'size':40}))),
TR(null,TD({'colspan':2, 'id':'uploadformmessage'})),
TR(null,TD({'colspan':2},
INPUT({'name':'upload','type':'button','value':get_string('upload'),
'onclick':function () { if (self.sendform(false)) { cancelform(); } }}),
INPUT({'name':'replace','type':'button','value':get_string('overwrite'),
'onclick':function () { if (self.sendform(true)) { cancelform(); } }}),
INPUT({'type':'button','value':get_string('cancel'),'onclick':cancelform}))))));
hideElement(form.replace);
hideElement(form);
return form;
}
this.updatedestination = function (folderid, foldername) {
self.foldername = foldername;
self.folderid = folderid;
if ($('uploaddest')) {
$('uploaddest').innerHTML = foldername;
}
}
this.sendform = function (replacefile) {
if (!self.form.notice.checked) {
$('uploadformmessage').innerHTML = get_string('youmustagreetothecopyrightnotice');
return false;
}
var localname = self.form.userfile.value;
if (isEmpty(localname)) {
$('uploadformmessage').innerHTML = get_string('filenamefieldisrequired');
return false;
}
var destname = self.form.title.value;
if (isEmpty(destname)) {
$('uploadformmessage').innerHTML = get_string('titlefieldisrequired');
return false;
}
localname = self.filepart(localname);
if (!replacefile && self.fileexists(destname)) {
$('uploadformmessage').innerHTML = get_string('uploadfileexistsoverwritecancel');
// Show replace button
setDisplayForElement('inline', self.form.replace);
self.form.title.focus();
return false;
}
$('uploadformmessage').innerHTML = '';
hideElement(self.form.replace);
// Create iframe in which to load the file
appendChildNodes(self.element,
createDOM('iframe',{'name':'iframe'+self.nextupload,
'id':'iframe'+self.nextupload,
'src':'blank.html',
'style':'display: none;'}));
setNodeAttribute(self.form, 'target', 'iframe' + self.nextupload);
var collideaction = replacefile ? 'replace' : 'fail';
appendChildNodes(self.form,
INPUT({'type':'hidden', 'name':'collideaction', 'value':collideaction}),
INPUT({'type':'hidden', 'name':'uploadnumber', 'value':self.nextupload}));
if (self.folderid) {
appendChildNodes(self.form,
INPUT({'type':'hidden', 'name':'parentfolder', 'value':self.folderid}));
}
if (self.createid) {
appendChildNodes(self.form,
INPUT({'type':'hidden', 'name':'createid', 'value':self.createid}));
}
self.form.submit();
// Display upload status
insertSiblingNodesBefore(self.form,
DIV({'id':'uploadstatusline'+self.nextupload}, IMG({'src':config.themeurl+'loading.gif'}),
get_string('uploadingfiletofolder',localname,self.foldername)));
self.nextupload += 1;
return true;
}
this.getresult = function(data) {
if (!data.error) {
var image = 'success.gif';
}
else {
var image = 'failure.gif';
}
replaceChildNodes($('uploadstatusline'+data.uploadnumber),
IMG({'src':config.themeurl+image}), data.message);
this.uploadcallback(data);
}
addLoadEvent(this.init);
}
\ No newline at end of file
function FileUploader(element, uploadscript, foldername, folderid, uploadcallback, fileexists) {
var self = this;
this.element = element;
this.uploadscript = uploadscript;
this.folderid = folderid;
this.foldername = foldername ? foldername : get_string('home');
this.uploadcallback = uploadcallback;
if (typeof(fileexists) == 'function') {
this.fileexists = fileexists;
}
else {
this.fileexists = function (filename) { alert(filename); };
}
this.init = function() {
self.nextupload = 1;
// Create the upload form
self.form = self.initform();
// Create the button which opens up the upload form
var button = INPUT({'type':'button','value':get_string('uploadfile'), 'onclick':function () {
hideElement(self.openbutton);
showElement(self.form);
}});
self.openbutton = button;
appendChildNodes(self.element, self.form, self.openbutton);
}
this.filepart = function (path) {
if (path.indexOf('/') > -1) {
var separator = '/';
}
else {
var separator = '\\';
}
return path.substring(path.lastIndexOf(separator)+1, path.length);
}
this.initform = function () {
var form = FORM({'method':'post', 'id':'uploadform',
'enctype':'multipart/form-data', 'encoding':'multipart/form-data',
'action':self.uploadscript, 'target':''});
var cancelform = function () {
if ($('uploadformmessage')) {
$('uploadformmessage').innerHTML = '';
}
self.form.notice.checked = '';
self.form.userfile.value = '';
self.form.title.value = '';
self.form.description.value = '';
hideElement(self.form.replace);
hideElement(self.form);
showElement(self.openbutton);
};
var notice = SPAN(null);
notice.innerHTML = copyrightnotice;
var destinationattrib