php – 创建Joomla用户配置文件插件

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 创建Joomla用户配置文件插件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我直接克隆了用户配置文件插件,用于我的JooMLa 2.5.9安装.

我已经将插件文件重命名为’PRofiletest’,类似于旧的1.6 tutorial.

我在表单中添加一个新输入,一切都在后端工作,新条目在前端的注册表单中按预期显示.但是,当您注册时,我从未看到#__user_profiles表已更新.

这里有很多代码,但它是User profile插件的副本(/ plugins / user / profile /).这是profiletest.PHP onUserAfterSave函数

function onUserAfterSave($data,$isNew,$result,$error)
{
    $userId = JArrayHelPEr::getValue($data,'id','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;
}

永远不会在DB中插入任何东西,因为它永远不会进入这个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','calendar'));
        }
        if (!JHtml::isRegistered('users.tos'))
        {
            JHtml::register('users.tos','tos'));
        }
    }

    return true;
}

我再次注意到我从未进入过这里:

if (!isset($data->profiletest) and $userId > 0)

这可能会影响onUserAfterSave函数.

编辑这是onContentPrepareForm的函数

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.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,($this->params->get('profile-require_' . $field) == 2) ? 'required' : '','profiletest');
            }
        }
    }

    return true;
}

我究竟做错了什么?

编辑var_dump($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" }
首先使用 JDumpVAR_dump()数组$data-> profiletest来检查你是否有数据.如果没有,那么我想你需要去分析onContentPrepareForm方法.如果没有,那么去检查UserID是否正在提取有效结果.其中一个必须给出“失败”if语句的无效结果.一旦你完成了那篇文章回到这里结果:)

脚本宝典总结

以上是脚本宝典为你收集整理的php – 创建Joomla用户配置文件插件全部内容,希望文章能够帮你解决php – 创建Joomla用户配置文件插件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。