JOOMLA FICTION LAB
(3 votes)

How to use the Jomsocial API - Introduction

Saturday, 03 September 2011 23:41
The Jomsocial Developers have equipped Jomsocial with a simple and easy-to-use API. You can use this API to create extensions that are integrated with Jomsocial.

1. The Jomsocial User Object


In Jomsocial you can request the User Object with the class CFactory.

How to get the User Object

// Return current user
$user =& CFactory::getUser();
// Return user with given id
$user =& CFactory::getUser($userId);

Including the Jomsocial core

If you are just customizing the Jomsocial template files or adding new functionalities to Jomsocial pages then the above code is enough.
But if you want to call the CFactory class from outside Jomsocial (for example from another component or from within a module) then you must include the Jomsocial core. In this case, the above code should be:
// Include Jomsocial core
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
// Return current user
$user =& CFactory::getUser();
// Return user with given id
$user =& CFactory::getUser($userId);

2. Using Jomsocial Features in your Extension


The Jomsocial API lets you integrate some basic Jomsocial functionalities with your extension.

JomSocial build-in personal messaging system

With this code, you can send a message to a user through the Jomsocial messaging system. You must use an onclick action to add this functionality to any link.
// Include Jomsocial core
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
// Include Messaging library
include_once($jspath.DS.'libraries'.DS.'messaging.php');
// Add onclick action
$onclick = CMessaging::getPopup($userid);
echo '<a onclick="<?php echo $onclick; ?>" href="javascript:void(0);">';
The $userid can be a specific user id or the current user (see code above).

Link to User Profile Page

By using CRoute which is a replacement for Joomla JRoute, you can link to any part of JomSocial and the link will have the correct Itemid.
// Include Jomsocial core
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
// Get CUser object
$link = CRoute::_('index.php?option=com_community&view=profile&userid='.$userid);
echo '<a href="/.$link.">User profile</a>';





Add comment


Security code
Refresh