<?php
/**
 * View all topic created in a forum title
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Forums topics###
 * @author 		shankar_76ag08
 * @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/'.$CFG['admin']['index']['forum_module'].'/admin/forumsTopics.php';
$CFG['lang']['include_files'][] = 'languages/%s/common/email_notify.inc.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
$CFG['lang']['include_files'][] = 'languages/%s/lists_array/months_list_array.inc.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';

if ($_POST)
		$CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';
//compulsory
$CFG['site']['is_module_page']='forums';
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * File to include the header file, database access file, session management file, help file and necessary functions
 */
require_once($CFG['site']['project_path'].'common/application_top.inc.php');
class ForumsTopicFormHandler extends AnovaHandler
	{
		//Variable to store forum details
		public $forum_details_arr;

		/**
		 * 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 buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}


		/**
		 * To check if the forum id is valid
		 *
		 * @param 		string $table_name forums
		 * @param 		integer $forum_id
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function isValidForumId($forum_id, $err_tip='')
			{
				$sql = 'SELECT forum_id, forum_title, forum_description, forum_status '.
						'FROM '.$this->CFG['db']['tbl']['forums'].' '.
						'WHERE forum_id = '.$this->dbObj->Param($forum_id);

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

				if ($this->forum_details_arr = $rs->FetchRow())
					{
						return true;
				    }
				$this->setCommonErrorMsg($err_tip);
				return false;
			}

		/**
		 * To get forum ids of given topic ids
		 *
		 * @param string $search_value
		 * @access public
		 * @return void
		 **/
		public function getTopicIds($search_value)
			{
				$sql = 'SELECT DISTINCT(topic_id) AS topic_id FROM '.$this->CFG['db']['tbl']['forum_response'].
						' WHERE forum_response LIKE \'%'.addslashes($search_value).'%\'';

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

				$topic_ids = 0;
				while($row = $rs->FetchRow())
					{
						$topic_ids .= ', '.$row['topic_id'];
					}
				return $topic_ids;
			}

		/**
		 * To active all forum titles, forum topics, responses
		 *
		 * @access public
		 * @return void
		 **/
		public function activeForumTopics()
			{
				$topic_ids = $this->fields_arr['topic_ids'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_topics'].' SET topic_status = \'Yes\' '.
						'WHERE topic_id IN ('.$topic_ids.') AND topic_status = \'No\'';

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

				$this->updateForumsTable($topic_ids);
				return true;
			}

		/**
		 * To inactive all forum titles, forum topics, responses
		 *
		 * @access public
		 * @return void
		 **/
		public function inactiveForumTopics()
			{
				$topic_ids = $this->fields_arr['topic_ids'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_topics'].' SET topic_status = \'No\' '.
						'WHERE topic_id IN ('.$topic_ids.') AND topic_status = \'Yes\'';

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

				$this->updateForumsTable($topic_ids);
				return true;

			}

		/**
		 * ForumsTopicFormHandler::publishForumTopics()
		 *
		 * @return
		 */
		public function publishForumTopics()
			{
				$topic_ids = $this->fields_arr['topic_ids'];
				$sql = 'SELECT topic_id, forum_id FROM '.$this->CFG['db']['tbl']['forum_topics'].
						' WHERE topic_id IN('.$topic_ids.')'.
						' AND topic_status = \'ToActivate\'';

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

				while($row = $rs->FetchRow())
					{
						$this->sendPublishedMailAlert($row['forum_id'], $row['topic_id']);
					}

				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_topics'].' SET topic_status = \'Yes\' '.
					'WHERE topic_id IN ('.$topic_ids.') AND topic_status = \'ToActivate\'';

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

				$this->updateForumsTable($topic_ids);
				return true;

			}

		/**
		 * ForumsTopicFormHandler::sendPublishedMailAlert()
		 *
		 * @param mixed $forum_id
		 * @param mixed $topic_id
		 * @return
		 */
		public function sendPublishedMailAlert($forum_id, $topic_id)
			{
				$forum_details = $this->forumRelatedDetails($forum_id);
				$topic_details = $this->forumRelatedDetails('', $topic_id);
				$site_link = "<a target=\"_blank\" href=\"".URL($this->CFG['site']['url'])."\">".URL($this->CFG['site']['url'])."</a>";
				$topic_url = getUrl('forumsresponses', '?title='.$forum_details['forum_seo'].'&amp;topic_title='.$topic_details['topic_seo'], $forum_details['forum_seo'].'/'.$topic_details['topic_seo'].'/', 'root');
				$topic_link = "<a target=\"_blank\" href=\"".$topic_url."\"><strong>".$topic_details['topic']."</strong></a>";
				$topic_subject = $this->getMailContent($this->LANG['forum_topic_publish_subject'], array('VAR_USERNAME'=>$topic_details['topic_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$topic_link, 'VAR_LINK'=>$site_link));
				$topic_content = $this->getMailContent($this->LANG['forum_topic_publish_message'], array('VAR_USERNAME'=>$topic_details['topic_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$topic_link, 'VAR_LINK'=>$site_link));
				$this->_sendMail($topic_details['topic_owneremail'], $topic_subject, $topic_content, $this->CFG['site']['noreply_name'], $this->CFG['site']['noreply_email']);
				$forum_link = "<a target=\"_blank\" href=\"".$topic_url."\"><strong>".$topic_details['topic']."</strong></a>";
				$forum_subject = $this->getMailContent($this->LANG['forum_topic_admin_publish_subject'], array('VAR_USERNAME'=>$forum_details['forum_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$forum_details['forum'], 'VAR_TOPIC'=>$forum_link, 'VAR_LINK'=>$site_link));
				$forum_content = $this->getMailContent($this->LANG['forum_topic_admin_publish_message'], array('VAR_USERNAME'=>$forum_details['forum_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$forum_details['forum'], 'VAR_TOPIC'=>$forum_link, 'VAR_LINK'=>$site_link));
				$this->_sendMail($forum_details['forum_owneremail'], $forum_subject, $forum_content, $this->CFG['site']['noreply_name'], $this->CFG['site']['noreply_email']);
			}
		/**
		 * ForumsTopicFormHandler::_sendMail()
		 *
		 * @param mixed $to_email
		 * @param mixed $subject
		 * @param mixed $body
		 * @param mixed $sender_name
		 * @param mixed $sender_email
		 * @return
		 */
		private function _sendMail($to_email, $subject, $body, $sender_name, $sender_email)
			{
				$this->buildEmailTemplate($subject, $body, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				$from_address = $sender_name.'<'.$sender_email.'>';
				return $EasySwift->send($to_email, $from_address, $this->getEmailSubject());
			}

		/**
		 * To update number of forums topics and responses in forums table
		 *
		 * @access public
		 * @return void
		 **/
		public function updateForumsTable($topic_ids)
			{
				$sql = 'SELECT count(topic_id) AS total_topic FROM '.$this->CFG['db']['tbl']['forum_topics'].
						' WHERE forum_id = '.$this->dbObj->Param($this->fields_arr['forum_id']).
						' AND topic_status = \'Yes\'';

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

				$row = $rs->FetchRow();
				$total_topic = $row['total_topic'];
				$sql = 'SELECT count(response_id) AS total_response FROM '.$this->CFG['db']['tbl']['forum_response'].' AS fr '.
						', '.$this->CFG['db']['tbl']['forum_topics'].' AS ft WHERE '.
						'fr.topic_id = ft.topic_id AND forum_id = '.$this->dbObj->Param($this->fields_arr['forum_id']).
						' AND topic_status = \'Yes\' AND response_status = \'Yes\' ';

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

				$row = $rs->FetchRow();
				$total_response = $row['total_response'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forums'].' SET '.
						'total_topics = '.$this->dbObj->Param($total_topic).
						', total_response = '.$this->dbObj->Param($total_response).' '.
						'WHERE forum_id = '.$this->dbObj->Param($this->fields_arr['forum_id']);

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

				return true;
			}

		/**
		 * To delete all forum titles, forum topics, responses
		 *
		 * @access public
		 * @return void
		 **/
		public function deleteForumTopics()
			{
				$topic_ids = $this->fields_arr['topic_ids'];
				$this->deleteGroupForumTopicsTable($topic_ids); //
				$this->updateForumsTable($topic_ids);
			}

		/**
		 * To delete from group_forum_response table
		 *
		 * @param integer $group_forum_topic_id
		 * @access public
		 * @return void
		 **/
		public function deleteGroupForumResponseTable($topic_ids)
			{
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['forum_response'].' '.
						'WHERE topic_id IN ('.$topic_ids.') ';

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

				return true;
			}

		/**
		 * To delete from group_forum_topics table
		 *
		 * @param integer $group_forum_id
		 * @access public
		 * @return void
		 **/
		public function deleteGroupForumTopicsTable($topic_ids)
			{
				$this->deleteGroupForumResponseTable($topic_ids);
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['forum_topics'].' '.
						'WHERE topic_id IN ('.$topic_ids.') ';

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

				return true;
			}

		/**
		 * To update total_topics, total_response in forums table
		 *
		 * @access public
		 * @return void
		 **/
		public function updateForums($column_name, $affected_rows)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forums'].' SET '.
						''.$column_name.' = '.$column_name.' - '.$affected_rows.' '.
						'WHERE forum_id = '.$this->dbObj->Param($this->fields_arr['forum_id']).' '.
						'AND '.$column_name.' > 0';

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

				return TRUE;
			}

		/**
		 * To display the forums titles
		 *
		 * @access public
		 * @return void
		 **/
		public function showForumTopics()
			{
				if(!$this->isResultsFound())
					{
						return ;
					}
				$showForumTopics_arr = array();
				$inc = 1;
				while($row = $this->fetchResultRecord())
					{
						$showForumTopics_arr[$inc]['record'] = $row;
						$topicUserDetails['user_id'] 			= $row['topic_user_id'];
						$topicUserDetails['name'] 				= $row['topic_user_name'];
						$topicUserDetails['image_path'] 		= $row['topic_user_image_path'];
						$topicUserDetails['gender'] 			= $row['topic_user_gender'];
						$topicUserDetails['img_user_id'] 		= $row['topic_img_user_id'];
						$topicUserDetails['t_width'] 			= $row['topic_t_width'];
						$topicUserDetails['t_height']			= $row['topic_t_height'];
						$topicUserDetails['s_width'] 			= $row['topic_s_width'];
						$topicUserDetails['s_height']			= $row['topic_s_height'];
						$topicUserDetails['photo_server_url']	= $row['topic_photo_server_url'];
						$topicUserDetails['photo_ext']			= $row['topic_photo_ext'];
						$lastPostUserDetails['user_id'] 		= $row['last_user_id'];
						$lastPostUserDetails['name'] 			= $row['last_user_name'];
						$lastPostUserDetails['image_path'] 		= $row['last_user_image_path'];
						$lastPostUserDetails['gender'] 			= $row['last_user_gender'];
						$lastPostUserDetails['img_user_id'] 	= $row['last_img_user_id'];
						$lastPostUserDetails['t_width'] 		= $row['last_t_width'];
						$lastPostUserDetails['t_height']		= $row['last_t_height'];
						$lastPostUserDetails['s_width'] 		= $row['last_s_width'];
						$lastPostUserDetails['s_height']		= $row['last_s_height'];
						$lastPostUserDetails['photo_server_url']= $row['last_photo_server_url'];
						$lastPostUserDetails['photo_ext']		= $row['last_photo_ext'];
						$showForumTopics_arr[$inc]['topicUserDetails'] = $topicUserDetails;
						$showForumTopics_arr[$inc]['lastPostUserDetails'] = $lastPostUserDetails;
						$showForumTopics_arr[$inc]['checkbox_checked'] = '';
						if((is_array($this->fields_arr['topic_ids'])) && (in_array($row['topic_id'], $this->fields_arr['topic_ids'])))
							$showForumTopics_arr[$inc]['checkbox_checked'] = "CHECKED";
						$showForumTopics_arr[$inc]['topicUserDetails_name_manual'] = stripString($topicUserDetails['name'], $this->CFG['username']['short_length']);
						$showForumTopics_arr[$inc]['row_forum_topic_manual'] = wordWrapManual($row['forum_topic'], 50);

						if ($row['rating_count'] && $row['rating_total'])
							{
								$showForumTopics_arr[$inc]['average_rating'] = round($row['rating_total']/$row['rating_count']);
						    }
						$showForumTopics_arr[$inc]['lastPostUserDetails_name_manual'] = stripString($lastPostUserDetails['name'], $this->CFG['username']['short_length']);
						$inc++;
					}
				return $showForumTopics_arr;
			}
	}
//<<<<<<<--------------class ForumsTopicFormHandler---------------//
//--------------------Code begins-------------->>>>>//
$forums = new ForumsTopicFormHandler();
$forums->left_navigation_div = 'forumsForums';
$forums->setPageBlockNames(array('form_forum_title', 'form_show_topics', 'form_search', 'form_confirm'));
$forums->setAllPageBlocksHide();
//default form fields and values...
$forums->setFormField('forum_id', '');
$forums->setFormField('topic_ids', array());
$forums->setFormField('srch', '');
$forums->setFormField('action', '');
$forums->setFormField('srch_topic', '');
$forums->setFormField('srch_response', '');
$forums->setFormField('srch_response_cnt', '');
$forums->setFormField('response_condition', '');
$forums->setFormField('srch_views', '');
$forums->setFormField('views_condition', '');
$forums->setFormField('srch_uname', '');
$forums->setFormField('srch_date_from', '');
$forums->setFormField('srch_date', '');
$forums->setFormField('srch_month', '');
$forums->setFormField('srch_year', '');
$forums->setFormField('srch_date_to', '');
$forums->setFormField('srch_todate', '');
$forums->setFormField('srch_tomonth', '');
$forums->setFormField('srch_toyear', '');
$forums->setFormField('topic_status', '');
$condition_operators = array('equalto', 'greaterthan', 'greaterthanequal', 'lessthan', 'lessthanequal', 'notequal');
$operators_arr = array('equalto' => "=", 'greaterthan' => ">", 'greaterthanequal' => ">=", 'lessthan' => "<", 'lessthanequal' => "<=", 'notequal' => "!=");
$forums->setMonthsListArr($LANG_LIST_ARR['months']);
/***********set form field for navigation****/
$forums->setFormField('orderby_field', 'ft.forum_topic');
$forums->setFormField('orderby', 'DESC');
/*************Start navigation******/
//fetch number of records show in the result--numpg
$condition = '';
$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******/
$forums->sanitizeFormInputs($_REQUEST);
if ($forums->isFormGETed($_REQUEST, 'forum_id'))
	{
		$forums->chkIsNotEmpty('forum_id', $LANG['err_tip_compulsory'])and
			$forums->chkIsNumeric('forum_id', $LANG['err_tip_compulsory'])and
				$forums->isValidForumId($forums->getFormField('forum_id'), $LANG['forums_err_tip_invalid_forum_id']);
	}
else
	{
		//now redirect to forums main page...
		Redirect2URL('forums.php');
	}
if ($forums->isValidFormInputs())
	{
		$forums->setAllPageBlocksHide();
		if ($forums->isFormPOSTed($_POST, 'confirm_action'))
			{
				$forums->chkIsNotEmpty('topic_ids', $LANG['err_tip_compulsory'])or
					$forums->setCommonErrorMsg($LANG['forums_err_tip_select_topics']);
				$forums->chkIsNotEmpty('action', $LANG['err_tip_compulsory'])or
					$forums->setCommonErrorMsg($LANG['err_tip_select_action']);
				if ($forums->isValidFormInputs())
					{
					 	switch($forums->getFormField('action'))
							 {
						 		case 'Delete':
									$forums->deleteForumTopics();
									$forums->setCommonSuccessMsg($LANG['forums_success_delete_message']);
									break;

								case 'Active':
									$forums->activeForumTopics();
									$forums->setCommonSuccessMsg($LANG['forums_success_active_message']);
									break;

								case 'Inactive':
									$forums->inactiveForumTopics();
									$forums->setCommonSuccessMsg($LANG['forums_success_inactive_message']);
									break;

								case 'Publish':
									$forums->publishForumTopics();
									$forums->setCommonSuccessMsg($LANG['forums_success_publish_message']);
									break;
						 	} // switch
						$forums->setFormField('topic_ids', array());
						$forums->setPageBlockShow('block_msg_form_success');
					}
				else //error in form inputs
					{
						$forums->setCommonErrorMsg($LANG['common_msg_error_sorry']);
						$forums->setPageBlockShow('block_msg_form_error');
					}
			}
		if ($forums->isFormPOSTed($_POST, 'forums_cancel'))
			{
				$forums->setFormField('topic_ids', array());
			}
		$forums->getFormField('srch_response_cnt')and
			$forums->chkIsNumeric('srch_response_cnt', $LANG['forums_err_tip_enter_numbers']);
		$forums->getFormField('srch_views')and
			$forums->chkIsNumeric('srch_views', $LANG['forums_err_tip_enter_numbers']);
		if ($forums->getFormField('srch_date') || $forums->getFormField('srch_month') || $forums->getFormField('srch_year'))
			{
				$forums->chkIsCorrectDate($forums->getFormField('srch_date'), $forums->getFormField('srch_month'), $forums->getFormField('srch_year'), 'srch_date_from', $LANG['forums_err_tip_date_empty'], $LANG['forums_err_tip_date_invalid']);
		    }
		if ($forums->getFormField('srch_todate') || $forums->getFormField('srch_tomonth') || $forums->getFormField('srch_toyear'))
			{
				$forums->chkIsCorrectDate($forums->getFormField('srch_todate'), $forums->getFormField('srch_tomonth'), $forums->getFormField('srch_toyear'), 'srch_date_to', $LANG['forums_err_tip_date_empty'], $LANG['forums_err_tip_date_invalid']);
		    }
		if ($forums->getFormField('srch_date_from') && $forums->getFormField('srch_date_to'))
			{
				$from_date = explode('-', $forums->getFormField('srch_date_from'));
				$from_date = mktime(0, 0, 0, $from_date[1]  , $from_date[2], $from_date[0]);
				$to_date = explode('-', $forums->getFormField('srch_date_to'));
				$to_date = mktime(0, 0, 0, $to_date[1]  , $to_date[2], $to_date[0]);
				if ($to_date < $from_date)
					{
						$forums->setCommonErrorMsg($LANG['forums_err_tip_date_invalid']);
						$forums->setPageBlockShow('block_msg_form_error');
				    }
			}
		$search_condition = '';
		$searchArr = array();
		$searchArr[] = 'forum_id';
		$queryStr = '';
		if ($forums->isValidFormInputs())
			{
				if ($forums->getFormField('srch_topic'))
					{
						$search_condition .= ' AND ft.forum_topic like \'%'.$forums->getFormField('srch_topic').'%\'';
						$searchArr[] = 'srch_topic';
						$queryStr .= '&srch_topic='.$forums->getFormField('srch_topic');
				    }
				if ($forums->getFormField('srch_response'))
					{
						$topic_ids = $forums->getTopicIds($forums->getFormField('srch_response'));
						$search_condition .= ' AND ft.topic_id IN ('.$topic_ids.')';
						$searchArr[] = 'srch_response';
						$queryStr .= '&srch_response='.$forums->getFormField('srch_response');
				    }
				if ($forums->getFormField('topic_status'))
					{
						$search_condition .= ' AND ft.topic_status = \''.$forums->getFormField('topic_status').'\'';
						$searchArr[] = 'topic_status';
						$queryStr .= '&topic_status='.$forums->getFormField('topic_status');
				    }
				if ($forums->getFormField('srch_response_cnt'))
					{
						$operator = '=';
						if (in_array($forums->getFormField('response_condition'), $condition_operators))
							$operator = $operators_arr[$forums->getFormField('response_condition')];
						$search_condition .= ' AND ft.total_response '.$operator.' \''.addslashes($forums->getFormField('srch_response_cnt')).'\'';
						$searchArr[] = 'srch_response_cnt';
						$searchArr[] = 'response_condition';
						$queryStr .= '&srch_response_cnt='.$forums->getFormField('srch_response_cnt');
						$queryStr .= '&response_condition='.$forums->getFormField('response_condition');
				    }
				if ($forums->getFormField('srch_views'))
					{
						$operator = '=';
						if (in_array($forums->getFormField('views_condition'), $condition_operators))
							$operator = $operators_arr[$forums->getFormField('views_condition')] ;
						$search_condition .= ' AND ft.total_views '.$operator.' \''.addslashes($forums->getFormField('srch_views')).'\'';
						$searchArr[] = 'srch_response_cnt';
						$searchArr[] = 'views_condition';
						$queryStr .= '&srch_views='.$forums->getFormField('srch_views');
						$queryStr .= '&views_condition='.$forums->getFormField('views_condition');
				    }
				if ($forums->getFormField('srch_uname'))
					{
						$search_condition .= ' AND u.'.$forums->getUserTableField('name').' like \'%'.addslashes($forums->getFormField('srch_uname')).'%\'';
						$searchArr[] = 'srch_uname';
						$queryStr .= '&srch_uname='.$forums->getFormField('srch_uname');
				    }
				if ($forums->getFormField('srch_date_from') && $forums->getFormField('srch_date_to'))
					{
						$search_condition .= ' AND ft.date_added BETWEEN \''.$forums->getFormField('srch_date_from').' 00:00:00\'';
						$search_condition .= ' AND \''.$forums->getFormField('srch_date_to').' 23:59:59\'';
						$searchArr[] = 'srch_date'; $queryStr .= '&srch_date='.$forums->getFormField('srch_date');
						$searchArr[] = 'srch_month'; $queryStr .= '&srch_month='.$forums->getFormField('srch_month');
						$searchArr[] = 'srch_year'; $queryStr .= '&srch_year='.$forums->getFormField('srch_year');
						$searchArr[] = 'srch_todate'; $queryStr .= '&srch_todate='.$forums->getFormField('srch_todate');
						$searchArr[] = 'srch_tomonth'; $queryStr .= '&srch_tomonth='.$forums->getFormField('srch_tomonth');
						$searchArr[] = 'srch_toyear'; $queryStr .= '&srch_toyear='.$forums->getFormField('srch_toyear');
				    }
			}
		else
			{
				$forums->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forums->setPageBlockShow('block_msg_form_error');
			}
		$forums->setFormField('queryStr', $queryStr);

		$return_fields = array('ft.forum_id', 'ft.abuse_count', 'ft.topic_id', 'ft.forum_topic', 'ft.user_id', 'CASE WHEN ft.topic_status = \'Yes\' THEN \''.$LANG['common_display_active'].'\' WHEN ft.topic_status = \'ToActivate\' THEN \''.$LANG['common_unpublished'].'\' ELSE \''.$LANG['common_display_inactive'].'\' END AS topic_status', 'ft.total_response', 'ft.total_views', 'ft.last_post_user_id', 'ft.last_post_date', 'ft.rating_total', 'ft.rating_count', 'DATE_FORMAT(ft.date_added, \''.$CFG['format']['date'].'\') AS date_added');
		if($forums->getUserTableField('user_id'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('user_id').' AS topic_user_id';
				$return_fields[] = 'u.'.$forums->getUserTableField('user_id').' AS topic_img_user_id';
				$return_fields[] = 'u1.'.$forums->getUserTableField('user_id').' AS last_user_id';
				$return_fields[] = 'u1.'.$forums->getUserTableField('user_id').' AS last_img_user_id';
			}
		if($forums->getUserTableField('display_name'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('display_name').' AS topic_display_name';
				$return_fields[] = 'u1.'.$forums->getUserTableField('display_name').' AS last_display_name';
			}
		if($forums->getUserTableField('name'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('name').' AS topic_user_name';
				$return_fields[] = 'u1.'.$forums->getUserTableField('name').' AS last_user_name';
			}
		if($forums->getUserTableField('image_path'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('image_path').' AS topic_user_image_path';
				$return_fields[] = 'u1.'.$forums->getUserTableField('image_path').' AS last_user_image_path';
			}
		if($forums->getUserTableField('gender'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('gender').' AS topic_user_gender';
				$return_fields[] = 'u1.'.$forums->getUserTableField('gender').' AS last_user_gender';
			}
		if($forums->getUserTableField('t_height'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('t_height').' AS topic_t_height';
				$return_fields[] = 'u1.'.$forums->getUserTableField('t_height').' AS last_t_height';
			}
		if($forums->getUserTableField('t_width'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('t_width').' AS topic_t_width';
				$return_fields[] = 'u1.'.$forums->getUserTableField('t_width').' AS last_t_width';
			}
		if($forums->getUserTableField('s_height'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('s_height').' AS topic_s_height';
				$return_fields[] = 'u1.'.$forums->getUserTableField('s_height').' AS last_s_height';
			}
		if($forums->getUserTableField('s_width'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('s_width').' AS topic_s_width';
				$return_fields[] = 'u1.'.$forums->getUserTableField('s_width').' AS last_s_width';
			}
		if($forums->getUserTableField('photo_server_url'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('photo_server_url').' AS topic_photo_server_url';
				$return_fields[] = 'u1.'.$forums->getUserTableField('photo_server_url').' AS last_photo_server_url';
			}
		if($forums->getUserTableField('photo_ext'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('photo_ext').' AS topic_photo_ext';
				$return_fields[] = 'u1.'.$forums->getUserTableField('photo_ext').' AS last_photo_ext';
			}

		/*************Start navigation******/
		//Set tables and fields to return
		$forums->setTableNames(array($CFG['db']['tbl']['forum_topics'].' as ft LEFT JOIN '.$CFG['db']['tbl']['users'].' AS u1 ON ft.last_post_user_id = u1.'.$forums->getUserTableField('user_id'), $CFG['db']['tbl']['users'].' as u'));
		$forums->setReturnColumns($return_fields);

		//Condition of the query
		$condition = 'ft.user_id = u.'.$forums->getUserTableField('user_id').' AND u.'.$forums->getUserTableField('usr_status').' = \'Ok\' AND ft.forum_id = \''.$forums->getFormField('forum_id').'\'';
		if ($search_condition)
			{
				$condition .= $search_condition;
		    }
		$forums->buildSelectQuery();
		$forums->buildConditionQuery($condition);
		$responseClass = $viewClass = $defaultClass = '';
		if ($forums->getFormField('srch') == 'mostresponse')
			{
				$forums->setFormField('orderby_field', 'ft.total_response');
				$forums->setFormField('orderby', 'DESC');
				$searchArr[] = 'srch';
				$responseClass = 'class="clsActivePhotoSubLink"';
			}
		elseif ($forums->getFormField('srch') == 'mostviews')
			{
				$forums->setFormField('orderby_field', 'ft.total_views');
				$forums->setFormField('orderby', 'DESC');
				$searchArr[] = 'srch';
				$viewClass = 'class="clsActivePhotoSubLink"';
			}
		else
			{
				$defaultClass = 'class="clsActivePhotoSubLink"';
			}
		$forums->buildSortQuery();
		$forums->buildQuery();
		$forums->executeQuery();
		/*************End navigation******/
		$forums->setPageBlockShow('form_forum_title');
		$forums->setPageBlockShow('form_show_topics');
		$forums->setPageBlockShow('form_search');
	}
else
	{
		$forums->setPageBlockShow('block_msg_form_error');
		Redirect2URL('forums.php');
	}
//<<<<--------------------Code Ends----------------------//
$forums->current_year = date('Y');
$forums->hidden_arr = array('srch', 'start', 'forum_id', 'srch_topic', 'srch_response', 'srch_response_cnt', 'response_condition', 'srch_views', 'views_condition', 'srch_uname', 'srch_date', 'srch_month', 'srch_year', 'srch_todate', 'srch_tomonth', 'srch_toyear', 'topic_status', 'orderby_field', 'orderby');
$forums->populateConditionOperators_arr = array("equalto" => $LANG['topicsearch_equal_to'], "greaterthan" => $LANG['topicsearch_greater_than'],
											"greaterthanequal" => $LANG['topicsearch_greater_than_equal'], "lessthan" => $LANG['topicsearch_less_than'],
											"lessthanequal" => $LANG['topicsearch_less_than_equal'], "notequal" => $LANG['topicsearch_not_equal']);
if ($forums->isShowPageBlock('form_show_topics'))
	{
		$forums->form_show_topics['showForumTopics_arr'] = $forums->showForumTopics();
		if($forums->isResultsFound())
			{
				$forums->form_show_topics['anchor'] = 'dAltMlti';
				$forums->form_show_topics['hidden_arr'] = array('srch', 'start', 'forum_id', 'srch_topic', 'srch_response', 'srch_response_cnt', 'response_condition', 'srch_views', 'views_condition', 'srch_uname', 'srch_date', 'srch_month', 'srch_year', 'srch_todate', 'srch_tomonth', 'srch_toyear', 'topic_status', 'orderby_field', 'orderby');
		        $forums->form_show_topics['action_arr'] = array("Active" => $LANG['common_display_active'],
		                                						"Inactive" => $LANG['common_display_inactive'], "Delete" => $LANG['common_delete'],
		                                						"Publish" => $LANG['common_publish']);
				$smartyObj->assign('smarty_paging_list', $forums->populatePageLinksPOST($forums->getFormField('start'), 'selFormShowTopics'));
			}
	}
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$forums->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('forumsTopics.tpl');
?>
<script language="javascript" type="text/javascript">
	var block_arr= new Array('selMsgConfirm');
	var replace_url = '<?php echo $CFG['site']['url'];?>login.php';
	var please_select_action = '<?php echo $LANG['err_tip_select_action'];?>';
	var confirm_message = '';
	function getAction()
		{
			var act_value = document.selFormShowTopics.action.value;
			if(act_value)
				{
					switch (act_value)
						{
							case 'Delete':
								confirm_message = '<?php echo $LANG['forums_delete_confirm_message'];?>';
								break;
							case 'Active':
								confirm_message = '<?php echo $LANG['forums_active_confirm_message'];?>';
								break;
							case 'Inactive':
								confirm_message = '<?php echo $LANG['forums_inactive_confirm_message'];?>';
								break;
							case 'Publish':
								confirm_message = '<?php echo $LANG['forums_publish_confirm_message'];?>';
								break;
						}
					$('confirmMessage').innerHTML = confirm_message;
					document.msgConfirmform.action.value = act_value;
					Confirmation('selMsgConfirm', 'msgConfirmform', Array('topic_ids'), Array(multiCheckValue), Array('value'),'selFormShowTopics');
				}
				else
					alert_manual(please_select_action, 'dAltMlti', -25, -290);
		}
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$forums->includeFooter();
?>