<?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 Macro Manager. Deals with search, menu generation etc.
 *
 * @author Varun Shoor
 */
class SWIFT_MacroManager extends SWIFT_Library
{
	static protected $_macroCategoryCache = false;
	static protected $_macroRepliesCache = false;

	/**
	 * 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;
	}


	/**
	 * Retrieve the Macro Category Options
	 *
	 * @author Varun Shoor
	 * @param int $_selectedMacroCategoryID (OPTIONAL) The Macro Category ID to be selected by default
	 * @return mixed "$_optionContainer" (ARRAY) on Success, "false" otherwise
	 */
	static public function GetMacroCategoryOptions($_selectedMacroCategoryID = false, $_activeMacroCategoryID = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!self::$_macroCategoryCache)
		{
			self::$_macroCategoryCache = SWIFT_MacroCategory::RetrieveMacroCategories();
		}

		$_macroCategoryCache = self::$_macroCategoryCache;

		$_optionContainer = array();

		$_optionContainer[0]['title'] = $_SWIFT->Language->Get('parentcategoryitem');
		$_optionContainer[0]['value'] = '0';
		$_optionContainer[0]['selected'] = IIF($_selectedMacroCategoryID == 0, true, false);

		self::GetSubMacroCategoryOptions($_selectedMacroCategoryID, 0, $_optionContainer, 0, $_activeMacroCategoryID);

