Jump to content

How to create hooks in invision new version?


Go to solution Solved by teraßyte,

Recommended Posts

Can you please tell me which event is called when New topic is created.

in the processForm function - It is called during both time while editing and creating. 

But i need only when its created. I used save() , processAfterCreate(), createFromForm() and some mor but they are not being called while creating new topic.

 
Link to comment
Share on other sites

22 minutes ago, Adriano Faria said:

I have a ton of resources using this. Show your code.

        public function processAfterCreate($comment, $values)
        {
            $result = \IPS\Db::i()->query("SELECT * FROM xxxx WHERE user_id = " .  $user_id); // SET field_a='value' WHERE id_field=1");
            if ($result->num_rows > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    // $row is now an associative array containing column names as keys
                    $cc  = $reward_mailing_points + intval($row['cc']);
                }
            }
            return parent::processAfterCreate($comment, $values);
        }

        public function save()
        {
            $result = \IPS\Db::i()->query("SELECT * FROM xxxx WHERE user_id = " .  $user_id); // SET field_a='value' WHERE id_field=1");
            if ($result->num_rows > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    // $row is now an associative array containing column names as keys
                    $cc  = $reward_mailing_points + intval($row['cc']);
                }
            }
            return parent::save();
        }

Link to comment
Share on other sites

A few things:

  1. In the query, you are using $user_id, but there is no such value defined anywhere in the code.
  2. You are not saving the updated $cc value back to the database, that's why it's not working.
  3. You're using a mix of IPS and non-IPS functions to access the DB data. You should stick to using a single method to keep things consistent.
  4. Is the table you're trying to update in the same database as Invision Community? If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one.
Edited by teraßyte
Link to comment
Share on other sites

1 hour ago, teraßyte said:

A few things:

  1. In the query, you are using $user_id, but there is no such value defined anywhere in the code.
  2. You are not saving the updated $cc value back to the database, that's why it's not working.
  3. You're using a mix of IPS and non-IPS functions to access the DB data. You should stick to using a single method to keep things consistent.
  4. Is the table you're trying to update in the same database as Invision Community? If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one.

point 2 in the try block i m using below statement also

$result = \IPS\Db::i()->query("UPDATE xxx SET credit_balance = " . $credit_balance . "   WHERE user_id = '" .  $user_id . "'"); 
point 1 User id i m getting using below code 
            if (\IPS\Member::loggedIn()->member_id) {
                //  // Get the current user's member object
                $currentUser = \IPS\Member::loggedIn();
                //  // Access user data
                # Fetch member details

                $user_id = $currentUser->member_id;
                $username = $currentUser->name;
                $email = $currentUser->email;
                $user_group_id = $currentUser->member_group_id;
            } else {
                $user_id = 0;
            }

point 3 - my table name does not start with tbl_prefix. that is why i have to use mix  IPS and non-IPS

4. Is the table you're trying to update in the same database as Invision Community? - YES.

If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one. - My table is in the same db not the external db.

 
Link to comment
Share on other sites

I'm having a really hard time understanding your code because it's all partial or missing context and variables.

Anyway, you can temporarily disable the prefix in the database class if your tables don't have it. Or you can keep using manual query() calls, but there is no need to use mysqli_fetch_assoc.

 

See the code below, I tried to update your processAfterCreate() function above with more consistent code:

	public function processAfterCreate($comment, $values)
	{
		# Remove prefix
		$previousPrefix = \IPS\Db::i()->prefix;
		\IPS\Db::i()->prefix = '';
		
		
		# Vars
		$reward_mailing_points = 10; # Not sure where you're pulling this value from, so I set an arbitrary 10 here as example
		$cc = 0;
		
		# Get result - If this query always returns only 1 row, it can be changed to use ->first()
		$result = \IPS\Db::i()->select('*', 'xxxx', array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) );
		
		foreach( $result as $row )
		{
			$cc += $reward_mailing_points + intval($row['cc']);
		}
		
		# Update database with new $cc value
		\IPS\Db::i()->update( 'xxx', array( 'credit_balance' => $cc ), array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) );
		
		# Reset prefix
		\IPS\Db::i()->prefix = $previousPrefix;
		
		return parent::processAfterCreate($comment, $values);
	}
