<?php
/**
 * View messages
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Mail###
 * @author 		selvaraj_47ag04
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-22
 **/
require_once('./common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/members/mail.php';
$CFG['lang']['include_files'][] = 'languages/%s/members/mailRightLinks.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_MailHandler.lib.php';
$CFG['lang']['include_files'][] = 'common/configs/config_mails.inc.php';
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['auth']['is_authenticate'] = 'members';
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
//--------------class MailFormHandler--------------->>>//
/**
 * mail handler
 */
class MailFormHandler extends MailHandler
	{
		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function buildMailQuery()
		    {
				global $smartyObj;
				switch($this->fields_arr['folder'])
					{
						case 'inbox':
							$smartyObj->assign('heading', $this->LANG['nav_mail_inbox']);
							$this->setTableNames(array($this->CFG['db']['tbl']['users'].' AS u', $this->CFG['db']['tbl']['messages'].' AS m', $this->CFG['db']['tbl']['messages_info'].' AS mi'));
							$this->setReturnColumns(array('mi.info_id, mi.message_id, mi.to_id, mi.from_id, '.$this->getUserTableField('user_id').' AS img_user_id, '.$this->getUserTableFields(array('user_id', 'name', 'display_name', 'image_path', 'gender', 'photo_server_url', 't_width', 't_height', 's_width', 's_height', 'photo_ext')).', m.subject, DATE_FORMAT(m.mess_date, \''.$this->CFG['format']['date'].'\') as mess_date, DATE_FORMAT(m.mess_date, \''.$this->CFG['format']['time'].'\') as mess_time, mi.to_viewed, mi.to_answer'));
							//Condition of the query
							$condition = 'mi.to_id = \''. $this->CFG['user']['user_id'] .'\' AND mi.from_id = u.'.$this->getUserTableField('user_id').' AND mi.message_id = m.message_id AND mi.to_delete = \'No\' AND mi.to_stored = \'No\'';
							//sort details of the query
							$this->fields_arr['orderby_field'] = 'mi.info_id';
							$this->fields_arr['orderby'] = 'DESC';
							//page title
							$this->mail_title = $this->LANG['mail_title_inbox'];
							break;
						case 'sent':
							$smartyObj->assign('heading', $this->LANG['send_mail']);
							$this->setTableNames(array($this->CFG['db']['tbl']['users'].' AS u', $this->CFG['db']['tbl']['messages'].' AS m', $this->CFG['db']['tbl']['messages_info'].' AS mi'));
							$this->setReturnColumns(array('mi.info_id, mi.message_id, mi.to_id, mi.from_id, '.$this->getUserTableField('user_id').' AS img_user_id, '.$this->getUserTableFields(array('user_id', 'name', 'display_name', 'image_path', 'gender', 'photo_server_url', 't_width', 't_height', 's_width', 's_height', 'photo_ext')).', m.subject, DATE_FORMAT(m.mess_date, \''.$this->CFG['format']['date'].'\') as mess_date, DATE_FORMAT(m.mess_date, \''.$this->CFG['format']['time'].'\') as mess_time, mi.from_viewed, mi.from_answer'));
							//Condition of the query
							$condition = 'mi.from_id = \''. $this->CFG['user']['user_id'] .'\' AND mi.to_id = u.'.$this->getUserTableField('user_id').' AND mi.message_id = m.message_id AND mi.from_delete = \'No\' AND mi.from_stored = \'No\' AND mi.email_status = \'Normal\'';
							//sort details of the query
							$this->fields_arr['orderby_field'] = 'mi.info_id';
							$this->fields_arr['orderby'] = 'DESC';
							//page title
							$this->mail_title = $this->LANG['mail_title_sent'];
							break;
					} // switch
				$this->sql_condition = $condition;
		    }

		/**
		 * To sort the query
		 *
		 * @access public
		 * @return void
		 **/
		public function buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}

		/**
		 * To populate the messages
		 *
		 * @access public
		 * @return void
		 **/
		public function populateMessages()
			{
				global $smartyObj;
				switch($this->fields_arr['folder'])
					{
						case 'sent':
							$this->LANG['sender_name_title'] = $this->LANG['mail_to'];
							break;
						default:
							$this->LANG['sender_name_title'] = $this->LANG['common_from'];
							break;
					}
				$data_arr = array();
				$inc = 0;
				while($row = $this->fetchResultRecord())
					{
						$data_arr[$inc] = $row;
						$userDetails = $row;
						$userDetails['user_id'] = $row['user_id'];
						$data_arr[$inc]['user_id'] = $userDetails;
						//$data_arr[$inc]['subject'] = character_limiter($row['subject'], $this->CFG['admin']['mails']['subject_length'], $this->CFG['word_limiter']['end_character']);
						$data_arr[$inc]['subject'] = wordWrapManual($row['subject'], 35, 100);
						switch($this->fields_arr['folder'])
							{
								case 'sent':
									$data_arr[$inc]['answer'] = $row['from_answer'];
									$data_arr[$inc]['viewed'] = $row['from_viewed'];
									$userDetails = $this->getUserDetail('user_id', $row['to_id']);
									break;
								default:
									$data_arr[$inc]['answer'] = $row['to_answer'];
									$data_arr[$inc]['viewed'] = $row['to_viewed'];
									$userDetails = $this->getUserDetail('user_id', $row['from_id']);
									break;
							}
						$data_arr[$inc] = $data_arr[$inc]+$userDetails;
						$data_arr[$inc]['display_user_name'] = getUserDisplayName($userDetails);
						$data_arr[$inc]['user_profiles_link'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);

						if ($data_arr[$inc]['answer'] == 'Reply')
							{
								$data_arr[$inc]['row_css'] = 'clsMailReplied';
								$data_arr[$inc]['mail_status'] = $this->LANG['replied'];
							}
						else if ($data_arr[$inc]['answer'] == 'Forward')
							{
								$data_arr[$inc]['row_css'] = 'clsMailForwarded';
								$data_arr[$inc]['mail_status'] = $this->LANG['forward'];
							}
						else if ($data_arr[$inc]['viewed'] == 'No')
							{
								$data_arr[$inc]['row_css'] = 'clsMailNotRead';
								$data_arr[$inc]['mail_status'] = $this->LANG['new_mail'];
							}
						else
							{
								$data_arr[$inc]['row_css'] = 'clsMailRead';
								$data_arr[$inc]['mail_status'] = $this->LANG['read'];
							}
						$data_arr[$inc]['mail_read_link'] = getUrl('mailread', '?folder='.$this->getFormField('folder').'&amp;message_id='.$row['info_id'], $this->getFormField('folder').'/?message_id='.$row['info_id'], 'members');
						$inc++;
					}
				$smartyObj->assign('populateMessages_arr', $data_arr);
			}

		/**
		 * To delete messages selected
		 *
		 * @param 	array $message_ids selected message ids
		 * @access public
		 * @return boolean
		 **/
		public function deleteMessages($message_ids)
			{
				$message_ids_arr = explode(',', $message_ids);
				foreach($message_ids_arr as $combined_id)
					{
						$id_arr = explode('_', $combined_id);
						$from_id = $id_arr[0];
						$to_id = $id_arr[1];
						$message_id = $id_arr[2];
						if($this->fields_arr['folder']=='trash')
							{
								if ($to_id == $this->CFG['user']['user_id'])
									{
		 								$this->updateMessageStatusTrash('to_delete', $message_id);
									}
								if ($from_id == $this->CFG['user']['user_id'])
									{
										$this->updateMessageStatusTrash('from_delete', $message_id);
									}
							}
						else
							{
								if ($to_id == $this->CFG['user']['user_id'])
									{
		 								$this->updateMessageStatusDelete('to_delete', $message_id);
									}
								if ($from_id == $this->CFG['user']['user_id'])
									{
										$this->updateMessageStatusDelete('from_delete', $message_id);
									}
							}
					}
			}
	}
