What's the best way to call userfield value from PHP file

Cyb3r

Well-known member
I tried using
XenForo_Template_Helper_Core::callHelper
and
XenForo_Template_Helper_Core::helperUserFieldValue
and
XenForo_Template_Helper_Core::helperCallbacks

but the main problem is that I don't know which params I should pass to get the userfield value, any help will be much appreciated.

Thanks.
 
Do you mean a user field from the current user? If so:
PHP:
  $userFieldName = "username";
  $userFieldValue = XenForo_Visitor::getInstance()->get($userFieldName);
 
Do you mean a user field from the current user? If so:
PHP:
  $userFieldName = "username";
  $userFieldValue = XenForo_Visitor::getInstance()->get($userFieldName);

Thank you for your kind reply, but I'm trying to get $user not $visitor, I'm extending XenForo_Template_Helper_Core.
 
The code I posted should also work in this class. What exactly are you trying to accomplish?

I'm trying to get the value of a custom field, I used to use this it work sometimes:

PHP:
        $userFieldModel = XenForo_Model::create('XenForo_Model_UserField');
        $fields = $userFieldModel->getUserFieldValues($user['user_id']);
        $field = $fields['mycustomfield'];

but sometimes it wont work for some reason, so i figured i could use one of the previous methods to get the value instead.
 
All right. And why do you think the code I posted isn't suited in this context?

Your code returns the value for the viewer which is $visitor, but I want to get it for the $user.

My question is do you know how to use any of those helpers:
PHP:
XenForo_Template_Helper_Core::callHelper
XenForo_Template_Helper_Core::helperUserFieldValue
XenForo_Template_Helper_Core::helperCallbacks
 
Oh now I see..

However, I can't spot any problems in your code. When does it not work? For certain user field(s) maybe?
 
Oh now I see..

However, I can't spot any problems in your code. When does it not work? For certain user field(s) maybe?

You know my bad I was using it in the wrong function. =(

Finally got it working, thank you for all your help.

I got another 2 quick questions do you know how to make a button to do a db query?

Also how to make a limit changing custom field once per week?
 
I got another 2 quick questions do you know how to make a button to do a db query?

Well, in general, if you click on a button/link the corresponding controller action will be performed. And in this action you can do „everything“ you want including DB queries. Does that answer your question?

Also how to make a limit changing custom field once per week?

That's actually not trivial but would be a good idea for a new add-on. There are probably several different ways you can do this. but you probably would need to evtend the Account controller and make sure that the user can't change the field value if he's not allowed to by e.g. not showing the field. For this you obviously need to save tha last edit time somewhere in the database. Also if the user changes the field value the last edit time would need to be updated. Optimally you would also perform another check before saving the field.
 
Well, in general, if you click on a button/link the corresponding controller action will be performed. And in this action you can do „everything“ you want including DB queries. Does that answer your question?



That's actually not trivial but would be a good idea for a new add-on. There are probably several different ways you can do this. but you probably would need to evtend the Account controller and make sure that the user can't change the field value if he's not allowed to by e.g. not showing the field. For this you obviously need to save tha last edit time somewhere in the database. Also if the user changes the field value the last edit time would need to be updated. Optimally you would also perform another check before saving the field.

Thank you for all your help, I think i'm gonna make cron job runs weekly to empty that field isn't that better?

Can you show me an example on how to make function to run db query to empty custom field.

Thanks.
 
Thank you for all your help, I think i'm gonna make cron job runs weekly to empty that field isn't that better?
Well, that depends on what you actually want to achieve. Limiting the number of changes a user can make and periodically removing the user field value are quite different things.

Can you show me an example on how to make function to run db query to empty custom field.
The following method will remove the value of the user field „fieldName“ for all (!) users except for the users with id 1 and 3.
PHP:
<?php

class CronEntries_UserFieldValuesReset
{
  public static function resetUserFieldValues()
  {  
    $db = XenForo_Application::getDb();  
  
    $db->query("UPDATE xf_user_field_value SET field_value = ''
                WHERE field_id = 'fieldName' AND user_id NOT IN ('1', '3')");  
  }
}
 
Well, that depends on what you actually want to achieve. Limiting the number of changes a user can make and periodically removing the user field value are quite different things.


The following method will remove the value of the user field „fieldName“ for all (!) users except for the users with id 1 and 3.
PHP:
<?php

class CronEntries_UserFieldValuesReset
{
  public static function resetUserFieldValues()
  { 
    $db = XenForo_Application::getDb(); 
 
    $db->query("UPDATE xf_user_field_value SET field_value = ''
                WHERE field_id = 'fieldName' AND user_id NOT IN ('1', '3')"); 
  }
}

This is awesome, but after a second thought I might use the first method because I think this will make much load on the database.

Here's what i'm gonna do, I will make a new custom field "lastReset" and a button to reset the data, when he press the button the current timestamp get inserted into the "lastReset" field, next time when he try to reset the field it will compare the current time with the one stored in "lastReset" field and check if it's >= 7 days, if true then reset the field, if false give an error says you have to wait {time} before you can reset. {time} = the time period before next reset.

Is this easy to implement?
 
Are you sure? How often would the cron entry need to run?


That depends on how how experience you have with XF development. If you have very little this will probably be rather difficult..

Well I'm learning as fast as I can, I still a noob in PHP and XenForo structure but I have been developing in other languages so I know most of the programming basics. =)
 
Back
Top Bottom