Using atkActionListener to capture action events
From Achievo/ATK Wiki
|
ATK Howto: Using atkActionListener to capture action events
|
You can use listeners to perform actions after an action has been performed. You only need to derive a class from atkActionListener and implement its actionPerformed method, like this:
atkimport('atk.utils.atkactionlistener'); class productListener extends atkActionListener { function actionPerformed($action,$record) { $email = 'test@test.com'; $user = getUser(); switch ($action) { case 'save': mail($email,'New Product!', "User {$user['name']} has added product {$record['name']}\n"); break; case 'delete': mail($email,'Product removed!', "User {$user['name']} has removed {$record['atkorgrec']['name']}\n"); break; case 'update': mail($email,'Product changed!', "User {$user['name']} has changed a product.\n". "Was:\n name: {$record['atkorgrec']['name']}\n type: {$record['atkorgrec']['type']}\n". "Is now:\n name: {$record['name']}\n type: {$record['type']}\n"); break; } } }
Next, you can use it in a node like this:
class product extends atkNode { function product() { $this->atkNode('product',NF_TRACK_CHANGES); $this->add(new atkAttribute('id',AF_AUTOKEY)); $this->add(new atkAttribute('name')); $this->add(new atkListAttribute('type',array('non-food','food'))); $this->setTable('shop_product'); $this->addListener(new productListener(array('delete','save','update'))); } }
Action listeners are more flexible than postAdd/postUpdate/postDelete triggers, because they can be added dynamically, for example from a modifier.
Actions listeners can't be used as pre- triggers though.
See also
- atkEventLog - an off the shelf action logger
- atkActionListener - the class used in this howto
- atkTriggerListener - Listen to database events instead of ATK actions