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

Callback Custom Profile Fields

Discussion in 'XenForo Development Discussions' started by silence, May 8, 2013.

  1. silence

    silence Well-Known Member

    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();
        }
    }
    ?>
     
  2. silence

    silence Well-Known Member

    Alright I'm posting sad puppies until someone responds!
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
     
    0xym0r0n likes this.
  3. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  4. silence

    silence Well-Known Member

  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
  6. silence

    silence Well-Known Member

    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?
     
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  8. silence

    silence Well-Known Member

    Yeah I got it all working, now just gotta figure out how to hook into donation manager >.<
     
  9. silence

    silence Well-Known Member

    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
     
  10. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Exactly what it says.

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

    self::

    Instead of:

    $this->
     
  11. silence

    silence Well-Known Member

    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
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  13. silence

    silence Well-Known Member

  14. silence

    silence Well-Known Member

    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
     
  15. Akinak

    Akinak Well-Known Member

    You can give all code?
    We will fix and you will see the difference.
     
  16. silence

    silence Well-Known Member

    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);
        }
    }
    ?>
     
  17. Akinak

    Akinak Well-Known Member

    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);
        }
    }
    ?>
     
  18. silence

    silence Well-Known Member

    I'm new to this :) Also I couldn't get it working otherwise for some reason.
     
  19. silence

    silence Well-Known Member

    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
     
  20. Akinak

    Akinak Well-Known Member

    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);
     

Share This Page