Jump to content


Photo

Custom actions


  • Please log in to reply
13 replies to this topic

#1 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 09 July 2012 - 05:00 PM

Hi,
Wasn't sure where to put this.
I'm very confused about how the custom actions works.
I looked at this tutorial: http://community.inv...om-actions-r496
But it's not of much help really..
I tried with this very basic function:


public function onPaid( $member, $package, $invoice )
        {
            $this->DB->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
        }
Well, no errors was displayed when trying to purchase my item, however that query didn't get executed, or it didn't change anything. No PHP errors at all, nothing to debug with.
Also, i would like to use the custom fields for my product, how can I do this?

#2 DavidAM

DavidAM

    Spam Happy

  • +Clients
  • 542 posts

Posted 09 July 2012 - 06:14 PM

Remember that IPB is using own driver..

Example from one of my actions..
public function onPaid($member, $package, $invoice) {
	 $db = $this->connect();

	 foreach ($invoice->items as $key => $item) {
		 continue;
	 }

	 (...)

	 sqlsrv_query($db, $sql, $params);

	 $db = ipsRegistry::DB();

	 $db->query("SHOW TABLE STATUS LIKE 'igzxccn_nexus_purchases'");
	 while ($row = $db->fetch()) {
		 $statusTable = $row;
	 }

	 $data = array(
		 'lkey_key' => $hash,
		
			 (...)
	 );

	 $db->insert('nexus_licensekeys', $data);
}

Also, onPaid will get executed only after the payment will be confirmed. So purchase made, and payment received where onPurchase gonna run after the invoice will be generated as I remember.

p.s. before I determined how this all exactly works like I had to create ~6-8 tickets to get spec info :tongue:
  • Mikkel-T likes this

#3 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 10 July 2012 - 02:14 AM

Your lucky they would answer your tickets, they wouldn't support me :P
Tried this:

public function onPaid( $member, $package, $invoice )
        {
            $db = $this->connect();
            $db = ipsRegistry::DB();
            $db->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
        }
Wouldn't work either.. :(

#4 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,383 posts

Posted 10 July 2012 - 05:57 AM

Your class isn't extending anything, and you haven't set $this->DB, so you're calling a property that doesn't exist :)

You want:
public function onPaid( $member, $package, $invoice )
{
     ipsRegistry::DB()->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
}

Mark Wade
Developer

zce-php5-3-logo.gif php5-zce-logo-new.gif


#5 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 10 July 2012 - 07:01 AM

Hi Mark,
I thought I achieved that by using this:

$db = ipsRegistry::DB();
$db->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
Anyway, I tried yours without luck.

Then I tried this:

private $registry;
    private $DB;
    private $settings;
    private $request;
    private $lang;
    private $member;
    private $memberData;
    private $cache;
    private $caches;

    function __construct($registry){
            $this->registry   =  $registry;
            $this->DB         =  $this->registry->DB();
            $this->settings   =& $this->registry->fetchSettings();
            $this->request    =& $this->registry->fetchRequest();
            $this->lang       =  $this->registry->getClass('class_localization');
            $this->member     =  $this->registry->member();
            $this->memberData =& $this->registry->member()->fetchMemberData();
            $this->cache      =  $this->registry->cache();
            $this->caches     =& $this->registry->cache()->fetchCaches();
    }
        /**
         * Item Purchased (run before onPurchaseGenerated)
         *
         * @param       array   The member purchasing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @return      void
         */
        public function onPaid( $member, $package, $invoice )
        {
            $this->DB->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
        }
And it didn't appear to be working either..
Is the registry sent when this class is created?

#6 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,383 posts

Posted 10 July 2012 - 09:51 AM

Nexus doesn't send the registry instance to the constructor.

What is the entire file you're using? You're probably naming the class wrong.

Mark Wade
Developer

zce-php5-3-logo.gif php5-zce-logo-new.gif


#7 DavidAM

DavidAM

    Spam Happy

  • +Clients
  • 542 posts

Posted 10 July 2012 - 10:36 AM

Does the UPDATE struct is fine? Make sure the first value should be a table name without the prefix, the TABLE should be located in forum database as using the above what @Mark stated you connect to forum db, not external.

#8 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 10 July 2012 - 10:39 AM

This is my class:

<?php

class custom_actions_WowJob //extends ipsCommand
{

