<?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 Ticket Manage View
 *
 * @author Varun Shoor
 */
class View_Manage extends SWIFT_View
{
	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __construct()
	{
		parent::__construct();

		return true;
	}

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

		return true;
	}

	/**
	 * Render the Ticket Manage Grid
	 *
	 * @author Varun Shoor
	 * @param int $_departmentID The Department ID
	 * @param int $_ticketStatusID The Ticket Status ID
	 * @param int $_ticketTypeID The Ticket Type ID
	 * @param constant $_ownerFilter The Owner Filter
	 * @param int $_searchStoreID The Search Store ID
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function RenderGrid($_departmentID, $_ticketStatusID, $_ticketTypeID, $_ownerFilter, $_searchStoreID)
	{
		$_SWIFT = SWIFT::GetInstance();
		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$this->UserInterfaceGrid = $this->Controller->UserInterfaceGrid;

		SWIFT_TicketViewRenderer::Render($this->UserInterface, $this->UserInterfaceGrid, $_departmentID, $_ticketStatusID, $_ticketTypeID,
				$_ownerFilter, $_searchStoreID);

		$this->UserInterfaceGrid->SetRenderCallback(array('View_Manage', 'GridRender'));
		if ($_departmentID == '0' && $_SWIFT->Staff->GetPermission('staff_tcandeleteticket') != '0') {
			$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('delete'), 'icon_delete.gif',
					array('Controller_Manage', 'DeleteList'), $this->Language->Get('actionconfirm')));
			$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('putback'), 'icon_undo.png',
					array('Controller_Manage', 'PutBackList'), $this->Language->Get('actionconfirm')));
			$this->UserInterfaceGrid->AddAction(array($this->Language->Get('emptytrash'), 'icon_delete.gif', '/Tickets/Manage/EmptyTrash', $this->Language->Get('emptytrashconfirm')));
		}


		if ($_departmentID != '0') {

			if ($_SWIFT->Staff->GetPermission('staff_tcanupdateticket') != '0' && $_SWIFT->Staff->GetPermission('staff_tcanviewticket') != '0')
			{
				$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('spam'), 'icon_email2spam.png',
						array('Controller_Manage', 'SpamList'), $this->Language->Get('actionconfirm')));
				$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('watch'), 'icon_ticketwatch.png',
						array('Controller_Manage', 'WatchList'), $this->Language->Get('actionconfirm')));

				$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('reply'), 'icon_ticketreply.png',
						array('Controller_Manage', 'MassReplyList'), '',
						array($this->Language->Get('massreply'), '600', '480', array($this->Controller, '_MassReplyDialog'))));
				$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('merge'), 'icon_ticketmerge.png',
						array('Controller_Manage', 'MergeList'), $this->Language->Get('actionconfirm')));
			}

			if ($_SWIFT->Staff->GetPermission('staff_tcantrashticket') != '0') {
				$this->UserInterfaceGrid->AddMassAction(new SWIFT_UserInterfaceGridMassAction($this->Language->Get('trash'), 'icon_delete.gif',
						array('Controller_Manage', 'TrashList'), $this->Language->Get('actionconfirm')));
			}
		}

		$this->UserInterfaceGrid->Render();
		$this->Controller->_LoadDisplayData();
		$this->UserInterface->Header($this->Language->Get('tickets') . ' > ' . $this->Language->Get('manage'), Controller_Manage::MENU_ID,
				Controller_Manage::NAVIGATION_ID, $this->Controller->TagCloud->Render());
			SWIFT_TicketViewRenderer::DispatchMenu($_departmentID, $_ticketStatusID, $_ticketTypeID);

		$this->UserInterfaceGrid->Display();

		return true;
	}

	/**
	 * The Grid Rendering Function
	 *
	 * @author Varun Shoor
	 * @param array $_fieldContainer The Field Record Value Container
	 * @return array The Processed Field Container Array
	 */
	static public function GridRender($_fieldContainer)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_emailQueueCache = $_SWIFT->Cache->Get('queuecache');
		$_departmentCache = $_SWIFT->Cache->Get('departmentcache');
		$_staffCache = $_SWIFT->Cache->Get('staffcache');
		$_ticketStatusCache = $_SWIFT->Cache->Get('statuscache');
		$_ticketPriorityCache = $_SWIFT->Cache->Get('prioritycache');
		$_ticketTypeCache = $_SWIFT->Cache->Get('tickettypecache');
		$_templateGroupCache = $_SWIFT->Cache->Get('templategroupcache');
		$_slaPlanCache = $_SWIFT->Cache->Get('slaplancache');
		$_escalationRuleCache = $_SWIFT->Cache->Get('escalationrulecache');
		$_bayesianCategoryCache = $_SWIFT->Cache->Get('bayesiancategorycache');

		$_userOrganizationTicketListCustomFieldMap = SWIFT::Get('_userOrganizationTicketListCustomFieldMap');
		$_userTicketListCustomFieldMap = SWIFT::Get('_userTicketListCustomFieldMap');

		if ($_fieldContainer['userid'] != '0' && isset($_userTicketListCustomFieldMap[$_fieldContainer['userid']])) {
			$_fieldContainer = array_merge($_fieldContainer, $_userTicketListCustomFieldMap[$_fieldContainer['userid']]);
		}

		if (isset($_fieldContainer['userorganizationid'])
			&& $_fieldContainer['userorganizationid'] != '0'
			&& isset($_userOrganizationTicketListCustomFieldMap[$_fieldContainer['userorganizationid']]))
		{
			$_fieldContainer = array_merge($_fieldContainer, $_userOrganizationTicketListCustomFieldMap[$_fieldContainer['userorganizationid']]);
		}

		// Make the ticket URL
		$_ticketListType = 'inbox';
		$_ticketTreeDepartmentID = $_ticketTreeStatusID = $_ticketTreeTypeID = -1;

		if (SWIFT::Get('tickettreedepartmentid') !== false) {
			$_ticketTreeDepartmentID = intval(SWIFT::Get('tickettreedepartmentid'));
		}

		if (SWIFT::Get('tickettreestatusid')) {
			$_ticketTreeStatusID = intval(SWIFT::Get('tickettreestatusid'));
		}

		if (SWIFT::Get('tickettreetypeid')) {
			$_ticketTreeTypeID = intval(SWIFT::Get('tickettreetypeid'));
		}

		if (SWIFT::Get('tickettreelisttype')) {
			$_ticketListType = SWIFT::Get('tickettreelisttype');
		}

		$_ticketURL = SWIFT::Get('basename') . '/Tickets/Ticket/View/' . $_fieldContainer['ticketid'] . '/' . $_ticketListType . '/' .
				$_ticketTreeDepartmentID . '/' . $_ticketTreeStatusID . '/' . $_ticketTreeTypeID;

		// Properties Icon
		$_propertiesIcon = '';
		$_propertiesIconAlt = '';
		if ($_fieldContainer['hasnotes'] == '1' && $_fieldContainer['hasbilling'] == '1') {
			$_propertiesIcon = 'icon_ticketnotebilling.gif';
			$_propertiesIconAlt =  $_SWIFT->Language->Get('alt_tickethasbilling');
		} else if ($_fieldContainer['hasnotes'] == '1') {
			$_propertiesIcon = 'icon_ticketnote.gif';
			$_propertiesIconAlt = $_SWIFT->Language->Get('alt_tickethasnote');
		} else if ($_fieldContainer['hasbilling'] == '1') {
			$_propertiesIcon = 'icon_tickettimetrack.png';
			$_propertiesIconAlt = $_SWIFT->Language->Get('alt_tickethastimetracking');
		}

		if (!empty($_propertiesIcon)) {
			$_fieldContainer['propertyicon'] = '<img src="' . SWIFT::Get('themepathimages') . $_propertiesIcon .
				'" align="absmiddle" alt="' . $_propertiesIconAlt . '" title="' . $_propertiesIconAlt . '" border="0" />';
		}

		$_ticketIcon = 'icon_ticket.png';
		$_ticketIconAlt = '';
		// New Ticket
		if ($_fieldContainer['lastactivity'] > $_SWIFT->Staff->GetProperty('lastvisit')) {
			$_ticketIcon = 'icon_ticketred.png';
			$_ticketIconAlt = $_SWIFT->Language->Get('alt_ticketunread');
		}

		if ($_fieldContainer['isphonecall'] == '1') {
			$_ticketIcon = 'icon_ticketphone.png';
			$_ticketIconAlt = $_SWIFT->Language->Get('alt_ticketphonetype');
		}

		$_fieldContainer['icon'] = '<img src="' . SWIFT::Get('themepath') . 'images/' . $_ticketIcon . '"
			align="absmiddle" alt="' . $_ticketIconAlt . '" title="' . $_ticketIconAlt . '" border="0" />';

		$_toolTip = '';
		if ($_SWIFT->Settings->Get('t_tpreview') == 1)
		{
			$_toolTip = ' onmouseover="javascript: TicketTipBubble(this, ' . $_fieldContainer['ticketid'] . ');"';
		}

		// Subject Prefixes
		$_subjectPrefix = '';
		if ($_fieldContainer['hasattachments'] == '1') {
			$_subjectPrefix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_paperclip.gif' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_hasattachments') . '" title="' . $_SWIFT->Language->Get('alt_hasattachments') . '" border="0" />&nbsp;';
		}

		// Subject Suffix
		$_subjectSuffix = '';
		if ($_fieldContainer['isescalated'] == '1') {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_ticketescalated.png' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_isescalated') . '" title="' . $_SWIFT->Language->Get('alt_isescalated') . '" border="0" />&nbsp;';
		}

		if ($_fieldContainer['islinked'] == '1') {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_link.png' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_linkedticket') . '" title="' . $_SWIFT->Language->Get('alt_linkedticket') . '" border="0" />&nbsp;';
		}

		if ($_fieldContainer['hasfollowup'] == '1') {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_followup.png' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_followupset') . '" title="' . $_SWIFT->Language->Get('alt_followupset') . '" border="0" />&nbsp;';
		}

		if ($_fieldContainer['ownerstaffid'] == $_SWIFT->Staff->GetStaffID()) {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_marker.png' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_assignedotyou') . '" title="' . $_SWIFT->Language->Get('alt_assignedotyou') . '" border="0" />&nbsp;';
		}

		if (isset($_fieldContainer['ticketwatcherstaffid']) && !empty($_fieldContainer['ticketwatcherstaffid'])
				&& $_fieldContainer['ticketwatcherstaffid'] == $_SWIFT->Staff->GetStaffID()) {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_ticketwatch.png' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_watchingticket') . '" title="' . $_SWIFT->Language->Get('alt_watchingticket') . '" border="0" />&nbsp;';
		}

		$_lockThreshold = DATENOW -  $_SWIFT->Settings->Get('t_locktimeout');
		if (!empty($_fieldContainer['lockstaffid']) && $_fieldContainer['lockstaffid'] != $_SWIFT->Staff->GetStaffID() && $_fieldContainer['lockdateline'] > $_lockThreshold) {
			$_subjectSuffix .= '&nbsp;<img src="' . SWIFT::Get('themepathimages') . 'icon_lock.gif' .
				'" align="absmiddle" alt="' . $_SWIFT->Language->Get('alt_ticketlocked') . '" title="' . $_SWIFT->Language->Get('alt_ticketlocked') . '" border="0" />&nbsp;';
		}

		$_smallPagination = '';
		$_finalTotalReplies = intval($_fieldContainer['totalreplies'])+1;
		if ($_SWIFT->Settings->Get('t_enpagin') == '1' && $_finalTotalReplies > $_SWIFT->Settings->Get('t_postlimit')) {
			$_smallPagination = SWIFT_UserInterfaceGrid::RenderSmallPagination($_ticketURL . '/%d', $_finalTotalReplies, $_SWIFT->Settings->Get('t_postlimit'), -1, true, true);
		}


		$_fieldContainer['tickets.subject'] = '<span style="float: left;">' . $_subjectPrefix . '<a ' . $_toolTip . ' href="' . $_ticketURL . '" viewport="1">' . htmlspecialchars(StripName($_fieldContainer['subject'], 255)) . '</a>' . $_smallPagination . '</span><span style="float: right;">' . $_subjectSuffix . '</span>';
		$_fieldContainer['tickets.ticketid'] = '<a ' . $_toolTip . ' href="' . $_ticketURL . '" viewport="1">' . htmlspecialchars(IIF($_SWIFT->Settings->Get('t_eticketid') == 'seq', intval($_fieldContainer['ticketid']), $_fieldContainer['ticketmaskid'])) . '</a>';


		// Email Queue
		if (isset($_emailQueueCache['list'][$_fieldContainer['emailqueueid']])) {
			$_fieldContainer['tickets.emailqueueid'] = htmlspecialchars(StripName($_emailQueueCache['list'][$_fieldContainer['emailqueueid']]['email'], 15));
		} else {
			$_fieldContainer['tickets.emailqueueid'] = '';
		}

		// Department
		if (isset($_departmentCache[$_fieldContainer['departmentid']])) {
			$_fieldContainer['tickets.departmentid'] = htmlspecialchars(StripName($_departmentCache[$_fieldContainer['departmentid']]['title'], 15));
			$_fieldContainer['tickets.departmenttitle'] = $_fieldContainer['tickets.departmentid'];
		} else if ($_fieldContainer['departmentid'] == '0') {
			$_fieldContainer['tickets.departmentid'] = $_SWIFT->Language->Get('trash');
			$_fieldContainer['tickets.departmenttitle'] = $_fieldContainer['tickets.departmentid'];
		} else {
			$_fieldContainer['tickets.departmentid'] = $_SWIFT->Language->Get('na');
			$_fieldContainer['tickets.departmenttitle'] = htmlspecialchars($_fieldContainer['departmenttitle']);
		}

		// Ticket Status
		if (isset($_ticketStatusCache[$_fieldContainer['ticketstatusid']])) {
			$_ticketStatusContainer = $_ticketStatusCache[$_fieldContainer['ticketstatusid']];
			$_displayIconImage = '';
			if (!empty($_ticketStatusContainer['displayicon'])) {
				$_displayIconImage = '<img src="' . ProcessDisplayIcon($_ticketStatusContainer['displayicon']) .
					'" align="absmiddle" border="0" /> ';
			}

			$_fieldContainer['ticketstatus.displayorder'] = $_displayIconImage . htmlspecialchars($_ticketStatusCache[$_fieldContainer['ticketstatusid']]['title']);
			$_fieldContainer['ticketstatus.displayorder:'] = 'background-color: ' . $_ticketStatusContainer['statusbgcolor'] . '; color: ' . $_ticketStatusContainer['statuscolor'] . ';';
		} else {
			$_fieldContainer['ticketstatus.displayorder'] = htmlspecialchars($_fieldContainer['ticketstatustitle']);
		}

		// Ticket Priority
		if (isset($_ticketPriorityCache[$_fieldContainer['priorityid']])) {
			$_ticketPriorityContainer = $_ticketPriorityCache[$_fieldContainer['priorityid']];

			/*
			$_displayIconImage = '';
			if (!empty($_ticketPriorityContainer['displayicon'])) {
				$_displayIconImage = '<img src="' . ProcessDisplayIcon($_ticketPriorityContainer['displayicon']) .
					'" align="absmiddle" border="0" /> ';
			}
			*/

			$_fieldContainer['ticketpriorities.displayorder'] = htmlspecialchars($_ticketPriorityCache[$_fieldContainer['priorityid']]['title']);
			$_fieldContainer['ticketpriorities.displayorder:'] = 'background-color: ' . $_ticketPriorityContainer['bgcolorcode'] . '; color: ' .
				$_ticketPriorityContainer['frcolorcode'] . ';';
		} else {
			$_fieldContainer['ticketpriorities.displayorder'] = htmlspecialchars($_fieldContainer['prioritytitle']);
		}

		// Ticket Type
		if (isset($_ticketTypeCache[$_fieldContainer['tickettypeid']])) {
			$_ticketTypeContainer = $_ticketTypeCache[$_fieldContainer['tickettypeid']];
			$_displayIconImage = '';
			if (!empty($_ticketTypeContainer['displayicon'])) {
				$_displayIconImage = '<img src="' . ProcessDisplayIcon($_ticketTypeContainer['displayicon']) .
					'" align="absmiddle" border="0" /> ';
			}

			$_fieldContainer['tickets.tickettypetitle'] = $_displayIconImage . htmlspecialchars($_ticketTypeCache[$_fieldContainer['tickettypeid']]['title']);
			$_fieldContainer['tickets.tickettypetitleicon'] = $_displayIconImage;
		} else {
			$_fieldContainer['tickets.tickettypetitle'] = htmlspecialchars($_fieldContainer['tickettypetitle']);
		}

		// Template Group
		if (isset($_templateGroupCache[$_fieldContainer['tgroupid']])) {
			$_fieldContainer['tickets.tgroupid'] = htmlspecialchars($_templateGroupCache[$_fieldContainer['tgroupid']]['title']);
		} else {
			$_fieldContainer['tickets.tgroupid'] = $_SWIFT->Language->Get('na');
		}

		// Owner
		if (empty($_fieldContainer['ownerstaffid']))
		{
			$_fieldContainer['tickets.ownerstaffname'] = $_SWIFT->Language->Get('unassigned');
		} else {
			if (isset($_staffCache[$_fieldContainer['ownerstaffid']]))
			{
				$_fieldContainer['tickets.ownerstaffname'] = htmlspecialchars($_staffCache[$_fieldContainer['ownerstaffid']]['fullname']);
			} else {
				$_fieldContainer['tickets.ownerstaffname'] = $_SWIFT->Language->Get('na');
			}
		}

		// Ticket Flag
		$_SWIFT_TicketFlagObject = new SWIFT_TicketFlag();
		$_flagList = $_SWIFT_TicketFlagObject->GetFlagContainer();
		if (isset($_flagList[$_fieldContainer['flagtype']])) {
			$_fieldContainer['tickets.flagtype'] = '<a href="javascript: void(0);" onclick="javascript: ToggleFlag(\'' .
				$_fieldContainer['ticketid'] . '\', \'' . $_flagList[SWIFT_TicketFlag::FLAG_RED][1] . '\', \'' .
				$_flagList[SWIFT_TicketFlag::FLAG_RED][2] . '\');"><img id="ticketflagimg_' . $_fieldContainer['ticketid'] .
				'" src="' . SWIFT::Get('themepathimages') . $_flagList[$_fieldContainer['flagtype']][2] .
				'" align="absmiddle" border="0" /></a>';
			$_fieldContainer['tickets.flagtype:'] = 'background-color: ' . $_flagList[$_fieldContainer['flagtype']][1] . ';';
		} else {
			$_fieldContainer['tickets.flagtype'] = '<a href="javascript: void(0);" onclick="javascript: ToggleFlag(\'' .
				$_fieldContainer['ticketid'] . '\', \'' . $_flagList[SWIFT_TicketFlag::FLAG_RED][1] . '\', \'' .
				$_flagList[SWIFT_TicketFlag::FLAG_RED][2] . '\');"><img id="ticketflagimg_' . $_fieldContainer['ticketid'] .
				'" src="' . SWIFT::Get('themepathimages') . 'icon_flagblank.gif' .
				'" align="absmiddle" border="0" /></a>';
		}

		// SLA Plan
		if (isset($_slaPlanCache[$_fieldContainer['slaplanid']])) {
			$_fieldContainer['tickets.slaplanid'] = htmlspecialchars(StripName($_slaPlanCache[$_fieldContainer['slaplanid']]['title'], 18));
		}

		// Due Time
		if (!empty($_fieldContainer['duetime'])) {
			if ($_fieldContainer['duetime'] < DATENOW) {
				$_fieldContainer['tickets.duetime'] = $_SWIFT->Language->Get('overdue');
				$_fieldContainer['tickets.duetime:'] = 'color: red; font-weight: bold;';
				$_fieldContainer[':'] = 'background-color: ' . htmlspecialchars($_SWIFT->Settings->Get('t_overduecolor')) . ';';
			} else {
				$_dueTimeSeconds = $_fieldContainer['duetime'] - DATENOW;

				$_fieldContainer['tickets.duetime'] = SWIFT_Date::ColorTime($_dueTimeSeconds, true);
			}
		}

		if (!empty($_fieldContainer['resolutionduedateline'])) {
			if ($_fieldContainer['resolutionduedateline'] < DATENOW) {
				$_fieldContainer['tickets.resolutionduedateline'] = $_SWIFT->Language->Get('overdue');
				$_fieldContainer['tickets.resolutionduedateline:'] = 'color: red; font-weight: bold;';
				$_fieldContainer[':'] = 'background-color: ' . htmlspecialchars($_SWIFT->Settings->Get('t_overduecolor')) . ';';
			} else {
				$_resolutionDueTimeSeconds = $_fieldContainer['resolutionduedateline'] - DATENOW;

				$_fieldContainer['tickets.resolutionduedateline'] = SWIFT_Date::ColorTime($_resolutionDueTimeSeconds, true);
			}
		}

		// General Properties
		$_fieldContainer['tickets.fullname'] = htmlspecialchars(StripName($_fieldContainer['fullname'], 15));
		$_fieldContainer['tickets.lastreplier'] = htmlspecialchars(StripName($_fieldContainer['lastreplier'], 15));
		$_fieldContainer['tickets.email'] = htmlspecialchars(StripName($_fieldContainer['email'], 15));

		$_replyCount = 0;
		$_massReplyTicketIDList = SWIFT::Get('massreplyticketidlist');
		if (_is_array($_massReplyTicketIDList) && in_array($_fieldContainer['ticketid'], $_massReplyTicketIDList)) {
			$_replyCount++;
		}

		$_fieldContainer['tickets.totalreplies'] = intval($_fieldContainer['totalreplies']) + $_replyCount;
		$_fieldContainer['users.usergroupid'] = htmlspecialchars(StripName($_fieldContainer['usergrouptitle'], 18));
		$_fieldContainer['userorganizations.organizationname'] = htmlspecialchars(StripName($_fieldContainer['userorganizationname'], 18));
		$_fieldContainer['tickets.timeworked'] = SWIFT_Date::ColorTime($_fieldContainer['timeworked'], false, true);

		// Escalations
		if (isset($_escalationRuleCache[$_fieldContainer['escalationruleid']])) {
			$_fieldContainer['tickets.escalationruleid'] =
				htmlspecialchars(StripName($_escalationRuleCache[$_fieldContainer['escalationruleid']]['title'], 18));
		}

		if (!empty($_fieldContainer['escalatedtime'])) {
			$_escalationSeconds = DATENOW - $_fieldContainer['escalatedtime'];

			$_fieldContainer['tickets.escalatedtime'] = SWIFT_Date::ColorTime($_escalationSeconds);
		}

		// Bayesian
		if (isset($_bayesianCategoryCache[$_fieldContainer['bayescategoryid']])) {
			$_fieldContainer['tickets.bayescategoryid'] =
				htmlspecialchars(StripName($_bayesianCategoryCache[$_fieldContainer['bayescategoryid']]['category'], 15));
		}

		// Date Related Properties
		$_lastActivitySeconds = DATENOW - $_fieldContainer['lastactivity'];
		$_fieldContainer['tickets.lastactivity'] = SWIFT_Date::ColorTime($_lastActivitySeconds);

		$_fieldContainer['tickets.dateline'] = SWIFT_Date::Get(SWIFT_Date::TYPE_DATETIME, $_fieldContainer['dateline']);

		$_fieldContainer['tickets.laststaffreplytime'] = '';
		if (!empty($_fieldContainer['laststaffreplytime'])) {
			$_lastStaffReplySeconds = DATENOW - $_fieldContainer['laststaffreplytime'];
			$_fieldContainer['tickets.laststaffreplytime'] = SWIFT_Date::ColorTime($_lastStaffReplySeconds);
		}

		$_fieldContainer['tickets.lastuserreplytime'] = '';
		if (!empty($_fieldContainer['lastuserreplytime'])) {
			$_lastUserReplySeconds = DATENOW - $_fieldContainer['lastuserreplytime'];
			$_fieldContainer['tickets.lastuserreplytime'] = SWIFT_Date::ColorTime($_lastUserReplySeconds);
		}

		return $_fieldContainer;
	}

	/**
	 * Render the MassReply Form
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function RenderMassReply()
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$this->UserInterface->Start('MassReplyDialog', $_POST['_gridURL'], SWIFT_UserInterface::MODE_INSERT, false, false);

		$this->UserInterface->Toolbar->AddButton($this->Language->Get('dispatchsend'), 'icon_check.gif');
		$this->UserInterface->Toolbar->AddButton($this->Language->Get('help'), 'icon_help.gif', SWIFT_Help::RetrieveHelpLink('userorganization'), SWIFT_UserInterfaceToolbar::LINK_NEWWINDOW);

		/*
		 * ###############################################
		 * BEGIN REPLY TAB
		 * ###############################################
		 */

		$_ReplyTabObject = $this->UserInterface->AddTab($this->Language->Get('tabreply'), 'icon_ticketreply.png', 'reply', true);
		$_ReplyTabObject->SetColumnWidth('150');
		$_ReplyTabObject->TextArea('replycontents', '', '', '', '30', '17', false, '');

		if (isset($_POST['itemid']) && _is_array($_POST['itemid']))
		{
			foreach ($_POST['itemid'] as $_ticketID) {
				$_ReplyTabObject->Hidden('ticketid[]', $_ticketID);
			}
		}

		/*
		 * ###############################################
		 * END GENERAL TAB
		 * ###############################################
		 */


		$this->UserInterface->End();

		return true;
	}
}
?>