<?php
/**
 * File to create topic in forum title
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		ForumTopicFormHandler
 * @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-10-29
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['forum_module'].'/forumsTopicCreate.php';
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['forum_module'].'/forumsRightLinks.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';

$CFG['mods']['include_files'][] = 'common/classes/class_inputfilter_clean.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.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_AnovaHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnsVideoUpload.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_ForumHandler.lib.php';

//compulsory
$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['auth']['is_authenticate'] = 'members';
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
if($CFG['feature']['html_editor'] == 'fckeditor')
	require($CFG['site']['project_path'].'fckeditor/fckEditorIntegration.php');
//-------------- Class ForumTopicFormHandler begins --------------->>>>>//
/**
 * @category	Visual answer
 * @package		ForumTopicFormHandler
 * @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-10-29
 */
class ForumTopicFormHandler extends ForumHandler
	{
		//Variable to store group forum details
		public $forum_details_arr;
		public $_dir_path = '';

		/**
		 * To count the number of topic created.
		 * 3
		 * @param 		string	$forum_topics_table forum_topics table
		 * @param 		integer $forum_id
		 * @param 		integer $max_allowed
		 * @param 		string $err_tip error message
		 * @access public
		 * @return void
		 **/
		public function countForumTopic($forum_id, $max_allowed, $err_tip='')
			{
				$sql = 'SELECT COUNT(ft.topic_id) AS count '.
						'FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft '.
						'WHERE ft.forum_id = '.$this->dbObj->Param($forum_id).' '.
						'AND ft.user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']).' ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id, $this->CFG['user']['user_id']));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				if ($row['count'] < $max_allowed)
					{
						return true;
					}
				$err_tip = str_ireplace('VAR_COUNT', $row['count'], $err_tip);
				$err_tip = str_ireplace('VAR_FORUM', '"'.$this->forum_details_arr['forum_title'].'"', $err_tip);
				$this->setCommonErrorMsg($err_tip);
				return false;
			}

		/**
		 * 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 given forum topic exists or not
		 *
		 * @param 		string $forum_title forum title
		 * @param 		integer $id
		 * @param		integer $forum_id
		 * @param 		string $table_name groups
		 * @param 		string $err_tip error tip
		 * @access public
		 * @return boolean
		 **/
		public function chkIsNotExists($forum_topic, $forum_id, $err_tip='')
			{
				$sql = 'SELECT COUNT(topic_id) AS count '.
						'FROM '.$this->CFG['db']['tbl']['forum_topics'].' '.
						'WHERE forum_id = '.$this->dbObj->Param($forum_id).' '.
						'AND forum_topic = '.$this->dbObj->Param($forum_topic).' ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id, $forum_topic));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				if ($row['count'])
					{
						$this->fields_err_tip_arr['forum_topic'] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * To create a forum topic
		 *
		 * @access public
		 * @return void
		 **/
		public function createForumTopic()
			{
				$this->setFormField('seo_title', $this->getSeoTitleForForumTopic($this->fields_arr['forum_topic'], $this->fields_arr['topic_id']));
				$topic_id = $this->insertForumTopic();
				if ($topic_id)
					{
						if ($this->CFG['debug']['store_log'])
							{
								$this->createErrorLogFile('Forumresponse');
							}
						$this->insertForumResponse($topic_id);
						if($this->fields_arr['publish_status']== 'Yes' or $this->forum_details_arr['forum_userid'] == $this->CFG['user']['user_id'])
							{
								$this->updateForums($this->fields_arr['forum_id']);
							}
						$this->addNewVideo();
						$this->addExternalVideo('Forumresponse');
						$this->addNewAudio();
						$this->addExternalAudio('Forumresponse');
						if ($this->CFG['debug']['store_log'])
							{
									$this->closeErrorLogFile('Forumresponse');
							}
						$this->updateUsersAnsLog();
				    }
			}

		/**
		 * To update the forum_topic table
		 *
		 * @access public
		 * @return integer
		 **/
		public function insertForumTopic()
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['forum_topics'].' SET '.
						'forum_id = '.$this->dbObj->Param($this->fields_arr['forum_id']).', '.
						'forum_topic = '.$this->dbObj->Param($this->fields_arr['forum_topic']).', '.
						'seo_title = '.$this->dbObj->Param($this->fields_arr['seo_title']).', '.
						'user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']).', '.
						'total_response = 1, '.
						'total_views = 1, '.
						'last_post_user_id = '.$this->dbObj->Param($this->CFG['user']['user_id']).', '.
						'publish_status = '.$this->dbObj->Param('publish').', '.
						'last_post_date = now(), '.
						'date_added = now()';

				if($this->fields_arr['publish_status']== 'No' and $this->forum_details_arr['forum_userid'] != $this->CFG['user']['user_id'])
						$sql .= ' ,topic_status=\'ToActivate\'';
				//die($sql);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['forum_id'],
														 $this->fields_arr['forum_topic'],
														 $this->fields_arr['seo_title'],
														 $this->CFG['user']['user_id'],
														 $this->CFG['user']['user_id'],
														 $this->fields_arr['publish']));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				return $this->dbObj->Insert_ID();
			}

		/**
		 * 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 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);
			}

		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'] = getAnovaUrl('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'] = getAnovaUrl('ansviewvideo', '?pg=Forumresponse_'.$this->getFormField('video_id'), '?pg=Forumresponse_'.$this->getFormField('video_id'));
						if ($this->fields_arr['video'])
							$manageForums_arr['video_preview_url'] = getAnovaUrl('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'] = getAnovaUrl('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'] = getAnovaUrl('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'] = getAnovaUrl('ansviewaudio', '?pg=Forumresponse_'.$this->getFormField('audio_id'), '?pg=Forumresponse_'.$this->getFormField('audio_id'));
						if ($this->fields_arr['audio'])
							$manageForums_arr['audio_preview_url'] = getAnovaUrl('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'] = getAnovaUrl('ansviewaudio', '?pg=Forumresponse_'.$this->getFormField('external_audio_id'), '?pg=Forumresponse_'.$this->getFormField('external_audio_id'));
											}
									}
							}
					}

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

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

				return $manageForums_arr;
			}

			public function paymentUpgradeDisplay()
				{
					$notifyForumTopic_arr = array();

					if(chkIsPaymentAllowed('payment_forumtopic'))
						{
							if(chkIsPaymentAlertMessageAllowed())
								{
									$req_count = $this->getPlanLogForFourmTopic();
									$req_count = $req_count < 0?'0':$req_count;

									if($req_count == $this->CFG['plan']['unlimited_text'])
										$notifyForumTopic_arr['total_notify'] = $this->LANG['notify_unlimited'];
									elseif(chkIsPaymentUpgradeAllowed())
										{
											$notifyForumTopic_arr['total_notify'] = str_ireplace('VAR_LIMIT', $req_count, $this->LANG['total_notify_upgrade']);
											$notifyForumTopic_arr['total_notify'] = str_ireplace('VAR_UPGRADE', '<a href="'.getAnovaUrl('paymentupgrade', '', '', 'members').'">'.$this->LANG['forumtopic_upgrade'].'</a>', $notifyForumTopic_arr['total_notify']);
										}
									else
										$notifyForumTopic_arr['total_notify'] = str_ireplace('VAR_LIMIT', $req_count, $this->LANG['total_notify_admin']);

								}

						}
					return $notifyForumTopic_arr;
				}

			public function getPlanLogForFourmTopic()
				{
					$sql = 'SELECT topic_id FROM '.$this->CFG['db']['tbl']['forum_topics'].
							' WHERE user_id = '.$this->dbObj->Param('user_id').
							' AND date_added >= '.$this->dbObj->Param('date_added');

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

					$numrows = $rs->PO_RecordCount();

					if($this->CFG['plan']['plan_total_forumtopic'] != $this->CFG['plan']['unlimited'])
						return ($this->CFG['plan']['plan_total_forumtopic'] - $numrows);
					else
						return $this->CFG['plan']['unlimited_text'];

				}

			public function totalPostedFourmTopicCount($user_id)
				{
					if(!chkIsPaymentAllowed('payment_forumtopic'))
						return true;

					$sql = 'SELECT topic_id FROM '.$this->CFG['db']['tbl']['forum_topics'].
					       ' WHERE user_id = '.$this->dbObj->Param('user_id').
						   ' AND date_added >= '.$this->dbObj->Param('date_added');

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

					if($numrows < $this->CFG['plan']['plan_total_forumtopic'] || $this->CFG['plan']['plan_total_forumtopic'] == $this->CFG['plan']['unlimited'])
						return true;
					else
						return false;
				}

	}
//<<<<<-------------- Class ForumTopicFormHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$forumstopic = new ForumTopicFormHandler();
if(!chkIsAllowedModule(array('forums')))
	Redirect2URL(getForumsUrl($CFG['redirect']['dsabled_module_url']['file_name']));
$forumstopic->setPageBlockNames(array('form_create_topic', 'forumTopicUpgrade'));

//default form fields and values...
$forumstopic->setFormField('video', '');
$forumstopic->setFormField('video_id', '');
$forumstopic->setFormField('audio', '');
$forumstopic->setFormField('audio_id', '');
$forumstopic->setFormField('external_video_id', '');
$forumstopic->setFormField('external_audio_id', '');
$forumstopic->setFormField('video_external_flv_url', '');
$forumstopic->setFormField('flv_upload_type', '');
$forumstopic->setFormField('file_extern', '');
$forumstopic->setFormField('audio_external_url', '');
$forumstopic->setFormField('audio_extern', '');
$forumstopic->setFormField('rid', '');
$forumstopic->setFormField('forum_id', '');
$forumstopic->setFormField('title', '');
$forumstopic->setFormField('seo_title', '');
$forumstopic->setFormField('topic_id', '');
$forumstopic->setFormField('forum_topic', '');
$forumstopic->setFormField('forum_response', '');
$forumstopic->setFormField('publish_status', '');
$forumstopic->setFormField('publish', 'Yes');

$forumstopic->setAllPageBlocksHide();
if ($forumstopic->isFormGETed($_GET, 'title'))
	{
		$forumstopic->sanitizeFormInputs($_GET);
		$forumstopic->chkIsNotEmpty('title', $LANG['err_tip_compulsory'])and
			$forumstopic->isValidForumId($forumstopic->getFormField('title'), $LANG['forums_err_tip_invalid_forum_id'])and
				$forumstopic->countForumTopic($forumstopic->getFormField('forum_id'), $CFG['admin']['forums']['topic']['max_allowed'], $LANG['forumstopic_err_tip_cannot_create_forum_topic']);
		if (!isMember())
			{
				$_SESSION['url'] = getForumsUrl('forumstopiccreate', '?title='.$forumstopic->getFormField('title'), $forumstopic->getFormField('title').'/', 'members');
				$_SESSION['login_error'] = 'login_err_authorization_required';
				Redirect2URL(getUrl('login'));
			}
		if (!$forumstopic->isAllowedToAsk())
			{
				$forumstopic->setCommonErrorMsg($LANG['info_not_allowed_to_post']);
			}

		if ($forumstopic->isValidFormInputs())
			{
				$forumstopic->setPageBlockShow('form_create_topic');
			}
		else
			{
				$forumstopic->setPageBlockShow('block_msg_form_error');
			}

		if ($forumstopic->isFormPOSTed($_POST, 'forumstopic_submit'))
			{
				$forumstopic->sanitizeFormInputs($_POST);

				if(chkVideoModuleAllowed('forumresponse'))
					$forumstopic->chkIsValidCaptureVideoFile();
				if(chkAudioModuleAllowed('forumresponse'))
					$forumstopic->chkIsValidCaptureAudioFile();

				//validations...
				$forumstopic->chkIsNotEmpty('forum_topic', $LANG['err_tip_compulsory'])and
					$forumstopic->chkIsNotExists($forumstopic->getFormField('forum_topic'), $forumstopic->getFormField('forum_id'), $LANG['forumstopic_err_tip_topic_exists']);
				$forumstopic->chkIsNotEmpty('forum_response', $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
				$forumstopic->setFormfield('forum_response' ,$filter->process($forumstopic->getFormfield('forum_response')));

				if ($forumstopic->isValidFormInputs())
					{
						$forumstopic->createForumTopic();

						$forumstopic->setAllPageBlocksHide();
						$forumstopic->setPageBlockShow('form_create_topic');
						$success_msg = 'approve';
						if($forumstopic->getFormField('publish_status')== 'Yes' or $forumstopic->forum_details_arr['forum_userid'] == $CFG['user']['user_id'])
							$success_msg = 'sucesss';

						Redirect2URL(getForumsUrl('forumstopics', '?title='.$forumstopic->getFormField('title').'&msg='.$success_msg, $forumstopic->getFormField('title').'/?msg='.$success_msg));

					}
				else //error in form inputs
					{
						$forumstopic->setAllPageBlocksHide();
						$forumstopic->setCommonErrorMsg($LANG['common_msg_error_sorry']);
						$forumstopic->setPageBlockShow('block_msg_form_error');
						$forumstopic->setPageBlockShow('form_create_topic');
					}
			}

		if ($forumstopic->isFormPOSTed($_POST, 'forumstopic_cancel'))
			{
				$forumstopic->sanitizeFormInputs($_POST);
				Redirect2URL(getForumsUrl('forumstopics', '?title='.$forumstopic->getFormField('title'), $forumstopic->getFormField('title').'/'));
			}
	}
else
	{
		$forumstopic->setAllPageBlocksHide();
		Redirect2URL(getForumsUrl('forums'));
	}

$forumstopic->myforums_url = getForumsUrl('forumstopics', '?title='.$forumstopic->getFormField('title'), $forumstopic->getFormField('title').'/');
$forumstopic->myforums_title = nl2br(wordWrapManual($forumstopic->forum_details_arr['forum_title'],15));
if(!$forumstopic->getFormField('rid'))
	$forumstopic->generateRandomId();
if ($forumstopic->isShowPageBlock('form_create_topic'))
	{
		$forumstopic->form_create_topic_arr = array();
		$forumstopic->form_create_topic_arr['forumlistall_url'] = getForumsUrl('forumstopics', '?title='.$forumstopic->getFormField('seo_title'), $forumstopic->getFormField('seo_title').'/');
		$forumstopic->form_create_topic_arr['manageForums_arr'] = $forumstopic->declareForumValues();
		$forumstopic->form_create_topic_arr['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 (chkIsPaymentAllowed('payment_forumtopic'))
			{
				$forumstopic->forumtopic_add['notifyForumTopic_arr'] = $forumstopic->paymentUpgradeDisplay();

				if($forumstopic->totalPostedFourmTopicCount($CFG['user']['user_id']))
					{
						$forumstopic->setPageBlockShow('forumTopicUpgrade');
					}
				else
					{
						$forumstopic->setAllPageBlocksHide();
						$forumstopic->setPageBlockShow('block_msg_form_error');
						$upgrade = '<a href="'.getAnovaUrl('paymentupgrade').'">'.$forumstopic->LANG['forumtopic_upgrade'].'</a>';
						if(!chkIsPaymentUpgradeAllowed())
								$upgrade = $LANG['inform_site_admin'];
						$LANG['info_limit_exceeded_to_ask_forumtopic'] = str_ireplace('VAR_UPGRADE', $upgrade, $LANG['info_limit_exceeded_to_ask_forumtopic']);
						$forumstopic->setCommonErrorMsg($LANG['info_limit_exceeded_to_ask_forumtopic']);
					}
			}
	}
//<<<<<-------------------- Code ends----------------------//

//-------------------- Page block templates begins -------------------->>>>>//
//include the header file
$forumstopic->includeHeader();
//include the content of the page
setTemplateFolder('members/', $CFG['admin']['index']['forum_module']);
$smartyObj->display('forumsTopicCreate.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']);
//<<<<<-------------------- Page block templates ends -------------------//
?>
<script type="text/javascript" language="javascript">
	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
//include the footer of the page
$forumstopic->includeFooter();
?>