Example 5

Playing around the events

This example highlights the code required to make and manage custom events.

The example is taken from my script who shows all open tickets and allows the user to add notes and close the tickets.

First, I need to add two custom links on each row (YAP shows one ticket per row).
Therefore, I write my custom function to call on each row. This function adds two links: add note, close the ticket. That job requires two new events YAP_TCKCLOSE and YAP_ADDNOTE.


/**
*
* Function used to add other link to each data row.
* It adds links to add note, close ticket and show the related device.
*/
function OtherFunctionAll($row)
{
// This link fires my custom event YAP_ADDNOTE.
// All event name must begin with YAP_
$link=$_SERVER['PHP_SELF'].'?id='.$row['id'].'&ev=YAP_ADDNOTE';
$text[0]='<a href='.$link.' onMouseOver="ToolTip(\'Add comments to this ticket\')" onMouseOut="ToolTip()" ><img src=\'images/postit.gif\' border=0></a>';
if(
$row['DateClose']=='2000-01-01')  
   {
   
// and this link fires my custom event YAP_TKCLOSE
   
$link=$_SERVER['PHP_SELF'].'?id='.$row['id'].'&amp;ev=YAP_TKCLOSE';
   
$text[1]='<a href='.$link.' onMouseOver="ToolTip(\'Close the ticket\')" onMouseOut="ToolTip()" ><img src=\'images/tk_close.gif\' border=0></a>';   
   }
return(
$text);
}

Then I tell Yap to call my function on each row.


$Show_info=array('cfgfile' => '../cfg/itemallticket.cfg',
                 
'languagefile' => '../include/language.en',  
                 
'exit' => $_SESSION[$_SERVER['PHP_SELF'].'returnTo'],
                  
// the following parameter tells to CYap to call the function OtheFunctioAll on
                  // each row. This function add two custom links to each row.
                 
'rowcallback' => 'OtherFunctionAll',    
                  );   

Now I must tell Yap how to handle the new event. The following statement set the function to run when the event fire.


$p=new CPciYap(array(), array(), $db_info, $Show_info,$ModifyInfoCfg);
/*
* In this example I defined two custom event 'YAP_TKCLOSE' and 'YAP_ADDNOTE'.
* Here I set the function to run on events.
*/
$p->RegisterEvent('YAP_TKCLOSE','OnCloseTicket');
$p->RegisterEvent('YAP_ADDNOTE','OnAddNote');
$p->showpage();     

In addition, finally, I write the functions that close the ticket and add the note to the ticket.


/*
* Here I define the two functions. These function may be global or defined inside
* CYap.
*/
function OnAddNote()
{
...
}

/*
* ..and this function handle the event YAP_TKCLOSE
*/
function OnCloseTicket()
{
...
}