<?php
/**
 * Created by James
 * Date: 14/05/12
 * Time: 2:17 PM
 * To change this template use File | Settings | File Templates.
 */

class Mobile_IndexController extends Atmail_Controller_Base
{

    public function indexAction()
    {
		$moduleName = $_SERVER['PHP_SELF'];
		
		if( $moduleName[strlen($moduleName)-1] == '/') 
		{
			$this->_redirect("mobile");
			return;
		}		
		
		$appBaseUrl = $this->_request->getBaseUrl() . (strpos($this->_request->getBaseUrl(),'index.php')?'':'/index.php');
		$global = Zend_Registry::get('config')->global;
		$this->view->mobileUIEnabled = (array_key_exists('mobileui', $global) && $global['mobileui'] == '1') ? true : false;
        $this->view->setEncoding('UTF-8');
        $this->view->siteBaseUrl = Zend_Registry::get('siteBaseUrl');
        $this->view->appBaseUrl = $this->_request->getBaseUrl() . (strpos($this->_request->getBaseUrl(),'index.php')?'':'/index.php');
        $this->view->moduleBaseUrl = $this->view->appBaseUrl . ($this->view->requestParams['module']=='default'?'':'/' . $this->view->requestParams['module']);
        $this->view->thisActionURL = $this->view->moduleBaseUrl . '/' . $this->view->requestParams['controller'] . '/' . $this->view->requestParams['action'];
		$this->view->mobileTheme = $global['mobilecssTheme'];
		$this->view->enableSpamReporting = $global['enableSpamReporting'];
		
		// Use default theme if set theme is invalid
		if (empty($this->view->mobileTheme) || !file_exists(APP_ROOT . "css/themes/" . $this->view->mobileTheme)) {
			$this->view->mobileTheme = "original";
		}
		
		unset($global);
		$this->_helper->pluginCall('preMobileUiRender');
    }
	
	public function getAbookProfile($account)
	{
		$contacts = new contacts( array('Account' => $account, 'largeInstall' => '0') );

		$query = array('UserEmail'=>$account, 'GroupID'=>0);
		$search_results = $contacts->searchContact($query);

		return $search_results? $search_results : null;
	}	

    public function checkauthAction()
    {
        $arr = array( "auth" => Atmail_FormAuth::authenticated()? "success" : "failed" );

        if($arr['auth'] === 'success')
        {
	        $settings = Zend_Registry::get('UserSettings');

	        if(!empty($settings['Signature'])) {
	    		require_once('class.html2text.inc');
	        	$html2text = new html2text( $settings['Signature'] );
	        	$settings['Signature'] = $html2text->get_text();
	        }
			
			$profile = $this->getAbookProfile($settings['Account']);

			$settings['profile'] = $profile;
			
	        $arr['settings'] = $settings;
        }

        $json = Zend_Json::encode($arr);
        $this->getResponse()->setHeader('Content-Type','application/x-javascript')->appendBody($json);
        $this->_helper->viewRenderer->setNoRender();
    }

    public function html2textAction()
    {
        require_once('class.html2text.inc');

        $requestParams = $this->getRequest()->getParams();

        // Add the HTML and text parts
        $html2text = new html2text($requestParams['html']);
        $emailBodyText = $html2text->get_text();

        $this->renderJson(array('text' => $emailBodyText));
    }

}
