0

In iTop, How is it possible to save caller's IP address in tickets (User Request and Incident)

I tried to modify datamodel.itop-tickets.xml in my extension module. I added a field named 'ip' successfully but in <methods> section I can not get client's IP using $_SERVER['REMOTE_ADDR'] .

<methods>
            <method id="DBInsertNoReload"  _delta="redefine">
                <static>false</static>
                <access>public</access>
                <type>Overload-DBObject</type>
                <code><![CDATA[
public function DBInsertNoReload()
{
      $oMutex = new iTopMutex('ticket_insert');
      $oMutex->Lock();
      $iNextId = MetaModel::GetNextKey(get_class($this));
      $sRef = $this->MakeTicketRef($iNextId);
      $this->Set('ref', $sRef);
      $iKey = parent::DBInsertNoReload();
      $oMutex->Unlock();
      return $iKey;

      $this->Set('ip', $_SERVER['REMOTE_ADDR'] );
}
    ]]></code>
            </method>               
        </methods>
Amir Keshavarz
  • 2,135
  • 2
  • 15
  • 19

2 Answers2

1

There is another option, in itop customs extension you can include another datamodel. (you can use XML or PHP datamodel). So, you have to create a new php file and write the class you want inside to extend the datamodel. You have to extends them with : https://www.combodo.com/documentation/api-ref-extensions/packages/Extensibility.html

If you use the interface "iApplicationObjectExtension", you can use the method OnDBInsert to set other field in your object/

for example

Class YourClassName implements iApplicationObjectExtension {

    public function OnIsModified($oObject){}

    public function OnCheckToWrite($oObject){}

    public function OnCheckToDelete($oObject){}

    public function OnDBUpdate($oObject, $oChange = null){}

    public function OnDBDelete($oObject, $oChange = null){}

    public function OnDBInsert($oObject, $oChange = null) {
        if ($oObject instanceof UserRequest) {
            // Do what you want with $oObject

            $oObject->DBUpdate(); // Update object
        }
    }
}
D. LP
  • 11
  • 2
0

After lots of attempts I finally found the solution :) We must redefine a method of type LifeCycleAction and so I've just redefined ComputeImpactedItems method in both Inciudent and UserRequest classes.

For make it much clear I show one of them here:

<class id="Incident">
        <methods>
                <method id="ComputeImpactedItems"  _delta="redefine">
                        <static>false</static>
                        <access>public</access>
                        <type>LifecycleAction</type>
                        <code><![CDATA[ public function ComputeImpactedItems()
                            {
                                // This method is kept for backward compatibility
                                // in case a delta redefines it, but you may call
                                // UpdateImpactedItems directly
                                $this->UpdateImpactedItems();

                                // This line is added by this exstension for saving caller's ip
                                $this->Set('ip', $_SERVER['REMOTE_ADDR']);
                            }]]></code>
                </method>
        </methods>
    </class>
Amir Keshavarz
  • 2,135
  • 2
  • 15
  • 19