//<<<<<<<--------------class MailFormHandler---------------//
//--------------------Code begins-------------->>>>>//
$mail = new MailFormHandler();
$mail->mailcompose['populateMailNavigation'] =   $mail->populateMailNavigation();
if(!$mail->chkAllowedModule(array('mail')))
	Redirect2URL(getUrl($CFG['redirect']['dsabled_module_url']['file_name']));
$mail->setPageBlockNames(array('block_form_mail', 'block_form_confirm'));
$mail->setAllPageBlocksHide();
//default form fields and values...
$mail->setFormField('folder', '');
$mail->setFormField('user_id', '');
$mail->setFormField('message_ids', array());
$mail->setFormField('action', '');
/*********** Page Navigation Start *********/
$mail->setFormField('orderby_field', '');
$mail->setFormField('orderby', '');
$mail->setTableNames(array());
$mail->setReturnColumns(array());
/************ page Navigation stop *************/
$mail->sanitizeFormInputs($_REQUEST);
if (!$mail->chkIsValidFolder())
	{
		$smartyObj->assign('heading', '');
		$mail->setPageBlockShow('block_msg_form_error');
		$mail->setCommonErrorMsg($mail->LANG['mail_invalid_folder']);
	}
else
	{
		if ($mail->isFormGETed($_GET, 'msg'))
			{
				$mail->setAllPageBlocksHide();
				$mail->setPageBlockShow('block_msg_form_success');
				$mail->setCommonSuccessMsg($mail->LANG['mail_success_message']);
			}
		if ($mail->isFormGETed($_GET, 'del'))
			{
				$mail->setAllPageBlocksHide();
				$mail->setPageBlockShow('block_msg_form_success');
				$mail->setCommonSuccessMsg($mail->LANG['mail_success_delete_message']);
			}
		if ($mail->isFormPOSTed($_POST,'confirm'))
			{
				switch($mail->getFormField('action'))
					{
						case 'mail_delete':
							$mail->deleteMessages($mail->getFormField('message_ids'));
							$mail->setCommonSuccessMsg($mail->LANG['mail_success_delete_message']);
							break;
					} // switch
				$mail->setAllPageBlocksHide();
				$mail->setPageBlockShow('block_msg_form_success');
			}
		if ($mail->isFormPOSTed($_POST, 'cancel'))
			{
				$mail->setAllPageBlocksHide();
				$mail->setFormField('message_ids', array());
				$mail->setFormField('action', '');
			}
		$mail->setPageBlockShow('block_form_mail');
	}
