The Joomla! ® Community Magazine

JomSocial & K2 integration (Joomla! 1.7) - Part 1

Written by | Thursday, 01 December 2011 00:00 | Published in 2011 December
Level of Difficulty:Intermediate

Here is a step-by-step tutorial on how-to:

1. Make entry in the JomSocial feed on new/edited article.
2. Make entry in the JomSocial feed on new comment.
3. Add user points for new article.
4. Add user points for edited article.
5. Add user points for new comment.

The tutorial:

1. Make a backup. Whenever you change files, please make a backup first.
2. Open the file "administrator/components/com_k2/models/item.php"
In that file find:
case 'save':
default:

Right after it, place the following code:
// Hack to add new or edited articles in the JomSocial activity stream
// Check if JomSocial is installed
$jcheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'core.php';
// Check if the JomSocial library exist and if so, include it
if ( file_exists($jcheck)) {
require_once($jcheck);
}
// Get the current article title
$contentTitle = $row->title;
// Get the current article url
$link = 'index.php?option=com_k2&view=item&id='.$row->id;
// Add proper routing
$link = K2HelperRoute::getItemRoute($row->id.':'.urlencode($row->alias),$row->catid.':'.urlencode($row->category->alias));

// Post to JomSocial activity stream
$item->link=$link;
$act = new stdClass();
$act->cmd = 'wall.write';
$act->actor = $user->id;
$act->target = 0; // no target
if($isNew) {
$act->title = JText::_('K2_JOMSOCIAL_ARTICLE').' <a href="'.$link.'">'.$contentTitle.'</a>';
// Add user points for new article
if ( file_exists($jcheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.article.new');
}
}
else {
// Add user points for edited article
$act->title = JText::_('K2_JOMSOCIAL_EDIT').' <a href="'.$link.'">'.$contentTitle.'</a>';
if ( file_exists($jcheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.article.edit');
}
}
$act->content = '';
$act->app = 'wall';
$act->cid = 0;

CFactory::load('libraries', 'activities');
// Add support for like and comments in JomSocial activity stream
$act->comment_type  = 'k2.article';
$act->comment_id    = CActivities::COMMENT_SELF;
$act->like_type     = 'k2.article';
$act->like_id     = CActivities::LIKE_SELF;
CActivityStream::add($act);
// End JomSocial activity stream hack

Note: The code above does everything necessary for Item adding and editing in the frontend. If you want to fully cover the backend adding and editing articles, add the same code after:
case 'apply':

and after
case 'saveAndNew':

3. Open the file "components/com_k2/models/item.php"

Find in it:
if ($params->get('commentsPublishing')) {

And add right after it the following code:
// check if JomSocial is installed
$jscheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'core.php';

//activity stream  - added a comment
if ( file_exists($jscheck)) {
require_once($jscheck);
}

$contentTitle = $item->title;
$link = K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias),$item->catid.':'.urlencode($item->category->alias));

//$item->link=urldecode(JRoute::_($link));
$item->link=$link;

$act = new stdClass();
$act->cmd    = 'wall.write';
$act->actor    = $user->id;
$act->target    = 0; // no target
$act->title    = JText::_('K2_JOMSOCIAL_COMMENT').' <a href="'.$item->link.'">'.$contentTitle.'</a>';
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_k2.comment.add');
$act->content    = '';
$act->app    = 'wall';
$act->cid    = 0;
      
CFactory::load('libraries', 'activities');
$act->comment_type  = 'k2.comment';
$act->comment_id    = CActivities::COMMENT_SELF;
$act->like_type     = 'k2.comment';
$act->like_id     = CActivities::LIKE_SELF;
CActivityStream::add($act);

4. Create a new file named "jomsocial_rule.xml" with the content bellow and upload it to "components/com_k2/"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE jomsocial>
<jomsocial>
<component>com_k2</component>
<rules>
<rule>
<name>Add Article</name>
<description>Add New Blog Post.</description>
<action_string>com_k2.article.new</action_string>
<publish>true</publish>
<points>10</points>
<access_level>1</access_level>
</rule>
<rule>
<name>Edit Article</name>
<description>Edit New Blog Post.</description>
<action_string>com_k2.article.edit</action_string>
<publish>true</publish>
<points>1</points>
<access_level>1</access_level>
</rule>
<rule>
<name>Post Comment</name>
<description>Post New Blog Comment.</description>
<action_string>com_k2.comment.add</action_string>
<publish>true</publish>
<points>1</points>
<access_level>1</access_level>
</rule>
</rules>
</jomsocial>

5. Add the following lines at the bottom of "language/en-GB/en-GB.com_k2.ini" (or in your respective language):
;JomSocial
K2_JOMSOCIAL_COMMENT="{actor} commented on the article"
K2_JOMSOCIAL_ARTICLE="{actor} added the new article"
K2_JOMSOCIAL_EDIT="{actor} edited the article"


6. Go to JomSocial in the backend, there go to User Points, click on "Rule Scan" and it should find the three new rules (add article, edit article, add comment). Click on refresh. By default it gives 10 points for new article, 1 for edited article and 1 for comment. You can edit the points after refresh and clicking on the name of the rule.

That's it. Now when your users add or edit content or add comments, they will get points and this will appear in their stream in JomSocial.
We will try to write a tutorial on how to display JomSocial data in K2 items.

P.S. If You have any troubles or questions please feel free to ask a question at our forum post

Read 24217 times
Tagged under Administrators

Leave a comment

Make sure you enter the (*) required information where indicated.

[b] [i] [u] [s] [url] [quote] [code] [img]   

Comments (8)

  • avatar
    • 1
    • 0
    Danilo

    Hi,
    do you know if there's a way to insert JomSocial's Like/Dislike in articles? I managed to do so in Joomla! 1.5, I replicated the code on 1.7 but in the end I noticed everything works but the buttons. There has to be some problems with the javascript when it's not called inside the component, as if I force a Like with the function it actually inserts everything in the database as it should. Can you help me in any way? I can provide you with all the changes to the code I made.

  • avatar
    • 0
    • 0
    Grigor Mihov

    Do you mean the regular Joomla! articles?
    If so, the logic is very similar, however all changes will be lost with each update of Joomla!

  • avatar
    • 0
    • 0
    Torgrim Sandvoll

    Thank you Grigor for a very useful article, and looking forward to your next! :)

    PS: To make you article more visual informative and no so "texty" please add images of the final results. ("Heres an image of what you are going see after adding this code?";)

  • avatar
    • 0
    • 0
    Arkadas

    I can provide you with all the changes to the code I made.

  • avatar
    • 0
    • 0
    juan

    hello, it works perfect when i add an article from the front page, but it doesnt when i add from backend.. any suggestion?

  • avatar
    • 0
    • 0
    Rob Kneip

    Hi Grigor,
    I have my registration redirected to Jomsocial I dont care about full integration all I want is when the user registers in Jomsocial they are automatically asigned as registered users in K2. Its not doing it. I see the K2 user but he is not assigned to any group. I have to add them manually. Additionaly when I switch back to the K2 user registration Jomsocial sees them but I get an error that says email ####### not found in user account. I would grately apreciate any help Thank you

  • avatar
    • 0
    • 0
    scott adam

    Ok I NEED THIS lol...

    If i am looking at someones jom social profile how can i see a list of all their articles?

    I know i will see it in their stream as and when they write a new one..
    But how could i make a list..eg on profile

    my photos
    my video
    my BLOG POSTS(k2 items)

    thanks for your time