Link to comment
Share on other sites

14 hours ago, teraßyte said:

I'm having a really hard time understanding your code because it's all partial or missing context and variables.

Anyway, you can temporarily disable the prefix in the database class if your tables don't have it. Or you can keep using manual query() calls, but there is no need to use mysqli_fetch_assoc.

 

See the code below, I tried to update your processAfterCreate() function above with more consistent code:

	public function processAfterCreate($comment, $values)
	{
		# Remove prefix
		$previousPrefix = \IPS\Db::i()->prefix;
		\IPS\Db::i()->prefix = '';
		
		
		# Vars
		$reward_mailing_points = 10; # Not sure where you're pulling this value from, so I set an arbitrary 10 here as example
		$cc = 0;
		
		# Get result - If this query always returns only 1 row, it can be changed to use ->first()
		$result = \IPS\Db::i()->select('*', 'xxxx', array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) );
		
		foreach( $result as $row )
		{
			$cc += $reward_mailing_points + intval($row['cc']);
		}
		
		# Update database with new $cc value
		\IPS\Db::i()->update( 'xxx', array( 'credit_balance' => $cc ), array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) );
		
		# Reset prefix
		\IPS\Db::i()->prefix = $previousPrefix;
		
		return parent::processAfterCreate($comment, $values);
	}

Hello terabyte,

