Commit df442b6f authored by Melissa Draper's avatar Melissa Draper Committed by Gerrit Code Review
Browse files

Merge "Include "Unpublish" button for published blog posts Bug#605730"

parents a3e4487d 234e9296
......@@ -50,6 +50,7 @@ $string['blogpost'] = 'Journal entry';
$string['blogdeleted'] = 'Journal deleted';
$string['blogpostdeleted'] = 'Journal entry deleted';
$string['blogpostpublished'] = 'Journal entry published';
$string['blogpostunpublished'] = 'Journal entry unpublished';
$string['blogpostsaved'] = 'Journal entry saved';
$string['blogsettings'] = 'Journal settings';
$string['blogtitle'] = 'Title';
......@@ -97,6 +98,7 @@ $string['postedbyon'] = 'Posted by %s on %s';
$string['posttitle'] = 'Title';
$string['posts'] = 'entries';
$string['publish'] = 'Publish';
$string['unpublish'] = 'Unpublish';
$string['publishfailed'] = 'An error occurred. Your entry was not published';
$string['publishblogpost?'] = 'Are you sure you want to publish this entry?';
$string['published'] = 'Published';
......
......@@ -730,10 +730,8 @@ class ArtefactTypeBlogPost extends ArtefactType {
foreach ($data as &$post) {
// Format dates properly
if (is_null($viewoptions)) {
// My Blogs area: create forms for publishing & deleting posts.
if (!$post->published) {
$post->publish = ArtefactTypeBlogpost::publish_form($post->id);
}
// My Blogs area: create forms for changing post status & deleting posts.
$post->changepoststatus = ArtefactTypeBlogpost::changepoststatus_form($post->id, $post->published);
$post->delete = ArtefactTypeBlogpost::delete_form($post->id);
}
else {
......@@ -830,30 +828,42 @@ class ArtefactTypeBlogPost extends ArtefactType {
return true;
}
public static function publish_form($id) {
public static function changepoststatus_form($id, $published = null) {
//Get current post status from database
if ($published === null) {
$post = new ArtefactTypeBlogPost($id);
$published = $post->published;
}
if ($published) {
$strchangepoststatus = get_string('unpublish', 'artefact.blog');
}
else {
$strchangepoststatus = get_string('publish', 'artefact.blog');
}
return pieform(array(
'name' => 'publish_' . $id,
'successcallback' => 'publish_submit',
'name' => 'changepoststatus_' . $id,
'jssuccesscallback' => 'changepoststatus_success',
'successcallback' => 'changepoststatus_submit',
'jsform' => true,
'jssuccesscallback' => 'publish_success',
'renderer' => 'oneline',
'elements' => array(
'publish' => array(
'changepoststatus' => array(
'type' => 'hidden',
'value' => $id,
'help' => true,
),
'submit' => array(
'currentpoststatus' => array(
'type' => 'hidden',
'value' => $published,
),'submit' => array(
'type' => 'submit',
'class' => 'publish',
'value' => get_string('publish', 'artefact.blog'),
'value' => $strchangepoststatus,
'help' => true,
),
),
));
}
public static function delete_form($id) {
global $THEME;
return pieform(array(
......@@ -878,20 +888,20 @@ class ArtefactTypeBlogPost extends ArtefactType {
));
}
/**
* This function publishes the blog post.
* This function changes the blog post status.
*
* @param $newpoststatus: boolean 1=published, 0=draft
* @return boolean
*/
public function publish() {
public function changepoststatus($newpoststatus) {
if (!$this->id) {
return false;
}
$data = (object)array(
'blogpost' => $this->id,
'published' => 1
'blogpost' => $this->id,
'published' => (int) $newpoststatus
);
if (get_field('artefact_blog_blogpost', 'COUNT(*)', 'blogpost', $this->id)) {
......@@ -903,7 +913,6 @@ class ArtefactTypeBlogPost extends ArtefactType {
return true;
}
public static function get_links($id) {
$wwwroot = get_config('wwwroot');
......
......@@ -6,7 +6,11 @@
{str tag=published section=artefact.blog}
{else}
{str tag=draft section=artefact.blog}
{if !$post->locked && !$post->published} {$post->publish|safe}{/if}
{/if}
</th>
<th id="changepoststatus{$post->id}" class="changepoststatus">
{if !$post->locked}
{$post->changepoststatus|safe}
{/if}
</th>
<th class="controls">
......@@ -22,7 +26,7 @@
</th>
</tr>
<tr id="postdescription_{$post->id}">
<td colspan=3>{$post->description|clean_html|safe}</td>
<td colspan=4>{$post->description|clean_html|safe}</td>
</tr>
{if $post->files}
<tr id="postfiles_{$post->id}">
......
......@@ -36,8 +36,8 @@ 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 ($changepoststatus = param_integer('changepoststatus', null)) {
ArtefactTypeBlogpost::changepoststatus_form($changepoststatus);
}
if ($delete = param_integer('delete', null)) {
ArtefactTypeBlogpost::delete_form($delete);
......@@ -75,10 +75,19 @@ $pagination = array(
ArtefactTypeBlogPost::render_posts($posts, $template, array(), $pagination);
$strpublished = json_encode(get_string('published', 'artefact.blog'));
$strdraft = json_encode(get_string('draft', 'artefact.blog'));
$strchangepoststatuspublish = json_encode(get_string('publish', 'artefact.blog'));
$strchangepoststatusunpublish = json_encode(get_string('unpublish', 'artefact.blog'));
$js = <<<EOF
function publish_success(form, data) {
removeElement('publish_' + data.id);
$('poststatus' + data.id).innerHTML = {$strpublished};
function changepoststatus_success(form, data) {
if ($('changepoststatus_' + data.id + '_currentpoststatus').value == 0) {
$('poststatus' + data.id).innerHTML = {$strpublished};
$('changepoststatus_' + data.id + '_submit').value = {$strchangepoststatusunpublish};
}
else {
$('poststatus' + data.id).innerHTML = {$strdraft};
$('changepoststatus_' + data.id + '_submit').value = {$strchangepoststatuspublish};
}
}
function delete_success(form, data) {
addElementClass('postdetails_' + data.id, 'hidden');
......@@ -104,14 +113,21 @@ $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']);
function changepoststatus_submit(Pieform $form, $values) {
$blogpost = new ArtefactTypeBlogPost((int) $values['changepoststatus']);
$blogpost->check_permission();
$blogpost->publish();
$newpoststatus = !($values['currentpoststatus']);
$blogpost->changepoststatus($newpoststatus);
if ($newpoststatus) {
$strmessage = get_string('blogpostpublished', 'artefact.blog');
}
else {
$strmessage = get_string('blogpostunpublished', 'artefact.blog');
}
$form->reply(PIEFORM_OK, array(
'message' => get_string('blogpostpublished', 'artefact.blog'),
'message' => $strmessage,
'goto' => get_config('wwwroot') . 'artefact/blog/view/?id=' . $blogpost->get('parent'),
'id' => $values['publish'],
'id' => $values['changepoststatus'],
));
}
......
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