Difference between revisions of "JomSocial User Object"

(Using Proper Names)
(Accessing Joomla User Properties)
Line 47: Line 47:
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
$cuser = CFactory::getUser();
 
$cuser = CFactory::getUser();
$cuserName = $cuser->name;
+
$data = $cuser->name;
echo $cuserName;
+
echo $data;
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<br />
 
<br />

Revision as of 14:15, 5 September 2013

Retrieve User Object

There are several ways you can retrieve the user object which is going to be manipulated.
The basic way to retrieve the currently logged in user is shown bellow. If no one is logged in, it will return "guest" object.

$cuser = CFactory::getUser();


Most common practice to get the user object in third-party component which are likely to already have user object is with given ID

// $userid here is the given ID you should already have retrieved
$cuser = CFactory::getUser($userId);


And finaly, you can retrieve the user object of the currently requested user.
In simpler terms, this means that you can retrieve the user object that currently is being looked at.
This is particulary useful for profile applications as it will always return the currently requested user object (the user that is currently loaded)

$cuser = CFactory::getRequestUser();

Extending The CUser Object

JomSocial user object can be extended on many other ways. You can also get a lot of information from it. We will give you few examples

Accessing Joomla User Properties

Whenever CUser object is retrieved, it inherit most of the JUser object properties and you can access them as well

  • id - The unique, numerical user id.
  • name - The name of the user.
  • username - The username of the user
  • email - The email address of the user
  • password - The encrypted version of the user's password
  • block - Set to '1' when the user is set to 'blocked' in Joomla!.
  • registerDate - Set to the date when the user was first registered.
  • lastvisitDate - Set to the date the user last visited the site.

Usage:

$cuser->id;
$cuser->name;
$cuser->username;
$cuser->email;
$cuser->password;
$cuser->block;
$cuser->registerDate;
$cuser->lastvisitDate;


Example

$cuser = CFactory::getUser();
$data = $cuser->name;
echo $data;


This example echoes the raw username of the object. We however, advise you to use JomSocial's getDisplayName() method instead.

Accessing JomSocial CUser Properties

Besides native Joomla user properties, CUser adds a lot of new functionality you can use

Using Proper Names

Although Joomla User Object already suplies us with possibility to use either name or username property of the object, it still may cause some problems when you develop your extension. Most common problem would be, if you use name property it will always retrieve the name of user. This might cause serious conflicts with sites that specifically use usernames site-wide.
To tackle this, JomSocial implements the getDisplayName() method which will always return screen name that is currently in use. Either name, or username, you are safe not to think about forementioned problem anymore.

Usage

$cuser->getDisplayName();


Example

$cuser = CFactory::getUser();
$cuserName = $cuser->getDisplayName();
echo $cuserName;


Use Status

Sometimes, you will want to show user's status. It is very simple to do, just use:

$cuser->getStatus();


Example

$cuser = CFactory::getUser();
$cuserStatus = $cuser->getStatus();
echo $cuserStatus;


Retrieve Value From Custom Profile Field

If you need to retrieve any value from custom profile fields, you can use:

$cuser = CFactory::getUser($userId);
$data = $cuser->getInfo('FIELD_CODE');


Note: This API will retrieve any custom profile field information regardless of its privacy setting. If you echo the value, it will completely ignore the privacy set by user.