<?php
/**
 * ###############################################
 *
 * SWIFT Framework
 * _______________________________________________
 *
 * @author		Varun Shoor
 *
 * @package		SWIFT
 * @copyright	Copyright (c) 2001-2012, Kayako
 * @license		http://www.kayako.com/license
 * @link		http://www.kayako.com
 *
 * ###############################################
 */

/**
 * The TicketCount API Controller
 *
 * @author Varun Shoor
 */
class Controller_TicketCount extends Controller_api implements SWIFT_REST_Interface
{
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __construct()
	{
		parent::__construct();

		$this->Load->Library('XML:XML');

		return true;
	}

	/**
	 * Destructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __destruct()
	{
		parent::__destruct();

		return true;
	}

	/**
	 * Dispatch the ticket counts
	 *
	 * Example Output:
	 *
	 * <ticketstatuses>
	 *	<ticketstatus>
	 *		<id>3</id>
	 *		<title>Closed</title>
	 *		<displayorder>3</displayorder>
	 *		<departmentid>0</departmentid>
	 *		<displayicon>{$themepath}icon_ticketstatusclosed.png</displayicon>
	 *		<type>private</type>
	 *		<displayinmainlist>0</displayinmainlist>
	 *		<markasresolved>1</markasresolved>
	 *		<displaycount>0</displaycount>
	 *		<statuscolor>#5f5f5f</statuscolor>
	 *		<statusbgcolor>#5f5f5f</statusbgcolor>
	 *		<resetduetime>0</resetduetime>
	 *		<triggersurvey>1</triggersurvey>
	 *		<staffvisibilitycustom>1</staffvisibilitycustom>
	 *		<staffgroupid>1</staffgroupid>
	 *	</ticketstatus>
	 * </ticketstatuses>
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetList()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$_ticketCountCache = $this->Cache->Get('ticketcountcache');

		$this->XML->AddParentTag('ticketcount');
			/**
			 * Departments
			 */
			$this->XML->AddParentTag('departments');
			if (isset($_ticketCountCache['departments']) && _is_array($_ticketCountCache['departments'])) {
				foreach ($_ticketCountCache['departments'] as $_departmentID => $_countContainer) {
					$this->XML->AddParentTag('department', array('id' => $_departmentID));
						$this->XML->AddTag('totalitems', $_countContainer['totalitems']);
						$this->XML->AddTag('lastactivity', $_countContainer['lastactivity']);
						$this->XML->AddTag('totalunresolveditems', $_countContainer['totalunresolveditems']);

						foreach ($_countContainer['ticketstatus'] as $_ticketStatusID => $_ticketStatusCountContainer) {
							$this->XML->AddTag('ticketstatus', '', array('id' => $_ticketStatusID, 'lastactivity' => $_ticketStatusCountContainer['lastactivity'], 'totalitems' => $_ticketStatusCountContainer['totalitems']));
						}

						foreach ($_countContainer['tickettypes'] as $_ticketTypeID => $_ticketTypeCountContainer) {
							$this->XML->AddTag('tickettype', '', array('id' => $_ticketTypeID, 'lastactivity' => $_ticketTypeCountContainer['lastactivity'], 'totalitems' => $_ticketTypeCountContainer['totalitems'], 'totalunresolveditems' => $_ticketTypeCountContainer['totalunresolveditems']));
						}

						foreach ($_countContainer['ownerstaff'] as $_staffID => $_ticketOwnerCountContainer) {
							$this->XML->AddTag('ownerstaff', '', array('id' => $_staffID, 'lastactivity' => $_ticketOwnerCountContainer['lastactivity'], 'totalitems' => $_ticketOwnerCountContainer['totalitems'], 'totalunresolveditems' => $_ticketOwnerCountContainer['totalunresolveditems']));
						}
					$this->XML->EndParentTag('department');
				}
			}
			$this->XML->EndParentTag('departments');

			/**
			   Ticket Status
			 */
			$this->XML->AddComment('Ticket Count grouped by Status');
			$this->XML->AddParentTag('statuses');
				if (isset($_ticketCountCache['ticketstatus']) && _is_array($_ticketCountCache['ticketstatus'])) {
					foreach ($_ticketCountCache['ticketstatus'] as $_ticketStatusID => $_ticketStatusCountContainer) {
						$this->XML->AddTag('ticketstatus', '', array('id' => $_ticketStatusID, 'lastactivity' => $_ticketStatusCountContainer['lastactivity'], 'totalitems' => $_ticketStatusCountContainer['totalitems']));
					}
				}
			$this->XML->EndParentTag('statuses');


			/**
			   Owner Staff
			 */
			$this->XML->AddComment('Ticket Count grouped by Owner Staff');
			$this->XML->AddParentTag('owners');
				foreach ($_ticketCountCache['ownerstaff'] as $_staffID => $_ticketOwnerCountContainer) {
					$this->XML->AddTag('ownerstaff', '', array('id' => $_staffID, 'lastactivity' => $_ticketOwnerCountContainer['lastactivity'], 'totalitems' => $_ticketOwnerCountContainer['totalitems'], 'totalunresolveditems' => $_ticketOwnerCountContainer['totalunresolveditems']));
				}
			$this->XML->EndParentTag('owners');


			/**
			   Unassigned Tickets
			 */
			$this->XML->AddComment('Unassigned Ticket Count grouped by Department');
			$this->XML->AddParentTag('unassigned');
				foreach ($_ticketCountCache['unassigned'] as $_departmentID => $_ticketOwnerCountContainer) {
					$this->XML->AddTag('department', '', array('id' => $_departmentID, 'lastactivity' => $_ticketOwnerCountContainer['lastactivity'], 'totalitems' => $_ticketOwnerCountContainer['totalitems'], 'totalunresolveditems' => $_ticketOwnerCountContainer['totalunresolveditems']));
				}
			$this->XML->EndParentTag('unassigned');

		$this->XML->EndParentTag('ticketcount');

		$this->XML->EchoXML();

		return true;
	}
}
?>