A very big thanks to your help and prompt reply. I was able to do the changes as per your suggestions. Here is my final code and its working when user is adding Topic at my local machine.

        protected function processAfterCreate($comment, $values)
        {

            # Remove prefix
            $previousPrefix = \IPS\Db::i()->prefix;
            \IPS\Db::i()->prefix = '';

            $user_group_id = \IPS\Member::loggedIn()->member_group_id;
            if ($user_group_id == 3 || $user_group_id == 4 || $user_group_id == 7 || $user_group_id == 😎 //bronze // || $user_group_id == 4)
            {
                # no need to check for double points
                $assign_point = 1;
            }

            if ($assign_point == 1) {
                $reward_mailing_points = 250;
            }
            $result = \IPS\Db::i()->select('*', 'plug_mailing_master', array('user_id=?', \IPS\Member::loggedIn()->member_id));
            foreach ($result as $row) {
                $credit_balance  = $reward_mailing_points + intval($row['credit_balance']);
            }

            # Update database with new $cc value
            \IPS\Db::i()->update('plug_mailing_master', array('credit_balance' => $credit_balance), array('user_id=?', \IPS\Member::loggedIn()->member_id));

            # Reset prefix
            \IPS\Db::i()->prefix = $previousPrefix;
            return parent::processAfterCreate($comment, $values);
        }

 

Link to comment
Share on other sites

26 minutes ago, Jyoti Rani said:

Hello terabyte,

A very big thanks to your help and prompt reply. I was able to do the changes as per your suggestions. Here is my final code and its working when user is adding Topic at my local machine.

        protected function processAfterCreate($comment, $values)
        {

            # Remove prefix
            $previousPrefix = \IPS\Db::i()->prefix;
            \IPS\Db::i()->prefix = '';

            $user_group_id = \IPS\Member::loggedIn()->member_group_id;
            if ($user_group_id == 3 || $user_group_id == 4 || $user_group_id == 7 || $user_group_id == 😎 //bronze // || $user_group_id == 4)
            {
                # no need to check for double points
                $assign_point = 1;
            }

            if ($assign_point == 1) {
                $reward_mailing_points = 250;
            }
            $result = \IPS\Db::i()->select('*', 'plug_mailing_master', array('user_id=?', \IPS\Member::loggedIn()->member_id));
            foreach ($result as $row) {
                $credit_balance  = $reward_mailing_points + intval($row['credit_balance']);
            }

            # Update database with new $cc value
            \IPS\Db::i()->update('plug_mailing_master', array('credit_balance' => $credit_balance), array('user_id=?', \IPS\Member::loggedIn()->member_id));

            # Reset prefix
            \IPS\Db::i()->prefix = $previousPrefix;
            return parent::processAfterCreate($comment, $values);
        }

 

Its working on the betaversion of the site also. Thanks a Ton!

Link to comment
Share on other sites

  • Solution

Based on what you posted I now have a clearer idea of what you're doing, but I still see a few issues and optimizations that can be made:

  1. You can use a quick in_array() check for the group instead of multiple OR checks. Also, note that your code doesn't take into account secondary groups, if you want to check also those, you need to adjust the code.
  2. The $assign_point variable is not defined before the IF check, if the user is in a group outside of the listed IDs, PHP 8 will throw an error about an unknown variable.
  3. I see that you're basically loading the credit balance from a table, adding 250 to it, and then updating the same table back. The whole process can be simplified in a single query instead.
  4. The reset DB prefix code can be moved inside the IF check, if the user doesn't need a credit balance update, there's no point running it.

Here's an updated code based on the points above:

	protected function processAfterCreate($comment, $values)
	{
		# Comment/uncomment either line below based on what you want
		#if ( \in_array(  \IPS\Member::loggedIn()->member_group_id, array( 3, 4, 7, 8 ) ) ) # This line checks only the primary group
		if ( \IPS\Member::loggedIn()->inGroup( array( 3, 4, 7, 8 ) ) ) # This line checks also secondary groups
		{
			# Temporarily disable the DB prefix
			$previousPrefix = \IPS\Db::i()->prefix;
			\IPS\Db::i()->prefix = '';
			
			# Update credit_balance column to add 250 credits to its current value
			\IPS\Db::i()->update('plug_mailing_master', "credit_balance=credit_balance+250", array('user_id=?', \IPS\Member::loggedIn()->member_id));
			
			# Reset prefix
			\IPS\Db::i()->prefix = $previousPrefix;
		}
		
		return parent::processAfterCreate($comment, $values);
	}

The code checks if the user has one of the specified groups, and adds 250 to their credit balance value based on their member ID. No need for anything else.

 

P.S.: If you use the Code button in the editor the code you post will look better and formatted for the appropriate language. 😉

Edited by teraßyte
Link to comment
Share on other sites

Just now, teraßyte said:

Based on what you posted I now have a clearer idea of what you're doing, but I still see a few issues and optimizations that can be made:

  1. You can use a quick in_array() check for the group instead of multiple OR checks. Also, note that your code doesn't take into account secondary groups, if you want to check also those, you need to adjust the code.
  2. The $assign_point variable is not defined before the IF check, if the user is in a group outside of the listed IDs, PHP 8 will throw an error about an unknown variable.
  3. I see that you're basically loading the credit balance from a table, adding 250 to it, and then updating the same table back. The whole process can be simplified in a single query instead.

Here's an updated code based on the points above:

	protected function processAfterCreate($comment, $values)
	{
		# Remove prefix
		$previousPrefix = \IPS\Db::i()->prefix;
		\IPS\Db::i()->prefix = '';
		
		# Comment/uncomment either line below based on what you want
		#if ( \in_array(  \IPS\Member::loggedIn()->member_group_id, array( 3, 4, 7, 8 ) ) ) # This line checks only the primary group
		if ( \IPS\Member::loggedIn()->inGroup( array( 3, 4, 7, 8 ) ) ) # This line checks also secondary groups
		{
			# Update database with new $cc value
			\IPS\Db::i()->update('plug_mailing_master', "credit_balance=credit_balance+250", array('user_id=?', \IPS\Member::loggedIn()->member_id));
		}
		
		# Reset prefix
		\IPS\Db::i()->prefix = $previousPrefix;
		
		return parent::processAfterCreate($comment, $values);
	}

The code checks if the user has one of the specified groups, and adds 250 to their credit balance value based on their member ID. No need for anything else.

 

P.S.: If you use the Code button in the editor the code you post will look better and formatted for the appropriate language. 😉

One more query is, when some one replied to this Topic, which event is fired internally.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...