Jump to content

Triggers: Difference between revisions

From NusaATK
mNo edit summary
Line 17: Line 17:
Pre-trigger returning false does not necessarily halt the action being performed (because of backwards compatability). To actually halt the action, use the triggerError-function. Doing so will add an entry in the record called 'atkerror'. Upon detecting this, the action will halt.
Pre-trigger returning false does not necessarily halt the action being performed (because of backwards compatability). To actually halt the action, use the triggerError-function. Doing so will add an entry in the record called 'atkerror'. Upon detecting this, the action will halt.
And the message in the atkerror entry will be displayed as an error. For more information on the triggerError function, please see [http://www.achievo.org/docs/atk/latest/atk--/_atktools.inc.html#functiontriggerError the API documentation on the function].
And the message in the atkerror entry will be displayed as an error. For more information on the triggerError function, please see [http://www.achievo.org/docs/atk/latest/atk--/_atktools.inc.html#functiontriggerError the API documentation on the function].
 
eg
<syntaxhighlight lang="php">
if ($width > 400 || $height > 400) {
        triggerError($record, "picture", "You can upload only pictures smaller than 400 pixels in height and width");
    }
</syntaxhighlight>
an example for having the preAdd and preUpdate triggers run the same code:
an example for having the preAdd and preUpdate triggers run the same code:



Revision as of 04:29, 27 March 2016

You can use the following triggers on a node:

  • preAdd
  • postAdd
  • preUpdate
  • postUpdate
  • preDelete
  • postDelete

to perform various tasks after or before an action has been executed.

These triggers take only 1 argument, which is an array with the record.

Pre triggers

If the trigger is a pre- trigger, you can make the argument to be a pointer and you can adjust the record before it is added/updated/deleted.

Pre-trigger returning false does not necessarily halt the action being performed (because of backwards compatability). To actually halt the action, use the triggerError-function. Doing so will add an entry in the record called 'atkerror'. Upon detecting this, the action will halt. And the message in the atkerror entry will be displayed as an error. For more information on the triggerError function, please see the API documentation on the function. eg

if ($width > 400 || $height > 400) {
        triggerError($record, "picture", "You can upload only pictures smaller than 400 pixels in height and width");
    }

an example for having the preAdd and preUpdate triggers run the same code:

  function preAdd(&$record) 
  {
    $this->makeNames($record);
    return true;
  } 

  function preUpdate(&$record) 
  {
    $this->makeNames($record);
    return true;
  } 

  function makeNames(&$record)  
  {
    $record["full_name"] = $record["names_slot_1"] . $record["names_slot_2"];
  }

Listeners

Note that triggers are superseded by Listeners, a more flexible concept as Listeners can be applied at runtime.