<?php
/**
 * Class to handle the form fields
 *
 * This is having class MailHandler to handle the internal mails.
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		MailHandler
 * @author		senthil_52ag05
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id : $
 * @since 		2008-11-02
 */
if(class_exists('AnovaHandler'))
	{
		$parent_class = 2;
	}
else
	{
		$parent_class = 1;
	}

switch($parent_class)
	{
		case 2:
			class Handler extends AnovaHandler{}
			break;
		case 1:
		default:
			class Handler extends FormHandler{}
			break;
	}

class MailHandler extends Handler
	{
		//Default folder array
		public $allowed_folder = array('inbox', 'sent');
		//Fodler to update new mail count
		public $updateNewMailInFolder = array('inbox');
		//To store message title
		public $mail_title = '';

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function chkIsValidFolder()
		    {
				$ok = true;
				if (!in_array($this->fields_arr['folder'], $this->allowed_folder))
					$ok = false;
				return $ok;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getMailReadNormalUrl()
		    {
				switch($this->fields_arr['folder']){
					case 'inbox':
						$normalUrl = $this->CFG['admin']['mail_urls']['read_inbox']['normal'];
						break;
					case 'sent':
						$normalUrl = $this->CFG['admin']['mail_urls']['read_sent']['normal'];
						break;
					default:
						$normalUrl = $this->CFG['admin']['mail_urls']['read_inbox']['normal'];
						break;
				} // switch
				return $normalUrl;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getMailReadHtaccessUrl()
		    {
				switch($this->fields_arr['folder']){
					case 'inbox':
						$htaccessUrl = $this->CFG['admin']['mail_urls']['read_inbox']['htaccess'];
						break;
					case 'sent':
						$htaccessUrl = $this->CFG['admin']['mail_urls']['read_sent']['htaccess'];
						break;
					default:
						$htaccessUrl = $this->CFG['admin']['mail_urls']['read_inbox']['htaccess'];
						break;
				} // switch
				return $htaccessUrl;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getMailListNormalUrl()
		    {
				switch($this->fields_arr['folder']){
					case 'inbox':
						$normalUrl = $this->CFG['admin']['mail_urls']['inbox']['normal'];
						break;
					case 'sent':
						$normalUrl = $this->CFG['admin']['mail_urls']['sent']['normal'];
						break;
					default:
						$normalUrl = $this->CFG['admin']['mail_urls']['inbox']['normal'];
						break;
				} // switch
				return $normalUrl;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getMailListHtaccessUrl()
		    {
				switch($this->fields_arr['folder']){
					case 'inbox':
						$htaccessUrl = $this->CFG['admin']['mail_urls']['inbox']['htaccess'];
						break;
					case 'sent':
						$htaccessUrl = $this->CFG['admin']['mail_urls']['sent']['htaccess'];
						break;
					default:
						$htaccessUrl = $this->CFG['admin']['mail_urls']['inbox']['htaccess'];
						break;
				} // switch
				return $htaccessUrl;
		    }

		/**
		 * To change the status of the message
		 *
		 * @access public
		 * @return void
		 **/
		public function updateMessageStatusDelete($column_name, $message_ids)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['messages_info'].
						' SET '.$column_name.' = \'Yes\' WHERE info_id IN ('.$message_ids.')';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);
				return true;
			}

		/**
		 * To change the status of the message
		 *
		 * @access public
		 * @return void
		 **/
		public function updateMessageStatusTrash($column_name, $message_ids)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['messages_info'].
						' SET '.$column_name .' = \'Trash\' WHERE info_id IN ('.$message_ids.') ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);
				return true;
			}


		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function sendInternalMail($from_id, $to_id, $subject, $message)
			{
				$message_id = $this->insertMessages($subject, $message);
				$this->insertMessagesInfo($from_id, $to_id, $message_id);
				return true;
			}

		/**
		 * To insert subject and mail in mails table and return messgage id
		 *
		 * @param 		string $subject
		 * @param 		string $message
		 * @access public
		 * @return integer $message_id
		 **/
		public function insertMessages($subject, $message)
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['messages'].
						' SET subject = '.$this->dbObj->Param($subject).
						', message = '.$this->dbObj->Param($message).
						', mess_date = NOW()';
		        $stmt = $this->dbObj->Prepare($sql);
		        $rs = $this->dbObj->Execute($stmt, array($subject, $message));
		        if (!$rs)
		                trigger_db_error($this->dbObj);
		        return $this->dbObj->Insert_ID();
			}

		/**
		 * To insert from_id, to_id, and mail status in mails_info table and return true in sucess
		 *
		 * @access public
		 * @return boolean
		 **/
		public function insertMessagesInfo($from_id, $to_id, $message_id)
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['messages_info'].
						' SET message_id = '.$this->dbObj->Param($message_id).
						', from_id = '.$this->dbObj->Param($from_id).
						', to_id = '.$this->dbObj->Param($to_id);
		        $stmt = $this->dbObj->Prepare($sql);
		        $rs = $this->dbObj->Execute($stmt, array($message_id, $from_id, $to_id));
		        if (!$rs)
		                trigger_db_error($this->dbObj);
		        return $this->dbObj->Insert_ID();
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function messageContentReplace($message, $keywords_arr)
			{
				foreach($keywords_arr as $key=>$value)
					{
						$message = str_replace('{'.$key.'}', $value, $message);
					}
				return $message;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function populateMailNavigation()
			{

				$cssClassToHighlightSubLink = 'clsActiveMisNavLinks';

				$mail = $mailCompose = $mail_pg_inbox = $mail_pg_sent = $mail_pg_compose = '';
				$currentPage = $this->CFG['html']['current_script_name'];

				$$currentPage = $cssClassToHighlightSubLink;

				$pg = (isset($_REQUEST['folder']))?$_REQUEST['folder']:'';
				$pgRelatedPage = $currentPage.'_pg_'.$pg;
				$$pgRelatedPage = $cssClassToHighlightSubLink;
				if ($currentPage == 'mailRead' and $pg == 'inbox')
					$mail_pg_inbox = $cssClassToHighlightSubLink;
				if ($currentPage == 'mailRead' and $pg == 'sent')
					$mail_pg_sent = $cssClassToHighlightSubLink;

				$mail_arr['inbox']  = getUrl('mail', '?folder=inbox' , 'inbox/' , 'members')	;
				$mail_arr['inbox_class'] = $mail_pg_inbox;
				$mail_arr['sent']   = getUrl('mail', '?folder=sent' , 'sent/' , 'members')	;
				$mail_arr['sent_class'] = $mail_pg_sent;
				$mail_arr['compose']=getUrl('mailcompose', '', '' , 'members')	;
				$mail_arr['compose_class'] = $mailCompose;
				return $mail_arr;
			}
	}
?>