<?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 Troubleshooter Step Manager Class
 *
 * @author Varun Shoor
 */
class SWIFT_TroubleshooterStepManager extends SWIFT_Library
{
	static protected $_troubleshooterStepCache = false;
	static protected $_troubleshooterStepHits = array();

	/**
	 * 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 Troubleshooter Step Options
	 *
	 * @author Varun Shoor
	 * @param int $_troubleshooterCategoryID The Troubleshooter Category ID
	 * @param array $_selectedTroubleshooterStepIDList (OPTIONAL) The Troubleshooter Step IDs to be selected by default
	 * @param int $_activeTroubleshooterStepID (OPTIONAL) The Active Troubleshooter Step ID (during Edit)
	 * @param bool $_isCheckbox (OPTIONAL) Whether this is a checkbox container
	 * @return mixed "$_optionContainer" (ARRAY) on Success, "false" otherwise
	 */
	static public function GetCategoryOptions($_troubleshooterCategoryID, $_selectedTroubleshooterStepIDList = false, $_activeTroubleshooterStepID = false, $_isCheckbox = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!self::$_troubleshooterStepCache)
		{
			self::$_troubleshooterStepCache = SWIFT_TroubleshooterStep::RetrieveSteps($_troubleshooterCategoryID);
		}

		$_troubleshooterStepCache = self::$_troubleshooterStepCache;

		$_optionContainer = array();

		$_optionContainer[0]['title'] = $_SWIFT->Language->Get('parentcategoryitem');
		$_optionContainer[0]['value'] = '0';

		$_isSelected = false;
		if (_is_array($_selectedTroubleshooterStepIDList) && in_array(0, $_selectedTroubleshooterStepIDList))
		{
			$_isSelected = true;
		}

		if ($_isCheckbox)
		{
			$_optionContainer[0]['checked'] = $_isSelected;
			$_optionContainer[0]['icon'] = SWIFT::Get('themepathimages'). 'icon_folderyellow3.gif';
		} else {
			$_optionContainer[0]['selected'] = $_isSelected;
		}

		self::$_troubleshooterStepHits = array();

		self::GetSubCategoryOptions($_troubleshooterCategoryID, $_selectedTroubleshooterStepIDList, 0, $_optionContainer, 0, $_activeTroubleshooterStepID, $_isCheckbox);

