<?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 Client User Interface Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_UserInterfaceClient extends SWIFT_UserInterface
{
	protected $_defaultWidget = 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;
	}

	/**
	 * Initialize the Client Data
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	protected function InitializeClient()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$this->Template->Assign('_widgetContainer', SWIFT_Widget::GetWidgetListForUser());

		return true;
	}

	/**
	 * Set the default widget
	 *
	 * @author Varun Shoor
	 * @param string $_defaultWidget The Default Widget Name
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded or If Invalid Data is Provided
	 */
	public function SetWidget($_defaultWidget)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		} else if (empty($_defaultWidget)) {
			throw new SWIFT_Exception(SWIFT_INVALIDDATA);

			return false;
		}

		$this->_defaultWidget = $_defaultWidget;

		return true;
	}

	/**
	 * Retrieve the default widget
	 *
	 * @author Varun Shoor
	 * @return mixed "_defaultWidget" (STRING) on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetWidget()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		return $this->_defaultWidget;
	}

	/**
	 * Load the Header
	 *
	 * @author Varun Shoor
	 * @param string $_defaultWidget The Default Widget Name
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function Header($_defaultWidget = false)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		if (!empty($_defaultWidget))
		{
			$this->SetWidget($_defaultWidget);
		}

		$this->InitializeClient();
		$this->ProcessDialogs();

		$this->Template->Render('header');

		return true;
	}

	/**
	 * Load the Footer
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function Footer()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		echo $this->GetFooterScript();

		$this->Template->Render('footer');

		return true;
	}

	/**
	 * Get the Footer Script
	 *
	 * @author Varun Shoor
	 * @return string The Footer Script
	 * @throws SWIFT_Exception If the Class is not Loaded
	 */
	public function GetFooterScript()
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		}

		$_scriptHTML = '';

		// Process and dispatch error fields Javascript
		$_scriptHTML = '<script type="text/javascript"> $(function(){ $(\'.dialogerror, .dialoginfo, .dialogalert\').fadeIn(\'slow\');';
		$_scriptHTML .= '$("form").bind("submit", function(e){$(this).find("input:submit").attr("disabled", "disabled");});';
		foreach (SWIFT::GetErrorFieldContainer() as $_key => $_val)
		{
			$_scriptHTML .= '$(\'input[name="' . Clean($_val) . '"]\').addClass(\'swifttexterror\');';
			$_scriptHTML .= '$(\'textarea[name="' . Clean($_val) . '"]\').addClass(\'swifttexterror\');';
			$_scriptHTML .= '$(\'select[name="' . Clean($_val) . '"]\').addClass(\'swifttexterror\');';
		}

		$_scriptHTML .= '});</script>';

		return $_scriptHTML;
	}

	/**
	 * Processes the Dialogs
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function ProcessDialogs()
	{
		if (!$this->GetIsClassLoaded())
		{
			return false;
		}

		$_errorContainer = $_infoContainer = $_alertContainer = array();

		if (_is_array(SWIFT::GetInfoContainer()))
		{
			$_infoContainer = SWIFT::GetInfoContainer();
		}

		if (_is_array(SWIFT::GetErrorContainer()))
		{
			$_errorContainer = SWIFT::GetErrorContainer();
		}

		if (_is_array(SWIFT::GetAlertContainer()))
		{
			$_alertContainer = SWIFT::GetAlertContainer();
		}

		SWIFT::ResetAllContainers();

		$this->Template->Assign('_errorContainer', $_errorContainer);
		$this->Template->Assign('_infoContainer', $_infoContainer);
		$this->Template->Assign('_alertContainer', $_alertContainer);

		return true;
	}

	/**
	 * Get Error
	 *
	 * @author Varun Shoor
	 * @param string $_title The Title
	 * @param string $_message The Message
	 * @param string $_divID The Unique ID for this Div
	 * @param bool $_showBorder Whether to Show Border for this Div
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function GetError($_title, $_message, $_divID = '', $_showBorder = true)
	{
		if (!$this->GetIsClassLoaded() || empty($_title) || empty($_message))
		{
			return false;
		}

		return '<div class="dialogerror"><div class="dialogerrorsub"><div class="dialogerrorcontent">' . $_message . '</div></div></div>';
	}

	/**
	 * Get Alert
	 *
	 * @author Varun Shoor
	 * @param string $_title The Title
	 * @param string $_message The Message
	 * @param string $_divID The Unique ID for this Div
	 * @param bool $_showBorder Whether to Show Border for this Div
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function GetAlert($_title, $_message, $_divID = '', $_showBorder = true)
	{
		if (!$this->GetIsClassLoaded() || empty($_title) || empty($_message))
		{
			return false;
		}

		return '<div id="'. $_divID .'" style="padding: 4px; background: #f8f4eb;'. IIF($_showBorder, ' border-bottom: 1px SOLID #efe8da; PADDING-RIGHT: 7px; border-left: 1px solid #e9e1d1;') . '"><div class="dialogalert"><div class="hd"><div class="e">'. $_title .'</div><div class="d"><div class="c"></div></div></div><div class="bd"><div class="c"><div class="s">'. $_message .'</div></div></div><div class="ft"><div class="c"></div></div></div></div>';
	}

	/**
	 * Get Confirmation
	 *
	 * @author Varun Shoor
	 * @param string $_title The Title
	 * @param string $_message The Message
	 * @param string $_divID The Unique ID for this Div
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function GetInfo($_title, $_message, $_divID = '')
	{
		if (!$this->GetIsClassLoaded() || empty($_title) || empty($_message))
		{
			return false;
		}

		return '<div id="'. $_divID .'" style="padding: 4px; background: #f8f4eb; border-bottom: 1px SOLID #efe8da; PADDING-RIGHT: 7px;"><div class="dialogok"><div class="hd"><div class="e">'. $_title .'</div><div class="d"><div class="c"></div></div></div><div class="bd"><div class="c"><div class="s">'. $_message .'</div></div></div><div class="ft"><div class="c"></div></div></div></div>';
	}
}
?>