<?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 Knowledgebase Category Manager Class
 *
 * @author Varun Shoor
 */
class SWIFT_KnowledgebaseCategoryManager extends SWIFT_Library
{
	static protected $_knowledgebaseCategoryCache = 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 Knowledgebase Category Options
	 *
	 * @author Varun Shoor
	 * @param array $_selectedKnowledgebaseCategoryID (OPTIONAL) The Knowledgebase Category ID to be selected by default
	 * @param int $_activeKnowledgebaseCategoryID (OPTIONAL) The Active Knowledgebase Category 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($_selectedKnowledgebaseCategoryIDList = false, $_activeKnowledgebaseCategoryID = false, $_isCheckbox = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		if (!self::$_knowledgebaseCategoryCache)
		{
			self::$_knowledgebaseCategoryCache = SWIFT_KnowledgebaseCategory::RetrieveCategories();
		}

		$_knowledgebaseCategoryCache = self::$_knowledgebaseCategoryCache;

		$_optionContainer = array();

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

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

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

		self::GetSubCategoryOptions($_selectedKnowledgebaseCategoryIDList, 0, $_optionContainer, 0, $_activeKnowledgebaseCategoryID, $_isCheckbox);

		return $_optionContainer;
	}

	/**
	 * Retrieve the knowledgebase category options in a loop
	 *
	 * @author Varun Shoor
	 * @param array $_selectedKnowledgebaseCategoryIDList The Selected Knowledgebase Category ID List
	 * @param int $_parentKnowledgebaseCategoryID The Parent Knowledgebase 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 GetSubCategoryOptions($_selectedKnowledgebaseCategoryIDList, $_parentKnowledgebaseCategoryID, &$_optionContainer, $_indent = 0, 
			$_activeKnowledgebaseCategoryID = false, $_isCheckbox = false)
	{
		$_SWIFT = SWIFT::GetInstance();

		$_knowledgebaseCategoryCache = self::$_knowledgebaseCategoryCache;

		$_knowledgebaseParentMap = array();

		extract($_knowledgebaseCategoryCache);

		$_childCount = 0;

		if (isset($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID]))
		{
			$_childCount = count($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID]);
		}

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

			$_childPrefix = $_childIndent . '|- ';

			foreach ($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID] as $_val)
			{
				if ($_activeKnowledgebaseCategoryID == $_val['kbcategoryid'])
				{
					continue;
				}
				
				$_optionArray = array();
				$_optionArray['title'] = $_childPrefix . $_val['title'];
				$_optionArray['value'] = $_val['kbcategoryid'];


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

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

				$_optionContainer[] = $_optionArray;

				if (isset($_knowledgebaseParentMap[$_val['kbcategoryid']]) && count($_knowledgebaseParentMap[$_val['kbcategoryid']]) > 0)
				{
					self::GetSubCategoryOptions($_selectedKnowledgebaseCategoryIDList, $_val['kbcategoryid'], $_optionContainer, ($_indent + 1), $_activeKnowledgebaseCategoryID, $_isCheckbox);
				}
			}
		}

		return $_optionContainer;
	}

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

		$_returnHTML = '&nbsp;&nbsp;-&nbsp;&nbsp;' . '<div title="' . $_SWIFT->Language->Get('insertcategory') . '" class="kbcategorynew" onclick="javascript: InsertKnowledgebaseCategoryWindow(\'' . intval($_knowledgebaseCategoryID) . '\');">&nbsp;</div> <div title="' . $_SWIFT->Language->Get('insertarticle') . '" class="kbarticlenew" onclick="javascript: loadViewportData(\'/Knowledgebase/Article/Insert/' . intval($_knowledgebaseCategoryID) . '\');">&nbsp;</div> <div title="' . $_SWIFT->Language->Get('filterarticles') . '" class="kbarticlesearch" onclick="javascript: loadViewportData(\'' . SWIFT::Get('basename') . '/Knowledgebase/Article/QuickFilter/category/' . intval($_knowledgebaseCategoryID) . '/\');">&nbsp;</div> ';

		return $_returnHTML;
	}

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

		if (!self::$_knowledgebaseCategoryCache)
		{
			self::$_knowledgebaseCategoryCache = SWIFT_KnowledgebaseCategory::RetrieveCategories();
		}

		$_knowledgebaseCategoryCache = self::$_knowledgebaseCategoryCache;

		$_extendedKnowledgebaseCategoryTitle = '';

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

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

		self::GetSubKnowledgebaseCategoryTree($_selectedKnowledgebaseCategoryID, 0, $_renderHTML);

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

		return $_renderHTML;
	}

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

		$_knowledgebaseCategoryCache = self::$_knowledgebaseCategoryCache;

		extract($_knowledgebaseCategoryCache);

		$_childCount = 0;

		if (isset($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID]))
		{
			$_childCount = count($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID]);
		}

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

			foreach ($_knowledgebaseParentMap[$_parentKnowledgebaseCategoryID] as $_key => $_val)
			{
				$_knowledgebaseCategoryTitle = htmlspecialchars($_val['title']);
				if ($_selectedKnowledgebaseCategoryID == $_val['kbcategoryid'])
				{
					$_knowledgebaseCategoryTitle = '<b>' . htmlspecialchars($_val['title']) . '</b>';
				}

				$_renderHTML .= '<li><span class="folder"><a href="javascript: void(0);" onclick="javascript: EditKnowledgebaseCategoryWindow(\'' . intval($_val['kbcategoryid']) . '\');">' . $_knowledgebaseCategoryTitle . '</a>' . self::GetTreeItemOptions($_val['kbcategoryid']) . '</span>';

				if (isset($_knowledgebaseParentMap[$_val['kbcategoryid']]) && count($_knowledgebaseParentMap[$_val['kbcategoryid']]) > 0)
				{
					self::GetSubKnowledgebaseCategoryTree($_selectedKnowledgebaseCategoryID, $_val['kbcategoryid'], $_renderHTML);
				}

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

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

		return $_renderHTML;
	}
}
?>