Yes, it actually makes more sense to break it into two classes, one for events and one to hold special session info and processing.
I'm holding some custom form data that gets attached to a product added to the cart and follows it through checkout, so that's in the session object. Needed to handle some events in the cart to keep the data updated (like if product is deleted or cart restored or emptied, etc)...first thought was to have one class....but makes more sense to separate events from data.
Works fine once I separated them out...so no problem now.
When they were together, my event handler was something like this:
Code:
class hcEvents extends base {
function hcEvents() {
}
// setNotifiers call set up in 'config.hc_loader.php'
// event 191 (after everything else already done)
// must call after class set up in case it was already a session variable
// there is no 'wake up' call to session objects (that I can find)
function setNotifiers() {
global $zco_notifier;
$this->attach($this, array(
'NOTIFIER_CART_ADD_CART_END',
'NOTIFIER_CART_REMOVE_END',
'NOTIFIER_CART_RESTORE_CONTENTS_START',
'NOTIFIER_CART_INSTANTIATE_END',
'NOTIFIER_CART_RESET_END'
));
// procedural code overrides
//$zco_notifier->attach($this, array('NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION'));
}
function update(&$class, $eventID) {
// 'class' is object throwing event
switch ($eventID) {
case 'NOTIFIER_CART_INSTANTIATE_END':
case 'NOTIFIER_CART_RESET_END':
// reset
break;
case 'NOTIFIER_CART_ADD_CART_END':
case 'NOTIFIER_CART_REMOVE_END':
// NOTYET see if registration item in cart
break;
case 'NOTIFIER_CART_RESTORE_CONTENTS_START':
// do not restore registration items
// delete from customers basket, don't restore
break;
}
}
and here's the auto_init code:
Code:
// load hc observers code
$autoLoadConfig[10][] = array('autoType'=>'class',
'loadFile'=>'observers/hc_reg.php');
// create, after cart object created
// is stored in session
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
'className'=>'hcEvents',
'objectName'=>'hcevt',
'checkInstantiated'=>true,
'classSession'=>true
);
// re-attach notifiers, hc_reg is in session, so restored
// but notifiers are static and not restored, so set them each time
// call function after auto_load
$autoLoadConfig[191][] = array('autoType'=>'objectMethod',
'objectName'=>'hcevt',
'methodName' => 'setNotifiers');
It seems like this should have worked since it restored the observers...but when I stepped through the code, the events either didn't find the observer or it did not call the object.
Only a curiosity now, though...I think architecturally better to separate that functionality out anyway.
thanks
Bookmarks