Commit 11e31b3b authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Enable a single blog post to be displayed by post id (Bug #681210)"

parents f427a572 773cfc7a
......@@ -696,6 +696,43 @@ class ArtefactTypeBlogPost extends ArtefactType {
public static function collapse_config() {
}
/**
* This function returns the blog id and offset for a given post.
*
* @param integer $postid The id of the required blog post
* @return object An object containing the required data
*/
public static function get_post_data($postid) {
$post = new stdClass();
$post->blogid = get_field('artefact', 'parent', 'id', $postid, 'artefacttype', 'blogpost');
if (is_postgres()) {
$rownum = get_field_sql("SELECT rownum
FROM (SELECT id, ROW_NUMBER() OVER (ORDER BY id DESC) AS rownum
FROM {artefact}
WHERE parent = ?
ORDER BY id DESC) AS posts
WHERE id = ?",
array($post->blogid, $postid));
}
else if (is_mysql()) {
$initvar = execute_sql("SET @row_num = 0");
if ($initvar) {
$rownum = get_field_sql("SELECT rownum
FROM (SELECT id, @row_num := @row_num + 1 AS rownum
FROM {artefact}
WHERE parent = ?
ORDER BY id DESC) AS posts
WHERE id = ?",
array($post->blogid, $postid));
}
}
$post->offset = $rownum - 1;
return $post;
}
/**
* This function returns a list of posts in a given blog.
*
......@@ -824,6 +861,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
'jsonscript' => $pagination['jsonscript'],
'count' => $posts['count'],
'limit' => $posts['limit'],
'setlimit' => $pagination['setlimit'],
'offset' => $posts['offset'],
'numbersincludefirstlast' => false,
'resultcounttextsingular' => get_string('post', 'artefact.blog'),
......@@ -968,7 +1006,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
$wwwroot = get_config('wwwroot');
return array(
'_default' => $wwwroot . 'artefact/blog/post.php?blogpost=' . $id,
'_default' => $wwwroot . 'artefact/blog/view/index.php?blogpost=' . $id,
);
}
......
......@@ -18,6 +18,7 @@ require_once(get_config('libroot') . 'pieforms/pieform.php');
$id = param_integer('id');
$limit = param_integer('limit', 5);
$setlimit = param_integer('setlimit', 0);
$offset = param_integer('offset', 0);
if (!$USER->can_edit_artefact(new ArtefactTypeBlog($id))) {
......@@ -31,6 +32,7 @@ $pagination = array(
'id' => 'blogpost_pagination',
'jsonscript' => 'artefact/blog/view/index.json.php',
'datatable' => 'postlist',
'setlimit' => $setlimit,
);
ArtefactTypeBlogPost::render_posts($posts, $template, array(), $pagination);
......
......@@ -30,6 +30,15 @@ if ($delete = param_integer('delete', null)) {
}
$id = param_integer('id', null);
if ($blogpost = param_integer('blogpost', null)) {
$post = ArtefactTypeBlogPost::get_post_data($blogpost);
$id = $post->blogid;
$limit = 1;
$setlimit = 1;
$offset = $post->offset;
}
if (is_null($id)) {
if (!$records = get_records_select_array(
'artefact',
......@@ -47,8 +56,17 @@ else {
}
$blog->check_permission();
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
if (!isset($limit)) {
$limit = param_integer('limit', 5);
}
if (!isset($setlimit)) {
$setlimit = 0;
}
if (!isset($offset)) {
$offset = param_integer('offset', 0);
}
$posts = ArtefactTypeBlogPost::get_posts($id, $limit, $offset);
$template = 'artefact:blog:posts.tpl';
......@@ -57,6 +75,7 @@ $pagination = array(
'id' => 'blogpost_pagination',
'jsonscript' => 'artefact/blog/view/index.json.php',
'datatable' => 'postlist',
'setlimit' => $setlimit,
);
ArtefactTypeBlogPost::render_posts($posts, $template, array(), $pagination);
......
......@@ -3535,7 +3535,7 @@ function str_shorten_text($str, $maxlen=100, $truncate=false) {
* @param array $params Options for the pagination
*/
function build_pagination($params) {
$limitoptions = array(10, 20, 50, 100, 500);
$limitoptions = array(1, 10, 20, 50, 100, 500);
// Bail if the required attributes are not present
$required = array('url', 'count', 'limit', 'offset');
foreach ($required as $option) {
......
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