<?php
//--------------class ForumsFormHandler--------------->>>//
/**
 * @category	Anova
 * @package		ForumsFormHandler
 * @author 		shankar_76ag08
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		Release: @package_version@
 * @since 		2008-12-22
 **/
class ForumsFormHandler extends AnovaHandler
	{
		/**
		 * set the condition
		 *
		 * @access public
		 * @return void
		 **/
		public function buildConditionQuery($condition='')
			{
				$this->sql_condition = $condition;
			}

		/**
		 * To sort the query
		 *
		 * @access public
		 * @return void
		 **/
		public function checkSortQuery($field, $sort='asc')
			{
				if(!($this->sql_sort))
					{
						$this->sql_sort = $field . ' ' . $sort;
					}
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getUnpublishedTopics($forum_id)
			{
				$sql = 'SELECT count(topic_id) as total_unpublished_topics '.
						'FROM '.$this->CFG['db']['tbl']['forum_topics'].' '.
						'WHERE forum_id = '.$this->dbObj->Param($forum_id).' '.
						'AND topic_status = \'ToActivate\'';

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

		/**
		 * To display the forums titles
		 *
		 * @access public
		 * @return void
		 **/
		public function showForumTitles()
			{
				$showForumTitles_arr = array();
				$inc = 1;
				while($row = $this->fetchResultRecord())
					{
						$showForumTitles_arr[$inc]['record'] = $row;
						$userDetails = $row;
						$userDetails['user_id'] = $row['last_post_user_id'];
						$showForumTitles_arr[$inc]['record']['userDetails'] = $userDetails;
						$showForumTitles_arr[$inc]['forumTopics']['url'] = getForumsUrl('forumstopics', '?title='.$row['seo_title'], $row['seo_title'].'/');
						$showForumTitles_arr[$inc]['forumTopics']['title'] = wordWrapManual($row['forum_title'], 15);
						$showForumTitles_arr[$inc]['forum_description_manual'] = nl2br(wordWrapManual($row['forum_description'], 15));
						if($row['forum_userid'] == $this->CFG['user']['user_id'] and ($this->fields_arr['my']))
							{
								$publish_details = $this->getUnpublishedTopics($row['forum_id']);
								$showForumTitles_arr[$inc]['forums_unpublished_topics']['content'] = $this->LANG['forums_unpublished_topics'].'<a href="'.getForumsUrl('forumstopics', '?title='.$row['seo_title'].'&pbl=1', $row['seo_title'].'/?pbl=1').'">'.$publish_details['total_unpublished_topics'].'</a>';
							}
						$showForumTitles_arr[$inc]['myanswers']['url'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);
						$showForumTitles_arr[$inc]['display_name'] = stripString($userDetails['display_name'], $this->CFG['username']['short_length']);
						$inc++;
					}
					return $showForumTitles_arr;

			}

	}
//<<<<<<<--------------class ForumsFormHandler---------------//

//--------------------Code begins-------------->>>>>//
$forums = new ForumsFormHandler();
if(!chkIsAllowedModule(array('forums')))
	Redirect2URL(getUrl($CFG['redirect']['dsabled_module_url']['file_name']));
$forums->setPageBlockNames(array('form_show_forums', 'form_confirm'));

$forums->setAllPageBlocksHide();

//default form fields and values...
$forums->setFormField('my', '');
$forums->setFormField('numpg',0);
$forums->setFormField('start',0);
$forums->setFormField('prev_numpg',0);//To retain the last numpg
/***********set form field for navigation****/
$forums->setFormField('asc', '');
$forums->setFormField('dsc', 'f.date_added');
/*************Start navigation******/
//fetch number of records show in the result--numpg
$condition = $forums->alter_link = '';
$forums->LANG['forums_pagetitle'] = $LANG['forums'];
if(isMember())
	{
		$forums->alter_link = '<a href="'.getForumsUrl('forums', '?my=1', '?my=1', 'members').'">'.$LANG['forums_mytitles'].'</a>';
	}

$forums->numpg = $CFG['data_tbl']['numpg'];
$forums->setFormField('start', 0);
$forums->setFormField('numpg', $CFG['data_tbl']['numpg']);

$forums->setMinRecordSelectLimit($CFG['data_tbl']['min_record_select_limit']);
$forums->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$forums->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
//Set tables and fields to return
$forums->setTableNames(array());
$forums->setReturnColumns(array());
/*************End navigation******/

/*************Start navigation******/
$forums->setTableNames(array($CFG['db']['tbl']['forums'].' as f LEFT JOIN '.$CFG['db']['tbl']['users'].' AS u on f.last_post_user_id = u.'.$CFG['users']['user_id']));
$forums->setReturnColumns(array('f.forum_id', 'f.forum_title', 'f.forum_userid', 'f.seo_title', 'f.forum_description', 'f.forum_status', 'f.date_added', 'f.total_topics', 'f.total_response', 'f.last_post_date', 'f.last_post_user_id', 'f.last_post_user_id AS img_user_id', $forums->getUserTableFields(array('name', 'image_path', 'gender', 'display_name', 't_height', 't_width','s_height', 's_width','photo_server_url', 'photo_ext'))));
//Condition of the query
$condition = 'forum_status = \'Yes\'';
$forums->displaytabs['allForumsLI'] = $forums->displaytabs['myForumsLI'] = $forums->displaytabs['postForumsLI'] = '';
$clsLeftActiveQuestionsLink = 'clsLeftActiveQuestionsLink';
$forums->myforums_link = getForumsUrl('forums', '?my=1', '?my=1', 'members');
$forums->allforums_link = getForumsUrl('forums');
if ($forums->isFormPOSTed($_GET, 'my') and !isMember())
	{
		$_SESSION['url'] = $forums->myforums_link;
		$_SESSION['login_error'] = 'login_err_authorization_required';
		Redirect2URL(getUrl('login'));
	}
elseif ($forums->isFormPOSTed($_GET, 'my') and isMember())
	{
		$condition .= ' AND forum_userid ='.$CFG['user']['user_id'];
		$forums->LANG['forums_pagetitle'] .= ' - '.$LANG['forums_mytitles'];
		$forums->LANG['forums_pagetitle'] = $LANG['forums_mytitles'];
		$forums->alter_link = '<a href="'.getForumsUrl('forums', '', '', 'members').'">'.$LANG['forums_alltitles'].'</a>';
		$forums->displaytabs['myForumsLI'] = $clsLeftActiveQuestionsLink;
	}
else {
	$forums->displaytabs['allForumsLI'] = 'clsFirstQuestionLink '.$clsLeftActiveQuestionsLink;
}
if($CFG['admin']['forums']['order'] == 'ASC')
	{
		$forums->setFormField('asc', 'f.date_added');
	}

$forums->sanitizeFormInputs($_REQUEST);
$forums->buildSelectQuery();
$forums->buildConditionQuery($condition);
$forums->buildSortQuery();
$forums->checkSortQuery('f.forum_id', 'DESC');
$forums->buildQuery();
$forums->executeQuery();
/*************End navigation******/
$forums->setPageBlockShow('form_show_forums'); //default page block. show it. All others hidden
$forums->forumsAddTitle_url = getForumsUrl('forumsaddtitle', '', '', 'members');
$forums->login_url = getUrl('login', '?light_url='.$forums->forumsAddTitle_url, '?light_url='.$forums->forumsAddTitle_url, 'root');
$forums->login_myforums_url = getUrl('login', '?light_url='.$forums->myforums_link, '?light_url='.$forums->myforums_link, 'root');
if ($forums->isFormPOSTed($_GET, 'msg'))
	{
		if($CFG['admin']['forums']['auto_publish']['allowed'])
			$forums->setCommonSuccessMsg($LANG['forums_title_created_successfully']);
		else
			$forums->setCommonSuccessMsg($LANG['forums_title_waiting_for_approval']);
		$forums->setPageBlockShow('block_msg_form_success');
	}
//<<<<--------------------Code Ends----------------------//
$CFG['site']['title'] = $forums->LANG['forums_pagetitle'].' - '.$CFG['site']['title'];
//--------------------Page block templates begins-------------------->>>>>//
if ($forums->isShowPageBlock('form_show_forums'))
	{
		$forums->form_show_forums_arr = $forums->showForumTitles();
		$smartyObj->assign('smarty_paging_list', $forums->populatePageLinksGET($forums->getFormField('start')));
	}

//include the header file
$forums->includeHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('forums.tpl');
//<<<<<<--------------------Page block templates Ends--------------------//
//include the footer of the page
$forums->includeFooter();
?>
