Php 创建Joomla用户配置文件插件
我已经为Joomla 2.5.9安装直接克隆了用户配置文件插件 我已经相应地将插件和文件重命名为“profiletest”,类似于旧的1.6 我已经在表单中添加了一个新的输入,所有内容都在后端工作,新的条目在前端的注册表单中按预期显示。但是,当您注册时,我从未看到Php 创建Joomla用户配置文件插件,php,joomla,joomla2.5,Php,Joomla,Joomla2.5,我已经为Joomla 2.5.9安装直接克隆了用户配置文件插件 我已经相应地将插件和文件重命名为“profiletest”,类似于旧的1.6 我已经在表单中添加了一个新的输入,所有内容都在后端工作,新的条目在前端的注册表单中按预期显示。但是,当您注册时,我从未看到\uuuuu用户配置文件表更新 这里有很多代码,但它是用户配置文件插件(/plugins/User/profile/)的副本。以下是profiletest.php onUserAfterSave函数: function onUserAf
\uuuuu用户配置文件
表更新
这里有很多代码,但它是用户配置文件插件(/plugins/User/profile/)的副本。以下是profiletest.php onUserAfterSave函数:
function onUserAfterSave($data, $isNew, $result, $error)
{
$userId = JArrayHelper::getValue($data, 'id', 0, 'int');
if ($userId && $result && isset($data['profiletest']) && (count($data['profiletest'])))
{
try
{
//Sanitize the date
if (!empty($data['profiletest']['dob']))
{
$date = new JDate($data['profiletest']['dob']);
$data['profiletest']['dob'] = $date->format('Y-m-d');
}
$db = JFactory::getDbo();
$db->setQuery(
'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
" AND profile_key LIKE 'profiletest.%'"
);
if (!$db->query())
{
throw new Exception($db->getErrorMsg());
}
$tuples = array();
$order = 1;
foreach ($data['profiletest'] as $k => $v)
{
$tuples[] = '('.$userId.', '.$db->quote('profiletest.'.$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;
}
它从不向数据库中插入任何内容,因为它从不进入以下if语句:
if ($userId && $result && isset($data['profiletest']) && (count($data['profiletest'])))
基本上,此条件失败:$data['profiletest']
看起来很基本,因为我在插件中把“profile”改为“profiletest”。然而,要解决这个问题,我认为您需要了解我的另一个函数调用了什么onContentPrepareData
。虽然它也没有做任何不同的名称以外的变化。很抱歉,我倒了这么久
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;
JLog::add('Do I get into onContentPrepareData?');
if (!isset($data->profiletest) 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 'profiletest.%'" .
' ORDER BY ordering'
);
$results = $db->loadRowList();
JLog::add('Do I get sql result: '.$results);
// Check for a database error.
if ($db->getErrorNum())
{
$this->_subject->setError($db->getErrorMsg());
return false;
}
// Merge the profile data.
$data->profiletest= array();
foreach ($results as $v)
{
$k = str_replace('profiletest.', '', $v[0]);
$data->profiletest[$k] = json_decode($v[1], true);
if ($data->profiletest[$k] === null)
{
$data->profiletest[$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'));
}
}
return true;
}
我再次注意到我从来没有进入过这里:
if (!isset($data->profiletest) and $userId > 0)
这可能会影响onUserAfterSave
功能
编辑以下是ContentPrepareReform的功能:
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',
'dob',
'tos',
);
$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', 'profiletest');
}
else
{
// Push the TOS article ID into the TOS field.
$form->setFieldAttribute('tos', 'article', $tosarticle, 'profiletest');
}
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, 'profiletest');
}
}
// 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' : '', 'profiletest');
}
else
{
$form->removeField($field, 'profiletest');
}
}
// 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' : '', 'profiletest');
}
else
{
$form->removeField($field, 'profiletest');
}
}
}
return true;
}
我做错了什么
编辑变量转储($data);退出()代码>仅在保存后的onUserAfterSave中
array(20) { ["isRoot"]=> NULL ["id"]=> int(1291) ["name"]=> string(4) "test" ["username"]=> string(4) "test" ["email"]=> string(22) "test@test.com" ["password"]=> string(65) "5757d7ea6f205f0ee9102e41f66939b4:7dTHzEolpDFKa9P2wmZ4SYSjJSedWFXe" ["password_clear"]=> string(4) "test" ["usertype"]=> NULL ["block"]=> NULL ["sendEmail"]=> int(0) ["registerDate"]=> string(19) "2013-03-05 17:00:40" ["lastvisitDate"]=> NULL ["activation"]=> NULL ["params"]=> string(2) "{}" ["groups"]=> array(1) { [0]=> string(1) "2" } ["guest"]=> int(1) ["lastResetTime"]=> NULL ["resetCount"]=> NULL ["aid"]=> int(0) ["password2"]=> string(4) "test" }
我认为您在这里遇到了一个问题:“profiletest.%”您不应该将php连接运算符放在引号内,它将其视为字符串的一部分。就我个人而言,我通常在编写查询之前连接%。但是$db->quote('profiletest..$k)。“您稍后使用的更符合您的需求 首先使用或var_dump()数组$data->profiletest
检查是否有数据。如果没有,我想您需要分析onContentPrepareForm
方法。如果没有,那么检查UserID是否得到了有效的结果。其中一个必须给出无效结果才能使if语句“失败”。一旦你把结果发回到这里:)那么这里的关键功能实际上就是你没有包含的:onContentPrepareReform
。这是构建用户填写的表单的函数。您尚未更新此字段中的字段名,因此包含的代码中的检查失败
如果在打开插件的情况下进入注册页面,您应该会看到配置文件插件的所有字段。如果您检查任何字段(让我们使用地址1),它应该有这样一个名称:jform[profile][address1]
。我们希望这是jform[profiletype][address1]
,然后您的代码就可以工作了
在开始之前,让我解释一下代码。$data
变量应包含所提交表单中的所有信息。这与名称开头的jform
匹配,因为这是Joomla用于注册表的标准控件
$data
将包含一些单独的项目和数组配置文件
。要更新该名称,请查找plugins/user/profile/profiles/profile.xml中的文件,并将字段
名称从profile
更改为profiletype
。现在提交时,$data
将包含数组元素profiletype
,其余查询将运行。因为$data['profiletest']失败
尚未在xml中注册名称从配置文件到profiletest的更改
如果没有,请进行以下更改
在plugins\user\profile\profiles\profile.xml中
将更改为
也在user\profile\profile.xml中
更改profile.php
到
profiletest.php
I仅使用profiletest
更改了profile
,否则它就是核心Joomla代码。我什么也没改变。无论哪种方式,这都不重要,因为我从未接触到您所谈论的代码。高于您提到的代码的if语句从来都不是真的。所以我不认为你的建议会起到任何作用。你能澄清一下,你的目标是替换配置文件插件还是扩展它吗?还可以通过链接或pastebin?@cppl添加其他插件文件。我想拥有自己的插件,它可以完成用户配置文件插件的所有功能,但可以扩展以添加我需要的一些东西。我将尝试添加一个pastebin,但它与用户配置文件插件相同。我只使用profiletest
更改了profile
。一旦这是工作,然后我可以开始添加我的代码。感谢您的回复。我确实明白了:jform[profiletype][address1]
。profiles/profile.xml
中的我的字段确实有fields name=profiletest
。还有其他想法吗?我已经添加了onContentPrepareForm
,但这只是一个副本,在我的评论中将profile
更改为profiletest
错误。我使用的是profiletest
而不是profiletype
Ok。因此,如果您在站点上加载注册表,您将看到字段,并且它们具有正确的名称。然后,您应该检查$data是否包含保存时的数组。就在onUserAfterSave
函数addvar\u dump($data)中;退出()
。这是否显示了一个名为profiletest
的数组?是的,有一个包含许多字段的大数组。但是没有名为profiletest的数组。我在上面的问题中发布了数组。看起来就像是默认注册的东西,当然就是默认注册的东西。在这种情况下我真的不确定。如果您可以查看表单并填写所有概要文件字段,并且它们具有正确的名称,那么数据应该位于该变量中。我唯一能猜到的另一件事是检查字段是否由于某种原因被分割在两个表单标记之间。那是
<filename plugin="profile">profiletest.php</filename>