    private $registry;
    private $DB;
    private $settings;
    private $request;
    private $lang;
    private $member;
    private $memberData;
    private $cache;
    private $caches;

    function __construct($registry){
            $this->registry   =  $registry;
            $this->DB         =  $this->registry->DB();
            $this->settings   =& $this->registry->fetchSettings();
            $this->request    =& $this->registry->fetchRequest();
            $this->lang       =  $this->registry->getClass('class_localization');
            $this->member     =  $this->registry->member();
            $this->memberData =& $this->registry->member()->fetchMemberData();
            $this->cache      =  $this->registry->cache();
            $this->caches     =& $this->registry->cache()->fetchCaches();
    }
        /**
         * Item Purchased (run before onPurchaseGenerated)
         *
         * @param       array   The member purchasing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @return      void
         */
        public function onPaid( $member, $package, $invoice )
        {
            $this->DB->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
            ipsRegistry::DB()->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
        }

        /**
         * Purchase record generated (run after onPaid)
         *
         * @param       array   The member purchasing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @param       array   Row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onPurchaseGenerated( $member, $package, $invoice, $purchase )
        {
            $db = $this->connect();
            $db = ipsRegistry::DB();
            $db->update( "ccs_custom_database_3", array( 'field_34' => 1 ), 'primary_id_field=1' );
        }

        /**
         * Purchase Renewed, but was still active anyway
         *
         * @param       array   The member renewing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @param       array   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onRenew( $member, $package, $invoice, $purchase )
        {

        }        

        /**
         * Purchase Renewed after had expired
         *
         * @param       array                   The member renewing
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Invoice Model [May not be set if manually reactivating]
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onReactivate( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase Expired
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Will usually be null. If the purchase is expiring because the invoice which previously renewed it is now being marked unpaid, the invoice object will be passed.
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onExpire( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase Cancelled or Deleted
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Will usually be null. If the purchase is being cancelled because the invoice which was used to purchase it it is now being marked unpaid, the invoice object will be passed.
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onCancel( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase is transferred to another member
         *
         * @param       array           Old owner
         * @param       array           Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array           The row from nexus_purchases
         * @param       customer        New owner
         * @return      void
         */
        public function onTransfer( $oldOwner, $package, $purchase, $newOwner )
        {

        }

        /**
         * Purchase is upgraded/downgraded
         *
         * @param       array           The member the purchase belongs to
         * @param       array           Old package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array           The row from nexus_purchases
         * @param       package         New package object
         * @return      void
         */
        public function onchange( $member, $oldPackage, $purchase, $newPackage )
        {

        }

        /**
         * Purchases' parent purchase is changed
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array                   The row from nexus_purchases
         * @param       array|null        The previous parent (may be null if had no parent previously)
         * @param       array|null        The new parent (may be null if was previously associated and now not)
         * @return      void
         */
        public function onAssociate( $member, $package, $purchase, $oldParent, $newParent )
        {

        }

}


#9 DavidAM

DavidAM

    Spam Happy

  • +Clients
  • 542 posts

Posted 10 July 2012 - 10:42 AM

First of all

$db = $this->connect();
$db = ipsRegistry::DB();

you overwrite first $db with second $db.

#10 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 10 July 2012 - 11:43 AM

Yea, I don't use onPurchaseGenerated just onpaid, which is the one not working.

#11 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,383 posts

Posted 11 July 2012 - 09:41 AM

1. You're specifying a constructor and assuming the registry is passed to it, which it isn't.
2. You're calling $this->connect() which doesn't exist.

Mark Wade
Developer

zce-php5-3-logo.gif php5-zce-logo-new.gif


#12 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 11 July 2012 - 11:16 AM

Okay, if the registry is not passed how would I do then :)
Should I extend a specific class, and how would I eventually get on from there?

#13 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,383 posts

Posted 12 July 2012 - 09:38 AM

Instead of $this->DB-> use ipsRegistry::DB()->
It's a Singleton.

Mark Wade
Developer

zce-php5-3-logo.gif php5-zce-logo-new.gif


#14 Mikkel-T

Mikkel-T

    IPB Newbie

  • +Clients
  • 30 posts

Posted 12 July 2012 - 10:21 AM

Okay, but for some reason that doesn't work either.
Also if i want to use custom fields how can I do that? :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users