Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Blog Module

Discuss the Blog Module.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Combining Tagcloud/ RSS Feeds/ Latest Posts from multiple BlogHolders?


Reply

6 Posts   1890 Views

Avatar
Tama

12 October 2009 at 10:53am Community Member, 130 Posts

If you are running multiple blogs under the same SS installation (so multiple BlogHolders in the root level) what's the best way to combine their tagclouds, RSS feeds and latest posts?

For example having the tagcloud display the tags from all of the blogs in one place and clicking on a tag will display all relevant tagged posts across all blogs.

I'd also like to be able to setup a RSS feed which aggregates all of the blogs and have the latest posts from all blogs displayed on the home page. Tweaking the "LatestNews" function from the tutorial only pulls the latest posts from the first BlogHolder in the Sitetree

   function LatestPosts($num=5) {
    $posts = DataObject::get_one("BlogHolder");
    return ($posts) ? DataObject::get("BlogEntry", "ParentID = $posts->ID", "Date DESC", "", $num) : false;
   }

Any ideas?

Cheers
Tama

Avatar
Tama

14 October 2009 at 8:49am Community Member, 130 Posts

Just as a follow up - Latest Posts from all Blogs is very straightforward:

function LatestPosts($num=5) {
return DataObject::get("BlogEntry", "ParentID > 0", "Date DESC", "", $num);
}

For the Tagcloud and RSS feeds it looks like I'll have to rewrite the Widget code slightly - which is no biggy. My only hang up is how to do the tag links. Out of the box tag links look like this: http://www.yourdomain.com/blog-name/tag/satan - which needs to be changed to reflect the posts from all blogs.

Will let you know how I go.

Avatar
arnott

15 October 2009 at 6:59am Community Member, 16 Posts

hi Tama,
I am interested in combining stuff across multiple blogs. I am new to SS. So you make the changes in Widget.php and access it from the homepage ?

thanks,
arnott

Avatar
Tama

15 October 2009 at 7:23am Community Member, 130 Posts

Hi Arnott - I'm still feeling my way and haven't got everything working, yet. For the "global" tagcloud (combining tags from all Blogs) I used the following code in "Page.php"

function GlobalTags() {

$allTags = array();
$max = 0;

$entries = DataObject::get("BlogEntry","`ParentID` > 0","`BlogEntry`.Date DESC",'',"$limit");

if($entries) {
foreach($entries as $entry) {
$theseTags = split(" *, *", strtolower(trim($entry->Tags)));
foreach($theseTags as $tag) {
if($tag != "") {
$allTags[$tag] = isset($allTags[$tag]) ? $allTags[$tag] + 1 : 1; //getting the count into key => value map
$max = ($allTags[$tag] > $max) ? $allTags[$tag] : $max;
}
}
}

if($allTags) {
//TODO: move some or all of the sorts to the database for more efficiency
if($this->Limit > 0){
uasort($allTags, array($this, "column_sort_by_popularity")); //sort by popularity
$allTags = array_slice($allTags, 0, $this->Limit);
}
if($this->Sortby == "alphabet"){
$this->natksort($allTags);
}

$sizes = array();
foreach($allTags as $tag => $count){
$sizes[$count] = true;
}
$numsizes = count($sizes)-1; //Work out the number of different sizes
if($numsizes > 5){$numsizes = 5;}
foreach($allTags as $tag => $count) {

$popularity = floor($count / $max * $numsizes);

switch($popularity) {
case 0:
$class = "not-popular";
break;
case 1:
$class = "not-very-popular";
break;
case 2:
$class = "somewhat-popular";
break;
case 3:
$class = "popular";
break;
case 4:
$class = "very-popular";
break;
case 5:
$class = "ultra-popular";
break;
default:
$class = "broken";
break;
}

$output .='<a href="/home/tag/'.urlencode($tag).'" class="'.$class.'">'.$tag.'</a> ';

}
}

$output = "TagCloud: ".$output;
return $output;
}

return;
}

/**
* Helper method to compare 2 Vars to work out the results.
* @param mixed
* @param mixed
* @return int
*/
private function column_sort_by_popularity($a, $b){
if($a == $b) {
$result = 0;
}
else {
$result = $b - $a;
}
return $result;
}

private function natksort(&$aToBeSorted) {
$aResult = array();
$aKeys = array_keys($aToBeSorted);
natcasesort($aKeys);
foreach ($aKeys as $sKey) {
$aResult[$sKey] = $aToBeSorted[$sKey];
}
$aToBeSorted = $aResult;

return true;
}

Putting $GlobalTags in your template file will display the tagcloud.

However I've yet to write some code to recognise /home/tag/tagname - but I think I should be able to use my LatestPosts function to do that. Hope this makes sense, I'll post it up once I've got it working.

Avatar
arnott

15 October 2009 at 7:50am Community Member, 16 Posts

Thanks Tama. This is very useful.

On a different note, do you know how to create 'security group', so that users can only post comments to the blog ?

arnott

Avatar
arnott

23 October 2009 at 9:10am Community Member, 16 Posts

To get latest 10 posts from multiple blogs:

page.ss:

function LatestPosts($num=10) {
return DataObject::get("BlogEntry", "ParentID > 0", "Date DESC", "", $num);
}

and templates/Layout/Page.ss

Latest posts:
<ul id="PostsList">
<% control LatestPosts %>
<li class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
<li class="newsDateTitle">$Date.Nice</li>
<li class="newsSummary">$Content.FirstParagraph<a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></li>
<% end_control %>
</ul>

Is there a simple way to add the Blogname to the post title ?

Thanks,
arnott