Using CjForum user avatars in your component

From CjWiki
Jump to: navigation, search

Prerequisites:

Add the required CjForum API library:

require_once JPATH_ROOT.'/components/com_cjforum/lib/api.php';

Now get the profile API:

$profileApi = CjForumApi::getProfileApi();

Syntax: getUserAvatar

Syntax of the getUserAvatar API call to get full HTML image tag linked to CjForum profile page of the user:

getUserAvatar($identifiers, $size = 48, $username = 'name', array $attribs = array(), array $image_attribs = array())
$identifiers - int/array or ints
Single user id or array of user ids for which the user avatars need to be fetched. If array of user ids given, the function will return associative array of user profile urls/links with user id as key. Required parameter.
$size - int
Size of the avatar image in pixels. Default 48, optional parameter.
$username - string
Type of user name to be used as alt text. Possible values are name and username. Default: name. Optional parameter.
$attribs - array
Associative array of key value pairs used for building HTML link attributes. Default: array(). Optional parameter.
$image_attribs - array
Associative array of key value pairs used for building HTML image attributes. Default: array(). Optional parameter.

Examples:

Get avatar of the user with full html markup, linked to user profile page.

$avatarLink = $profileApi->getUserAvatar($userId, 48, 'username');

Get avatar of the user with full html markup, linked to user profile page, with user's original name as display name:

$avatarLink = $profileApi->getUserAvatar($userId, 64, 'name');

Get user avatar with some attributes to the html link and image of the markup produced by function call:

$avatarLink = $profileApi->getUserAvatar($userId, 48, 'name', array('class'=>'avatarlink'), array('class'=>'avatar', 'style'=>'border: 1px solid #ccc'));

Syntax: getUserAvatarImage

Syntax of the getUserAvatarImage API call to get location of the user avatar image:

getUserAvatarImage($identifiers, $size = 48, $force_reload = false)
$identifiers - int/array or ints
Single user id or array of user ids for which the user avatar images need to be fetched. If array of user ids given, the function will return associative array of user profile urls/links with user id as key. Required parameter.
$size - int
Size of the avatar image in pixels. Default 48, optional parameter.
$force_reload - boolean
if true, the user avatar name will be fetched from database even if it exists in cache. Optional parameter.

Examples:

Get location of the user avatar with size 48 pixels.

$url = $profileApi->getUserAvatarImage($userId, 48);

Get avatar images of set of users:

$url = $profileApi->getUserAvatarImage($userIds, 64);