<?php
/**
 * File to create response in forum title
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Forums Response create###
 * @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/forumsResponseCreate.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
if ($CFG['admin']['module']['video_audio_blogsforum'])
	{
		$CFG['mods']['include_files'][] = 'common/classes/class_GrapUrl.lib.php';
		$CFG['mods']['include_files'][] = 'common/classes/class_Videopian.lib.php';
	}

$CFG['mods']['include_files'][] = 'common/classes/class_ftp.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_Image.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnsVideoUpload.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ForumHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_inputfilter_clean.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');
if($CFG['feature']['html_editor'] == 'fckeditor')
require($CFG['site']['project_path'].'fckeditor/fckEditorIntegrationAdmin.php');
//-------------- Class ForumResponseFormHandler begins --------------->>>>>//
class ForumResponseFormHandler extends ForumHandler
	{
		//Variable to store group forum details
		public $forum_details_arr;
		public $_dir_path = '../../';

		//variable to store group forum topic details
		public $forum_topic_details_arr;

		/**
		 * To check the length of given url
		 *
		 * @param 		string $field_name form field name
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return Boolean
		 **/
		public function chkLength($field_name, $err_tip='')
			{
				$url_len = strlen($this->fields_arr[$field_name]);
				if ($url_len < 3 || $url_len > 20)
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * 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 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 create a group
		 *
		 * @access public
		 * @return void
		 **/
		public function createForumResponse()
			{
				if(chkVideoModuleAllowed('forumresponse'))
					$this->chkIsValidCaptureVideoFile();
				if(chkAudioModuleAllowed('forumresponse'))
					$this->chkIsValidCaptureAudioFile();

				if ($this->fields_arr['response_id'])
					{
						if ($this->CFG['debug']['store_log'])
							{
								$this->createErrorLogFile('Forumresponse');
							}
						$this->updateForumResponse($this->fields_arr['topic_id'], $this->fields_arr['response_id']);
						if ($this->CFG['debug']['store_log'])
							{
								$this->closeErrorLogFile();
							}
				    }
				else
					{
						if ($this->CFG['debug']['store_log'])
							{
								$this->createErrorLogFile('Forumresponse');
							}
						$this->insertForumResponse($this->fields_arr['topic_id']);
						$this->addNewVideo();
						$this->addExternalVideo('Forumresponse');
						$this->addNewAudio();
						$this->addExternalAudio('Forumresponse');
						if ($this->CFG['debug']['store_log'])
							{
								$this->closeErrorLogFile();
							}
						$this->updateUsers();
					}
				$this->updateForumTopic($this->fields_arr['topic_id']);
				$this->updateForumsTable();
			}

		public function addNewVideo()
			{
				if($this->fields_arr['video'])
					{
						#echo 'here'.$this->fields_arr['video'];die();
						$this->fields_arr['video_id'] = $this->insertAnsVideoTable();
						//added code for logging
						if($this->fp)
							{
								$log_str = 'Video Record created : '.$this->fields_arr['video_id']."\r\n";
								$this->writetoTempFile($log_str);
							}
						$video_name = getImageName($this->fields_arr['video_id']);

						$temp_dir = '../../'.$this->CFG['admin']['ans_videos']['temp_folder'];
						$this->chkAndCreateFolder($temp_dir);
						$temp_file = $temp_dir.$video_name.'.flv';

						$file_name = $this->CFG['admin']['video']['red5_flv_path'].$this->fields_arr['rid'].'.flv';
						copy($file_name, $temp_file);
						unlink($file_name);
						//added code for logging
						if($this->fp)
							{
								$log_str = 'Video stored in temp server : '.$temp_file."\r\n";
								$this->writetoTempFile($log_str);
							}

						$this->changeAnsVideoStatus($this->fields_arr['video_id'], 'No');
						if($this->CFG['admin']['ans_videos']['video_auto_encode'] or 1)
							{
								//added code for logging
								if($this->fp)
									{
										$log_str = 'Calling Video Encode \r\n';
										$this->writetoTempFile($log_str);
									}
								$this->videoEncode($this->fields_arr['video_id']);
							}

						$temp_dir = '../../'.$this->CFG['admin']['ans_videos']['temp_folder'];
						$temp_file = $temp_dir.$this->fields_arr['rid'].'.flv';
						if(file_exists($temp_file))
							unlink($temp_file);
					}
			}
		public function addNewAudio()
			{
				if($this->fields_arr['audio'])
					{
						$this->fields_arr['audio_id'] = $this->insertAnsAudioTable();

						//added code for logging
						if($this->fp)
							{
								$log_str = 'Audio Record created : '.$this->fields_arr['audio_id']."\r\n";
								$this->writetoTempFile($log_str);
							}
						$audio_name = getImageName($this->fields_arr['audio_id']);

						$temp_dir = '../../'.$this->CFG['admin']['ans_audios']['temp_folder'];
						$this->chkAndCreateFolder($temp_dir);
						$temp_file = $temp_dir.$audio_name.'.flv';

						$file_name = $this->CFG['admin']['video']['red5_flv_path'].$this->fields_arr['rid'].'AUD.flv';
						copy($file_name, $temp_file);
						unlink($file_name);
						//added code for logging
						if($this->fp)
							{
								$log_str = 'Audio stored in temp server : '.$temp_file."\r\n";
								$this->writetoTempFile($log_str);
							}

						$this->changeAnsAudioStatus($this->fields_arr['audio_id'], 'No');
						if($this->CFG['admin']['ans_audios']['audio_auto_encode'] or 1)
							{
								//added code for logging
								if($this->fp)
									{
										$log_str = 'Calling Audio Encode \r\n';
										$this->writetoTempFile($log_str);
									}
								$this->audioEncode($this->fields_arr['audio_id']);
							}

						$temp_dir = '../../'.$this->CFG['admin']['ans_audios']['temp_folder'];
						$temp_file = $temp_dir.$this->fields_arr['rid'].'AUD.flv';
						if(file_exists($temp_file))
							unlink($temp_file);
					}

			}

		public function insertAnsVideoTable()
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_video'].' SET'.
						' content_id='.$this->dbObj->Param('content_id').','.
						' user_id='.$this->dbObj->Param('user_id').','.
						' video_for=\'Forumresponse\','.
						' video_ext=\'flv\','.
						' date_added=NOW(),'.
						' flv_upload_type=\'Normal\','.
						' video_encoded_status=\'Partial\'';

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

				return $this->dbObj->Insert_ID();
			}

		public function insertAnsAudioTable()
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
						' content_id='.$this->dbObj->Param('content_id').','.
						' user_id='.$this->dbObj->Param('user_id').','.
						' audio_for=\'Forumresponse\','.
						' audio_ext=\'flv\','.
						' date_added=NOW(),'.
						' audio_upload_type=\'Normal\','.
						' audio_encoded_status=\'Partial\'';

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

				return $this->dbObj->Insert_ID();
			}

		/**
		 * VideoUpload::changeVideoStatus()
		 *
		 * @param $video_id
		 * @return
		 **/
		public function changeAnsVideoStatus($video_id, $status)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
						' video_encoded_status='.$this->dbObj->Param('video_encoded_status').
						' WHERE video_id='.$this->dbObj->Param('video_id');

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

		/**
		 * AudioUpload::changeAudioStatus()
		 *
		 * @param $audio_id
		 * @return
		 **/
		public function changeAnsAudioStatus($audio_id, $status)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
						' audio_encoded_status='.$this->dbObj->Param('audio_encoded_status').
						' WHERE audio_id='.$this->dbObj->Param('audio_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($status, $audio_id));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}
		/**
		 * 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 the User table
		 *
		 * @access public
		 * @return void
		 **/
		public function updateUsers()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users_ans_log'].
						' SET total_posts = total_posts + 1';

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

				return true;
			}

		/**
		 * To update the last post user_id
		 *
		 * @param integer $forum_id
		 * @access public
		 * @return void
		 **/
		public function updateForums($forum_id)
			{

				$sql = 'SELECT 1 FROM '.$this->CFG['db']['tbl']['forum_topics'].
						' WHERE topic_id = '.$this->dbObj->Param($this->fields_arr['topic_id']).
						' AND topic_status = \'Ok\'';

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

				if($rs->PO_RecordCount())
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['forums'].
								' SET last_post_user_id = \''.$this->CFG['user']['user_id'].'\''.
								', last_post_date = now()'.
								', total_response = total_response + 1'.
								' 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);
					}
				return true;
			}

		/**
		 * To update the forum_response
		 *
		 * @param string $topic_id
		 * @access public
		 * @return void
		 **/
		public function insertForumResponse($topic_id)
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['forum_response'].
						' SET topic_id = '.$this->dbObj->Param($topic_id).
						', forum_response = '.$this->dbObj->Param($this->fields_arr['forum_response']).
						', response_user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']).
						', response_status = '.$this->dbObj->Param($this->fields_arr['response_status']).
						', date_added = NOW()';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($topic_id, html_entity_decode($this->fields_arr['forum_response'], ENT_QUOTES, $this->CFG['site']['charset']),
														 $this->CFG['user']['user_id'], $this->fields_arr['response_status']));
				if (!$rs)
					trigger_db_error($this->dbObj);
				$this->fields_arr['content_id'] = $this->dbObj->Insert_ID();
				return true;
			}

		/**
		  * To update the forum_response
		 *
		 * @param string $topic_id
		 * @param string $response_id
		 * @access public
		 * @return void
		**/
		public function updateForumResponse($topic_id, $response_id)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
						' SET forum_response = '.$this->dbObj->Param($this->fields_arr['forum_response']).
						', response_status = '.$this->dbObj->Param($this->fields_arr['response_status']).
						' 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(html_entity_decode($this->fields_arr['forum_response'], ENT_QUOTES, $this->CFG['site']['charset']), $this->fields_arr['response_status'],
														 $topic_id, $response_id));
				if (!$rs)
					trigger_db_error($this->dbObj);

				if($this->fields_arr['video'] or ($this->fields_arr['video_id']==0))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
								' video_status=\'Deleted\' WHERE'.
								' content_id='.$this->dbObj->Param('content_id').' AND'.
								' video_id='.$this->dbObj->Param('video_id').' AND'.
								' video_for=\'Forumresponse\'';

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

						$this->fields_arr['content_id'] = $response_id;
						if($this->fields_arr['video'])
							$this->addNewVideo();
					}
				if($this->fields_arr['video_external_flv_url'] or $this->fields_arr['file_extern'] or ($this->fields_arr['external_video_id']==0))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
								' video_status=\'Deleted\' WHERE'.
								' content_id='.$this->dbObj->Param('content_id').' AND'.
								' video_id='.$this->dbObj->Param('external_video_id').' AND'.
								' video_for=\'Forumresponse\'';

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

						$this->fields_arr['content_id'] = $response_id;
						if($this->fields_arr['video_external_flv_url'] or $this->fields_arr['file_extern'])
							$this->addExternalVideo('Forumresponse');
					}

				if($this->fields_arr['audio'] or ($this->fields_arr['audio_id']==0))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
								' audio_status=\'Deleted\' WHERE'.
								' content_id='.$this->dbObj->Param('content_id').' AND'.
								' audio_id='.$this->dbObj->Param('audio_id').' AND'.
								' audio_for=\'Forumresponse\'';

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

						$this->fields_arr['content_id'] = $response_id;
						if($this->fields_arr['audio'])
							$this->addNewAudio();
					}
				if($this->fields_arr['audio_external_url'] or $this->fields_arr['audio_extern'] or ($this->fields_arr['external_audio_id']==0))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
								' audio_status=\'Deleted\' WHERE'.
								' content_id='.$this->dbObj->Param('content_id').' AND'.
								' audio_id='.$this->dbObj->Param('external_audio_id').' AND'.
								' audio_for=\'Forumresponse\'';

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

						$this->fields_arr['content_id'] = $response_id;
						if($this->fields_arr['audio_external_url'] or $this->fields_arr['audio_extern'])
							$this->addExternalAudio('Forumresponse');
					}

				$this->fields_arr['content_id'] = $response_id;
				return true;
			}

		/**
		 * To update the forum_topic table
		 *
		 * @param string $topic_id
		 * @access public
		 * @return integer
		 **/
		public function updateForumTopic($topic_id)
			{
				$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).
						', total_views = total_views + 1'.
						', last_post_user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']).
						', last_post_date = NOW()';
				$fields_value_arr = array($total_response, $this->CFG['user']['user_id']);
				if ($this->fields_arr['forum_ratings'])
					{
						$sql .= ', rating_total = rating_total + '.$this->dbObj->Param($this->fields_arr['forum_ratings']).
								', rating_count = rating_count + 1 ';
						$fields_value_arr[] = $this->fields_arr['forum_ratings'];
				    }
				$sql .=	' WHERE topic_id = '.$this->dbObj->Param($topic_id);
				$fields_value_arr[] = $topic_id;

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

				return true;
			}

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

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

				return true;
			}

		/**
		 * To populate ratings
		 *
		 * @param 	int		$ratings_value	rating value
		 * @access public
		 * @return void
		 **/
		public function populateRatings($ratings_value)
			{
				for ($i = 1; $i <= 10 ; $i++)
					{
?>
	<input type="radio" class="clsCheckRadio" name="forum_ratings" id="forum_ratings<?php echo $i;?>" tabindex="<?php $this->getTabIndex();?>" value="<?php echo $i;?>"<?php echo ($ratings_value == $i)? 'CHECKED' : '';?> /><?php echo $i;?>&nbsp;&nbsp;
<?php
	 			    }
			}

		/**
		 * To set error to the form field
		 *
		 * @param string $field_name
		 * @param string $err_tip
		 * @access public
		 * @return void
		 **/
		public function setError($field_name, $err_tip='')
			{
				$this->fields_err_tip_arr[$field_name] = $err_tip;
			}
		public function generateRandomId()
			{
				$time = time();
				$this->fields_arr['rid'] = md5($time);
			}
		public function declareForumValues()
			{
				$manageForums_arr = array();

				$manageForums_arr['style_add_video'] = $manageForums_arr['style_add_external_video']= '';
				$manageForums_arr['style_delete_video'] = $manageForums_arr['style_delete_external_video']= 'style="display:none"';
				$manageForums_arr['style_preview_video'] = $manageForums_arr['style_preview_external_video']= 'style="display:none"';
				$manageForums_arr['video_preview_url'] = $manageForums_arr['video_default_preview_url'] = getUrl('anspreviewvideo', '?pg=Forumresponse_'.$this->getFormField('rid'), '?pg=Forumresponse_'.$this->getFormField('rid'), 'members');
				$manageForums_arr['video_not_encoded_info'] = $this->LANG['video_not_encoded_info'];
				$manageForums_arr['external_video_onclick'] = 'return showVideoExternalPreview();';
				$manageForums_arr['external_video_preview_url'] = $_SERVER['REQUEST_URI'];

				if($this->fields_arr['video'] or $this->fields_arr['video_id'])
					{
						$manageForums_arr['style_delete_video'] = '';
						$manageForums_arr['style_preview_video'] = '';
						$manageForums_arr['style_add_video'] = 'style="display:none"';

						if ($this->fields_arr['video_id'])
							$manageForums_arr['video_preview_url'] = getUrl('ansviewvideo', '?pg=Forumresponse_'.$this->getFormField('video_id'), '?pg=Forumresponse_'.$this->getFormField('video_id'));
						if ($this->fields_arr['video'])
							$manageForums_arr['video_preview_url'] = getUrl('anspreviewvideo', '?pg=Forumresponse_'.$this->getFormField('video'), '?pg=Forumresponse_'.$this->getFormField('video'), 'members');
					}
				if($this->fields_arr['video_external_flv_url'] or $this->fields_arr['file_extern'] or $this->fields_arr['external_video_id'])
					{
						$manageForums_arr['style_delete_external_video'] = '';
						$manageForums_arr['style_preview_external_video'] = '';
						$manageForums_arr['style_add_external_video'] = 'style="display:none"';

						if (!$this->fields_arr['video_external_flv_url'] AND !$this->fields_arr['file_extern'])
							{
								if ($this->fields_arr['external_video_id'] AND $external_video_details = $this->getExternalVideoDetails($this->fields_arr['external_video_id']))
									{
										if ($external_video_details['video_status'] == 'Ok' AND $external_video_details['video_encoded_status'] == 'Yes')
											{
												$manageForums_arr['external_video_onclick'] = getBrowser('video');
												$manageForums_arr['external_video_preview_url'] = getUrl('ansviewvideo', '?pg=Forumresponse_'.$this->getFormField('external_video_id'), '?pg=Forumresponse_'.$this->getFormField('external_video_id'));
											}
									}
							}
					}

				$manageForums_arr['style_add_audio'] = $manageForums_arr['style_add_external_audio']= '';
				$manageForums_arr['style_delete_audio'] = $manageForums_arr['style_delete_external_audio']= 'style="display:none"';
				$manageForums_arr['style_preview_audio'] = $manageForums_arr['style_preview_external_audio']= 'style="display:none"';
				$manageForums_arr['audio_preview_url'] = $manageForums_arr['audio_default_preview_url'] = getUrl('anspreviewaudio', '?pg=Forumresponse_'.$this->getFormField('rid'), '?pg=Forumresponse_'.$this->getFormField('rid'));
				$manageForums_arr['audio_not_encoded_info'] = $this->LANG['audio_not_encoded_info'];
				$manageForums_arr['external_audio_onclick'] = 'return showAudioExternalPreview();';
				$manageForums_arr['external_audio_preview_url'] = $_SERVER['REQUEST_URI'];

				if($this->fields_arr['audio'] or $this->fields_arr['audio_id'])
					{
						$manageForums_arr['style_delete_audio'] = '';
						$manageForums_arr['style_preview_audio'] = '';
						$manageForums_arr['style_add_audio'] = 'style="display:none"';
						if ($this->fields_arr['audio_id'])
							$manageForums_arr['audio_preview_url'] = getUrl('ansviewaudio', '?pg=Forumresponse_'.$this->getFormField('audio_id'), '?pg=Forumresponse_'.$this->getFormField('audio_id'));
						if ($this->fields_arr['audio'])
							$manageForums_arr['audio_preview_url'] = getUrl('anspreviewaudio', '?pg=Forumresponse_'.$this->getFormField('audio'), '?pg=Forumresponse_'.$this->getFormField('audio'));
					}
				if($this->fields_arr['audio_external_url'] or $this->fields_arr['audio_extern'] or $this->fields_arr['external_audio_id'])
					{
						$manageForums_arr['style_delete_external_audio'] = '';
						$manageForums_arr['style_preview_external_audio'] = '';
						$manageForums_arr['style_add_external_audio'] = 'style="display:none"';

						if (!$this->fields_arr['audio_external_url'] AND !$this->fields_arr['audio_extern'])
							{
								if ($this->fields_arr['external_audio_id'] AND $external_audio_details = $this->getExternalAudioDetails($this->fields_arr['external_audio_id']))
									{
										if ($external_audio_details['audio_status'] == 'Ok' AND $external_audio_details['audio_encoded_status'] == 'Yes')
											{
												$manageForums_arr['external_audio_onclick'] = getBrowser('external_audio_preview');
												$manageForums_arr['external_audio_preview_url'] = getUrl('ansviewaudio', '?pg=Forumresponse_'.$this->getFormField('external_audio_id'), '?pg=Forumresponse_'.$this->getFormField('external_audio_id'));
											}
									}
							}
					}

				if(chkVideoModuleAllowed('forumresponse'))
					{
						 $manageForums_arr['video_upload_url'] = getUrl('videoupload', '?rid='.$this->fields_arr['rid'], '?rid='.$this->fields_arr['rid'], 'members');
					}
				if(chkExternalVideoModuleAllowed('forumresponse'))
					{
						 $manageForums_arr['external_video_upload_url'] = getUrl('externalvideoupload', '?rid='.$this->fields_arr['rid'], '?rid='.$this->fields_arr['rid'], 'members');
					}

				if(chkAudioModuleAllowed('forumresponse'))
					{
						$manageForums_arr['audio_upload_url'] = getUrl('audioupload', '?rid='.$this->fields_arr['rid'].'AUD', '?rid='.$this->fields_arr['rid'].'AUD', 'members');
					}
				if(chkExternalAudioModuleAllowed('forumresponse'))
					{
						 $manageForums_arr['external_audio_upload_url'] = getUrl('externalaudioupload',  '?rid='.$this->fields_arr['rid'], '?rid='.$this->fields_arr['rid'], 'members');
					}

				return $manageForums_arr;
			}
	}