/*************End navigation******/
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
if ($mail->isShowPageBlock('block_form_mail'))
	{
		/****** navigtion continue*********/
		$mail->buildMailQuery();
		$mail->buildSelectQuery();
		$mail->buildSortQuery();
		$mail->buildQuery();
		$mail->executeQuery();
		if(!$mail->isResultsFound())
			{
				$mail->setAllPageBlocksHide();
				$mail->setCommonAlertMsg($mail->LANG['common_no_mails_found']);
				$mail->setPageBlockShow('block_msg_form_alert');
				$mail->setPageBlockHide('listAdvertisementBlock');
			}
		else
			{
				$smartyObj->assign('msg_confirm_form_hidden_arr', array('start', 'orderby_field', 'orderby'));
				$smartyObj->assign('selFormMail_hidden_arr', array('start', 'orderby_field', 'orderby'));
				$smartyObj->assign('smarty_paging_list', $mail->populatePageLinksGET($mail->getFormField('start'), array('folder')));
				$smartyObj->assign('action_arr', array('mail_delete'=>$mail->LANG['common_delete']));
				$smartyObj->assign('mail_action_onclick', 'getMultiCheckBoxValue(\'selFormMail\', \'checkall\', \''.$mail->LANG['mail_select_message'].'\');if(multiCheckValue!=\'\'){getAction()}');
				$mail->populateMessages();
			}
	}
$mail->mailcompose['countUnReadMail']	     =	$mail->countUnReadMail();
//include the header file
$mail->includeHeader();
if ($mail->isShowPageBlock('block_form_mail'))
	{
?>
<script type="text/javascript" language="javascript">
	var block_arr= new Array('selMsgConfirm');
	var please_select_action = '<?php echo $mail->LANG['common_select_action'];?>';
	var confirm_message = '';
	function getAction()
		{
			var act_value = document.selFormMail.act.value;
			if(act_value)
				{
					switch (act_value)
						{
							case 'mail_delete':
								confirm_message = '<?php echo $mail->LANG['mail_confirm_delete_message'];?>';
								break;
						}
					$('confirmMessage').innerHTML = confirm_message;
					document.msgConfirmform.action.value = act_value;
					Confirmation('selMsgConfirm', 'msgConfirmform', Array('message_ids'), Array(multiCheckValue), Array('value'));
				}
			else
				alert_manual(please_select_action, 'dAltMlti');
		}
</script>
<?php
	}
//include the content of the page
setTemplateFolder('members/');
$smartyObj->display('mail.tpl');
//<<<<<-------------------- Page block templates ends -------------------//
$mail->includeFooter();
?>