Access to Custom Profile Fields

Discussion forum for MOD Writers regarding MOD Development.
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Access to Custom Profile Fields

Post by Atrox »

Is there a way for me to get access to the custom profile field data using the built-in phpBB3 functions and variables, or do I have to query the database myself?

I tried poking around in memberlist.php to see how they pull the data, and it seems to all be stored in a variable called $profile_fields, but I can't get any info from it. :( Not sure where I'm going wrong with it.
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Well, after poking around a bit more, I finally got some custom fields displaying, but it's not exactly the preferred method for me.

This works:
$profile_fields['row'][PROFILE_STEAM_ID_VALUE]

I'd like to be able to use the column name from the database, (pf_steam_id instead of PROFILE_STEAM_ID_VALUE).

But, it works at least, and I'm happy for that.
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Ok, I ran into another problem with custom profile fields. If I disable the display of a field, I can't show it anywhere.
The profile field will be shown in all places allowed within the load settings. Setting this to “no” will hide the field from topic pages, profiles and the memberlist.
Is there any way I can set this to no, so that it doesn't show up in viewtopic and such, but still get access to the data without writing custom handling code?
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Well, it seems that generate_profile_fields_template in includes/functions_profile_fields.php is what controls the data that's put into the profile_fields array. I tried setting up a new case within the function called "show_all" which was just a copy of "show", but without the if check to continue if the ID was blocked. However, there's a variable in the function called ident_ary that I can NOT find anywhere else. I think my grep function's broke, and won't do a recursive search. Can someone do a grep on their phpBB3 root and tell me where $ident_ary is declared so I can see how it's set up?
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52048
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Access to Custom Profile Fields

Post by Brf »

If you are writing your own page for showing a custom profile field, or you are trying to access custom profile fields on a page which doesnt normally access them, you can use:

Code: Select all

		$user->get_profile_fields( $user->data['user_id'] );
Then you can use

Code: Select all

$user->profile_fields['pf_fieldname'];
to access the value.
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Awesome, thanks a lot. This does exactly what I needed. I can still hide the custom options from the forum, and have them displayed where I want. Thanks a lot man.

I do, however, have another issue. I have a dropdown box in the custom profile fields, and when I access it with this method, I get the ID of the dropdown option. How can I get the data from the profile_fields_lang table that corresponds to the profile_fields_data table?
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Also, it seems that sometimes I can't use array members when they're lower-case. It''s worked fine in some instances, and some times it doesn't... Anyone know why that is?

Example:
{members.pf_steam_id}

