Commit 034328b5 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Rewrite blog/view page to use templates, paginator rather than tablerenderer


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent a46495dc
<h3>Publish</h3>
<p>By publishing a Draft posting you will make it available to users with access to it via a View.</p>
......@@ -597,14 +597,14 @@ class ArtefactTypeBlogPost extends ArtefactType {
}
/**
* This function returns a list of the current user's blog posts, for the
* given blog.
* This function returns a list of posts in a given blog.
*
* @param User
* @param integer
* @param integer
* @param integer
* @param boolean
*/
public static function get_posts(User $user, $id, $limit = self::pagination, $offset = 0) {
public static function get_posts($id, $limit, $offset, $editing=false) {
($result = get_records_sql_assoc("
SELECT a.id, a.title, a.description, " . db_format_tsfield('a.ctime', 'ctime') . ', ' . db_format_tsfield('a.mtime', 'mtime') . ", a.locked, bp.published
FROM {artefact} a
......@@ -612,18 +612,15 @@ class ArtefactTypeBlogPost extends ArtefactType {
ON a.id = bp.blogpost
WHERE a.parent = ?
AND a.artefacttype = 'blogpost'
AND a.owner = ?
ORDER BY bp.published ASC, a.ctime DESC
LIMIT ? OFFSET ?;", array(
$id,
$user->get('id'),
$limit,
$offset
)))
|| ($result = array());
$count = (int)get_field('artefact', 'COUNT(*)', 'owner', $user->get('id'),
'artefacttype', 'blogpost', 'parent', $id);
$count = count_records('artefact', 'artefacttype', 'blogpost', 'parent', $id);
if (count($result) > 0) {
// Get the attached files.
......@@ -640,11 +637,47 @@ class ArtefactTypeBlogPost extends ArtefactType {
foreach ($result as &$post) {
$post->ctime = format_date($post->ctime, 'strftimedaydatetime');
$post->mtime = format_date($post->mtime);
$post->description = clean_html($post->description);
if ($editing) {
if (!$post->published) {
$post->publish = ArtefactTypeBlogpost::publish_form($post->id);
}
$post->delete = ArtefactTypeBlogpost::delete_form($post->id);
}
}
}
return array($count, array_values($result));
return array(
'count' => $count,
'data' => array_values($result),
'limit' => $limit,
'offset' => $offset,
);
}
public function render_posts(&$posts, $blog=null) {
$smarty = smarty_core();
$smarty->assign('posts', $posts['data']);
$posts['tablerows'] = $smarty->fetch('artefact:blog:posts.tpl');
$baseurl = get_config('wwwroot') . 'artefact/blog/view';
if ($blog) {
$baseurl .= '/index.php?id=' . $blog;
}
$pagination = build_pagination(array(
'id' => 'blogpost_pagination',
'class' => 'center',
'datatable' => 'postlist',
'url' => $baseurl,
'jsonscript' => 'artefact/blog/view/index.json.php',
'count' => $posts['count'],
'limit' => $posts['limit'],
'offset' => $posts['offset'],
'numbersincludefirstlast' => false,
'resultcounttextsingular' => get_string('post', 'artefact.blog'),
'resultcounttextplural' => get_string('posts', 'artefact.blog'),
));
$posts['pagination'] = $pagination['html'];
$posts['pagination_js'] = $pagination['javascript'];
}
/**
......@@ -686,6 +719,52 @@ class ArtefactTypeBlogPost extends ArtefactType {
}
public static function publish_form($id) {
return pieform(array(
'name' => 'publish_' . $id,
'successcallback' => 'publish_submit',
'jsform' => true,
'jssuccesscallback' => 'publish_success',
'renderer' => 'oneline',
'elements' => array(
'publish' => array(
'type' => 'hidden',
'value' => $id,
'help' => true,
),
'submit' => array(
'type' => 'submit',
'value' => get_string('publish', 'artefact.blog'),
),
),
));
}
public static function delete_form($id) {
return pieform(array(
'name' => 'delete_' . $id,
'successcallback' => 'delete_submit',
'jsform' => true,
'jssuccesscallback' => 'delete_success',
'renderer' => 'oneline',
'elements' => array(
'delete' => array(
'type' => 'hidden',
'value' => $id,
'help' => true,
),
'submit' => array(
'class' => 'delete',
'type' => 'submit',
'value' => get_string('delete', 'artefact.blog'),
'confirm' => get_string('deleteblogpost?', 'artefact.blog'),
),
),
));
}
/**
* This function publishes the blog post.
*
......
{foreach from=$posts item=post}
<tr id="posttitle_{$post->id}">
<th class="posttitle">{$post->title}</th>
<th id="poststatus{$post->id}">
{if $post->published}{str tag=published section=artefact.blog}{else}{str tag=draft section=artefact.blog}{/if}
</th>
<th class="controls">
{if $post->locked}
{str tag=submittedforassessment section=view}
{else}
{if !$post->published}{$post->publish|safe}{/if}
<a class="btn-edit" href="{$WWWROOT}artefact/blog/post.php?id={$post->id}">{str tag=edit section=artefact.blog}</a>
{$post->delete|safe}
{/if}
</th>
</tr>
<tr id="postdescription_{$post->id}">
<td colspan=3>{$post->description|clean_html|safe}</td>
</tr>
{if $post->files}
<tr id="postfiles_{$post->id}">
<td colspan=3>
<table class="attachments fullwidth">
<col width="5%">
<col width="40%">
<col width="55%">
<tbody>
<tr><th colspan=3>{str tag=attachedfiles section=artefact.blog}</th></tr>
{foreach from=$post->files item=file}
<tr class="{cycle values='r1,r0'}">
<td><img src="{$file->icon}" alt=""></td>
<td><a href="{$WWWROOT}artefact/file/download.php?file={$file->attachment}">{$file->title}</a></td>
<td>{$file->description}</td>
</tr>
{/foreach}
</tbody>
</table>
</td>
</tr>
{/if}
<tr id="postdetails_{$post->id}"><td colspan=2 class="postdetails">{str tag=postedon section=artefact.blog} {$post->ctime}</td></tr>
{/foreach}
\ No newline at end of file
......@@ -48,6 +48,10 @@
cursor: pointer;
color: #333;
}
#postlist th.controls form,
#postlist th.controls form div {
display: inline;
}
#postlist p {
margin: 0 0 1em 0;
}
......
......@@ -11,12 +11,19 @@
<p>{clean_html($blog->get('description'))|safe}</p>
{if $blog->get('tags')}<p class="tags">{str tag=tags}: {list_tags owner=$blog->get('owner') tags=$blog->get('tags')}</p>{/if}
{if $blog->count_children() > 0}
<table id="postlist" class="hidden tablerenderer fullwidth nohead">
{if $posts}
<table id="postlist" class="tablerenderer fullwidth nohead">
<tbody>
<tr><td></td><td></td><td></td></tr>
{$posts.tablerows|safe}
</tbody>
</table>
<div id="blogpost_page_container" class="hidden">{$posts.pagination|safe}</div>
<script>
addLoadEvent(function() {literal}{{/literal}
{$posts.pagination_js|safe}
removeElementClass('blogpost_page_container', 'hidden');
{literal}}{/literal});
</script>
{else}
<div class="message">
{str tag=nopostsyet section=artefact.blog} {if !$blog->get('locked')}<a href="{$WWWROOT}artefact/blog/post.php?blog={$blog->get('id')}">{str tag=addone section=artefact.blog}</a>{/if}
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-blog
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
safe_require('artefact', 'blog');
json_headers();
$id = param_integer('id');
$blogpost = new ArtefactTypeBlogPost($id);
$blogpost->check_permission();
if ($blogpost->get('locked')) {
json_reply('local', get_string('submittedforassessment', 'view'));
}
$blogpost->delete();
json_reply(false, get_string('blogpostdeleted', 'artefact.blog'));
?>
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-blog
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
$enc_id = json_encode($id);
$enc_wwwroot = json_encode(get_config('wwwroot'));
$enc_draft = json_encode(get_string('draft', 'artefact.blog'));
$enc_published = json_encode(get_string('published', 'artefact.blog'));
$enc_publish = json_encode(get_string('publish', 'artefact.blog'));
$enc_publish_confirm = json_encode(get_string('publishblogpost?', 'artefact.blog'));
$enc_nopublish = json_encode(get_string('publishfailed', 'artefact.blog'));
$enc_edit = json_encode(get_string('edit', 'artefact.blog'));
$enc_error = json_encode(get_string('unknownerror'));
$enc_files = json_encode(get_string('attachedfiles', 'artefact.blog'));
$enc_delete = json_encode(get_string('delete', 'artefact.blog'));
$enc_delete_confirm = json_encode(get_string('deleteblogpost?', 'artefact.blog'));
$enc_postedon = json_encode(get_string('postedon', 'artefact.blog'));
$enc_cannotdeleteblogpost = json_encode(get_string('cannotdeleteblogpost', 'artefact.blog'));
$enc_submitted = json_encode(get_string('submittedforassessment', 'view'));
$enc_publish_help = json_encode(get_help_icon('artefact', 'blog', null, null, null, 'publish'));
$limit = ArtefactTypeBlog::pagination;
return <<<EOJAVASCRIPT
var postlist = new TableRenderer(
'postlist',
{$enc_wwwroot} + 'artefact/blog/view/index.json.php',
[undefined, undefined, undefined]
);
postlist.limit = $limit;
postlist.rowfunction = function(d, n, gd) {
var status = TH({'id':'poststatus'+d.id});
if (d.published == 1) {
status.innerHTML = {$enc_published};
}
else {
status.innerHTML = {$enc_draft};
}
var controls = [];
if (d.locked == 1) {
controls.push({$enc_submitted});
}
else {
if (d.published != 1) {
status.innerHTML = {$enc_draft};
pub = INPUT(
{ 'type' : 'button' , 'class' : 'button publish', 'value' : {$enc_publish}}
);
pubhelp = SPAN(null); pubhelp.innerHTML = {$enc_publish_help};
connect(pub, 'onclick', function(e) {
if (!confirm({$enc_publish_confirm})) {
return;
}
sendjsonrequest({$enc_wwwroot} + 'artefact/blog/view/publish.json.php', { 'id': d.id }, 'GET',
function (response) {
if (!response.error) {
$('poststatus'+d.id).innerHTML = {$enc_published};
hideElement(pub);
hideElement(pubhelp);
}
});
});
controls.push(pub, pubhelp);
}
var edit = FORM(
{
'method' : 'get',
'style' : 'display: inline;',
'action' : {$enc_wwwroot} + 'artefact/blog/post.php'
},
INPUT(
{
'type' : 'hidden',
'name' : 'blogpost',
'value' : d.id
}
),
INPUT(
{ 'type' : 'submit', 'class' : 'submit edit',
'value' : {$enc_edit}
}
)
);
var del = INPUT(
{ 'type' : 'button', 'class' : 'button delete', 'value': {$enc_delete} }
);
controls.push(edit, ' ', del);
}
var desctd = TD({'colSpan':3});
desctd.innerHTML = d.description;
var rows = [
TR(
null,
TH({'class':'posttitle'}, d.title),
status,
TH({'class': 'controls'}, controls)
),
TR(null, desctd)
];
if (d.files) {
var filerows = [TR(null, TH({'colSpan':3}, {$enc_files}))];
for (var i = 0; i < d.files.length; i++) {
filerows.push(TR({'class':'r'+((i+1)%2)},
TD(null, IMG({'src':d.files[i].icon})),
TD(null, A({'href':config.wwwroot+'artefact/file/download.php?file='+d.files[i].attachment},
d.files[i].title)),
TD(null, d.files[i].description)));
}
rows.push(TR(null, TD({'colSpan':3},
TABLE({'class': 'attachments fullwidth'},
createDOM('col', {'width':'5%'}),
createDOM('col', {'width':'40%'}),
createDOM('col', {'width':'55%'}),
TBODY(null, filerows)))));
}
rows.push(TR(null, TD({'colspan':2, 'class': 'postdetails'}, {$enc_postedon}, ' ', d.ctime)));
if (del) {
connect(del, 'onclick', function(e) {
if (!confirm({$enc_delete_confirm})) {
return;
}
sendjsonrequest({$enc_wwwroot} + 'artefact/blog/view/delete.json.php', { 'id' : d.id }, 'GET', function(response) {
if (!response.error) {
for (row in rows) {
rows[row].parentNode.removeChild(rows[row]);
}
}
});
});
}
return rows;
};
postlist.statevars.push('id');
postlist.id = {$enc_id};
postlist.updateOnLoad();
EOJAVASCRIPT;
?>
......@@ -30,23 +30,14 @@ define('JSON', 1);
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
safe_require('artefact', 'blog');
json_headers();
require_once(get_config('libroot') . 'pieforms/pieform.php');
$id = param_integer('id');
$limit = param_integer('limit', ArtefactTypeBlog::pagination);
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
list($count, $data) = ArtefactTypeBlogPost::get_posts($USER, $id, $limit, $offset);
$result = array(
'error' => false,
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'data' => $data
);
echo json_encode($result);
$posts = ArtefactTypeBlogPost::get_posts($id, $limit, $offset, true);
ArtefactTypeBlogPost::render_posts($posts, $id);
json_reply(false, array('data' => $posts));
?>
......@@ -34,6 +34,14 @@ define('SECTION_PAGE', 'view');
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
define('TITLE', get_string('viewblog','artefact.blog'));
safe_require('artefact', 'blog');
require_once(get_config('libroot') . 'pieforms/pieform.php');
if ($publish = param_integer('publish', null)) {
ArtefactTypeBlogpost::publish_form($publish);
}
if ($delete = param_integer('delete', null)) {
ArtefactTypeBlogpost::delete_form($delete);
}
$id = param_integer('id', null);
if (is_null($id)) {
......@@ -53,13 +61,32 @@ else {
}
$blog->check_permission();
// This javascript is used to generate a list of blog posts.
$js = '';
if ($blog->count_children()) {
$js = require('index.js.php');
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
$editing = true;
$posts = ArtefactTypeBlogPost::get_posts($id, $limit, $offset, $editing);
ArtefactTypeBlogPost::render_posts($posts, $id);
$strpublished = json_encode(get_string('published', 'artefact.blog'));
$js = <<<EOF
function publish_success(form, data) {
removeElement('publish_' + data.id);
$('poststatus' + data.id).innerHTML = {$strpublished};
}
function delete_success(form, data) {
addElementClass('postdetails_' + data.id, 'hidden');
if ($('postfiles_' + data.id)) {
addElementClass('postfiles_' + data.id, 'hidden');
}
addElementClass('postdescription_' + data.id, 'hidden');
addElementClass('posttitle_' + data.id, 'hidden');
}
EOF;
$smarty = smarty(array('tablerenderer'));
$smarty = smarty(array('paginator'));
$smarty->assign('PAGEHEADING', $blog->get('title'));
$smarty->assign('INLINEJAVASCRIPT', $js);
if (!$USER->get_account_preference('multipleblogs')
&& count_records('artefact', 'artefacttype', 'blog', 'owner', $USER->get('id')) == 1) {
......@@ -67,8 +94,33 @@ if (!$USER->get_account_preference('multipleblogs')
}
$smarty->assign_by_ref('blog', $blog);
$smarty->assign_by_ref('INLINEJAVASCRIPT', $js);
$smarty->assign('PAGEHEADING', $blog->get('title'));
$smarty->assign_by_ref('posts', $posts);
$smarty->display('artefact:blog:view.tpl');
exit;
function publish_submit(Pieform $form, $values) {
$blogpost = new ArtefactTypeBlogPost((int) $values['publish']);
$blogpost->check_permission();
$blogpost->publish();
$form->reply(PIEFORM_OK, array(
'message' => get_string('blogpostpublished', 'artefact.blog'),
'goto' => get_config('wwwroot') . 'artefact/blog/view/?id=' . $blogpost->get('parent'),
'id' => $values['publish'],
));
}
function delete_submit(Pieform $form, $values) {
$blogpost = new ArtefactTypeBlogPost((int) $values['delete']);
$blogpost->check_permission();
if ($blogpost->get('locked')) {
$form->reply(PIEFORM_ERR, get_string('submittedforassessment', 'view'));
}
$blogpost->delete();
$form->reply(PIEFORM_OK, array(
'message' => get_string('blogpostdeleted', 'artefact.blog'),
'goto' => get_config('wwwroot') . 'artefact/blog/view/?id=' . $blogpost->get('parent'),
'id' => $values['delete'],
));
}
?>
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-blog
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
safe_require('artefact', 'blog');