Best way to publish a function between modules

turnbulm

Joined: 2004-10-03
Posts: 431
Posted: Fri, 2005-07-01 17:18

I'm working on a pair of modules that plug together. I've created the parent module, defined an interface, and created a child that implements that interface. Everything works just fine that way.

However, I now want the child to be able to call a couple of functions in the parent to get some information. What's the best way of exposing those parent functions to the child module? So far all I can think of doing is putting them in a helper class, and having the child 'include' it. That just doesn't seem very neat though.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-07-01 17:43

if the modules are depending on each other, why put the functionality in two different modules?
you may want to look into how cart plugin modules work. we have a cart module and other modules (zip download, paypal, ...) implement a cart plugin. or other modules implement search functionality (search interface).

 
turnbulm

Joined: 2004-10-03
Posts: 431
Posted: Fri, 2005-07-01 17:50

The reason for creating the separation is that there can be multiple child modules plugging into the parent - this is the 'paypal checkout' module. There is a parent module that deals with getting all the product and quantity information together, then we fork out to one of the installed 'payment' plugins. So far there are two - pay by paypal, or just email the cart.

I understand how the interfaces work as in cart and search, and that is fine for letting the parent hand off to the child, but what I can't see is a way to let the child module call a function of the parent.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-07-01 18:43

not sure if i understand you correctly.
cart is the parent and paypal the child?
or paypal the parent and other modules can use the paypal module?

and what kind of information / calls would the child have to make to the parent, and the other way?

 
turnbulm

Joined: 2004-10-03
Posts: 431
Posted: Fri, 2005-07-01 20:10

OK... the parent module is called 'checkout', which is a plugin to cart. This handles product selection, pricing, etc, and finishes up with a fully populated cart ready for payment.

Then there are plugin payment modules, of which 'paypal' is one.

There is an interface defined for payment plugins, which allows the checkout module to hand off to them in the appropriate way at the appropriate time.

However, I want the interaction to be a bit more bi-directional. In particular, I want the payment module to be able to call back to the 'checkout' module to tell it to empty the cart only once the payment process has been completed. So, I'd envisaged the payment module being able to call a function along the lines of Checkout::PaymentComplete.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-07-01 20:36

when the checkout does the $paymentPlugin->handleRequest(...); couldn't be the success message "payment complete" be the return value of handleRequest(...)?

what i suggest:

Checkout.inc view loadTemplate() shows the option for the currently activate payment plugin and tab links for the alternative payment plugins, much like the ItemAdd view.
the controller would then load the plugin, call list($ret, $status, $data) = $plugin->handleRequest(....);
the payment module would complete the payment, e.g. a transaction with paypal and return the success status to the checkout module.
finally the checkout module shows the status message, does whatever is needed.

i'd suggest to copy the ItemAdd / addPlugin logic. maybe i haven't understood the involved problems though.

and yes, the are other options. you could do a postEvent in the payment modules as you suggested and register an event listener in the checkout module.

 
turnbulm

Joined: 2004-10-03
Posts: 431
Posted: Fri, 2005-07-01 21:03

The thing about it is that the payment plugin needs to do some processing and interaction with the user itself before the process is complete. I can't just process everything in the background and return a 'success' code.

I'll take a look at registering an event listener - any clues as to modules that already do this so I can have a look at how it's done?

Thanks for the help

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-07-01 21:11

is a single page / form not enough?

take a look at ItemAdd.inc. each itemAdd plugin has different webform fields etc.
you fill them out, ItemAdd receives the form data, delegates the form data to the correct plugin, the plugin does its job (in your case check form data, talk to paypal, ...), and returns the status to ItemAdd.

with a little javascript or so you could even have "multiple page" web form.

and if you ultimately want to have events for the sake of events fine ;) (but i still don't understand why the itemAdd approach doesn't work here).

events? quite easy. see modules/quotas/module.inc on how to register an event listener (module constructor + function).
posting an event is easy. see modules/core/classes/GalleryEntity.class function save.
the data you add to an event is up to you. you don't have to follow any rules there afaik.