3 minutes reading time (618 words)

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

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':

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)) {
// 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
$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');
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');
$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;
// 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)) {

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


$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');
$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;

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>
<name>Add Article</name>
<description>Add New Blog Post.</description>
<name>Edit Article</name>
<description>Edit New Blog Post.</description>
<name>Post Comment</name>
<description>Post New Blog Comment.</description>

5. Add the following lines at the bottom of "language/en-GB/en-GB.com_k2.ini" (or in your respective language):
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

A Newbie Asks: What Mascot Best Represents Joomla!


Already Registered? Login Here
No comments made yet. Be the first to submit a comment

By accepting you will be accessing a service provided by a third-party external to https://magazine.joomla.org/