PHP Callback - Method for render as well as validate?

TruDan

Member
Hi,

I have a custom Callback for a custom user field, I want to be able to store one value, but display another.

To put into actual context. The input will be a Minecraft Username, the validation converts it into the new Mojang UUID (gets data from mojang servers etc). But then when the field is displayed, i want it to show the player name, not the UUID.

This is my code atm:

PHP:
class TruDan_DwDMCLink {

    public static function validate($field, &$value, &$error) {
   
        // Check if legit minecraft account
        $response = file_get_contents("https://minecraft.net/haspaid.jsp?user=".rawurlencode($value));
        if(strpos($response, 'true') !== false) {
            // Legit minecraft
           
            // Get Data from Mojang
            $dat = ProfileUtils::getProfile($value);
           
            // Is the data from mojang? (Check for Mojang Migration)
            if($dat) {
               
                // Format the UUID to standards
                $value = ProfileUtils::formatUUID($dat->getUUID());
               
                // Has this UUID already been assigned to a user?
                $db = XenForo_Application::get('db');
                $exists = $db->fetchOne("SELECT v.`user_id` FROM xf_user_field_value v LEFT JOIN xf_user_field f ON v.`field_id`=f.`field_id` WHERE f.`field_id`='MCUUID' AND v.`field_value`='" . $value . "'");
               
                // No
                if($exists === false) {
                    return true;
                }
                elseif(XenForo_Visitor::getUserId() == $exists) {
                    return true;
                }
               
                // Yes
                $error = "That minecraft account has already been linked to a forums account!";
                return false;
            }
            else {
                $error = "Your account has not been migrated to a Mojang account!";
                return false;
            }
        }
        else {
            $error = "The minecraft account '".$value."' is not premium!";
            return false;
        }
   
    }
   
    public static function render($field, &$value, &$error) {
   
        // Get Mojang data for this UUID
        $dat = ProfileUtils::getProfile($value);
       
        // Set the value to the current players username.
        $value = $dat->getUsername();
    }
}

I want to basically call the validate method when its being changed, and the render method when its being displayed.

Any ideas on how to do this / is this even possible? I'm new to Xen developing, but i'm very experienced in PHP.
 
Back
Top Bottom