<?php
/**
 * View all responses created in a group forum topic
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Forums Response###
 * @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/forumsResponses.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'][] = 'common/configs/anova_configs/config_forums.inc.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';
$CFG['mods']['include_files'][] = 'common/classes/class_ForumHandler.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 ForumResponseFormHandler--------------->>>//
class ForumResponseFormHandler extends ForumHandler
	{
		//Variable to store group forum details
		public $forum_details_arr;

		//Variable to store response details
		public $forum_topic_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 checkSortQuery($field, $sort='asc')
			{
				if(!($this->sql_sort))
				{
					$this->sql_sort = $field . ' ' . $sort;
				}
			}

		/**
		 * 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, seo_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 check if the forum topic id is valid
		 *
		 * @param 		string $forums_topics_table groups_forums_topics table
		 * @param 		integer $forum_id
		 * @param 		interger $topic_id
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function isValidForumTopicId($forum_id, $topic_id, $err_tip='')
			{
				$sql = 'SELECT g.topic_id, g.forum_id, g.forum_topic, g.user_id, DATE_FORMAT(g.date_added, \''.$this->CFG['format']['date'].'\') as date_added'.
						', '.$this->getUserTableFields(array('name', 'display_name', 'image_path', 'gender')).
						' FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS g, '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE g.user_id = u.'.$this->getUserTableField('user_id').
						' AND topic_id = '.$this->dbObj->Param($topic_id).
						' AND forum_id = '.$this->dbObj->Param($forum_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($topic_id, $forum_id));
				if (!$rs)
					trigger_db_error($this->dbObj);

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

		/**
		* To check if the group response id is valid
		 *
		 * @param 		string $forums_response_table forums_response table
		 * @param 		integer $topic_id
		 * @param 		interger $response_id
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function isValidForumResponseId($forums_response_table, $topic_id, $response_id, $err_tip='')
			{
				$sql = 'SELECT response_id, topic_id, forum_response, response_user_id, response_status'.
						' FROM '.$this->CFG['db']['tbl']['forum_response'].
						' WHERE topic_id = '.$this->dbObj->Param($topic_id).
						' AND response_id = '.$this->dbObj->Param($response_id);

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

				if ($row = $rs->FetchRow())
					{
						$this->setFormField('forum_response', $row['forum_response']);
						$this->setFormField('response_status', $row['response_status']);
						return true;
				    }
				$this->setCommonErrorMsg($err_tip);
				return false;
			}

		/**
		 * To active the response id
		 *
		 * @param integer $group_forum_id
		 * @param integer $group_forum_topic_id
		 * @param integer $response_id
		 * @access public
		 * @return void
		 **/
		public function activeResponse()
			{
				$response_ids = $this->fields_arr['response_ids'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
						' SET response_status = \'Yes\''.
						' WHERE response_id IN ('.$response_ids.') AND response_status = \'No\'';

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

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

		/**
		 * To inactive the response id
		 *
		 * @param integer $group_forum_id
		 * @param integer $group_forum_topic_id
		 * @param integer $response_id
		 * @access public
		 * @return void
		 **/
		public function inactiveResponse()
			{
				$response_ids =$this->fields_arr['response_ids'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
						' SET response_status = \'No\''.
						' WHERE response_id IN ('.$response_ids.') AND response_status = \'Yes\'';

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

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

		/**
		 * ForumResponseFormHandler::publishResponse()
		 *
		 * @return
		 */
		public function publishResponse()
			{
				$response_ids =$this->fields_arr['response_ids'];

				$sql = 'SELECT response_id, topic_id FROM '.$this->CFG['db']['tbl']['forum_response'].
						' WHERE response_id IN('.$response_ids.')'.
						' AND response_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['topic_id'], $row['response_id']);
					}
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
						' SET response_status = \'Yes\''.
						' WHERE response_id IN ('.$response_ids.') AND response_status = \'ToActivate\'';

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

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

		/**
		 * ForumResponseFormHandler::sendPublishedMailAlert()
		 *
		 * @param mixed $topic_id
		 * @param mixed $response_id
		 * @return
		 */
		public function sendPublishedMailAlert($topic_id, $response_id)
			{
				$response_details = $this->forumRelatedDetails('','',$response_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='.$this->forum_details_arr['seo_title'].'&amp;topic_title='.$topic_details['topic_seo'], $this->forum_details_arr['seo_title'].'/'.$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_response_admin_publish_subject'], array('VAR_USERNAME'=>$topic_details['topic_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$topic_link, 'VAR_DESCRIPTION_OF_FORUM'=>$response_details['response'], 'VAR_LINK'=>$site_link));
				$topic_content = $this->getMailContent($this->LANG['forum_response_admin_publish_message'], array('VAR_USERNAME'=>$topic_details['topic_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$topic_link, 'VAR_DESCRIPTION_OF_FORUM'=>$response_details['response'], 'VAR_LINK'=>$site_link));
				$this->_sendMail($topic_details['topic_owneremail'], $topic_subject, $topic_content, $this->CFG['site']['noreply_name'], $this->CFG['site']['noreply_email']);
				$response_link = "<a target=\"_blank\" href=\"".$topic_url."\"><strong>".$topic_details['topic']."</strong></a>";
				$response_subject = $this->getMailContent($this->LANG['forum_response_publish_subject'], array('VAR_USERNAME'=>$response_details['response_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$response_link, 'VAR_DESCRIPTION_OF_FORUM'=>$response_details['response'], 'VAR_LINK'=>$site_link));
				$response_content = $this->getMailContent($this->LANG['forum_response_publish_message'], array('VAR_USERNAME'=>$response_details['response_ownername'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_FORUM'=>$response_link, 'VAR_DESCRIPTION_OF_FORUM'=>$response_details['response'], 'VAR_LINK'=>$site_link));
				$this->_sendMail($response_details['response_owneremail'], $response_subject, $response_content, $this->CFG['site']['noreply_name'], $this->CFG['site']['noreply_email']);
			}
		/**
		 * ForumResponseFormHandler::_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 delete the give response id
		 *
		 * @param 		integer $forum_id
		 * @param 		integer $topic_id
		 * @param 		interger $response_id
		 * @access public
		 * @return void
		 **/
		public function deleteResponse()
			{
				$response_ids = $this->fields_arr['response_ids'];
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['forum_response'].
						' WHERE response_id IN ('.$response_ids.') ';

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

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

		/**
		 * To update the total_response in forum_topic table
		 *
		 * @param 		integer $topic_id
		 * @access public
		 * @return void
		 **/
		public function updateTotalResponse()
			{
				$sql = 'SELECT count(response_id) AS total_response FROM '.$this->CFG['db']['tbl']['forum_response'].' AS fr'.
						' WHERE topic_id = '.$this->dbObj->Param($this->fields_arr['topic_id']).
						' AND fr.response_status = \'Yes\'';

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

				$row = $rs->FetchRow();
				$total_response = $row['total_response'];
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_topics'].
						' SET total_response = '.$this->dbObj->Param($total_response).
						' WHERE topic_id = '.$this->dbObj->Param($this->fields_arr['topic_id']);

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

				return true;
			}

		/**
		 * To update number of forums topics and responses in forums table
		 *
		 * @access public
		 * @return void
		 **/
		public function updateForumsTable()
			{
				$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 fr.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 update total_response count in forums table
		 *
		 * @param 		integer $forum_id
		 * @access public
		 * @return void
		 **/
		public function updateTotalResponseForums($forum_id)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forums'].
						' SET total_response = total_response - 1'.
						' WHERE forum_id = '.$this->dbObj->Param($forum_id).
						' AND total_response > 0';

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

				return true;
			}

		/**
		 * To display the forums topic responses
		 *
		 * @access public
		 * @return void
		 **/
		public function showForumResponse()
			{
				if(!$this->isResultsFound())
					{
						return ;
					}
				$showForumResponse_arr = array();
				$inc = 1;
				while($row = $this->fetchResultRecord())
					{
						$userDetails = $row;
						$userDetails['user_id'] = $row['response_user_id'];
						$showForumResponse_arr[$inc]['record'] = $row;
						$showForumResponse_arr[$inc]['userDetails'] = $userDetails;
						$showForumResponse_arr[$inc]['checkbox_checked'] = '';
						if((is_array($this->fields_arr['response_ids'])) && (in_array($row['response_id'], $this->fields_arr['response_ids'])))
							$showForumResponse_arr[$inc]['checkbox_checked'] = "CHECKED";
						$showForumResponse_arr[$inc]['userdetails_name_manual'] = stripString($userDetails['name'], $this->CFG['username']['short_length']);
						$row['forum_response'] = str_replace('&lt;a href=&quot;', '&lt;a target=&quot;_blank&quot; href=&quot;', $row['forum_response']);
						$showForumResponse_arr[$inc]['row_forum_response_manual'] = nl2br(wordWrapManual($row['forum_response'], 50));

						$showForumResponse_arr[$inc]['video_id'] = $row['video_id'];
						$showForumResponse_arr[$inc]['ans_view_video']['url'] = getUrl('ansviewvideo', '?pg=Forumresponse_'.$row['video_id'], '?pg=Forumresponse_'.$row['video_id']);
						$showForumResponse_arr[$inc]['ans_view_video']['on_click'] = 'blog_video_'.$row['video_id'];

						$showForumResponse_arr[$inc]['audio_id'] = $row['audio_id'];
						$showForumResponse_arr[$inc]['ans_view_audio']['url'] = getUrl('ansviewaudio', '?pg=Forumresponse_'.$row['audio_id'], '?pg=Forumresponse_'.$row['audio_id']);
						$showForumResponse_arr[$inc]['ans_view_audio']['on_click'] = 'blog_audio_'.$row['audio_id'];

						$showForumResponse_arr[$inc]['external_video_id'] = $row['external_video_id'];
						$showForumResponse_arr[$inc]['ext_ans_view_video']['url'] = getUrl('ansviewvideo', '?pg=Forumresponse_'.$row['external_video_id'], '?pg=Forumresponse_'.$row['external_video_id']);
						$showForumResponse_arr[$inc]['ext_ans_view_video']['on_click'] = 'external_blog_video_'.$row['external_video_id'];

						$showForumResponse_arr[$inc]['external_audio_id'] = $row['external_audio_id'];
						$showForumResponse_arr[$inc]['ext_ans_view_audio']['url'] = getUrl('ansviewaudio', '?pg=Forumresponse_'.$row['external_audio_id'], '?pg=Forumresponse_'.$row['external_audio_id']);
						$showForumResponse_arr[$inc]['ext_ans_view_audio']['on_click'] = 'external_blog_audio_'.$row['external_audio_id'];

						$inc++;
					}
				return $showForumResponse_arr;
			}

		/**
		 * To update the forum_topic table
		 *
		 * @param string $topic_id
		 * @access public
		 * @return integer
		 **/
		public function updateGroupForumTopic($topic_id)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_topics'].
						' SET total_views = total_views + 1'.
						' WHERE topic_id = '.$this->dbObj->Param($topic_id);

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

				return true;
			}

		/**
		 * ForumResponseFormHandler::printQuery()
		 *
		 * @return
		 */
		public function printQuery()
			{
				print 'Query:<br>'.$this->sql;
			}
	}
//<<<<<<<--------------class ForumResponseFormHandler---------------//
//--------------------Code begins-------------->>>>>//
$forums = new ForumResponseFormHandler();
$forums->left_navigation_div = 'forumsForums';
$forums->setPageBlockNames(array('form_title', 'form_forum_title', 'form_search', 'form_show_responses', 'form_confirm'));
$forums->setAllPageBlocksHide();
//default form fields and values...
$forums->setFormField('id', '');
$forums->setFormField('forum_id', '');
$forums->setFormField('topic_id', '');
$forums->setFormField('response_id', '');
$forums->setFormField('response_ids', '');
$forums->setFormField('srch_response', '');
$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('action', '');
$forums->setFormField('queryStr', '');
$condition_operators = array('equalto', 'greaterthan', 'greaterthanequal', 'lessthan', 'lessthanequal', 'notequal');
$operators_arr = array('equalto' => "=", 'greaterthan' => ">", 'greaterthanequal' => ">=", 'lessthan' => "<", 'lessthanequal' => "<=", 'notequal' => "!=");
$forums->setMonthsListArr($LANG_LIST_ARR['months']);
/*************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->isFormGETed($_REQUEST, 'topic_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'])and
					$forums->chkIsNotEmpty('topic_id', $LANG['err_tip_compulsory'])and
						$forums->chkIsNumeric('topic_id', $LANG['err_tip_compulsory'])and
							$forums->isValidForumTopicId($forums->getFormField('forum_id'), $forums->getFormField('topic_id'), $LANG['forums_err_tip_invalid_forum_topic_id']);
	}
else
	{
		Redirect2URL('forums.php');
	}
if ($forums->isValidFormInputs())
	{
		if ($forums->isFormPOSTed($_POST, 'forums_confirm'))
			{
				$forums->chkIsNotEmpty('response_ids', $LANG['err_tip_compulsory'])or
					$forums->setCommonErrorMsg($LANG['forums_err_tip_select_responses']);
				$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->deleteResponse();
								$forums->setCommonSuccessMsg($LANG['forums_success_delete_message']);
								break;

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

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

							case 'Publish':
								$forums->publishResponse();
								$forums->setCommonSuccessMsg($LANG['forums_success_published_message']);
								break;
					 	} // switch
						$forums->setFormField('response_ids', array());
						$forums->setPageBlockShow('block_msg_form_success');
					}
				else
					{
						$forums->setCommonErrorMsg($LANG['common_msg_error_sorry']);
						$forums->setPageBlockShow('block_msg_form_error');
					}
			}
		if ($forums->isFormPOSTed($_POST, 'forums_cancel'))
			{
				$forums->setFormField('response_ids', array());
			}
		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_enter_correct_dates']);
						$forums->setPageBlockShow('block_msg_form_error');
				    }
			}
		$search_condition = '';
		$searchArr = array();
		$searchArr[] = 'forum_id';
		$searchArr[] = 'topic_id';
		$queryStr = '';
		if ($forums->isValidFormInputs())
			{
				if ($forums->getFormField('srch_response'))
					{
						$search_condition .= ' gr.forum_response LIKE \'%'.addslashes($forums->getFormField('srch_response')).'%\' AND ';
						$searchArr[] = 'srch_response';
						$queryStr .= '&srch_response='.$forums->getFormField('srch_response');
				    }
				if ($forums->getFormField('srch_uname'))
					{
						$search_condition .= 'u.'.$forums->getUserTableField('display_name').' like \'%'.addslashes($forums->getFormField('srch_uname')).'%\' AND ';
						$searchArr[] = 'srch_uname';
						$queryStr .= '&srch_uname='.$forums->getFormField('srch_uname');
				    }
				if ($forums->getFormField('srch_date_from') && $forums->getFormField('srch_date_to'))
					{
						$search_condition .= ' gr.date_added BETWEEN \''.$forums->getFormField('srch_date_from').' 00:00:00\' AND ';
						$search_condition .= ' \''.$forums->getFormField('srch_date_to').' 23:59:59\' AND ';

						$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');
				    }
				$forums->setFormField('queryStr', $queryStr);
			}
		else
			{
				$forums->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forums->setPageBlockShow('block_msg_form_error');
			}
		$forums->updateGroupForumTopic($forums->getFormField('topic_id'));

		/*************Start navigation******/
		//Set tables and fields to return
		$forums->setTableNames(array($CFG['db']['tbl']['forum_response'].' as gr', $CFG['db']['tbl']['users'].' as u'));


		$return_fields = array('gr.response_id', 'gr.topic_id', 'gr.forum_response', 'gr.response_status', 'gr.response_user_id', 'gr.video_id', 'gr.audio_id', 'gr.external_video_id', 'gr.external_audio_id', 'DATE_FORMAT(gr.date_added, \''.$CFG['format']['date'].'\') as date_added');
		if($forums->getUserTableField('name'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('name').' AS name';
			}
		if($forums->getUserTableField('display_name'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('display_name').' AS display_name';
			}
		if($forums->getUserTableField('image_path'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('image_path').' AS image_path';
			}
		if($forums->getUserTableField('gender'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('gender').' AS gender';
			}
		if($forums->getUserTableField('user_id'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('user_id').' AS img_user_id';
			}
		if($forums->getUserTableField('t_height'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('t_height').' AS t_height';
			}
		if($forums->getUserTableField('t_width'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('t_width').' AS t_width';
			}
		if($forums->getUserTableField('s_height'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('s_height').' AS s_height';
			}
		if($forums->getUserTableField('s_width'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('s_width').' AS s_width';
			}
		if($forums->getUserTableField('photo_server_url'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('photo_server_url').' AS photo_server_url';
			}
		if($forums->getUserTableField('photo_ext'))
			{
				$return_fields[] = 'u.'.$forums->getUserTableField('photo_ext').' AS photo_ext';
			}

		$forums->setReturnColumns($return_fields);

		//Condition of the query
		$search_condition .= 'gr.response_user_id = u.'.$forums->getUserTableField('user_id').' AND gr.topic_id = \''.$forums->getFormField('topic_id').'\' ';
		if ($CFG['admin']['ignore_user'])
			{
				$search_condition .= ' AND  NOT EXISTS (SELECT 1 FROM '.$CFG['db']['tbl']['user_ignored'].' AS ui WHERE ui.user_id='.$CFG['user']['user_id'].' AND ui.ignored_id=gr.response_user_id)';
			}
		$forums->buildSelectQuery();
		$forums->buildConditionQuery($search_condition);
		$forums->buildSortQuery();
		$forums->checkSortQuery('gr.date_added', 'DESC');
		$forums->buildQuery();
		#$forums->printQuery();
		$forums->executeQuery();
		/*************End navigation******/
		$forums->setPageBlockShow('form_forum_title');
		$forums->setPageBlockShow('form_show_responses'); //default page block. show it. All others hidden
		$forums->setPageBlockShow('form_search');
		if ($forums->getFormField('response_id'))
			{
				$forums->setPageBlockShow('form_confirm');
			}
	}
else
	{
		$forums->setCommonErrorMsg($LANG['common_msg_error_sorry']);
		$forums->setPageBlockShow('block_msg_form_error');
		//now redirect to forums main page...
		Redirect2URL('forums.php');
	}
//<<<<--------------------Code Ends----------------------//
$forums->forum_topic_manual = wordWrapManual($forums->forum_topic_details_arr['forum_topic'], 50);
$forums->hidden_arr = array('start', 'forum_id', 'topic_id', 'response_id', 'srch_response', 'srch_uname', 'srch_date', 'srch_month', 'srch_year', 'srch_todate', 'srch_tomonth', 'srch_toyear');
$forums->current_year = date('Y');
if ($forums->isShowPageBlock('form_show_responses'))
	{
		$forums->form_show_responses['showForumResponse_arr'] = $forums->showForumResponse();
		if($forums->isResultsFound())
			{
				$forums->form_show_responses['anchor'] = 'dAltMlti';
				$forums->form_show_responses['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->populatePageLinksGET($forums->getFormField('start'), $searchArr));

			}
		$forums->form_show_responses['hidden_arr'] = array('forum_id', 'topic_id', 'response_id', 'srch_response', 'srch_uname', 'srch_date', 'srch_month', 'srch_year', 'srch_todate', 'srch_tomonth', 'srch_toyear');
	}
//--------------------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('forumsResponses.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.selFormShowResponces.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('response_ids'), Array(multiCheckValue), Array('value'),'selFormShowResponces');
				}
				else
					alert_manual(please_select_action);
		}
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$forums->includeFooter();
?>