• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Callback Custom Profile Fields

silence

Well-known member
#1
Basically I want to create a callback that checks if the input exists in a particular custom profile field. How can I go about doing this since documentation is extremely limited for devs :(
EDIT

Alright here is what I have so far, honestly I'm not sure this callback would work!
PHP:
<?php
 
class gameDonation
{
    function checkGameNames($name, $price, $time)
    {
        $db = XenForo_Application::get('db');
       
        $results = $db->query("
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = " . $name . "
        ");
       
        $storedtime = $db->query("
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = " . $time . "
        ");
       
        if (empty($results) && empty($storedtime))
        {
            addGameData::gameDataSave($response, $price);
            //not a linked user, but it is a new donation!
        }
       
        if (empty($results) && (!(empty($storedtime))))
        {
            return;
        }
       
        if (($results[0] == $name) && empty($storedtime))
        {
            addGameData::gameDataSave($response, $price);
            //linked user and a new donation!
        }
       
        if (($results[0] == $name) && (!(empty($storedtime))))
        {
            return;
        }
    }
}
 
class addGameData extends Merc_DonationManager_ControllerAdmin_Donation
{
    public function gameDataSave(&$response, $price)
    {
        $this->_assertPostOnly();
 
        $data = $this->_input->filter(array(
            'username' => $response[0],
            'user_id' => 0,
            'amount' => $price,
            'anonymous' => 1,
            'note' => "Game donation",
            'goal_id' => 5,
        ));
 
        $datetime = $time;
 
        if (empty($data['user_id']))
        {
            $userModel = $this->getModelFromCache('XenForo_Model_User');
            $data['user_id'] = $userModel->getUserIdFromUser($userModel->getUserByName($data['username']));
        }
 
        $dw = XenForo_DataWriter::create('Merc_DonationManager_DataWriter_Donation');
 
        $dw->bulkSet($data);
        $dw->set('transaction_id', 0);
 
        $dw->save();
    }
}
?>
 

Jake Bunce

XenForo moderator
Staff member
#5
You need to debug your code to ensure that all steps are working. I don't have the required context. If the validation isn't working then output some values to see what's going on.
 

silence

Well-known member
#6
Alright that was immensily helpful :D
I'm getting a weird error, and it's confusing me:
Code:
        $results = $db->query("
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = ' . $name . '
        ");
   
        $storedtime = $db->query("
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = ' . $time . '
        ");
   
        $username = $results[0]; <-- line 47
        $userid = $results[1];
Fatal error: Cannot use object of type Zend_Db_Statement_Mysqli as array in /home/xeno/public_html/xenforo/library/Donation/Game.php on line 47
So that query doesn't return an array?
 

silence

Well-known member
#9
Alright I'm almost there! Here is what I have so far but I cannot figure this out!!!
PHP:
    protected static function checkGameNames($name, $price, $time)
    {
        $db = XenForo_Application::get('db');
 
        $results = $db->query('
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"
        ');
 
        $storedtime = $db->query('
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time.'
        ');
     
        $data[] = $results;
        $timeresults[] = $storedtime;
 
        $username = (isset($data['field_value']) ? $data['field_value'] : null);
        $userid = (isset($data['user_id']) ? $data['user_id'] : null);
        $storedtime = (isset($timeresults['last_donation_date']) ? $timeresults['last_donation_date'] : null);
     
        if (empty($username) && empty($storedtime))
        {
            $anon = 1;
            self::CustomActionAdd(0, $username, $price, $anon, $time);
            //not a linked user, but it is a new donation!
        }
     
        if (($username == "") && $storedtime == $time)
        {
            print_r($storedtime);
            echo "---";
            return;
        }
     
        if (($username == $name) && $storedtime  != $time)
        {
            echo "username exists but no time exists\n";
            $anon = 0;
            self::CustomActionAdd($userid, $username, $price, $anon, $time);
            //linked user and a new donation!
        }
     
        if (($username == $name) && $storedtime == $time)
        {
            echo "username exists but time exists\n";
            return;
        }
    }
 
    protected static function CustomActionAdd($userid, $username, $price, $anon, $time)
    {
        $viewParams = $this->_getAddEditDateData() + array(
            'donation' => array(
                'user_donation_id' => 0,
                'user_id' => $userid,
                'username' => $username,
                'amount' => $amount,
                'note' => 'Game Donation',
                'anonymous' => $anon,
                'donation_date' => $time,
            )
        );
 
        return $this->_getAddEditResponse('Merc_DonationManager_ViewAdmin_Donation_Add', $viewParams);
    }
}
?>
Fatal error: Using $this when not in object context in /home/xeno/public_html/xenforo/library/Donation/Game.php on line 83
 

Jake Bunce

XenForo moderator
Staff member
#10
Fatal error: Using $this when not in object context in /home/xeno/public_html/xenforo/library/Donation/Game.php on line 83
Exactly what it says.

If you are calling a static function in the same class then you need to use:

self::

Instead of:

$this->
 

silence

Well-known member
#11
I gotta be honest with you I have no idea what I'm doing. I'm picking this up as I go.

Non-static method Merc_DonationManager_ControllerAdmin_Donation::_getAddEditDateData() should not be called statically

I'm extending using the following line (forgot to include it)
class Donation_Game extends Merc_DonationManager_ControllerAdmin_Donation
 

silence

Well-known member
#14
Another question, so I make everything call statically, but I get this error:
Non-static method Merc_DonationManager_ControllerAdmin_Donation::_getAddEditDateData() should not be called statically.
And apparently I can't use $this-> in a static function, so would I have to rewrite everything so it's not calling stuff statically?
I came to this conclusion when I got this error after adding $this-> to:
Fatal error: Using $this when not in object context in /home/xeno/public_html/xenforo/library/Donation/Gane.php on line 83
 

silence

Well-known member
#16
PHP:
<?php
 
class Donation_Game extends Merc_DonationManager_ControllerAdmin_Donation
{  
    public static function gather()
    {
        foreach ($payments['payload'] as $getPayments)
        {
            $name    = (isset($getPayments['ign']) ? $getPayments['ign'] : null);
            $price    = (isset($getPayments['price']) ? $getPayments['price'] : null);
            $time    = (isset($getPayments['time']) ? $getPayments['time'] : null);
 
            self::checkGameNames($name, $price, $time);
        }
    }
   
    protected static function checkGameNames($name, $price, $time)
    {
        $db = XenForo_Application::get('db');
 
        $results = $db->query('
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"
        ');
 
        $storedtime = $db->query('
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time.'
        ');
       
        $data[] = $results;
        $timeresults[] = $storedtime;
 
        $username = (isset($data['field_value']) ? $data['field_value'] : null);
        $userid = (isset($data['user_id']) ? $data['user_id'] : null);
        $storedtime = (isset($timeresults['last_donation_date']) ? $timeresults['last_donation_date'] : null);
       
        if (empty($username) && empty($storedtime))
        {
            $anon = 1;
            self::CustomActionAdd(0, $username, $price, $anon, $time);
            //not a linked user, but it is a new donation!
        }
       
        if (($username == "") && $storedtime == $time)
        {
            print_r($storedtime);
            echo "---";
            return;
        }
       
        if (($username == $name) && $storedtime  != $time)
        {
            echo "username exists but no time exists\n";
            $anon = 0;
            self::CustomActionAdd($userid, $username, $price, $anon, $time);
            //linked user and a new donation!
        }
       
        if (($username == $name) && $storedtime == $time)
        {
            echo "username exists but time exists\n";
            return;
        }
    }
   
    protected static function CustomActionAdd($userid, $username, $price, $anon, $time)
    {
        $viewParams = $this->_getAddEditDateData() + array(
            'donation' => array(
                'user_donation_id' => 0,
                'user_id' => $userid,
                'username' => $username,
                'amount' => $amount,
                'note' => 'Game Donation',
                'anonymous' => $anon,
                'donation_date' => $time,
            )
        );
 
        return self::_getAddEditResponse('Merc_DonationManager_ViewAdmin_Donation_Add', $viewParams);
    }
}
?>
 

Akinak

Well-known member
#17
Fix some line, but still there are issues such as what should be in the end.
And why so much of static methods?

Code:
<?php
 
class Donation_Game extends Merc_DonationManager_ControllerAdmin_Donation
{
    public static function gather() //Mb must be gather($payments)? Where this method use?
    {
        foreach ($payments['payload'] as $getPayments) //$payments dont declare
        {
            $name    = (isset($getPayments['ign']) ? $getPayments['ign'] : null);
            $price    = (isset($getPayments['price']) ? $getPayments['price'] : null);
            $time    = (isset($getPayments['time']) ? $getPayments['time'] : null);
 
            self::checkGameNames($name, $price, $time);
        }
//this method no return anything
    }
 
    protected static function checkGameNames($name, $price, $time)
    {
        $db = XenForo_Application::get('db');
 
        $results = $db->query('
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"
        ');
 
        $storedtime = $db->query('
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time.'
        ');
 
        $data[] = $results;
        $timeresults[] = $storedtime;
 
        $username = (isset($data['field_value']) ? $data['field_value'] : null);
        $userid = (isset($data['user_id']) ? $data['user_id'] : null);
        $storedtime = (isset($timeresults['last_donation_date']) ? $timeresults['last_donation_date'] : null);
 
        if (empty($username) && empty($storedtime))
        {
            $anon = 1;
            self::CustomActionAdd(0, $username, $price, $anon, $time);
            //not a linked user, but it is a new donation!
        }
 
        if (($username == "") && $storedtime == $time)
        {
            print_r($storedtime);
            echo "---";
            return;
        }
 
        if (($username == $name) && $storedtime  != $time)
        {
            echo "username exists but no time exists\n";
            $anon = 0;
            self::CustomActionAdd($userid, $username, $price, $anon, $time);
            //linked user and a new donation!
        }
 
        if (($username == $name) && $storedtime == $time)
        {
            echo "username exists but time exists\n";
            return;
        }
    }
 
    protected static function CustomActionAdd($userid, $username, $price, $anon, $time)
    {
        $viewParams = self::_getAddEditDateData() + array(
            'donation' => array(
                'user_donation_id' => 0,
                'user_id' => $userid,
                'username' => $username,
                'amount' => $price, // or $amount? I dont know
                'note' => 'Game Donation',
                'anonymous' => $anon,
                'donation_date' => $time,
            )
        );
 
        return self::_getAddEditResponse('Merc_DonationManager_ViewAdmin_Donation_Add', $viewParams);
    }
}
?>
 

silence

Well-known member
#18
Fix some line, but still there are issues such as what should be in the end.
And why so much of static methods?

Code:
<?php
 
class Donation_Game extends Merc_DonationManager_ControllerAdmin_Donation
{
    public static function gather() //Mb must be gather($payments)? Where this method use?
    {
        foreach ($payments['payload'] as $getPayments) //$payments dont declare
        {
            $name    = (isset($getPayments['ign']) ? $getPayments['ign'] : null);
            $price    = (isset($getPayments['price']) ? $getPayments['price'] : null);
            $time    = (isset($getPayments['time']) ? $getPayments['time'] : null);
 
            self::checkGameNames($name, $price, $time);
        }
//this method no return anything
    }
 
    protected static function checkGameNames($name, $price, $time)
    {
        $db = XenForo_Application::get('db');
 
        $results = $db->query('
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"
        ');
 
        $storedtime = $db->query('
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time.'
        ');
 
        $data[] = $results;
        $timeresults[] = $storedtime;
 
        $username = (isset($data['field_value']) ? $data['field_value'] : null);
        $userid = (isset($data['user_id']) ? $data['user_id'] : null);
        $storedtime = (isset($timeresults['last_donation_date']) ? $timeresults['last_donation_date'] : null);
 
        if (empty($username) && empty($storedtime))
        {
            $anon = 1;
            self::CustomActionAdd(0, $username, $price, $anon, $time);
            //not a linked user, but it is a new donation!
        }
 
        if (($username == "") && $storedtime == $time)
        {
            print_r($storedtime);
            echo "---";
            return;
        }
 
        if (($username == $name) && $storedtime  != $time)
        {
            echo "username exists but no time exists\n";
            $anon = 0;
            self::CustomActionAdd($userid, $username, $price, $anon, $time);
            //linked user and a new donation!
        }
 
        if (($username == $name) && $storedtime == $time)
        {
            echo "username exists but time exists\n";
            return;
        }
    }
 
    protected static function CustomActionAdd($userid, $username, $price, $anon, $time)
    {
        $viewParams = self::_getAddEditDateData() + array(
            'donation' => array(
                'user_donation_id' => 0,
                'user_id' => $userid,
                'username' => $username,
                'amount' => $price, // or $amount? I dont know
                'note' => 'Game Donation',
                'anonymous' => $anon,
                'donation_date' => $time,
            )
        );
 
        return self::_getAddEditResponse('Merc_DonationManager_ViewAdmin_Donation_Add', $viewParams);
    }
}
?>
I'm new to this :) Also I couldn't get it working otherwise for some reason.
 

silence

Well-known member
#19
Alright I have almost everything working, I have just one last error:
HTML:
        $db = XenForo_Application::get('db');
 
        $results = $db->query('
                SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"
        ');
        $storedtime = $db->query('
                SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time.'
        ');
     
        $data = $results->fetch_array(MYSQLI_ASSOC);
        $gettime = $storedtime->fetch_array(MYSQLI_ASSOC);
And I get the error: ----------- Fatal error: Call to undefined method Zend_Db_Statement_Mysqli::fetch_array() in /home/xeno/public_html/xenforo/library/Donation/Game.php on line 49
 

Akinak

Well-known member
#20
Code:
        $db = XenForo_Application::getDb();
 
        $data = $db->fetchAll('SELECT `field_value`,`user_id`
                FROM `xf_user_field_value`
                WHERE field_value = "'.$name.'"');
        $gettime = $db->fetchAll('SELECT `last_donation_date`
                FROM `merc_donor`
                WHERE last_donation_date = '.$time);