<?php
/**
 * File handling the My Subscribed Forum
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###My subscribed forum###
 * @author 		karthiselvam_75ag08
 * @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-19
 */
class subscribedForum extends FormHandler
	{
		public $user_details = array();

		/**
		 * subscribedForum::populateForumList()
		 *
		 * @return
		 */
		public function populateForumList()
			{
				$sql = 'SELECT ub.content_id FROM '.$this->CFG['db']['tbl']['user_bookmarked'].' as ub, '.$this->CFG['db']['tbl']['forum_topics'].' as ft'.
						' WHERE ub.content_type = \'Forum\' AND ub.content_id=ft.topic_id AND ub.user_id = '.$this->dbObj->Param('user_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['uid']));
				if (!$rs)
					trigger_db_error($this->dbObj);

				$inc=1;
				$populateForumList_arr=array();
				while($row = $rs->FetchRow())
					{
						//$populateForumList_arr[$inc]['record']=$forumDetails = $this->populateForumDetails($row['content_id']);
						$populateForumList_arr[$inc]['forumDetails'] = $forumDetails = $this->getForumDetails($row['content_id']);
						$populateForumList_arr[$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_response'];
						$populateForumList_arr[$inc]['view_plural'] = $view_plural = $this->LANG['common_total_view'];

						$populateForumList_arr[$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
						if ($inc%2)
							$populateForumList_arr[$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';

						if($forumDetails['total_response'] != 1)
							$populateForumList_arr[$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_responses'];
						if($forumDetails['total_views'] != 1)
							$populateForumList_arr[$inc]['view_plural'] = $view_plural = $this->LANG['common_total_views'];

						$populateForumList_arr[$inc]['forumsres_url']=getForumsUrl('forumsresponses','?title='.$forumDetails['title'].'&amp;topic_title='.$forumDetails['seo_title'], $forumDetails['title'].'/'.$forumDetails['seo_title'].'/');
						$populateForumList_arr[$inc]['forum_topic']	= wordWrapManual($forumDetails['forum_topic'], 15, $this->CFG['admin']['forum']['line_length']);
						$inc++;
					}
				return 	$populateForumList_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getForumDetails($forum_id)
			{
				$forumDetails = array();

				$sql = 'SELECT ft.forum_id, ft.topic_id, ft.forum_topic, ft.seo_title, ft.user_id, ft.total_response'.
						', ft.total_views, ft.last_post_user_id, ft.last_post_date, UNIX_TIMESTAMP(date_added) as date_added'.
						', u.'.$this->getUserTableField('user_id').' AS topic_user_id, u.'.$this->getUserTableField('name').' AS topic_user_name, u.'.$this->getUserTableField('display_name').' AS topic_user_display_name'.
						', u.'.$this->getUserTableField('image_path').' AS image_path, u.'.$this->getUserTableField('gender').' AS gender'.
						', u.'.$this->getUserTableField('user_id').' AS img_user_id, u.'.$this->getUserTableField('t_height').' AS t_height'.
						', u.'.$this->getUserTableField('t_width').' AS t_width, u.'.$this->getUserTableField('photo_server_url').' AS photo_server_url'.
						', u.'.$this->getUserTableField('s_width').' AS s_width, u.'.$this->getUserTableField('s_height').' AS s_height'.
						', u.'.$this->getUserTableField('photo_ext').' AS photo_ext, u.'.$this->getUserTableField('name').' AS name'.
						', u1.'.$this->getUserTableField('user_id').' AS last_user_id, u1.'.$this->getUserTableField('name').' AS last_user_name, u1.'.$this->getUserTableField('display_name').' AS last_user_display_name'.
						' FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft'.
						' LEFT JOIN '.$this->CFG['db']['tbl']['users'].' AS u1 ON ft.last_post_user_id = u1.'.$this->getUserTableField('user_id').
						', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE ft.topic_id='.$this->dbObj->Param($forum_id).
						' AND ft.user_id = u.'.$this->getUserTableField('user_id').
						' AND u.'.$this->getUserTableField('usr_status').' = \'Ok\''.
						' AND topic_status = \'Yes\'';
				//echo $sql;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount())
					{
						$forumDetails = $rs->FetchRow();
						if (!($forumDetails['title'] = $this->getForumTitle($forumDetails['forum_id'])))
							return false;
						$forumDetails['forum_link'] = '<a href="'.getForumsUrl('forumsresponses','?title='.$forumDetails['title'].'&amp;topic_title='.$forumDetails['seo_title'], $forumDetails['title'].'/'.$forumDetails['seo_title'].'/').'">'.wordWrapManual($forumDetails['forum_topic'], $this->CFG['admin']['forum']['line_length'], $this->CFG['admin']['forum']['short_length']).'</a>';
						$forumDetails['topic_user_name_link'] = '<a href="'.getMemberUrl($forumDetails['topic_user_id'], $forumDetails['topic_user_name']).'">'.stripString($forumDetails['topic_user_display_name'], $this->CFG['username']['medium_length']).'</a>';
					}
				return $forumDetails;
			}
		/**
		 * subscribedForum::populateUserDetailsArr()
		 *
		 * @return
		 */
		public function populateUserDetailsArr()
			{
				$fields_arr = array('user_id', 'name', 'display_name', 'email', 'image_path', 'gender', 'usr_status', 'user_access', 't_height', 't_width', 's_height', 's_width', 'photo_server_url', 'photo_ext');

				$sql = 'SELECT '. $this->getUserTableFields($fields_arr).' ,'.$this->getUserTableField('user_id').' as img_user_id FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE '.$this->getUserTableField('name').' = '.$this->dbObj->Param('name').
						' AND '.$this->getUserTableField('usr_status').'=\'OK\'';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['uid']));
				if (!$rs)
					trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->user_details = $row;
						$this->setFormField('uid', $row['user_id']);
						return true;
					}
				return false;
			}
	}
//--------------------Code begins-------------->>>>>//
$subscribedForum = new subscribedForum();
if(!$subscribedForum->chkAllowedModule(array('forums')))
	Redirect2URL(getAnovaUrl($CFG['redirect']['dsabled_module_url']['file_name']));
//set blocks
$subscribedForum->setPageBlockNames(array('forum_list_block'));
//show default pages
$subscribedForum->setFormField('uid', '');
$subscribedForum->sanitizeFormInputs($_REQUEST);
if(!$subscribedForum->getFormField('uid'))
	$subscribedForum->setFormField('uid', $CFG['user']['user_id']);
if($subscribedForum->populateUserDetailsArr())
	{
		$subscribedForum->setPageBlockShow('forum_list_block');
		$LANG['mysubscribedforum_back_to_user_profile_user'] = str_ireplace('VAR_USERNAME', $subscribedForum->user_details['display_name'], $LANG['mysubscribedforum_back_to_user_profile_user']);
		$LANG['mysubscribedforum_back_to_user_profile'] = ($CFG['user']['user_id']==$subscribedForum->getFormField('uid'))?$LANG['mysubscribedforum_back_to_user_profile']:$LANG['mysubscribedforum_back_to_user_profile_user'];
		$LANG['mysubscribedforum_mysubscribedforum_title_user_name'] = str_ireplace('VAR_USERNAME', $subscribedForum->user_details['display_name'], $LANG['mysubscribedforum_title_user_name']);
		$LANG['mysubscribedforum_title'] = ($subscribedForum->getFormField('uid')==$CFG['user']['user_id'])?$LANG['mysubscribedforum_title']:$LANG['mysubscribedforum_title_user_name'];
		$back_link = getMemberUrl($subscribedForum->user_details['user_id'], $subscribedForum->user_details['name']);
		$profile_name = $LANG['back_to_profile'];
		$subscribedForum->populateUserDetailsArr['back_link']	=	$back_link;
		$subscribedForum->populateUserDetailsArr['profile_name']	=	$profile_name;
		$subscribedForum->populateUserDetailsArr['profile_tooltip']=$LANG['mysubscribedforum_back_to_user_profile'];
	}
else
	{
		$subscribedForum->setPageBlockShow('block_msg_form_error');
		$subscribedForum->setCommonErrorMsg($LANG['err_tip_invalid_user']);
	}
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
if ($subscribedForum->isShowPageBlock('forum_list_block'))
	{
		$display_username =	$LANG['common_subscribed'];
		$uid = $subscribedForum->getFormField('uid');
		$dname = $subscribedForum->user_details['display_name'];
		$uname = $subscribedForum->user_details['name'];
		if($CFG['user']['user_id'] != $uid)
			{
				$display_username = str_ireplace('VAR_USERNAME', ucwords($dname), $LANG['common_subscribed_others']);
			}
		$subscribedForum->forum_list_block['display_username']	=	$display_username;
		$subscribedForum->forum_list_block['tag_url']	=	getAnovaUrl('mysubscribedtag', '?uid='.$uname, $uname.'/');
		$subscribedForum->forum_list_block['user_url']	=	getAnovaUrl('mysubscribeduser', '?uid='.$uname, $uname.'/');
		$subscribedForum->forum_list_block['forum_url']	=	getForumsUrl('mysubscribedforum', '?uid='.$uname, $uname.'/');
		$subscribedForum->forum_list_block['populateForumList']	=	$subscribedForum->populateForumList();
	}
//include the header file
$subscribedForum->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('mySubscribedForum.tpl');
//includ the footer of the page
?>
<script language="javascript" type="text/javascript">
	var block_arr= new Array('selMsgConfirm');
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$subscribedForum->includeFooter();
?>