Commit 5f5aa85a authored by Son Nguyen's avatar Son Nguyen
Browse files

Add blog post directly from a page (bug #792645)



Three blocktypes have been updated
1. Blog block (entire journal) -> add shortcut "New entry" to this blog
2. recentposts block (most recent journal entries):
	if there is only one journal -> add shortcut "New entry"
	if more than on -> display a dropdown list of blogs to select
	(these blogs are taken from the block configuration)
3. taggedposts block (tagged journal entries):
    all journals owned by this user will be displayed in the dropdown
list

*Note: Javascript code has been removed from the template.
*Note: Call initialization js code of all blocks

Change-Id: Ibdd63dfee39787f74d872a6a31b3286c63dee70a
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent 662aec08
......@@ -109,6 +109,8 @@ class PluginBlocktypeBlog extends PluginBlocktype {
$smarty->assign('owner', $blog->get('owner'));
$smarty->assign('tags', $blog->get('tags'));
$smarty->assign('blockid', $instance->get('id'));
$smarty->assign('editing', $editing);
$smarty->assign('blogid', $blog->get('id'));
$smarty->assign('posts', $posts);
$result = $smarty->fetch('artefact:blog:blog.tpl');
......
function addNewPostShortcut(blockid) {
forEach(
getElementsByTagAndClassName('a', 'shortcut', 'blockinstance_' + blockid),
function(a) {
disconnectAll(a);
connect(a, 'onclick', function(e) {
e.stop();
var p = getFirstParentByTagAndClassName(a, 'div', 'shortcut');
var blogselect = getFirstElementByTagAndClassName('select','select', p);
window.open(config.wwwroot + 'artefact/blog/post.php?blog=' + blogselect.value,'_blank');
});
}
);
}
......@@ -42,6 +42,16 @@ class PluginBlocktypeRecentposts extends PluginBlocktype {
return array('blog');
}
public static function get_instance_javascript(BlockInstance $bi) {
$blockid = $bi->get('id');
return array(
array(
'file' => 'js/recentposts.js',
'initjs' => "addNewPostShortcut($blockid);",
)
);
}
public static function render_instance(BlockInstance $instance, $editing=false) {
$configdata = $instance->get('configdata');
......@@ -77,6 +87,17 @@ class PluginBlocktypeRecentposts extends PluginBlocktype {
$smarty = smarty_core();
$smarty->assign('mostrecent', $mostrecent);
$smarty->assign('view', $instance->get('view'));
$smarty->assign('blockid', $instance->get('id'));
$smarty->assign('editing', $editing);
if ($editing) {
// Get id and title of configued blogs
$recentpostconfigdata = $instance->get('configdata');
$wherestm = ' WHERE id IN (' . join(',', array_fill(0, count($recentpostconfigdata['artefactids']), '?')) . ')';
if (!$selectedblogs = get_records_sql_array('SELECT id, title FROM {artefact}'. $wherestm, $recentpostconfigdata['artefactids'])) {
$selectedblogs = array();
}
$smarty->assign('blogs', $selectedblogs);
}
$result = $smarty->fetch('blocktype:recentposts:recentposts.tpl');
}
......
{if ($editing)}
<div class="shortcut right nojs-hidden-block">
<div{if (count($blogs) == 1)} class="hidden"{/if}>
<span class="text">{str tag='shortcutaddpost' section='artefact.blog'}</span>
<select id="blogselect_{$blockid}" class="select">
{foreach from=$blogs item=blog}
<option value="{$blog->id}"> {$blog->title} </option>
{/foreach}
</select>
<a class="shortcut btn">{str tag='shortcutgo' section='artefact.blog'}</a>
</div>
<a class="shortcut btn{if (count($blogs) != 1)} hidden{/if}">{str tag='shortcutnewentry' section='artefact.blog'}</a>
</div>
{/if}
<ul class="recentblogpost">
{foreach from=$mostrecent item=post}
<li>
......
function addNewTaggedPostShortcut(blockid) {
forEach(
getElementsByTagAndClassName('a', 'shortcut', 'blockinstance_' + blockid),
function(a) {
disconnectAll(a);
connect(a, 'onclick', function(e) {
e.stop();
var p = getFirstParentByTagAndClassName(a, 'div', 'shortcut');
var selectedBlog = getFirstElementByTagAndClassName('select','select', p);
var currentTag = getFirstElementByTagAndClassName('input','select', p);
var BlogIDInput = INPUT({'name': 'blog', 'type': 'text', 'value': selectedBlog.value});
var TagInput = INPUT({'name': 'tagselect', 'type': 'text', 'value': currentTag.value});
var myForm = FORM(
{'action': config.wwwroot + 'artefact/blog/post.php', 'target': '_blank', 'method': 'POST'},
BlogIDInput,
TagInput
);
document.body.appendChild(myForm);
myForm.submit();
});
}
);
}
......@@ -41,6 +41,16 @@ class PluginBlocktypeTaggedposts extends SystemBlocktype {
return array('blog');
}
public static function get_instance_javascript(BlockInstance $bi) {
$blockid = $bi->get('id');
return array(
array(
'file' => 'js/taggedposts.js',
'initjs' => "addNewTaggedPostShortcut($blockid);",
)
);
}
public static function render_instance(BlockInstance $instance, $editing=false) {
global $USER;
......@@ -70,6 +80,17 @@ class PluginBlocktypeTaggedposts extends SystemBlocktype {
$results = get_records_sql_array($sql, array($view, $tagselect, $limit));
$smarty->assign('blockid', $instance->get('id'));
$smarty->assign('editing', $editing);
if ($editing) {
// Get list of blogs owned by this user to create the "Add new post" shortcut while editing
$viewowner = $instance->get_view()->get('owner');
if (!$viewowner || !$blogs = get_records_select_array('artefact', 'artefacttype = \'blog\' AND owner = ?', array($viewowner), 'title ASC', 'id, title')) {
$blogs = array();
}
$smarty->assign('tagselect', $tagselect);
$smarty->assign('blogs', $blogs);
}
// if posts are not found with the selected tag, notify the user
if (!$results) {
......
{if ($editing)}
<div class="shortcut right nojs-hidden-block">
<div{if (count($blogs) == 1)} class="hidden"{/if}>
<span class="text">{str tag='shortcutaddpost' section='artefact.blog'}</span>
<select id="blogselect_{$blockid}" class="select">
{foreach from=$blogs item=blog}
<option value="{$blog->id}"> {$blog->title} </option>
{/foreach}
</select>
<input class="select" type="hidden" value="{$tagselect}">
<a class="shortcut btn">{str tag='shortcutgo' section='artefact.blog'}</a>
</div>
<a class="shortcut btn{if (count($blogs) != 1)} hidden{/if}">{str tag='shortcutnewentry' section='artefact.blog'}</a>
</div>
{/if}
{if $configerror}
{str tag='configerror' section='blocktype.blog/taggedposts'}
{elseif $badtag}
......
......@@ -146,3 +146,7 @@ $string['feedrights'] = 'Copyright %s.';
$string['enablemultipleblogstext'] = 'You have one journal. If you would like to start a second one, enable the multiple journals option on the <a href="%saccount/">account settings</a> page.';
$string['hiddenblogsnotification'] = 'Additional journal(s) have been made for you, but your account does not have the multiple journals option. You can set that on the <a href="%saccount/">account settings</a> page.';
$string['shortcutaddpost'] = 'Add a new entry to';
$string['shortcutgo'] = 'Go';
$string['shortcutnewentry'] = 'New entry';
......@@ -44,6 +44,10 @@ safe_require('artefact', 'file');
*/
$blogpost = param_integer('blogpost', param_integer('id', 0));
if (!$blogpost) {
/*
* For a new post, a tag can be set from tagged blogpost block
*/
$tagselect = param_variable('tagselect', '');
$blog = param_integer('blog');
if (!get_record('artefact', 'id', $blog, 'owner', $USER->get('id'))) {
// Blog security is also checked closer to when blogs are added, this
......@@ -53,7 +57,7 @@ if (!$blogpost) {
}
$title = '';
$description = '';
$tags = array();
$tags = array($tagselect);
$checked = '';
$pagetitle = get_string('newblogpost', 'artefact.blog', get_field('artefact', 'title', 'id', $blog));
$focuselement = 'title';
......
{if ($editing)}
<div class="right">
<span> <a class="btn" href="{$WWWROOT}artefact/blog/post.php?blog={$blogid}" target="_blank">{str tag='shortcutnewentry' section='artefact.blog'}</a> </span>
</div>
{/if}
{if !$options.hidetitle}
<h2>{$artefacttitle|safe}</h2>
{/if}
......
......@@ -364,6 +364,9 @@ form.pagination {
background-color: #FFFFFF;
border: 1px solid #D1D1D1;
}
.blockinstance-content div.shortcut {
padding: 3px 0px 3px 0px;
}
/**************************** Forms ****************************/
form {
margin: 0;
......
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