Sinisakrisan (Talk | contribs) (→Accessing Joomla User Properties) |
Sinisakrisan (Talk | contribs) (→Retrieving Various Checks From CUser Object) |
||
(32 intermediate revisions by the same user not shown) | |||
Line 47: | Line 47: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
$cuser = CFactory::getUser(); | $cuser = CFactory::getUser(); | ||
− | $ | + | $data = $cuser->username; |
− | echo $ | + | echo $data; |
</syntaxhighlight> | </syntaxhighlight> | ||
<br /> | <br /> | ||
+ | This example echoes the raw username of the object. We however, advise you to use JomSocial's '''getDisplayName()''' method instead. | ||
===Accessing JomSocial CUser Properties=== | ===Accessing JomSocial CUser Properties=== | ||
+ | Besides native Joomla user properties, CUser adds a lot of new functionality you can use | ||
− | ===Retrieve Value From Custom Profile Field=== | + | ====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.<br/> | ||
+ | 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 | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getDisplayName(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getDisplayName(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Use Status==== | ||
+ | Sometimes, you will want to show user's status. It is very simple to do, just use: | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getStatus(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getStatus(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Retrieve Big Avatar==== | ||
+ | If you need to use big avatar of the user, you can | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getAvatar(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | <?php | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getAvatar(); | ||
+ | ?> | ||
+ | <img src="<?php echo $data; ?>" /> | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Retrieve Small Avatar==== | ||
+ | Using the small avatar is also possible | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getThumbAvatar(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | <?php | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getThumbAvatar(); | ||
+ | ?> | ||
+ | <img src="<?php echo $data; ?>" /> | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Retrieve Cover==== | ||
+ | You can even use the full cover of user if needed | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getCover(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | <?php | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getCover(); | ||
+ | ?> | ||
+ | <img src="<?php echo $data; ?>" /> | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Show Karma Points==== | ||
+ | Number of karma points is also available in CUser object. To access it use | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getKarmaPoint(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getKarmaPoint(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | ====Show Karma Image==== | ||
+ | To show the Karma image, we will need to use CUserPoints library | ||
+ | |||
+ | Usage: | ||
+ | <syntaxhighlight lang="php"> | ||
+ | CUserPoints::getPointsImage($cuser); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example: | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = CUserPoints::getPointsImage($cuser); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | {{alert|'''Note:''' This example retrieves the raw path to the karma image. You will need to wrap it into proper '''<nowiki><img></nowiki>''' tag in your layout|alert-warning}} | ||
+ | |||
+ | ====Show View Count==== | ||
+ | If you want to display the number of view for any user do the following | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getViewCount(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getViewCount(); | ||
+ | echo $data ; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====User's Timezone==== | ||
+ | Retrieves the users timezone<br/> | ||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getTimezone(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getTimezone(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Get the Number of Friends==== | ||
+ | You can easily obtain the number of friends for current user | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getFriendCount(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getFriendCount(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | |||
+ | ====Get the Profile Type of User==== | ||
+ | You can retrieve the ID of profile type for given user | ||
+ | |||
+ | Usage | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser->getProfileType(); | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getProfileType(); | ||
+ | echo $data; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | This will echo the ID of the profiletype user is currently member of.<br/> | ||
+ | If you want to convert this to actual name of profiletype, use | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $multiprofile = JTable::getInstance( 'MultiProfile' , 'CTable' ); | ||
+ | $multiprofile->load( $cuser->getProfileType() ); | ||
+ | echo $multiprofile->name; | ||
+ | </syntaxhighlight> | ||
+ | <br /> | ||
+ | {{alert|'''Note:''' If Profile Type option is disabled in JomSocial, returned ID will always be 0, and name of profiletype won't be shown|alert-warning}} | ||
+ | |||
+ | ====Retrieve Value From Custom Profile Field==== | ||
If you need to retrieve any value from custom profile fields, you can use: | If you need to retrieve any value from custom profile fields, you can use: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
− | $cuser = CFactory::getUser( | + | $cuser->getInfo('FIELD_CODE'); |
− | $data = $cuser->getInfo(' | + | </syntaxhighlight> |
+ | <br /> | ||
+ | Example | ||
+ | <syntaxhighlight lang="php"> | ||
+ | $cuser = CFactory::getUser(); | ||
+ | $data = $cuser->getInfo('FIELD_GENDER'); | ||
+ | echo $data; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br /> | <br /> | ||
{{alert|'''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.|alert-danger}} | {{alert|'''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.|alert-danger}} | ||
+ | |||
+ | ==Retrieving Various Checks From CUser Object== | ||
+ | When you develop your integration, you may also check for various different values | ||
+ | * '''isOnline()''' - returns 1 if user is online, | ||
+ | * '''canCreateGroups()''' - returns 1 if true. | ||
+ | * '''canCreateEvents()''' - returns 1 if true | ||
+ | |||
+ | ==See Also== | ||
+ | * [[Integrating Your Component]] |
Latest revision as of 10:42, 25 October 2013
Contents
- 1 Retrieve User Object
- 2 Extending The CUser Object
- 2.1 Accessing Joomla User Properties
- 2.2 Accessing JomSocial CUser Properties
- 2.2.1 Using Proper Names
- 2.2.2 Use Status
- 2.2.3 Retrieve Big Avatar
- 2.2.4 Retrieve Small Avatar
- 2.2.5 Retrieve Cover
- 2.2.6 Show Karma Points
- 2.2.7 Show Karma Image
- 2.2.8 Show View Count
- 2.2.9 User's Timezone
- 2.2.10 Get the Number of Friends
- 2.2.11 Get the Profile Type of User
- 2.2.12 Retrieve Value From Custom Profile Field
- 3 Retrieving Various Checks From CUser Object
- 4 See Also
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->username; 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(); $data = $cuser->getDisplayName(); echo $data;
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(); $data = $cuser->getStatus(); echo $data;
Retrieve Big Avatar
If you need to use big avatar of the user, you can
Usage
$cuser->getAvatar();
Example
<?php $cuser = CFactory::getUser(); $data = $cuser->getAvatar(); ?> <img src="<?php echo $data; ?>" />
Retrieve Small Avatar
Using the small avatar is also possible
Usage
$cuser->getThumbAvatar();
Example
<?php $cuser = CFactory::getUser(); $data = $cuser->getThumbAvatar(); ?> <img src="<?php echo $data; ?>" />
Retrieve Cover
You can even use the full cover of user if needed
Usage
$cuser->getCover();
Example
<?php $cuser = CFactory::getUser(); $data = $cuser->getCover(); ?> <img src="<?php echo $data; ?>" />
Show Karma Points
Number of karma points is also available in CUser object. To access it use
$cuser->getKarmaPoint();
Example
$cuser = CFactory::getUser(); $data = $cuser->getKarmaPoint(); echo $data;
Show Karma Image
To show the Karma image, we will need to use CUserPoints library
Usage:
CUserPoints::getPointsImage($cuser);
Example:
$cuser = CFactory::getUser(); $data = CUserPoints::getPointsImage($cuser); echo $data;
Show View Count
If you want to display the number of view for any user do the following
Usage
$cuser->getViewCount();
Example
$cuser = CFactory::getUser(); $data = $cuser->getViewCount(); echo $data ;
User's Timezone
Retrieves the users timezone
Usage
$cuser->getTimezone();
Example
$cuser = CFactory::getUser(); $data = $cuser->getTimezone(); echo $data;
Get the Number of Friends
You can easily obtain the number of friends for current user
Usage
$cuser->getFriendCount();
Example
$cuser = CFactory::getUser(); $data = $cuser->getFriendCount(); echo $data;
Get the Profile Type of User
You can retrieve the ID of profile type for given user
Usage
$cuser->getProfileType();
Example
$cuser = CFactory::getUser(); $data = $cuser->getProfileType(); echo $data;
This will echo the ID of the profiletype user is currently member of.
If you want to convert this to actual name of profiletype, use
$multiprofile = JTable::getInstance( 'MultiProfile' , 'CTable' ); $multiprofile->load( $cuser->getProfileType() ); echo $multiprofile->name;
Retrieve Value From Custom Profile Field
If you need to retrieve any value from custom profile fields, you can use:
$cuser->getInfo('FIELD_CODE');
Example
$cuser = CFactory::getUser(); $data = $cuser->getInfo('FIELD_GENDER'); echo $data;
Retrieving Various Checks From CUser Object
When you develop your integration, you may also check for various different values
- isOnline() - returns 1 if user is online,
- canCreateGroups() - returns 1 if true.
- canCreateEvents() - returns 1 if true