//<<<<<-------------- Class ForumResponseFormHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$forumsresponse = new ForumResponseFormHandler();
$forumsresponse->left_navigation_div = 'forumsForums';
$forumsresponse->setPageBlockNames(array('form_create_response'));
//default form fields and values...
$forumsresponse->setFormField('video', '');
$forumsresponse->setFormField('video_id', '');
$forumsresponse->setFormField('audio', '');
$forumsresponse->setFormField('audio_id', '');
$forumsresponse->setFormField('external_video_id', '');
$forumsresponse->setFormField('external_audio_id', '');
$forumsresponse->setFormField('video_external_flv_url', '');
$forumsresponse->setFormField('flv_upload_type', '');
$forumsresponse->setFormField('file_extern', '');
$forumsresponse->setFormField('audio_external_url', '');
$forumsresponse->setFormField('audio_extern', '');
$forumsresponse->setFormField('rid', '');
$forumsresponse->setFormField('act', '');
$forumsresponse->setFormField('forum_id', '');
$forumsresponse->setFormField('topic_id', '');
$forumsresponse->setFormField('response_id', '');
$forumsresponse->setFormField('forum_response', '');
$forumsresponse->setFormField('forum_ratings', '');
$forumsresponse->setFormField('response_status', 'Yes');
$forumsresponse->setAllPageBlocksHide();
$forumsresponse->sanitizeFormInputs($_REQUEST);
if(!$forumsresponse->getFormField('rid'))
	$forumsresponse->generateRandomId();

