Commit b2cf8cc4 authored by Nigel McNie's avatar Nigel McNie
Browse files

Make blog posts render in views without javascript.

This patch ditches use of the tablerenderer in favour of just showing the most recent posts, and linking off to view/artefact.php for the rest. I added pagination to assist that.
parent bd8dcda3
......@@ -2,34 +2,13 @@
{$description}
<table id="blog_renderfull{$blockid}">
<thead></thead>
<tbody></tbody>
</table>
<script type="text/javascript">
var blog_renderfull{$blockid} = new TableRenderer(
'blog_renderfull{$blockid}',
config['wwwroot'] + 'artefact/blog/blog_render_self.json.php',
[
{literal}function(r) {
var td = TD();
if (r.content.html) {
td.innerHTML = r.content.html;
}
else {
td.innerHTML = r.content;
}
return td;
}{/literal}
]
);
blog_renderfull{$blockid}.statevars.push('id');
blog_renderfull{$blockid}.id = {$enc_id};
blog_renderfull{$blockid}.limit = {$limit};
blog_renderfull{$blockid}.statevars.push('options');
blog_renderfull{$blockid}.options = {$enc_options};
blog_renderfull{$blockid}.updateOnLoad();
</script>
{foreach from=$postdata item=post}
{$post.content.html}
{/foreach}
{if $newerpostslink || $olderpostslink}
<div class="blog-pagination">
{if $olderpostslink}<div class="fr"><a href="{$olderpostslink|escape}">{str tag=olderposts section=artefact.blog}</a></div>{/if}
{if $newerpostslink}<div><a href="{$newerpostslink|escape}">{str tag=newerposts section=artefact.blog}</a></div>{/if}
</div>
{/if}
......@@ -83,10 +83,12 @@ $string['name'] = 'Name';
$string['newattachmentsexceedquota'] = 'The total size of the new files that you have uploaded to this post would exceed your quota. You may be able to save the post if you remove some of the attachments you have just added.';
$string['newblog'] = 'New Blog';
$string['newblogpost'] = 'New Blog Post in Blog "%s"';
$string['newerposts'] = 'Newer posts';
$string['nopostsaddone'] = 'No posts yet. %sAdd one%s!';
$string['noimageshavebeenattachedtothispost'] = 'No images have been attached to this post. You need to upload or attach an image to the post before you can insert it.';
$string['nofilesattachedtothispost'] = 'No attached files';
$string['noresults'] = 'No blog posts found';
$string['olderposts'] = 'Older posts';
$string['postbody'] = 'Body';
$string['postbodydesc'] = ' ';
$string['postedon'] = 'Posted on';
......
......@@ -179,12 +179,6 @@ class ArtefactTypeBlog extends ArtefactType {
* @return array A two key array, 'html' and 'javascript'.
*/
public function render_self($options) {
// This is because if there are multiple blocks on a page, they need separate
// js variables.
$blockid = isset($options['blockid'])
? $options['blockid']
: mt_rand();
$this->add_to_render_path($options);
$smarty = smarty_core();
......@@ -197,16 +191,49 @@ class ArtefactTypeBlog extends ArtefactType {
$smarty->assign('artefacttitle', hsc($this->get('title')));
}
$smarty->assign('blockid', $blockid);
$smarty->assign('options', $options);
$smarty->assign('enc_id', json_encode($this->id));
$smarty->assign('limit', self::pagination);
$smarty->assign('loading_img', theme_get_url('images/loading.gif'));
$smarty->assign('description', clean_html($this->get('description')));
// Remove unnecessary options for blog posts
unset($options['hidetitle']);
$smarty->assign('enc_options', json_encode(json_encode($options)));
$page = (isset($options['page'])) ? abs(intval($options['page'])) : abs(param_integer('page', 1));
$offset = $page ? $page * self::pagination - self::pagination : 1;
$postids = get_column_sql("
SELECT a.id
FROM {artefact} a
LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost
WHERE a.parent = ?
AND bp.published = 1
ORDER BY a.ctime DESC
LIMIT ? OFFSET ?", array($this->get('id'), self::pagination, $offset));
$postcount = $this->count_published_posts();
$data = array();
foreach($postids as $postid) {
$blogpost = new ArtefactTypeBlogPost($postid);
$data[] = array(
'id' => $postid,
'content' => $blogpost->render_self($options)
);
}
$smarty->assign('postdata', $data);
// Pagination
if ($postcount > self::pagination) {
$baselink = get_config('wwwroot') . 'view/artefact.php?artefact=' . $this->get('id');
if (isset($options['viewid'])) {
$baselink .= '&view=' . $options['viewid'];
}
if ($offset + self::pagination < $postcount) {
$smarty->assign('olderpostslink', $baselink . '&page=' . ($page + 1));
}
if ($offset > 0) {
$smarty->assign('newerpostslink', $baselink . '&page=' . ($page - 1));
}
}
return array('html' => $smarty->fetch('blocktype:blog:blog_render_self.tpl'), 'javascript' => '');
}
......@@ -293,6 +320,23 @@ class ArtefactTypeBlog extends ArtefactType {
$new->set('title', get_string('Copyof', 'mahara', $this->get('title')));
}
/**
* Returns the number of posts in this blog that have been published.
*
* The result of this function looked up from the database each time, so
* cache it if you know it's safe to do so.
*
* @return int
*/
public function count_published_posts() {
return (int)get_field_sql("
SELECT COUNT(*)
FROM {artefact} a
LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost
WHERE a.parent = ?
AND bp.published = 1", array($this->get('id')));
}
}
/**
......
......@@ -1918,6 +1918,15 @@ tr.highlight-file td {
color: #474220;
font-weight: normal;
}
.blog-pagination {
max-width: 20em;
margin: 0 auto;
font-weight: bold;
height: 1.2em;
}
.blog-pagination a {
text-decoration: underline;
}
.filebrowser .blogpost-attachments {
margin: 3px;
}
......
Markdown is supported
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