		return $_optionContainer;
	}

	/**
	 * Retrieve the macro category options in a loop
	 *
	 * @author Varun Shoor
	 * @param int $_selectedMacroCategoryID The Selected Macro Category ID
	 * @param int $_parentMacroCategoryID The Parent Macro Category ID
	 * @param array $_optionContainer The Option Container
	 * @param int $_index The Indent Counter
	 * @return mixed "_optionContainer" (ARRAY) on Success, "false" otherwise
	 */
	static protected function GetSubMacroCategoryOptions($_selectedMacroCategoryID, $_parentMacroCategoryID, &$_optionContainer, $_indent = 0, $_activeMacroCategoryID = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_macroCategoryCache = self::$_macroCategoryCache;

		extract($_macroCategoryCache);

		$_childCount = 0;

		if (isset($_macroCategoryParentMap[$_parentMacroCategoryID]))
		{
			$_childCount = count($_macroCategoryParentMap[$_parentMacroCategoryID]);
		}

		if ($_childCount > 0)
		{
			$_childIndent = str_repeat('   ', $_indent);
			$_childPrefix = $_childIndent . '|- ';

			foreach ($_macroCategoryParentMap[$_parentMacroCategoryID] as $_key => $_val)
			{
				if ($_activeMacroCategoryID == $_val['macrocategoryid'])
				{
					continue;
				}
				
				if ($_val['categorytype'] == SWIFT_MacroCategory::TYPE_PUBLIC || ($_val['categorytype'] == SWIFT_MacroCategory::TYPE_PRIVATE && $_val['staffid'] == $_SWIFT->Staff->GetStaffID()))
				{
					$_optionArray = array();
					$_optionArray['title'] = $_childPrefix . $_val['title'];
					$_optionArray['value'] = $_val['macrocategoryid'];
					$_optionArray['selected'] = IIF($_selectedMacroCategoryID == $_val['macrocategoryid'], true, false);

					$_optionContainer[] = $_optionArray;

					if (isset($_macroCategoryParentMap[$_val['macrocategoryid']]) && count($_macroCategoryParentMap[$_val['macrocategoryid']]) > 0)
					{
						self::GetSubMacroCategoryOptions($_selectedMacroCategoryID, $_val['macrocategoryid'], $_optionContainer, ($_indent + 1), $_activeMacroCategoryID);
					}
				}
			}
		}

		return $_optionContainer;
	}

	/**
	 * Render the options for the given category id
	 *
	 * @author Varun Shoor
	 * @param int $_macroCategoryID The Macro Category ID
	 * @return string The Rendered Item HTML
	 */
	static protected function GetTreeItemOptions($_macroCategoryID)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_returnHTML = '&nbsp;&nbsp;-&nbsp;&nbsp;' . '<div title="' . $_SWIFT->Language->Get('insertcategory') . '" class="macrocategorynew" onclick="javascript: InsertMacroCategoryWindow(\'' . intval($_macroCategoryID) . '\');">&nbsp;</div> <div title="' . $_SWIFT->Language->Get('insertmacro') . '" class="macroreplynew" onclick="javascript: InsertMacroReplyWindow(\'' . intval($_macroCategoryID) . '\');">&nbsp;</div> <div title="' . $_SWIFT->Language->Get('filterreplies') . '" class="macroreplysearch" onclick="javascript: loadViewportData(\'' . SWIFT::Get('basename') . '/Tickets/MacroCategory/QuickReplyFilter/category/' . intval($_macroCategoryID) . '/\');">&nbsp;</div> ';

		return $_returnHTML;
	}

	/**
	 * Retrieve the Macro Category Tree HTML
	 *
	 * @author Varun Shoor
	 * @param int $_selectedMacroCategoryID (OPTIONAL) The Macro Category ID to be selected by default
	 * @return mixed "_renderHTML" (STRING) on Success, "false" otherwise
	 */
	static public function GetMacroCategoryTree($_selectedMacroCategoryID = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!self::$_macroCategoryCache)
		{
			self::$_macroCategoryCache = SWIFT_MacroCategory::RetrieveMacroCategories();
		}

		if (!self::$_macroRepliesCache)
		{
			self::$_macroRepliesCache = SWIFT_MacroReply::RetrieveMacroReplies(false);
		}

		$_macroCategoryCache = self::$_macroCategoryCache;
		$_macroRepliesCache = self::$_macroRepliesCache;

		extract($_macroRepliesCache);

		$_extendedMacroCategoryTitle = '';
		if (isset($_replyParentMap[0]) && count($_replyParentMap[0]) > 0)
		{
			$_extendedMacroCategoryTitle .= ' <font color="red">(' . count($_replyParentMap[0]) . ')</font>';
		}

		$_renderHTML = '<ul class="swifttree">';

		$_renderHTML .= '<li><span class="folder">' . $_SWIFT->Language->Get('rootcategory') . $_extendedMacroCategoryTitle . self::GetTreeItemOptions(0) . '</span>';

		self::GetSubMacroCategoryTree($_selectedMacroCategoryID, 0, $_renderHTML);

		$_renderHTML .= '</li></ul>';

		return $_renderHTML;
	}

	/**
	 * Retrieve the macro category tree HTML in a loop
	 *
	 * @author Varun Shoor
	 * @param int $_selectedMacroCategoryID The Selected Macro Category ID
	 * @param int $_parentMacroCategoryID The Parent Macro Category ID
	 * @param string $_renderHTML The Rendered HTML
	 * @return mixed "_renderHTML" (STRING) on Success, "false" otherwise
	 */
	static protected function GetSubMacroCategoryTree($_selectedMacroCategoryID, $_parentMacroCategoryID, &$_renderHTML)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_macroCategoryCache = self::$_macroCategoryCache;
		$_macroRepliesCache = self::$_macroRepliesCache;

		extract($_macroCategoryCache);
		extract($_macroRepliesCache);

		$_childCount = 0;

		if (isset($_macroCategoryParentMap[$_parentMacroCategoryID]))
		{
			$_childCount = count($_macroCategoryParentMap[$_parentMacroCategoryID]);
		}

		if ($_childCount > 0)
		{
			$_renderHTML .= '<ul>';

			foreach ($_macroCategoryParentMap[$_parentMacroCategoryID] as $_key => $_val)
			{
				if ($_val['categorytype'] == SWIFT_MacroCategory::TYPE_PUBLIC || ($_val['categorytype'] == SWIFT_MacroCategory::TYPE_PRIVATE && $_val['staffid'] == $_SWIFT->Staff->GetStaffID()))
				{
					if ($_selectedMacroCategoryID == $_val['macrocategoryid'])
					{
						$_macroCategoryTitle = '<b>' . htmlspecialchars($_val['title']) . '</b>';
					} else {
						$_macroCategoryTitle = htmlspecialchars($_val['title']);
					}

					if (isset($_replyParentMap[$_val['macrocategoryid']]) && count($_replyParentMap[$_val['macrocategoryid']]) > 0)
					{
						$_macroCategoryTitle .= ' <font color="red">(' . count($_replyParentMap[$_val['macrocategoryid']]) . ')</font>';
					}

					$_renderHTML .= '<li><span class="' . IIF($_val['categorytype'] == SWIFT_MacroCategory::TYPE_PUBLIC, 'folder', 'folderfaded') . '"><a href="javascript: void(0);" onclick="javascript: EditMacroCategoryWindow(\'' . intval($_val['macrocategoryid']) . '\');">' . $_macroCategoryTitle . '</a>' . self::GetTreeItemOptions($_val['macrocategoryid']) . '</span>';

					if (isset($_macroCategoryParentMap[$_val['macrocategoryid']]) && count($_macroCategoryParentMap[$_val['macrocategoryid']]) > 0)
					{
						self::GetSubMacroCategoryTree($_selectedMacroCategoryID, $_val['macrocategoryid'], $_renderHTML);
					}

					$_renderHTML .= '</li>';
				}
			}

			$_renderHTML .= '</ul>';
		}

		return $_renderHTML;
	}
}
?>
