:: Version 3.2.0 ::
Add a log entry when an attachment is added
Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.
- learning:
- Use of event to execute action
- level:
- Advanced
- domains:
- PHP, Automation, Event, Attachment, Caselog
- min version:
- 3.1.2
This requires at least iTop
3.1.2
Need: add a log entry on a UserRequest ticket each time someone add an attachment to the ticket
With the notion of event, you can subscribe to a particular event and execute a method as soon as this event is triggered.
-
We will create a method on the UserRequest class to add a log entry,
-
Subscribe to two different events and specify the callback method to use.
Add log entry
- class::UserRequest
-
public function AddLogEntryOnAttachment(Combodo\iTop\Service\Events\EventData $oEventData) { // Get the log, the returned object is an ormCaselog, so an object $oLog = $this->Get('public_log'); // Get the Attachment object from the event $oAttachment = $oEventData->Get('attachment'); // Get an ormDocument from the Attachment $oDocument = $oAttachment->Get('contents'); // Test which "event" has triggered the callback if ($oEventData->GetEvent() == EVENT_ADD_ATTACHMENT_TO_OBJECT) // Add an entry to the log, with the name of the file attached $oLog->AddLogEntry('Add an attachment: '.$oDocument->GetFileName()); else $oLog->AddLogEntry('Removed attachment: '.$oDocument->GetFileName()); // Set back that modified Log on the current UserRequest $this->Set('public_log', $oLog); $this->DBUpdate(); }
Subscribe to event
Here we subscribe to 2 different events, but the same callback method will handle the reaction
- itop-design / classes
-
<class id="UserRequest" _delta="must_exist"> <event_listeners> <event_listener id="AddLogEntryOnAttachment" _delta="define"> <event>EVENT_ADD_ATTACHMENT_TO_OBJECT</event> <callback>AddLogEntryOnAttachment</callback> <rank>0</rank> </event_listener> <event_listener id="AddLogEntryOnAttachmentRemoval" _delta="define"> <event>EVENT_REMOVE_ATTACHMENT_FROM_OBJECT</event> <callback>AddLogEntryOnAttachment</callback> <rank>0</rank> </event_listener> </event_listeners> </class>
Full delta
<?xml version="1.0" encoding="UTF-8"?> <itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"> <classes> <class id="UserRequest"> <event_listeners> <event_listener id="AddLogEntryOnAttachment" _delta="define"> <event>EVENT_ADD_ATTACHMENT_TO_OBJECT</event> <callback>AddLogEntryOnAttachment</callback> <rank>0</rank> </event_listener> <event_listener id="AddLogEntryOnAttachmentRemoval" _delta="define"> <event>EVENT_REMOVE_ATTACHMENT_FROM_OBJECT</event> <callback>AddLogEntryOnAttachment</callback> <rank>0</rank> </event_listener> </event_listeners> <methods> <method id="AddLogEntryOnAttachment" _delta="define"> <static>false</static> <access>public</access> <type>EventListener</type> <code><![CDATA[ public function AddLogEntryOnAttachment(Combodo\iTop\Service\Events\EventData $oEventData) { blablabla... } ]]> </ code> </method> </methods> </class> </classes> </itop_design>
With an iTop 3.1.0+ but lower than 3.1.2, you can maybe react on the Attachment class to the event:
-
EVENT_DB_AFTER_WRITE,
-
EVENT_DB_AFTER_DELETE
Then retrieve the object class, object key associated with the Attachment and if it's a User Request add a log entry…
3_2_0/customization/log-attachment.txt · Last modified:
2024/08/13 09:54 (external edit)