<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2">
  <module_parameters>
    <parameters id="itop-assign-to-me" _delta="define">
      <agent_must_change type="bool">true</agent_must_change>
      <allowed_classes type="hash">
        <UserRequest type="hash">
          <dispatched>ev_assign</dispatched>
          <assigned>ev_reassign</assigned>
          <redispatched>ev_assign</redispatched>
        </UserRequest>
        <Incident type="hash">
          <dispatched>ev_assign</dispatched>
          <assigned>ev_reassign</assigned>
          <redispatched>ev_assign</redispatched>
        </Incident>
      </allowed_classes>
    </parameters>
  </module_parameters>
  <snippets>
    <snippet id="AssignToMeMenuExtension" _delta="define">
      <placement>module</placement>
      <rank>10</rank>
      <module>itop-assign-to-me</module>
      <content><![CDATA[
class AssignToMeMenuExtension implements iPopupMenuExtension {
	/**
	 * Checks the ticket is in a state where assign to me action can be applied
	 */
	public static function IsActionAllowed($oTicket, $iAgentid, $sStimulus) {
		$sTicketClass = get_class($oTicket);

		// Check transition is valid
		$aTransitions = $oTicket->EnumTransitions();
		$aStimuli = MetaModel::EnumStimuli($sTicketClass);
		if (!isset($aTransitions[$sStimulus])) {
			return false;
		}

		// Check user has rights to perform the transition
		if (!UserRights::IsStimulusAllowed($sTicketClass, $sStimulus)) {
			return false;
		}

		// If agent must change, check ticket is not already assigned to me
		$bAgentMustChange = MetaModel::GetModuleSetting('itop-assign-to-me', 'agent_must_change', true);
		$iTicketAgentId = $oTicket->Get('agent_id');
		if ($bAgentMustChange && ($iAgentid == $iTicketAgentId)) {
			return false;
		}

		// Check agent is in current team
		$iTicketTeamId = $oTicket->Get('team_id');
		$bIsInTeam = false;
		if (!is_null($iTicketTeamId))
		{
			$sOQL = "SELECT lnkPersonToTeam AS l WHERE l.person_id = :p_id AND l.team_id = :t_id";
			$oLnkSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array(), array('p_id' => $iAgentid, 't_id' => $iTicketTeamId));
			if ($oLnkSet->Count() > 0) {
				$bIsInTeam = true;
			}
		}
		if (!$bIsInTeam) {
			return false;
		}

		return true;
	}

	public static function EnumItems($iMenuId, $param) {
		switch($iMenuId) {
			case iPopupMenuExtension::MENU_OBJLIST_ACTIONS:
				// $param is a DBObjectSet
				$aResult = array();
				break;

			case iPopupMenuExtension::MENU_OBJLIST_TOOLKIT:
				// $param is a DBObjectSet
				$aResult = array();
				break;

			case iPopupMenuExtension::MENU_OBJDETAILS_ACTIONS:
				// $param is a DBObject
				$aResult = array();
				$oObj = $param;

				// "Assign to me" applies to Tickets only
				if ($oObj instanceof Ticket) {
					$sTicketClass = get_class($oObj);
					$aAllowedClasses = MetaModel::GetModuleSetting('itop-assign-to-me', 'allowed_classes', array());

					foreach ($aAllowedClasses as $sAllowedClass => $aClass) {
						// Check $sAllowedClass is a valid class
						if (($sAllowedClass != '') && !MetaModel::IsValidClass($sAllowedClass)) {
							IssueLog::Debug('Module itop-assign-to-me - invalid class #'.$sAllowedClass.' - name "'.$sAllowedClass.'" is not a valid class');
							break;
						}

						if ($sAllowedClass == $sTicketClass) {
							// Check ticket has status where event can be applied
							$sTicketStatus = $oObj->Get('status');
							$bIsInRightStatus = false;
							foreach ($aClass as $sStatus => $sEvent) {
								if ($sTicketStatus == $sStatus) {
									$bIsInRightStatus = true;
									$sEventToApply = $sEvent;
								}
							}

							if ($bIsInRightStatus) {
								$iAgentid = UserRights::GetContactId();

								// Check that transition is allowed and that agent can apply it
								if (self::IsActionAllowed($oObj, $iAgentid, $sEventToApply)) {
									// Set Assign to me additional menu
									$oAppContext = new ApplicationContext();
									$aParams = $oAppContext->GetAsHash();

									$aParams['class'] = $sTicketClass;
									$aParams['id'] = $oObj->GetKey();
									$aParams['operation'] = 'stimulus';
									$aParams['stimulus'] = $sEventToApply;
									$aParams['agent_id'] = $iAgentid;
									$sMenu = 'Menu:AssignToMe';
									$aResult = array(
											new SeparatorPopupMenuItem(),
											new URLPopupMenuItem(
													$sMenu.' from '.$sTicketClass,
													Dict::S($sMenu),
													utils::GetAbsoluteUrlModulePage('itop-assign-to-me', 'assign-to-me.php', $aParams)
											),
									);
								}
							}
							break;
						}
					}
				}
				break;

			case iPopupMenuExtension::MENU_DASHBOARD_ACTIONS:
				// $param is a Dashboard
				$aResult = array();
				break;

			default:
				// Unknown type of menu, do nothing
				$aResult = array();
				break;
		}
		return $aResult;
	}
}
]]></content>
    </snippet>
  </snippets>
</itop_design>