		return $_optionContainer;
	}

	/**
	 * Retrieve the troubleshooter step options in a loop
	 *
	 * @author Varun Shoor
	 * @param int $_troubleshooterCategoryID The Troubleshooter Category ID
	 * @param array $_selectedTroubleshooterStepIDList The Troubleshooter Step IDs to be selected by default
	 * @param int $_parentTroubleshooterStepID The Parent Troubleshooter Step ID
	 * @param int $_activeTroubleshooterStepID (OPTIONAL) The Active Troubleshooter Step ID (during Edit)
	 * @param array $_optionContainer The Option Container
	 * @param int $_index The Indent Counter
	 * @return mixed "_optionContainer" (ARRAY) on Success, "false" otherwise
	 */
	static protected function GetSubCategoryOptions($_troubleshooterCategoryID, $_selectedTroubleshooterStepIDList, $_parentTroubleshooterStepID, &$_optionContainer, $_indent = 0,
			$_activeTroubleshooterStepID = false, $_isCheckbox = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_troubleshooterStepCache = self::$_troubleshooterStepCache;

		$_troubleshooterParentMap = array();

		extract($_troubleshooterStepCache);

		$_childCount = 0;

		if (isset($_troubleshooterParentMap[$_parentTroubleshooterStepID]))
		{
			$_childCount = count($_troubleshooterParentMap[$_parentTroubleshooterStepID]);
		}

		if ($_childCount > 0)
		{
			$_childIndent = str_repeat('   ', $_indent);

			$_childPrefix = $_childIndent . '|- ';

			foreach ($_troubleshooterParentMap[$_parentTroubleshooterStepID] as $_val)
			{
				// Dont show the currently active node
				if ($_activeTroubleshooterStepID == $_val['troubleshooterstepid'])
				{
					continue;
				}

				if ($_parentTroubleshooterStepID == 0)
				{
					self::$_troubleshooterStepHits = array();
				}

				// To prevent recursion
				if (!isset(self::$_troubleshooterStepHits[$_val['troubleshooterstepid']]))
				{
					self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] = 0;
				}

				self::$_troubleshooterStepHits[$_val['troubleshooterstepid']]++;

				$_optionArray = array();
				$_optionArray['title'] = $_childPrefix . $_val['subject'] . IIF(self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] == 4, $_SWIFT->Language->Get('recursionsuffix'));
				$_optionArray['value'] = $_val['troubleshooterstepid'];

				$_isSelected = false;
				if (_is_array($_selectedTroubleshooterStepIDList) && in_array($_val['troubleshooterstepid'], $_selectedTroubleshooterStepIDList))
				{
					$_isSelected = true;
				}

				if ($_isCheckbox)
				{
					$_optionArray['checked'] = $_isSelected;
					$_optionArray['icon'] = SWIFT::Get('themepathimages'). 'icon_folderyellow3.gif';
				} else {
					$_optionArray['selected'] = $_isSelected;
				}

				$_optionContainer[] = $_optionArray;

				if (isset($_troubleshooterParentMap[$_val['troubleshooterstepid']]) && count($_troubleshooterParentMap[$_val['troubleshooterstepid']]) > 0
						&& self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] <= 3)
				{
					self::GetSubCategoryOptions($_troubleshooterCategoryID, $_selectedTroubleshooterStepIDList, $_val['troubleshooterstepid'], $_optionContainer, ($_indent + 1),
							$_activeTroubleshooterStepID, $_isCheckbox);
				}
			}
		}

		return $_optionContainer;
	}

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

		$_returnHTML = '&nbsp;&nbsp;-&nbsp;&nbsp;' . '<div title="' . $_SWIFT->Language->Get('insertstep') . '" class="kbarticlenew" onclick="javascript: loadViewportData(\'/Troubleshooter/Step/Insert/' . intval($_troubleshooterStepID) . '/' . intval($_troubleshooterCategoryID) . '\');">&nbsp;</div>';

		return $_returnHTML;
	}

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

		$_troubleshooterCategoryContainer = array();
		$_SWIFT->Database->Query("SELECT * FROM " . TABLE_PREFIX . "troubleshootercategories ORDER BY displayorder ASC");
		while ($_SWIFT->Database->NextRecord()) {
			$_troubleshooterCategoryContainer[$_SWIFT->Database->Record['troubleshootercategoryid']] = $_SWIFT->Database->Record;
		}

		$_renderHTML = '<ul class="swifttree">';
		foreach ($_troubleshooterCategoryContainer as $_troubleshooterCategory) {
			$_extendedCategoryTitle = ' (' . SWIFT_TroubleshooterCategory::GetCategoryTypeLabel($_troubleshooterCategory['categorytype']) . ')';

			$_renderHTML .= '<li><span class="folder">' . htmlspecialchars($_troubleshooterCategory['title']) . $_extendedCategoryTitle . self::GetTreeItemOptions($_troubleshooterCategory['troubleshootercategoryid']) . '</span>';

			self::$_troubleshooterStepCache = SWIFT_TroubleshooterStep::RetrieveSteps($_troubleshooterCategory['troubleshootercategoryid']);

			self::GetSubTroubleshooterTree($_SWIFT->Database->Record['troubleshootercategoryid'], 0, $_renderHTML);

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

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

		return $_renderHTML;
	}

	/**
	 * Retrieve the troubleshooter step options in a loop
	 *
	 * @author Varun Shoor
	 * @param int $_troubleshooterCategoryID The Troubleshooter Category ID
	 * @param int $_parentTroubleshooterStepID The Parent Troubleshooter Step ID
	 * @param string $_renderHTML The Render HTML
	 * @return mixed "_optionContainer" (ARRAY) on Success, "false" otherwise
	 */
	static protected function GetSubTroubleshooterTree($_troubleshooterCategoryID, $_parentTroubleshooterStepID, &$_renderHTML)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_troubleshooterStepCache = self::$_troubleshooterStepCache;

		$_troubleshooterParentMap = array();

		extract($_troubleshooterStepCache);

		$_childCount = 0;

		if (isset($_troubleshooterParentMap[$_parentTroubleshooterStepID]))
		{
			$_childCount = count($_troubleshooterParentMap[$_parentTroubleshooterStepID]);
		}

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

			foreach ($_troubleshooterParentMap[$_parentTroubleshooterStepID] as $_val)
			{
				if ($_parentTroubleshooterStepID == 0)
				{
					self::$_troubleshooterStepHits = array();
				}

				// To prevent recursion
				if (!isset(self::$_troubleshooterStepHits[$_val['troubleshooterstepid']]))
				{
					self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] = 0;
				}

				self::$_troubleshooterStepHits[$_val['troubleshooterstepid']]++;

				$_stepTitle =  htmlspecialchars($_val['subject']) . IIF(self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] == 4, $_SWIFT->Language->Get('recursionsuffix'));

				$_renderHTML .= '<li><span class="file"><a href="' . SWIFT::Get('basename') . '/Troubleshooter/Step/Edit/' . intval($_val['troubleshooterstepid']) . '" viewport="1">' . $_stepTitle . '</a>' . self::GetTreeItemOptions($_val['troubleshootercategoryid'], $_val['troubleshooterstepid']) . '</span>';


				if (isset($_troubleshooterParentMap[$_val['troubleshooterstepid']]) && count($_troubleshooterParentMap[$_val['troubleshooterstepid']]) > 0
						&& self::$_troubleshooterStepHits[$_val['troubleshooterstepid']] <= 3)
				{
					self::GetSubTroubleshooterTree($_troubleshooterCategoryID, $_val['troubleshooterstepid'], $_renderHTML);
				}

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

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

		return true;
	}
}
?>