Commit c2851e43 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Upload to temp directory for blog post attachments

parent ea47b70f
......@@ -31,19 +31,19 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
safe_require('artefact', 'blog');
// The createid is used to upload and attach files
$createid = $SESSION->get('createid');
if (empty($createid)) {
$createid = 0;
}
$SESSION->set('createid', $createid + 1);
// For a new post, the 'blog' parameter will be set to the blog's artefact id.
// For an existing post, the 'post' parameter will be set to the blogpost's artefact id.
$blogpost = param_integer('blogpost', 0);
if (!$blogpost) {
// This is a new post; get a create id so we can attach files to it.
$createid = $SESSION->get('createid');
if (empty($createid)) {
$createid = 0;
}
$SESSION->set('createid', $createid + 1);
$blog = param_integer('blog');
$blogpost = 0;
$title = '';
$description = '';
$checked = '';
......@@ -61,38 +61,9 @@ else {
$pagetitle = 'editblogpost';
}
$getstring = quotestrings(array(
'mahara' => array(
),
'artefact.blog' => array(
'nofilesattachedtothispost',
)));
$attachedhtml = '<h3>' . get_string('attachedfiles', 'artefact.blog') . "</h3>\n";
$attachedhtml .= "<table id='attachedfiles'><tbody><tr><td></td></tr></tbody></table>\n";
//$blogpostjs = $blogpost ? $blogpost : 'null';
$javascript = <<< EOF
var attached = new TableRenderer(
'attachedfiles',
'attachedfiles.json.php',
[
'title',
'description',
function () { return TD(null); }
]
);
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.updateOnLoad();
EOF;
// 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.
$form = pieform(array(
'name' => 'editpost',
......@@ -133,24 +104,60 @@ $form = pieform(array(
'description' => get_string('thisisdraftdesc', 'artefact.blog'),
'checked' => $checked
),
'attachedfiles' => array(
'type' => 'html',
'value' => $attachedhtml,
),
'submit' => array(
'type' => 'submitcancel',
'value' => array(
get_string('save', 'artefact.blog'),
get_string('cancel', 'artefact.blog')
)
)
)
));
$smarty = smarty(array('tablerenderer'));
$getstring = quotestrings(array(
'mahara' => array(
),
'artefact.blog' => array(
'blogpost',
'nofilesattachedtothispost',
)));
// Insert this automatically sometime.
$copyright = get_field('site_content', 'content', 'name', 'uploadcopyright');
$javascript = <<< EOF
var copyrightnotice = '{$copyright}';
var uploader = new FileUploader('uploader', 'upload.php', {$getstring['blogpost']}, false,
attachtopost, fileexists);
uploader.createid = {$createid};
var attached = new TableRenderer(
'attachedfiles',
'attachedfiles.json.php',
[
'title',
'description',
function () { return TD(null); }
]
);
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}); };
// This function adds a newly uploaded file to the attached files list.
function attachtopost(data) {
return true;
}
// This function checks if there's 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->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign_by_ref('textinputform', $form);
$smarty->assign('pagetitle', $pagetitle);
$smarty->assign_by_ref('editpostform', $form);
$smarty->display('artefact:blog:editpost.tpl');
/**
......
......@@ -34,6 +34,7 @@ $string['newblog'] = 'New Blog';
$string['attachedfiles'] = 'Attached files';
$string['nofilesattachedtothispost'] = 'No attached files';
$string['blogpost'] = 'post';
$string['blogtitle'] = 'Title';
$string['blogtitledesc'] = 'e.g., ‘Jill’s Travel Blog’.';
......
......@@ -549,6 +549,20 @@ class ArtefactTypeBlogPost extends ArtefactType {
return true;
}
// Where to store temporary blog post files under dataroot
static $blogattachmentroot = 'artefact/blog/uploads/';
/**
* This function saves an uploaded file to a temporary directory in dataroot
*
*/
public static function save_blogpost_attachment($inputname, $dirname, $filename) {
require_once('uploadmanager.php');
$um = new upload_manager($inputname);
return $um->process_file_upload(self::$blogattachmentroot . $dirname, $filename);
}
/**
* This function publishes the blog post.
*
......
......@@ -16,7 +16,10 @@
<span class="cnr-tl"><span class="cnr-tr"><span class="cnr-bl"><span class="cnr-br">
<div class="maincontent">
<h2>{str section="artefact.blog" tag=$pagetitle}</h2>
{$editpostform}
{$textinputform}
<h3>{str section=artefact.blog tag=attachedfiles}</h3>
<div id='uploader'></div>
<table id='attachedfiles'><tbody><tr><td></td></tr></tbody></table>
</div>
</span></span></span></span>
</div>
......
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage artefact-blog
* @author Richard Mansfield <richard.mansfield@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
// Upload a temporary file to attach to a blog post.
// The uploaded file will not be saved as an artefact until the blog post is saved.
$result = new StdClass;
$result->title = param_variable('title');
$result->description = param_variable('description', null);
$result->uploadnumber = param_integer('uploadnumber'); // id of target iframe
$createid = param_variable('createid');
// Ignore possible file name clashes; they should be dealt with in the
// javascript on the edit blog post page.
safe_require('artefact', 'blog');
if (ArtefactTypeBlogPost::save_blogpost_attachment('userfile', session_id() . $createid,
$result->uploadnumber)) {
$result->error = false;
$result->message = get_string('uploadoffilecomplete', 'artefact.file', $result->title);
}
else {
$result->error = 'uploadfailed';
$result->message = get_string('uploadoffilefailed', 'artefact.file', $result->title);
}
$r = json_encode($result);
$frame = <<< EOF
<html><head><script>
<!--
function senduploadresult() {
var x = {$r};
parent.uploader.getresult(x);
}
// -->
</script></head>
<body onload="senduploadresult()"></body>
</html>
EOF;
header('Content-type: text/html');
echo $frame;
?>
......@@ -138,6 +138,10 @@ function FileUploader(element, uploadscript, foldername, folderid, uploadcallbac
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();
......
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