Use Of Notifiers - How To
I have read the wiki tutorial and previous v1.3 anouncment posts in the forum and I understand the concept of notifieriers, but cannot get it working...
I presumed, that when I save a new class which has
Code:
$_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
or similar in the class consturctor, then the observer class should implement the code and wake it up, when notified, but nothing... [Tried also with the original code in the wiki]
As far as I understand, the application_top does not read in the classes that are saved under includes/classes/observers catalog, so the class and its constructor that should attach notifier to praticular event is never called and observers array stays empty... Should aplication_top read in all observers that are saved into this folder or should I explicitly say somewhere, that these classes should be included, so the class constructors are waked and observers are added to observers array?
Debuging the code with breakpoint at class.base notify function indicates indeed that $this->observers array is empty in spite of the fact that I copied the observer code from the wiki API...
Re: Use Of Notifiers - How To
I am trying to write the observer class attached to events during the checkout process. I need the session class with several variables storing values for session.
But I can not properly initialize them and get them reinitialized several times during the checkout process. A a result I loose the values assigned to these variables on previous entry to the class. It looks like I do not understand how does the ONS work.
For test purposes I wrote a stupid testclass attached to the same events. But it does not work at all. I would appreciate if someone could advice what is wrong.
The file config.testclass.php in the includes/auto_loaders directory contains:
Code:
<?php
/*
* instantiate class.testclass at 180 - when
* everything is initialized
* in <root>/includes/auto_loaders
*/
$autoLoadConfig[180][]= array('autoType' => 'class',
'loadFile' => 'observers/class.testclass.php');
$autoLoadConfig[180][]= array('autoType' => 'classInstantiate',
'className' => 'testclass',
'objectName' => 'testClassObject',
'checkInstantiated' => true,
'classSession' => true );
?>
and class.testclass.php in includes/classes/observers looks like this:
Code:
<?php
class testclass extends base {
var $initialized= false;
var $field;
/*constructor*/
function testclass() {
global $zco_notifier;
$this->test_log($this->initialized ."\t=initialized at start constructor");
$this->test_log($this->field ."\t=field at start constructor");
$zco_notifier -> attach($this,
array('NOTIFY_HEADER_START_CHECKOUT_CONFIRMATION',
'NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION',
/*'NOTIFY_HEADER_END_CHECKOUT_SUCCESS',*/
'NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS',
'NOTIFY_HEADER_START_ACCOUNT_HISTORY_INFO'));
if( !($this->initialized) ) {
$this->initialized= true;
$this->field= -10;
}
$this->test_log($this->initialized ."\t=initialized at end constructor");
$this->test_log($this->field ."\t=field at end constructor");
}
/* update method */
function update(&$callingClass, $eventID, $paramsArray) {
if($eventID == 'NOTIFY_HEADER_START_CHECKOUT_CONFIRMATION') {
$this->test_log($this->initialized ."\t=initialized at start " .$eventID);
$this->test_log($this->field ."\t=field at start " .$eventID);
$this->field= 200;
$this->test_log($this->initialized ."\t=initialized at end " .$eventID);
$this->test_log($this->field ."\t=field at end " .$eventID);
} else if($eventID == 'NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION') {
$this->test_log($this->initialized ."\t=initialized at start " .$eventID);
$this->test_log($this->field ."\t=field at start " .$eventID);
$this->field= 300;
$this->test_log($this->initialized ."\t=initialized at end " .$eventID);
$this->test_log($this->field ."\t=field at end " .$eventID);
} else if($eventID == 'NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS') {
$this->test_log($this->initialized ."\t=initialized at start " .$eventID);
$this->test_log($this->field ."\t=field at start " .$eventID);
$this->field= 400;
$this->test_log($this->initialized ."\t=initialized at end " .$eventID);
$this->test_log($this->field ."\t=field at end " .$eventID);
} else if($eventID == 'NOTIFY_HEADER_START_ACCOUNT_HISTORY_INFO') {
$this->test_log($this->initialized ."\t=initialized at start " .$eventID);
$this->test_log($this->field ."\t=field at start " .$eventID);
$this->field= 500;
$this->test_log($this->initialized ."\t=initialized at end " .$eventID);
$this->test_log($this->field ."\t=field at end " .$eventID);
}
}
/* write debug */
function test_log($message) {
$flog= fopen("/tmp/zc_log.txt","a");
fwrite($flog,$message ."\n");
fclose($flog);
}
} /* end testclass */
?>
As you can see the class only changes the value of the $this->field and writes simple trace to the file. But the written zc_log.txt file contains only the output from the constructor method. It seems the update method is never called.
What is wrong here?