Commit 09ea00e2 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Simplify my blogs listing page -- remove use of tablerenderer and ajax delete

parent 2124a30c
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* 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-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('artefact', 'blog');
$action = param_variable('action', 'list');
$id = param_variable('id', null);
json_headers();
if ($action == 'list') {
$limit = param_integer('limit', ArtefactTypeBlog::pagination);
$offset = param_integer('offset', 0);
list($count, $data) = ArtefactTypeBlog::get_blog_list($USER, $limit, $offset);
echo json_encode(array(
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'data' => $data
));
}
else if ($action == 'delete') {
$blog = artefact_instance_from_id($id);
if ($blog instanceof ArtefactTypeBlog) {
$blog->check_permission();
$blog->delete();
json_reply(false, get_string('blogdeleted', 'artefact.blog'));
}
throw new ArtefactNotFoundException(get_string('blogdoesnotexist', 'artefact.blog'));
}
?>
......@@ -34,69 +34,26 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('myblogs','artefact.blog'));
safe_require('artefact', 'blog');
// This is the wwwroot.
$wwwroot = get_config('wwwroot');
$enc_delete = json_encode(get_string('delete', 'artefact.blog'));
$enc_confirmdelete = json_encode(get_string('deleteblog?', 'artefact.blog'));
$enc_addpost = json_encode(get_string('addpost', 'artefact.blog'));
$enc_settings = json_encode(get_string('settings', 'artefact.blog'));
// This JavaScript creates a table to display the blog entries.
$js = <<<EOF
var bloglist = new TableRenderer(
'bloglist',
'index.json.php',
[
function(r) {
return TD(
null,
A({'href':'{$wwwroot}artefact/blog/view/?id=' + r.id}, r.title)
);
},
function(r) {
var td = TD();
td.innerHTML = r.description;
return td;
},
function (r) {
var deleteButton = A({'class': 'btn-del', 'href': ''}, {$enc_delete});
connect(deleteButton, 'onclick', function (e) {
e.stop();
if (!confirm({$enc_confirmdelete})) {
return false;
}
sendjsonrequest(
'index.json.php',
{
'action': 'delete',
'id': r.id
},
'POST',
function (data) {
bloglist.doupdate();
}
);
return false;
});
return TD({'class': 'controls'}, [
deleteButton,
A({'class': 'btn-edit', 'href':'{$wwwroot}artefact/blog/settings/?id=' + r.id}, {$enc_settings}),
A({'class': 'btn-add', 'href':'{$wwwroot}artefact/blog/post.php?blog=' + r.id}, {$enc_addpost})
]);
}
]
if ($delete = param_integer('delete', 0)) {
$blog = artefact_instance_from_id($delete);
if ($blog instanceof ArtefactTypeBlog) {
$blog->check_permission();
$blog->delete();
$SESSION->add_ok_msg(get_string('blogdeleted', 'artefact.blog'));
}
}
$blogs = (object) array(
'offset' => param_integer('offset', 0),
'limit' => param_integer('limit', 10),
);
bloglist.updateOnLoad();
EOF;
list($blogs->count, $blogs->data) = ArtefactTypeBlog::get_blog_list($blogs->limit, $blogs->offset);
ArtefactTypeBlog::build_blog_list_html($blogs);
$smarty = smarty(array('tablerenderer'));
$smarty->assign_by_ref('INLINEJAVASCRIPT', $js);
$smarty = smarty();
$smarty->assign_by_ref('blogs', $blogs);
$smarty->assign('PAGEHEADING', hsc(get_string("myblogs", "artefact.blog")));
$smarty->display('artefact:blog:list.tpl');
$smarty->display('artefact:blog:index.tpl');
?>
......@@ -273,21 +273,43 @@ class ArtefactTypeBlog extends ArtefactType {
* @param User
* @return array (count: integer, data: array)
*/
public static function get_blog_list(User $user, $limit = self::pagination, $offset = 0) {
public static function get_blog_list($limit, $offset) {
global $USER;
($result = get_records_sql_array("
SELECT id, title, description
FROM {artefact}
WHERE owner = ?
AND artefacttype = 'blog'
ORDER BY title
LIMIT ? OFFSET ?", array($user->get('id'), $limit, $offset)))
LIMIT ? OFFSET ?", array($USER->get('id'), $limit, $offset)))
|| ($result = array());
$count = (int)get_field('artefact', 'COUNT(*)', 'owner', $user->get('id'), 'artefacttype', 'blog');
$count = (int)get_field('artefact', 'COUNT(*)', 'owner', $USER->get('id'), 'artefacttype', 'blog');
return array($count, $result);
}
public static function build_blog_list_html(&$blogs) {
$smarty = smarty_core();
$smarty->assign_by_ref('blogs', $blogs);
$blogs->html = $smarty->fetch('artefact:blog:bloglist.tpl');
$blogs->pagination = build_pagination(array(
'id' => 'bloglist_pagination',
'class' => 'center',
'url' => get_config('wwwroot') . 'artefact/blog/index.php',
'count' => $blogs->count,
'limit' => $blogs->limit,
'offset' => $blogs->offset,
'firsttext' => '',
'previoustext' => '',
'nexttext' => '',
'lasttext' => '',
'numbersincludefirstlast' => false,
'resultcounttextsingular' => get_string('blog', 'artefact.blog'),
'resultcounttextplural' => get_string('blogs', 'artefact.blog'),
));
}
/**
* This function creates a new blog.
*
......
{if empty($blogs->data)}
<div>{str tag=youhavenoblogs section=artefact.blog}</div>
{else}
<table class="tablerenderer fullwidth">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{foreach from=$blogs->data item=blog}
<tr class="{cycle name=rows values=r1,r0}">
<td>
<div><strong><a href="{$WWWROOT}artefact/blog/view/?id={$blog->id}">{$blog->title|escape}</a></strong></div>
<div>{$blog->description|clean_html}</div>
</td>
<td class="right">
<a href="{$WWWROOT}artefact/blog/settings/?id={$blog->id}" class="btn-edit">{str tag=settings}</a>
<a href="{$WWWROOT}artefact/blog/index.php?delete={$blog->id}" class="btn-del">{str tag=delete}</a>
<a href="{$WWWROOT}artefact/blog/post.php?blog={$blog->id}" class="btn-add">{str tag=addpost section=artefact.blog}</a>
</td>
</tr>
{/foreach}
</tbody>
</table>
{/if}
......@@ -3,18 +3,9 @@
<div class="rbuttons">
<a class="btn-add" href="{$WWWROOT}artefact/blog/new/">{str section="artefact.blog" tag="addblog"}</a>
</div>
<table id="bloglist" class="hidden tablerenderer fullwidth">
<thead>
<tr>
<th>{str section="artefact.blog" tag="title"}</th>
<th>{str section="artefact.blog" tag="description"}</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div id="bloglist">
{$blogs->html}
{*include file="artefact:blog:bloglist.tpl" blogs=$blogs*}
</div>
{$blogs->pagination.html}
{include file="footer.tpl"}
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