Source for file profile.php
Documentation is available at profile.php
* @subpackage User.profile
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* An example custom profile plugin.
* @subpackage User.profile
* Load the language file on instantiation.
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @param string $context The context for the data
* @param integer $data The user id
// Check we are manipulating a valid form.
if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile')))
$userId = isset
($data->id) ?
$data->id :
0;
if (!isset
($data->profile) and $userId >
0)
// Load the profile data from the database.
'SELECT profile_key, profile_value FROM #__user_profiles' .
' WHERE user_id = ' . (int)
$userId .
" AND profile_key LIKE 'profile.%'" .
$results =
$db->loadRowList();
catch
(RuntimeException $e)
$this->_subject->setError($e->getMessage());
// Merge the profile data.
$data->profile =
array();
if ($data->profile[$k] ===
null)
$data->profile[$k] =
$v[1];
if (!JHtml::isRegistered('users.url'))
JHtml::register('users.url', array(__CLASS__
, 'url'));
if (!JHtml::isRegistered('users.calendar'))
JHtml::register('users.calendar', array(__CLASS__
, 'calendar'));
if (!JHtml::isRegistered('users.tos'))
JHtml::register('users.tos', array(__CLASS__
, 'tos'));
public static function url($value)
return JHtml::_('users.value', $value);
// Convert website url to utf8 for display
if (substr($value, 0, 4) ==
"http")
return '<a href="' .
$value .
'">' .
$value .
'</a>';
return '<a href="http://' .
$value .
'">' .
$value .
'</a>';
return JHtml::_('users.value', $value);
return JHtml::_('date', $value, null, null);
public static function tos($value)
* @param JForm $form The form to be altered.
* @param array $data The associated data for the form.
if (!($form instanceof
JForm))
$this->_subject->setError('JERROR_NOT_A_FORM');
// Check we are manipulating a valid form.
$name =
$form->getName();
if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration')))
// Add the registration fields to the form.
$form->loadFile('profile', false);
//Change fields description when displayed in front-end
$form->setFieldAttribute('address1', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('address2', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('city', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('region', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('country', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('postal_code', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('phone', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('website', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('favoritebook', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('aboutme', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('dob', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
$form->setFieldAttribute('tos', 'description', 'PLG_USER_PROFILE_FIELD_TOS_DESC_SITE', 'profile');
$tosarticle =
$this->params->get('register_tos_article');
$tosenabled =
$this->params->get('register-require_tos', 0);
// We need to be in the registration form, field needs to be enabled and we need an article ID
if ($name !=
'com_users.registration' ||
!$tosenabled ||
!$tosarticle)
// We only want the TOS in the registration form
$form->removeField('tos', 'profile');
// Push the TOS article ID into the TOS field.
$form->setFieldAttribute('tos', 'article', $tosarticle, 'profile');
foreach ($fields as $field)
// Case using the users manager in admin
if ($name ==
'com_users.user')
// Remove the field if it is disabled in registration and profile
if ($this->params->get('register-require_' .
$field, 1) ==
0
&&
$this->params->get('profile-require_' .
$field, 1) ==
0
$form->removeField($field, 'profile');
elseif ($name ==
'com_users.registration')
// Toggle whether the field is required.
if ($this->params->get('register-require_' .
$field, 1) >
0)
$form->setFieldAttribute($field, 'required', ($this->params->get('register-require_' .
$field) ==
2) ?
'required' :
'', 'profile');
$form->removeField($field, 'profile');
if ($this->params->get('register-require_dob', 1) >
0)
$form->setFieldAttribute('spacer', 'type', 'spacer', 'profile');
// Case profile in site or admin
elseif ($name ==
'com_users.profile' ||
$name ==
'com_admin.profile')
// Toggle whether the field is required.
if ($this->params->get('profile-require_' .
$field, 1) >
0)
$form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' .
$field) ==
2) ?
'required' :
'', 'profile');
$form->removeField($field, 'profile');
if ($this->params->get('profile-require_dob', 1) >
0)
$form->setFieldAttribute('spacer', 'type', 'spacer', 'profile');
* Method is called before user data is stored in the database
* @param array $user Holds the old user data.
* @param boolean $isnew True if a new user is stored.
* @param array $data Holds the new user data.
* @throws InvalidArgumentException on invalid date.
// Check that the date is valid.
if (!empty($data['profile']['dob']))
// Convert website url to punycode
$date =
new JDate($data['profile']['dob']);
$this->_date =
$date->format('Y-m-d');
// Throw an exception if date is not valid.
throw
new InvalidArgumentException(JText::_('PLG_USER_PROFILE_ERROR_INVALID_DOB'));
if ($userId &&
$result && isset
($data['profile']) &&
(count($data['profile'])))
$data['profile']['dob'] =
$this->_date;
$query =
$db->getQuery(true)
->delete($db->quoteName('#__user_profiles'))
->where($db->quoteName('user_id') .
' = ' . (int)
$userId)
->where($db->quoteName('profile_key') .
' LIKE ' .
$db->quote('profile.%'));
foreach ($data['profile'] as $k =>
$v)
$tuples[] =
'(' .
$userId .
', ' .
$db->quote('profile.' .
$k) .
', ' .
$db->quote(json_encode($v)) .
', ' .
$order++ .
')';
$db->setQuery('INSERT INTO #__user_profiles VALUES ' .
implode(', ', $tuples));
catch
(RuntimeException $e)
$this->_subject->setError($e->getMessage());
* Remove all user profile information for the given user ID
* Method is called after user data is deleted from the database
* @param array $user Holds the user data
* @param boolean $success True if user was succesfully stored in the database
* @param string $msg Message
'DELETE FROM #__user_profiles WHERE user_id = ' .
$userId .
" AND profile_key LIKE 'profile.%'"
$this->_subject->setError($e->getMessage());
Documentation generated on Tue, 19 Nov 2013 15:11:20 +0100 by phpDocumentor 1.4.3