if ($forumsresponse->isFormPOSTed($_POST, 'forumsresponse_submit'))
	{
		//validations...
		$forumsresponse->chkIsNotEmpty('forum_id', $LANG['err_tip_compulsory'])and
			$forumsresponse->chkIsNumeric('forum_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->isValidForumId($forumsresponse->getFormField('forum_id'), $LANG['forums_err_tip_invalid_forum_id']);
		$forumsresponse->chkIsNotEmpty('topic_id', $LANG['err_tip_compulsory'])and
			$forumsresponse->chkIsNumeric('topic_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->isValidForumTopicId($forumsresponse->getFormField('forum_id'), $forumsresponse->getFormField('topic_id'), $LANG['forumsresponse_err_tip_invalid_forum_topic_id']);
		$forumsresponse->getFormField('response_id')and
			$forumsresponse->chkIsNotEmpty('response_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->chkIsNumeric('response_id', $LANG['err_tip_compulsory']);
		// create the new InputFilter instance
		 $filter = new InputFilter($CFG['fckeditor']['allowed_tags'], $CFG['fckeditor']['allowed_attr']);
		 // use the new filter to process the string
		 $forumsresponse->setFormfield('forum_response' ,$filter->process($forumsresponse->getFormfield('forum_response')));
		$forumsresponse->chkIsNotEmpty('forum_response', $LANG['err_tip_compulsory']);
		$forumsresponse->chkIsNotEmpty('response_status', $LANG['err_tip_compulsory']);
		$forumsresponse->isValidFormInputs()and
			$forumsresponse->createForumResponse();
		if ($forumsresponse->isValidFormInputs())
			{
				$forumsresponse->setAllPageBlocksHide();
				$forumsresponse->setPageBlockShow('form_create_response');
				Redirect2URL('forumsResponses.php?forum_id='.$forumsresponse->getFormField('forum_id').'&topic_id='.$forumsresponse->getFormField('topic_id'));
			}
		else
			{
				$forumsresponse->setAllPageBlocksHide();
				$forumsresponse->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forumsresponse->setPageBlockShow('block_msg_form_error');
				$forumsresponse->setPageBlockShow('form_create_response');
			}
	}
elseif ($forumsresponse->isFormPOSTed($_POST, 'forumsresponse_cancel'))
	{
		Redirect2URL('forumsResponses.php?forum_id='.$forumsresponse->getFormField('forum_id').'&topic_id='.$forumsresponse->getFormField('topic_id'));
	}
elseif ($forumsresponse->isFormGETed($_GET, 'forum_id') && $forumsresponse->isFormGETed($_GET, 'topic_id'))
	{
		$forumsresponse->chkIsNotEmpty('forum_id', $LANG['err_tip_compulsory'])and
			$forumsresponse->chkIsNumeric('forum_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->isValidForumId($forumsresponse->getFormField('forum_id'), $LANG['forums_err_tip_invalid_forum_id']);
		$forumsresponse->chkIsNotEmpty('topic_id', $LANG['err_tip_compulsory'])and
			$forumsresponse->chkIsNumeric('topic_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->isValidForumTopicId($forumsresponse->getFormField('forum_id'), $forumsresponse->getFormField('topic_id'), $LANG['forumsresponse_err_tip_invalid_forum_topic_id']);
		$forumsresponse->getFormField('response_id')and
			$forumsresponse->chkIsNotEmpty('response_id', $LANG['err_tip_compulsory'])and
				$forumsresponse->chkIsNumeric('response_id', $LANG['err_tip_compulsory'])and
					$forumsresponse->isValidForumResponseId($forumsresponse->getFormField('topic_id'), $forumsresponse->getFormField('response_id'), $LANG['forumsresponse_err_tip_invalid_forum_response_id']);
		if ($forumsresponse->isValidFormInputs())
			{
				$forumsresponse->setPageBlockShow('form_create_response');
			}
		else
			{
				$forumsresponse->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forumsresponse->setPageBlockShow('block_msg_form_error');
			}
	}
else
	{
		$forumsresponse->setAllPageBlocksHide();
		$forumsresponse->setCommonErrorMsg($LANG['common_msg_error_sorry']);
		$forumsresponse->setPageBlockShow('block_msg_form_error');
	}
if ($forumsresponse->isShowPageBlock('form_create_response'))
	{
		$forumsresponse->form_create_response_arr = array();
		$forumsresponse->response_block['manageForums_arr'] = $forumsresponse->declareForumValues();
		$forumsresponse->response_block['populateHidden'] = array('start', 'topic_id', 'from', 'rid', 'video', 'video_external_flv_url', 'flv_upload_type', 'file_extern', 'audio', 'audio_external_url', 'audio_extern', 'video_id', 'external_video_id', 'audio_id', 'external_audio_id');
		if($forumsresponse->getFormField('forum_response'))
			$LANG['common_submit'] = $LANG['common_update'];
	}
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
//include the header file
$forumsresponse->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('forumsResponseCreate.tpl');
$video_format_arr = '';
$audio_format_arr = '';
if(isset($CFG['admin']['ans_videos']['format_arr']) && $CFG['admin']['ans_videos']['format_arr'])
	$video_format_arr = implode(',', $CFG['admin']['ans_videos']['format_arr']);
if(isset($CFG['admin']['ans_audios']['format_arr']) && $CFG['admin']['ans_audios']['format_arr'])
	$audio_format_arr = implode(',', $CFG['admin']['ans_audios']['format_arr']);
?>
<script type="text/javascript" language="javascript">
	var palette_url = '<?php echo $CFG['site']['url'].'editor/palette.htm';?>';
	var block_arr= new Array('selMsgConfirm');
</script>
<?php if($CFG['admin']['module']['video_audio_blogsforum']) { ?>
<script language="javascript">
var video_extension = '<?php echo $video_format_arr;?>';
var audio_extension = '<?php echo $audio_format_arr;?>';
function thisMovie(movieName) {
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window.document[movieName]

  } else {
   return window.document[movieName]
  }
}
function createJSFCommunicatorObject(playerObj)
{
	fc = new JSFCommunicator(playerObj);
}
var onUploadComplete = function()
{
	var str = arguments[0];
	var index = str.lastIndexOf('.');
	str = str.substring(index+1);
	var video_extension_array = video_extension.split(",");
	var audio_extension_array = audio_extension.split(",");

	if (extensionLookup(video_extension_array, str)){
		$('file_extern').value = str;
		mediaType = 'Video';
		$('external_video_add').style.display = 'none';
		$('external_video_add1').style.display = 'none';
		$('external_video_add2').style.display = '';
		$('external_video_delete').style.display = '';
		if (($('external_video_preview'))) {
			$('external_video_preview').style.display = '';
			$('external_video_preview').href = externalVideoPreviewUrl;
			$('external_video_preview').onclick = showVideoExternalPreview;
			$('external_video_preview').removeClassName('lightwindow');
			$('external_video_preview').href= '#';
		}
	} else if (extensionLookup(audio_extension_array, str)) {
		$('audio_extern').value = str;
		mediaType = 'Audio';
		$('external_audio_add').style.display = 'none';
		$('external_audio_add1').style.display = 'none';
		$('external_audio_add2').style.display = '';
		$('external_audio_delete').style.display = '';
		if (($('external_audio_preview'))) {
			$('external_audio_preview').style.display = '';
			$('external_audio_preview').href = externalAudioPreviewUrl;
			$('external_audio_preview').onclick = showAudioExternalPreview;
			$('external_audio_preview').removeClassName('lightwindow');
			$('external_audio_preview').href= '#';
		}
	}
	ffMacOsCloseWindow();
}
function extensionLookup(mediaArray, extern) {
    for (var i = 0; i < mediaArray.length ; i++) {
        if (mediaArray[i] == extern) {
            return true;
        }
    }
    return false;
}
function onClickThis()
{
		fc.callFunction("_level0","uploadSelcted");
}
function showNormalUpload()
{
	var obj = document.getElementById('selUploadFlash');
	obj.style.display = 'none';
	var obj = document.getElementById('selUploadNormal');
	obj.style.display = '';
	return false;
}
function makeDisable()
{
	document.getElementById('Upload').disabled = true;
}
function makeEnable()
{
	//document.getElementById('Upload').disabled = false;
	//Upload.disabled = false;
}
</script>
<? } ?>
<script language="javascript" type="text/javascript">
	var saveVideo = function(){
		$('video_add').style.display = 'none';
		$('video_delete').style.display = '';
		if (($('video_preview'))) {
			$('video_preview').style.display = '';
			$('video_preview').href = videoPreviewUrl;
		}
		$('video_secnd').style.display = '';
		$('video_first').style.display = 'none';
		$('video').value = $('rid').value;
		ffMacOsCloseWindow();
	}
	var removeVideo = function(){
		alert_manual('<?php echo $LANG['common_video_deleted'];?>');
		$('video_delete').style.display = 'none';
		if (($('video_preview'))) $('video_preview').style.display = 'none';
		$('video_add').style.display = '';
		$('video').value = '';
		$('video_id').value = '0';
		$('video_secnd').style.display = 'none';
		$('video_first').style.display = '';
		return false;
	}
	var removeExternalVideo = function(){
		alert_manual('<?php echo $LANG['successfully_external_video_deleted'];?>', 'external_video_delete');
		$('external_video_delete').style.display = 'none';
		if (($('external_video_preview'))) $('external_video_preview').style.display = 'none';
		$('external_video_add').style.display = '';
		$('external_video_add1').style.display = '';
		$('external_video_add2').style.display = 'none';
		$('video_external_flv_url').value = '';
		$('file_extern').value = '';
		$('external_video_id').value = '0';
		return false;
	}
	var confrimRemove = function(){
		var act_value = arguments[0];
		var anchorLink = arguments[1];
		var msg_confirm = arguments[2];
		if(arguments.length>=4)
			add_top_position = arguments[4];
		if(arguments.length>=6)
			add_left_position = arguments[5];

		var confirm_message = msg_confirm;

		$('confirmMessage').innerHTML = confirm_message;
		document.formConfirm.action.value = act_value;
		Confirmation('selMsgConfirm', 'formConfirm', Array(), Array(), Array());

		return false;
	}
	var doRemoveAction = function(){
		if (document.formConfirm.action.value == 'external_video')
			removeExternalVideo();
		else if (document.formConfirm.action.value == 'external_audio')
			removeExternalAudio();
		else if (document.formConfirm.action.value == 'video')
			removeVideo();
		else
			removeAudio();
	}
	var deleteVideo = function(){
		ffMacOsCloseWindow();
	}
	var saveAudio = function(){
		$('audio_add').style.display = 'none';
		$('audio_delete').style.display = '';
		$('audio_secnd').style.display = '';
		$('audio_first').style.display = 'none';
		if (($('audio_preview'))) {
			$('audio_preview').style.display = '';
			$('audio_preview').href = audioPreviewUrl;
		}
		$('audio').value = $('rid').value;
		ffMacOsCloseWindow();
	}
	var removeAudio = function(){
		alert_manual('<?php echo $LANG['common_audio_deleted'];?>');
		$('audio_delete').style.display = 'none';
		$('audio_secnd').style.display = 'none';
		$('audio_first').style.display = '';

		if (($('audio_preview'))) $('audio_preview').style.display = 'none';
		$('audio_add').style.display = '';
		$('audio').value = '';
		$('audio_id').value = '0';
		return false;
	}
	var removeExternalAudio = function(){
		alert_manual('<?php echo $LANG['successfully_external_audio_deleted'];?>', 'external_audio_delete');
		$('external_audio_delete').style.display = 'none';
		if (($('external_audio_preview'))) $('external_audio_preview').style.display = 'none';
		$('external_audio_add').style.display = '';
		$('external_audio_add1').style.display = '';
		$('external_audio_add2').style.display = 'none';
		$('audio_external_url').value = '';
		$('audio_extern').value = '';
		$('external_audio_id').value = '0';
		return false;
	}

	var deleteAudio = function(){
		ffMacOsCloseWindow();
	}
	var removeId = 0;
	function add_more_upload(lang) {

		var id = removeId++;
		var imgdiv = $('tdID');
		var ptags = imgdiv.getElementsByTagName("p");
		var len = ptags.length;

		if(len > <?php echo $CFG['admin']['attachments_allowed']['count']+1;?>)
			$('brsBtn').style.display = 'none';

		if(len > <?php echo $CFG['admin']['attachments_allowed']['count']+2;?>)
			return;

		var rowTemplate = '<p class="clsAttachments" id="p_'+id+'"><input type="file" name="attachments[]" value=""> <a class="clsRemoveAttachment" href="#" onClick="removeThis('+id+');return false;">'+lang+'<\/a><\/p>';

		new Insertion.Before('brsBtn', rowTemplate);
	}

	function removeThis(id)	{
		var remId = id;
		$('p_'+remId).remove();
		$('brsBtn').style.display = '';
	}
</script>
<?php
//<<<<<-------------------- Page block templates ends -------------------//
$forumsresponse->includeFooter();
?>