excuting PHP Code on new post

Nudaii

Well-known member
Hello all

I'm pretty new to php so bear with me.

basically what i need is for a way for with every new post made there is a random chance for a query to run which includes the posters userid

in raw php/forms ive been able to do it, but i cannot replicate it to work with xenforo submit post button.

any pointers?
 
Hello all

I'm pretty new to php so bear with me.

basically what i need is for a way for with every new post made there is a random chance for a query to run which includes the posters userid

in raw php/forms ive been able to do it, but i cannot replicate it to work with xenforo submit post button.

any pointers?

PHP:
<?php

/*
* Extends XenForo DataWriter DiscussionMessage Post.
*/

class Icewind_IcewindDale_Listener_Proxy_DataWriter_Post extends XFCP_Icewind_IcewindDale_Listener_Proxy_DataWriter_Post
{
    protected function _updateUserMessageCount($isDelete = false)
    {
        $parent = parent::_updateUserMessageCount($isDelete);
        if ($isDelete)
        {
            return $parent;
        }

        // your code here

        return $parent;
    }
}
 
I had got the addon working just prior to these replies using Controller, however i know proper standard is to use a model for it.

any pointers on how this (working) code should be split up to conform to normal xf standards?

PS yes the queries are trash, they are just dumping to a test table while i decide what table names wil be called lol


PHP:
<?php
  class RRWS_PostLoot_ControllerPublic_Thread extends XFCP_RRWS_PostLoot_ControllerPublic_Thread {

    public function actionAddReply()
    {
    //Random Number Generator
    $RNG = rand(1,15);
    $bronzeDrop = 5;
    $silverDrop = 10;
    $goldDrop = 15;

   if($RNG == $bronzeDrop) {
    //Bronze Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='Bronze rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'Bronze rn 2' WHERE `user_id` = $user_id");
    }elseif ($RNG == $silverDrop) {
    // Silver Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='silver rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'silver rn 2' WHERE `user_id` = $user_id");
    }elseif ($RNG == $goldDrop) {
    // Gold Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='gold rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'gold rn 2' WHERE `user_id` = $user_id");
    }

    //Return the Parent
    return parent::actionAddReply();

    }
    }

?>
 
I had got the addon working just prior to these replies using Controller, however i know proper standard is to use a model for it.

any pointers on how this (working) code should be split up to conform to normal xf standards?

PS yes the queries are trash, they are just dumping to a test table while i decide what table names wil be called lol


PHP:
<?php
  class RRWS_PostLoot_ControllerPublic_Thread extends XFCP_RRWS_PostLoot_ControllerPublic_Thread {

    public function actionAddReply()
    {
    //Random Number Generator
    $RNG = rand(1,15);
    $bronzeDrop = 5;
    $silverDrop = 10;
    $goldDrop = 15;

   if($RNG == $bronzeDrop) {
    //Bronze Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='Bronze rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'Bronze rn 2' WHERE `user_id` = $user_id");
    }elseif ($RNG == $silverDrop) {
    // Silver Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='silver rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'silver rn 2' WHERE `user_id` = $user_id");
    }elseif ($RNG == $goldDrop) {
    // Gold Loot

    //Snag the User ID
    $visitor = XenForo_Visitor::getInstance();
    $user_id = $visitor['user_id'];
    //Run the queries
    $db = XenForo_Application::getDb();
    $lootDrop = $db->query("UPDATE `test_table` SET `test_imput`='gold rng 2',`test_coin`=6002 WHERE `user_id` = $user_id");
    $LootNotice = $db->query("UPDATE `xf_user` SET `loot_alert` = 'gold rn 2' WHERE `user_id` = $user_id");
    }

    //Return the Parent
    return parent::actionAddReply();

    }
    }

?>
I don't think this way is safe. In the case you post an message with error and your code still work fine :D
 
Ouuuu, awarding loot, I like it! I need to ask, why are you using controller public thread to run your code? In the example I posted above I use the datawriter, because it checks for errors first. When my code gets executed I know I have a valid post to work with.

You don't need to create a model to update your custom table, you can do it within your postloot class, and you can place it as a function so you only need to write it once to handle all three of your if results:

$this->_updateTestTable($loot, $coin, $userId);

Also, you only need to get one instance of $visitor as all three of your if statements uses it:

Near the top of your method put this in, $visitor = XenForo_Visitor::getInstance(); and now to get the user_id, just use this: $userId = $visitor->getUserId();

XenForo model user comes with a method to update specific user data; you can call it like this:

$userLootData = array(
'loot_alert' => $loot
);

$this->_getUserModel()->update($userId, $userLootData);

protected function _updateTestTable($testImput = 0, $testCoin = 0, $userId)
{
// note the xf_ added to the table name, all custom tables should be preceded with xf_
return $this->_db->query('
UPDATE xf_test_table
SET
test_imput = ?, test_coin = ?
WHERE user_id = ?', array(
$testImput, $testCoin, $userId
)
);
}

protected function _getUserModel()
{
return $this->getModelFromCache('XenForo_Model_User');
}
 
Oh and yeah I run a RP forum, we decided to begin adding some RP features :D

as for why I did via controller, it was my first attempt at xF addon lol so didn't really know what way to do it lol

I will be swapping to your data writer method!
 
Back
Top Bottom