Sometimes it works, sometimes it doesn't. I've used print_r to check the array and make sure the data's there, and it is, I just can't display it on the template page. :(
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Ok, get_profile_fields didn't work out how I needed it. I didn't pay attention to when I used it, but it only displays the custom profile data of the user that you're logged in to. I need to be able to get full access to the custom profile field of any member at any time.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52048
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Access to Custom Profile Fields

Post by Brf »

Atrox wrote: {members.pf_steam_id}
Template variables should be in uppercase, you should have used {members.PF_STEAM_ID}
You are creating that template variable yourself arent you? If it was already created by phpbb, it would have been something like "PROFILE_STEAM_ID_DATA".

Atrox wrote:Ok, get_profile_fields didn't work out how I needed it. I didn't pay attention to when I used it, but it only displays the custom profile data of the user that you're logged in to. I need to be able to get full access to the custom profile field of any member at any time.
You could probably use the code from memberlist.php:

Code: Select all

			include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
				$cp = new custom_profile();

				// Grab all profile fields from users in id cache for later use - similar to the poster cache
				$profile_fields_cache = $cp->generate_profile_fields_template('grab', $user_list);
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

That code is based on the ident_ary, which is only filled out by variables that are checked to be shown on the forum. It looks like I'm going to have to query the database myself. It's not really a big deal, I just wanted to use the built-in phpBB3 functions and variables to keep everything standard.
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Alright, I got this completely working. I had to make a second build_cache function, and a second case in generate_profile_fields_template to use the new build_cache function.

Now, I can use this code and display all the custom profile fields when I want, without showing them on the forum:

Code: Select all

$profile_fields = array();
if ($config['load_cpf_viewprofile'])
{
        include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
        $cp = new custom_profile();
        $profile_fields = $cp->generate_profile_fields_template('grab_all', $user_id);
        $profile_fields = (isset($profile_fields[$user_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields[$user_id]) : array();
}

if (!empty($profile_fields['row']))
{
        $template->assign_vars($profile_fields['row']);
}
Notice the "grab_all" in generate_profile_fields_template. In the template files, I can now use the variable names for any field:

Code: Select all

{PROFILE_MYFIELDNAME_VALUE}

If anyone else needs to do anything like this, let me know and I'll make a mod out of this faster. If no one needs it, I'll post it whenever I get around to it.

If a moderator wants to lock this thread, it's done.
Maelwys
Registered User
Posts: 3
Joined: Tue Feb 05, 2008 5:52 pm

Re: Access to Custom Profile Fields

Post by Maelwys »

Atrox,
I'm new to this mod thing but the mod you made here seems perfect to me. Only question... in which file do I put that php code?
Atrox
Registered User
Posts: 108
Joined: Tue Jan 21, 2003 6:26 pm
Contact:

Re: Access to Custom Profile Fields

Post by Atrox »

Firstly, you have to edit includes/functions_profile_fields.php and add some code.

Around line 429, find this: else if ($mode == 'show') and add this code before it:

Code: Select all

                else if ($mode == 'grab_all')
                {
                        if (!is_array($user_id))
                        {
                                $user_id = array($user_id);
                        }

                        if (!sizeof($this->profile_cache))
                        {
                                $this->build_cache_full();
                        }

                        if (!sizeof($user_id))
                        {
                                return array();
                        }

                        $sql = 'SELECT *
                                FROM ' . PROFILE_FIELDS_DATA_TABLE . '
                                WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
                        $result = $db->sql_query($sql);

                        $field_data = array();
                        while ($row = $db->sql_fetchrow($result))
                        {
                                $field_data[$row['user_id']] = $row;
                        }
                        $db->sql_freeresult($result);

                        $user_fields = array();

                        return $user_fields;
                }
That's all you should need to change in the code. I feel like I'm forgetting something, so if it still doesn't work for you, let me know and I'll figure it out for ya.

To use it, put the following code in the php file that corresponds to the template file you want to use:

Code: Select all

    $profile_fields = array();
    if ($config['load_cpf_viewprofile'])
    {
            include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
            $cp = new custom_profile();
            $profile_fields = $cp->generate_profile_fields_template('grab_all', $user_id);
            $profile_fields = (isset($profile_fields[$user_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields[$user_id]) : array();
    }

    if (!empty($profile_fields['row']))
    {
            $template->assign_vars($profile_fields['row']);
    }
Then, in the template file you can use the following fields to display whatever you want
{PROFILE_YOURFIELD_VALUE}
{PROFILE_YOURFIELD_TYPE}
{PROFILE_YOURFIELD_NAME}
{PROFILE_YOURFIELD_EXPLAIN}
Where YOURFIELD is whatever the name of your custom field is. If you have a field name first_name, for example, it'd be PROFILE_FIRST_NAME_VALUE to display what the user's first name is.

If you have any trouble getting it to work, let me know and I'll help you out. Enjoy. :)
Maelwys
Registered User
Posts: 3
Joined: Tue Feb 05, 2008 5:52 pm

Re: Access to Custom Profile Fields

Post by Maelwys »

Awesome, thanks very much! I'll let you know how it goes once I get a chance to install it.
Maelwys
Registered User
Posts: 3
Joined: Tue Feb 05, 2008 5:52 pm

Re: Access to Custom Profile Fields

Post by Maelwys »

Atrox wrote:If you have any trouble getting it to work, let me know and I'll help you out. Enjoy. :)
Hmm... I seem to be having problems with this.

What I'm trying to do is make it so I can read the custom profile fields from the viewtopic template, and display extra 'badges' next to each poster's name if they have certain values assigned to certain fields. The reason that this doesn't work normally is because I need it set so only the admin's can control the custom fields, so I've got to have them set for hidden (to lock them), which unfortunately also means that only the admin (and the targeted user) can actually see the badges (so everybody can see their own badge, but nobody else's). So I was trying to get around that restriction with this code. Unfortunately it doesn't seem to be allowing that... maybe this code is only meant to get around the 'where do I show this' restriction and not the 'only admins' restriction? Do you know any other way to get around that? Or am I out of luck?
Locked

Return to “[3.0.x] MOD Writers Discussion”