/**
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_BASE') or die;
jimport('joomla.utilities.date');
jimport('joomla.user.helper');
jimport('joomla.user.user');
jimport('joomla.utilities.arrayhelper');
/**
* An example custom profile plugin.
*
* @package Joomla.Plugin
* @subpackage User.profile
* @version 1.6
*/
class plgUserProfile7 extends JPlugin
{
/**
* Constructor
*
* @access protected
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @since 1.5
*/
public function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* @param string $context The context for the data
* @param int $data The user id
* @param object
*
* @return boolean
* @since 1.6
*/
function onContentPrepareData($context, $data)
{
// Check we are manipulating a valid form.
if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile'))) {
return true;
}
if (is_object($data))
{
$userId = isset($data->id) ? $data->id : 0;
if (!isset($data->profile) and $userId > 0) {
// Load the profile data from the database.
$db = JFactory::getDbo();
$db->setQuery(
'SELECT profile_key, profile_value FROM #__user_profiles' .
' WHERE user_id = '.(int) $userId." AND profile_key LIKE 'profile7.%'" .
' ORDER BY ordering'
);
$results = $db->loadRowList();
// Check for a database error.
if ($db->getErrorNum())
{
$this->_subject->setError($db->getErrorMsg());
return false;
}
// Merge the profile data.
$data->profile7 = array();
foreach ($results as $v)
{
$k = str_replace('profile7.', '', $v[0]);
$data->profile7[$k] = json_decode($v[1], true);
if ($data->profile7[$k] === null)
{
$data->profile7[$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'));
}
if (!JHtml::isRegistered('users.subscribe')) {
JHtml::register('users.subscribe', array(__CLASS__, 'subscribe'));
}
}
return true;
}
public static function url($value)
{
if (empty($value))
{
return JHtml::_('users.value', $value);
}
else
{
$value = htmlspecialchars($value);
if(substr ($value, 0, 4) == "http") {
return '<a href="'.$value.'">'.$value.'</a>';
}
else {
return '<a href="http://'.$value.'">'.$value.'</a>';
}
}
}
public static function calendar($value)
{
if (empty($value)) {
return JHtml::_('users.value', $value);
} else {
return JHtml::_('date', $value, null, null);
}
}
public static function tos($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}
public static function subscribe($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}
/**
* @param JForm $form The form to be altered.
* @param array $data The associated data for the form.
*
* @return boolean
* @since 1.6
*/
function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
// 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'))) {
return true;
}
// Add the registration fields to the form.
JForm::addFormPath(dirname(__FILE__).'/profiles');
$form->loadFile('profile', false);
$fields = array(
'address1',
'address2',
'city',
'region',
'country',
'postal_code',
'phone',
'website',
'favoritebook',
'aboutme',
'tos',
'dob',
'subscribe',
);
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, 'profile7');
}
}
// Case registration
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' : '', 'profile7');
}
else {
$form->removeField($field, 'profile7');
}
}
// 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' : '', 'profile7');
}
else {
$form->removeField($field, 'profile7');
}
}
}
return true;
}
function onUserAfterSave($data, $isNew, $result, $error)
{
$userId = JArrayHelper::getValue($data, 'id', 0, 'int');
//
//BEGIN CUSTOM CODE
//
//put this inside the onUserAfterSave function in profile7.php
// This code will check if the user clicked the radio button for email
// updates by checking the 'subscribe' boolean in the data
// then assign the user to that group (group 9) if true
$groupID = 9; //9 is the value for the subscriber group in the ACL
//DEBUG code that forces if statement to come up true:
//$subscribeFlag = 1;
if ($userId && (isset($data['profile7']['subscribe'])))
{
$subscribeFlag = ($data['profile7']['subscribe']);
if (!($subscribeFlag == 0))
{
try
{
JUserHelper::addUserToGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
else
{
try
{
JUserHelper::removeUserFromGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
}
//END CUSTOM CODE
//
if ($userId && $result && isset($data['profile7']) && (count($data['profile7'])))
{
try
{
//Sanitize the date
if (!empty($data['profile7']['dob'])) {
$date = new JDate($data['profile7']['dob']);
$data['profile7']['dob'] = $date->format('Y-m-d');
}
$db = JFactory::getDbo();
$db->setQuery(
'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
" AND profile_key LIKE 'profile7.%'"
);
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
$tuples = array();
$order = 1;
foreach ($data['profile7'] as $k => $v)
{
$tuples[] = '('.$userId.', '.$db->quote('profile7.'.$k).', '.$db->quote(json_encode($v)).', '.$order++.')';
}
$db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples));
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
return true;
}
/**
* 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
*/
function onUserAfterDelete($user, $success, $msg)
{
if (!$success) {
return false;
}
$userId = JArrayHelper::getValue($user, 'id', 0, 'int');
if ($userId)
{
try
{
$db = JFactory::getDbo();
$db->setQuery(
'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
" AND profile_key LIKE 'profile.%'"
);
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
return true;
}
}
----------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="user">
<name>plg_user_profile7</name>
<author>Joomla! Project</author>
<creationDate>January 2008</creationDate>
<copyright>(C) 2005 - 2012 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>2.5.0</version>
<description>PLG_USER_PROFILE7_XML_DESCRIPTION</description>
<files>
<filename plugin="profile7">profile7.php</filename>
<filename>index.html</filename>
<folder>profiles</folder>
</files>
<languages>
<language tag="en-GB">en-GB.plg_user_profile7.ini</language>
<language tag="en-GB">en-GB.plg_user_profile7.sys.ini</language>
</languages>
<config>
<fields name="params">
<fieldset name="basic">
<field name="register-require-user" type="spacer" class="text"
label="PLG_USER_PROFILE7_FIELD_NAME_REGISTER_REQUIRE_USER"
/>
<field name="register-require_address1" type="list"
description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_address2" type="list"
description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_city" type="list"
description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_region" type="list"
description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_country" type="list"
description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_postal_code" type="list"
description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_phone" type="list"
description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_website" type="list"
description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_favoritebook"
type="list"
label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_aboutme"
type="list"
label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_tos"
type="list"
label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_subscribe"
type="list"
label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_dob"
type="list"
label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="spacer1" type="spacer"
hr="true"
/>
<field name="profile-require-user" type="spacer" class="text"
label="PLG_USER_PROFILE7_FIELD_NAME_PROFILE_REQUIRE_USER"
/>
<field name="profile-require_address1" type="list"
description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_address2" type="list"
description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_city" type="list"
description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_region" type="list"
description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_country" type="list"
description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_postal_code" type="list"
description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_phone" type="list"
description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_website" type="list"
description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_favoritebook"
type="list"
label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_aboutme"
type="list"
label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_tos"
type="list"
label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_dob"
type="list"
label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_subscribe"
type="list"
label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
</fieldset>
</fields>
</config>
</extension>
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="profile7">
<fieldset name="profile7"
label="PLG_USER_PROFILE7_SLIDER_LABEL"
>
<field
name="address1"
type="text"
id="address1"
description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
message="PLG_USER_PROFILE7_FIELD_ADDRESS1_MESSAGE"
size="30"
/>
<field
name="address2"
type="text"
id="address2"
description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
message="PLG_USER_PROFILE7_FIELD_ADDRESS1_MESSAGE"
size="30"
/>
<field
name="city"
type="text"
id="city"
description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
message="PLG_USER_PROFILE7_FIELD_CITY_MESSAGE"
size="30"
/>
<field
name="region"
type="text"
id="region"
description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
message="PLG_USER_PROFILE7_FIELD_REGION_MESSAGE"
size="30"
/>
<field
name="country"
type="text"
id="country"
description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
message="PLG_USER_PROFILE7_FIELD_COUNTRY_MESSAGE"
size="30"
/>
<field
name="postal_code"
type="text"
id="postal_code"
description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
message="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_MESSAGE"
size="30"
/>
<field
name="phone"
type="tel"
id="phone"
description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
message="PLG_USER_PROFILE7_FIELD_PHONE_MESSAGE"
size="30"
/>
<field
name="website"
type="url"
id="website"
description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
message="PLG_USER_PROFILE7_FIELD_WEB_SITE_MESSAGE"
size="30"
/>
<field
name="favoritebook"
type="text"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
message="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_MESSAGE"
size="30"
/>
<field
name="aboutme"
type="textarea"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC"
label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
message="PLG_USER_PROFILE7_FIELD_ABOUT_ME_MESSAGE"
cols="30"
rows="5"
filter="safehtml"
/>
<field
name="subscribe"
type="radio"
default="0"
label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="0">PLG_USER_PROFILE7_OPTION_NO</option>
<option value="1">PLG_USER_PROFILE7_OPTION_YES</option>
</field>
<field
name="tos"
type="radio"
label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC">
<option value="1">PLG_USER_PROFILE7_OPTION_AGREE</option>
</field>
<field
name="dob"
type="calendar"
label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC">
</field>
</fieldset>
</fields>
</form>
------------------------------------------------------------------------------------------------------------