<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.2">
  <classes>
    <class id="Attachment" _delta="define">
      <parent>DBObject</parent>
      <properties>
        <comment><![CDATA[/**
 * Module attachments
 * 
 * A quick and easy way to upload and attach files to *any* (see Configuration below) object in the CMBD in one click
 *
 * Configuration: the list of classes for which the "Attachments" tab is visible is defined via the module's 'allowed_classes'
 * configuration parameter. By default the tab is active for all kind of Tickets.
 *
 * @copyright   Copyright (C) 2010-2024 Combodo SAS
 * @license     http://opensource.org/licenses/AGPL-3.0
 */]]></comment>
        <category>addon,bizmodel</category>
        <abstract>false</abstract>
        <key_type>autoincrement</key_type>
        <db_table>attachment</db_table>
        <db_key_field>id</db_key_field>
        <db_final_class_field/>
        <naming>
          <attributes>
            <attribute id="item_class"/>
            <attribute id="temp_id"/>
          </attributes>
        </naming>
        <style>
          <icon/>
        </style>
        <reconciliation>
          <attributes>
            <attribute id=""/>
          </attributes>
        </reconciliation>
        <indexes>
          <index id="1">
            <attributes>
              <attribute id="temp_id"/>
            </attributes>
          </index>
          <index id="2">
            <attributes>
              <attribute id="item_class"/>
              <attribute id="item_id"/>
            </attributes>
          </index>
          <index id="3">
            <attributes>
              <attribute id="item_org_id"/>
            </attributes>
          </index>
        </indexes>
      </properties>
      <fields>
        <field id="expire" xsi:type="AttributeDateTime">
          <sql>expire</sql>
          <default_value/>
          <is_null_allowed>false</is_null_allowed>
        </field>
        <field id="temp_id" xsi:type="AttributeString">
          <sql>temp_id</sql>
          <default_value/>
          <is_null_allowed>true</is_null_allowed>
        </field>
        <field id="item_class" xsi:type="AttributeString">
          <sql>item_class</sql>
          <default_value/>
          <is_null_allowed>false</is_null_allowed>
        </field>
        <field id="item_id" xsi:type="AttributeObjectKey">
          <sql>item_id</sql>
          <is_null_allowed>true</is_null_allowed>
          <class_attcode>item_class</class_attcode>
        </field>
        <field id="item_org_id" xsi:type="AttributeInteger">
          <sql>item_org_id</sql>
          <default_value>0</default_value>
          <is_null_allowed>true</is_null_allowed>
        </field>
        <field id="contents" xsi:type="AttributeBlob"/>
        <field id="creation_date" xsi:type="AttributeDateTime">
          <sql>creation_date</sql>
          <default_value/>
          <is_null_allowed>true</is_null_allowed>
        </field>
        <field id="user_id" xsi:type="AttributeExternalKey">
          <sql>user_id</sql>
          <is_null_allowed>true</is_null_allowed>
          <on_target_delete>DEL_MANUAL</on_target_delete>
          <target_class>User</target_class>
          <allow_target_creation>false</allow_target_creation>
        </field>
        <field id="contact_id" xsi:type="AttributeExternalKey">
          <sql>contact_id</sql>
          <is_null_allowed>true</is_null_allowed>
          <on_target_delete>DEL_MANUAL</on_target_delete>
          <target_class>Person</target_class>
          <allow_target_creation>false</allow_target_creation>
        </field>
      </fields>
      <methods>
        <method id="DBInsertNoReload">
          <static>false</static>
          <access>public</access>
          <type>Overload-DBObject</type>
          <code><![CDATA[
    public function DBInsertNoReload()
    {
        $this->SetCurrentDateIfNull('creation_date');
        $iUserId = CMDBChange::GetCurrentUserId();
        if(utils::IsNotNullOrEmptyString($iUserId)){
               $this->SetIfNull('user_id', $iUserId);

              // Get Contact from user
              $oUser = MetaModel::GetObject('User', $iUserId, true, true);
              $this->SetIfNull('contact_id', $oUser->Get('contactid'));
         }
        return parent::DBInsertNoReload();
    }
        ]]></code>
        </method>
        <method id="MapContextParam">
          <comment><![CDATA[/**
	 * Maps the given context parameter name to the appropriate filter/search code for this class
	 * @param string $sContextParam Name of the context parameter, e.g. 'org_id'
	 * @return string Filter code, e.g. 'customer_id'
	 */]]></comment>
          <static>true</static>
          <access>public</access>
          <type>Overload-ExNihilo</type>
          <code><![CDATA[	public static function MapContextParam($sContextParam)
	{
		if ($sContextParam == 'org_id')
		{
			return 'item_org_id';
		}
		else
		{
			return null;
		}
	}]]></code>
        </method>
        <method id="SetItem">
          <comment><![CDATA[/**
	 * Set/Update all of the '_item' fields
	 * @param object $oItem Container item
	 * @return void
	 */]]></comment>
          <static>false</static>
          <access>public</access>
          <type>Overload-ExNihilo</type>
          <code><![CDATA[	public function SetItem($oItem, $bUpdateOnChange = false)
	{
		$sClass = get_class($oItem);
		$iItemId = $oItem->GetKey();

 		$this->Set('item_class', $sClass);
 		$this->Set('item_id', $iItemId);

		$aCallSpec = array($sClass, 'MapContextParam');
		if (is_callable($aCallSpec))
		{
			$sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter					
			if (MetaModel::IsValidAttCode($sClass, $sAttCode))
			{
				$iOrgId = $oItem->Get($sAttCode);
				if ($iOrgId > 0)
				{
					if ($iOrgId != $this->Get('item_org_id'))
					{
						$this->Set('item_org_id', $iOrgId);
						if ($bUpdateOnChange)
						{
							$this->DBUpdate();
						}
					}
				}
			}
		}
	}]]></code>
        </method>
        <method id="SetDefaultOrgId">
          <comment>/**
	 * Give a default value for item_org_id (if relevant...)
	 * @return void
	 */</comment>
          <static>false</static>
          <access>public</access>
          <type>Overload-ExNihilo</type>
          <code><![CDATA[	public function SetDefaultOrgId()
	{
    // Check that the organization CAN be fetched from the current user
    //
    if (MetaModel::IsValidClass('Person'))
    {
      $aCallSpec = array('Person', 'MapContextParam');
      if (is_callable($aCallSpec))
      {
        $sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
        if (MetaModel::IsValidAttCode('Person', $sAttCode))
        {
          // OK - try it
          //
          $oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
          if ($oCurrentPerson)
          {
            $this->Set('item_org_id', $oCurrentPerson->Get($sAttCode));
          }
        }
      }
		}
	}]]></code>
        </method>
      </methods>
      <presentation>
        <details>
          <items>
            <item id="temp_id">
              <rank>10</rank>
            </item>
            <item id="item_class">
              <rank>20</rank>
            </item>
            <item id="item_id">
              <rank>30</rank>
            </item>
            <item id="item_org_id">
              <rank>40</rank>
            </item>
          </items>
        </details>
        <search>
          <items>
            <item id="temp_id">
              <rank>10</rank>
            </item>
            <item id="item_class">
              <rank>20</rank>
            </item>
            <item id="item_id">
              <rank>30</rank>
            </item>
          </items>
        </search>
        <list>
          <items>
            <item id="temp_id">
              <rank>10</rank>
            </item>
            <item id="item_class">
              <rank>20</rank>
            </item>
            <item id="item_id">
              <rank>30</rank>
            </item>
            <item id="creation_date">
              <rank>40</rank>
            </item>
          </items>
        </list>
      </presentation>
    </class>
  </classes>
  <events>
    <event id="EVENT_ADD_ATTACHMENT_TO_OBJECT" _delta="define">
      <name>Attachment added</name>
      <description><![CDATA[An attachment has been added to an object]]></description>
      <replaces>Attachment::AfterUpdate</replaces>
      <sources>
        <source id="cmdbAbstractObject">cmdbAbstractObject</source>
      </sources>
      <event_data>
        <event_datum id="object">
          <description>The object where the attachment is added</description>
          <type>DBObject</type>
        </event_datum>
        <event_datum id="attachment">
          <description>The attachment added to the objet</description>
          <type>DBObject</type>
        </event_datum>
        <event_datum id="debug_info">
          <description>Debug string</description>
          <type>string</type>
        </event_datum>
      </event_data>
    </event>
    <event id="EVENT_REMOVE_ATTACHMENT_FROM_OBJECT" _delta="define">
      <name>Attachment removed</name>
      <description><![CDATA[An attachment has been removed from an object]]></description>
      <replaces>Attachment::AfterUpdate</replaces>
      <sources>
        <source id="cmdbAbstractObject">cmdbAbstractObject</source>
      </sources>
      <event_data>
        <event_datum id="object">
          <description>The object where the attachment is removed</description>
          <type>DBObject</type>
        </event_datum>
        <event_datum id="attachment">
          <description>The attachment removed</description>
          <type>DBObject</type>
        </event_datum>
        <event_datum id="debug_info">
          <description>Debug string</description>
          <type>string</type>
        </event_datum>
      </event_data>
    </event>
  </events>
</itop_design>
