<?php
/**
 * List all questions
 *
 * PHP version 5.0
 *
 * @category	###ANOVA###
 * @package		###questions###
 * @author 		karthiselvam_75ag08
 * @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-26
 */
/**
 * 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']['blog_module'].'/admin/blogComment.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';
//compulsory
$CFG['site']['is_module_page']='blog';
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
$CFG['lang']['include_files'][] = 'common/configs/anova_configs/config_blogs.inc.php';
$CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';
require($CFG['site']['project_path'].'common/application_top.inc.php');

class blogList extends AnovaHandler
	{
		public $enabled_edit_fields = array();
		public $msg = '';

		/**
		 * blogList::buildSortQuery()
		 *
		 * @return
		 **/
		public function buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}

		/**
		 * blogList::buildConditionQuery()
		 *
		 * @return
		 */
		public function buildConditionQuery()
			{
				$this->sql_condition = 'blog_id=\''.$this->fields_arr['blog_id'].'\'';
				$this->sql_condition .= 'AND main_comment_id=0';
				if ($this->CFG['admin']['ignore_user'])
					{
						$this->sql_condition .= ' AND NOT EXISTS (SELECT 1 FROM '.$this->CFG['db']['tbl']['user_ignored'].' AS ui WHERE ui.user_id='.$this->CFG['user']['user_id'].' AND ui.ignored_id=bc.user_id)';
					}
			}

		/**
		 * blogList::showBlogsList()
		 *
		 * @return
		 **/
		public function showCommentsList()
			{
				global $smartyObj;
				$this->buildSelectQuery();
				$this->buildConditionQuery();
				$this->buildSortQuery();
				$this->buildQuery();
				$this->executeQuery();
				$this->showCommentsList_arr	=	array();
				if($this->isResultsFound())
					{
						$this->showCommentsList_arr['RecordCount']	=	1;
						$this->paging_arr = array('blog_id');
						$smartyObj->assign('smarty_paging_list', $this->populatePageLinksGET($this->getFormField('start'),$this->paging_arr));
						$inc	=	0;
						$this->showCommentsList_arr['row']	=	array();
						$i = 0;
						while($row = $this->fetchResultRecord())
							{
								$i++;
								$this->showCommentsList_arr['row'][$inc]['record']	=	$row;
								$this->showCommentsList_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->showCommentsList_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->showCommentsList_arr['row'][$inc]['date_added']	=	'';
								if($row['date_added'] != '')
									{
										$this->showCommentsList_arr['row'][$inc]['date_added']	=	$this->getTimeDiffernceFormat($row['date_added']);
									}
								$userDetails = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $row['user_id']);
								$this->showCommentsList_arr['row'][$inc]['name']	=	$name = $userDetails['display_name'];
								$this->showCommentsList_arr['row'][$inc]['commentId']	=	$commentId = $row['comment_id'];
								$this->showCommentsList_arr['row'][$inc]['anchor']	=	$anchor = 'dAlt_'.$row['comment_id'];
								$this->showCommentsList_arr['row'][$inc]['anchor1']	=	$anchor1 = 'adAlt_'.$row['comment_id'];
								$this->showCommentsList_arr['row'][$inc]['liFormId']	=	$liFormId = 'liForm_'.$row['comment_id'];
						    	$this->showCommentsList_arr['row'][$inc]['liAnsId']	=	$liAnsId = 'liAns_'.$row['comment_id'];
								$this->showCommentsList_arr['row'][$inc]['wordWrapManual_cmd']	=	wordWrapManual($row['comment'], $this->CFG['admin']['blog']['line_length']);
								$this->showCommentsList_arr['row'][$inc]['stripString_name']	=	stripString($name, $this->CFG['username']['short_length']);
								if($row['status'] == 'ToActivate')
									{
											$this->showCommentsList_arr['row'][$inc]['publish_Confirmation']	=	'return Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'act\',\'comment_id\',\'msgConfirmText\'), Array(\'publish\',\''.$row['comment_id'].'\', \''.$this->LANG['blogcomment_confirm_message_publish'].'\'), Array(\'value\',\'value\',\'innerHTML\'));';
									}
								$this->showCommentsList_arr['row'][$inc]['delete_Confirmation']	=	'return Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'act\',\'comment_id\',\'msgConfirmText\'), Array(\'delete\',\''.$row['comment_id'].'\', \''.$this->LANG['blog_cmd_delete_confirmation'].'\'), Array(\'value\',\'value\',\'innerHTML\'));';
								if ($this->hasReply($row['comment_id']))
									{
										$this->showCommentsList_arr['row'][$inc]['fetchReply']	=	$this->fetchReply($row['comment_id']);
									}

								$this->showCommentsList_arr['row'][$inc]['video_id'] = $row['video_id'];
								$this->showCommentsList_arr['row'][$inc]['ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blogcomment_'.$row['video_id'], '?pg=Blogcomment_'.$row['video_id'], 'members');
								$this->showCommentsList_arr['row'][$inc]['ans_view_video']['on_click'] = 'blog_video_'.$row['blog_video'];

								$this->showCommentsList_arr['row'][$inc]['audio_id'] = $row['audio_id'];
								$this->showCommentsList_arr['row'][$inc]['ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blogcomment_'.$row['audio_id'], '?pg=Blogcomment_'.$row['audio_id'], 'members');
								$this->showCommentsList_arr['row'][$inc]['ans_view_audio']['on_click'] = 'blog_audio_'.$row['audio_id'];

								$this->showCommentsList_arr['row'][$inc]['external_video_id'] = $row['external_video_id'];
								$this->showCommentsList_arr['row'][$inc]['ext_ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blogcomment_'.$row['external_video_id'], '?pg=Blogcomment_'.$row['external_video_id'], 'members');
  								$this->showCommentsList_arr['row'][$inc]['ext_ans_view_video']['on_click'] = 'external_video_blog_'.$row['external_video_id'];

								$this->showCommentsList_arr['row'][$inc]['external_audio_id'] = $row['external_audio_id'];
								$this->showCommentsList_arr['row'][$inc]['ext_ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blogcomment_'.$row['external_audio_id'], '?pg=Blogcomment_'.$row['external_audio_id'], 'members');
								$this->showCommentsList_arr['row'][$inc]['ext_ans_view_audio']['on_click'] = 'external_audio_blog_'.$row['external_audio_id'];

								$inc++;
							}
						$this->showCommentsList_arr['publish_Confirmation_1']	=	'{Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'comment_id\', \'act\', \'msgConfirmText\'), Array(multiCheckValue, \'publish\', \''.$this->LANG['blogcomment_confirm_message_publish'].'\'), Array(\'value\', \'value\', \'innerHTML\'));}';
						$this->showCommentsList_arr['delete_Confirmation_1']	=	'{Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'comment_id\', \'act\', \'msgConfirmText\'), Array(multiCheckValue, \'delete\', \''.$this->LANG['blog_cmd_delete_confirmation'].'\'), Array(\'value\', \'value\', \'innerHTML\'));}';
					}
				else
					{
						$this->showCommentsList_arr['RecordCount']	=	0;
					}
				return $this->showCommentsList_arr;
			}

		/**
		 * blogList::resetFieldsArray()
		 *
		 * @return
		 */
		public function resetFieldsArray()
			{
				$this->setFormField('ajax_page', '');
				$this->setFormField('blog_id', '');
				$this->setFormField('act', '');
				$this->setFormField('comment', '');
				$this->setFormField('audio_id', '');
				$this->setFormField('video_id', '');
				$this->setFormField('external_audio_id', '');
				$this->setFormField('external_video_id', '');
				$this->setFormField('reason', '');
			}

		/**
		 * manageBlog::populateBlogsValue()
		 *
		 * @return
		 **/
		public function populateBlogsValue()
			{
				$sql = 'SELECT blog_id, subject, message, video_id, audio_id, external_video_id, external_audio_id, accept_comments, user_id, status,'.
						' UNIX_TIMESTAMP(date_added) AS date_added, total_views, total_comments'.
						' FROM '.$this->CFG['db']['tbl']['blogs'].' WHERE'.
						' blog_id='.$this->dbObj->Param('blog_id');

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

				if ($row = $rs->FetchRow())
				    {
				     	$row['date_added'] = ($row['date_added'] != '') ? $this->getTimeDiffernceFormat($row['date_added']) : '';
						$this->fields_arr['subject'] = $row['subject'];
						$this->fields_arr['message'] = $row['message'];
						$this->fields_arr['date_added'] = $row['date_added'];
						$this->fields_arr['user_id'] = $row['user_id'];
						$this->fields_arr['blog_video'] = $row['video_id'];
						$this->fields_arr['blog_audio'] = $row['audio_id'];
						$this->fields_arr['blog_ext_video'] = $row['external_video_id'];
						$this->fields_arr['blog_ext_audio'] = $row['external_audio_id'];
						$this->fields_arr['total_views'] = $row['total_views'];
						$this->fields_arr['total_comments'] = $row['total_comments'];
						$this->fields_arr['accept_comments'] = $row['accept_comments'];
						return true;
				    }
				return false;
			}

		/**
		 * blogList::displayBlog()
		 *
		 * @return
		 */
		public function displayBlog()
			{
				$this->displayBlog_arr	=	array();
				$this->displayBlog_arr['userDetails']	=	$userDetails =	$this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $this->fields_arr['user_id']);
				$this->displayBlog_arr['name']	=	$name = $userDetails['display_name'];
				$this->displayBlog_arr['anchor']	=	$anchor = 'abuseContent';
				$this->displayBlog_arr['wordWrapManual']	=	wordWrapManual($this->fields_arr['message'], $this->CFG['admin']['blog']['line_length']);
				$this->displayBlog_arr['stripString']	=	stripString($name, $this->CFG['username']['short_length']);
				$this->displayBlog_arr['manageBlogs_url']	=	$this->CFG['site']['relative_url'].'manageBlogs.php?act=edt&amp;blog_id='.$this->fields_arr['blog_id'].'&amp;redirect=1';
				$this->displayBlog_arr['Confirmation']	=	'return Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'act\', \'comment_id\',\'msgConfirmText\'), Array(\'delete_blog\',\''.$this->fields_arr['blog_id'].'\', \''.$this->LANG['blog_confirm_message'].'\'), Array(\'value\', \'value\', \'innerHTML\'));';

				if($this->fields_arr['video_id']) $this->displayBlog_arr['video_class'] = 'clsNoBorder';
				if($this->fields_arr['audio_id']) $this->displayBlog_arr['video_class'] = '';

				$this->displayBlog_arr['ext_audio_class'] ='clsNoBorder';
				$this->displayBlog_arr['ext_video_class'] ='';

				if($this->fields_arr['external_video_id']) $this->displayBlog_arr['ext_video_class'] = 'clsNoBorder';
				if($this->fields_arr['external_audio_id']) $this->displayBlog_arr['ext_video_class'] = '';

				$this->displayBlog_arr['ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blog_'.$this->fields_arr['blog_video'], '?pg=Blog_'.$this->fields_arr['blog_video'], 'members');
				$this->displayBlog_arr['ans_view_video']['on_click'] = 'blog_video_'.$this->fields_arr['blog_video'];

				$this->displayBlog_arr['ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blog_'.$this->fields_arr['blog_audio'], '?pg=Blog_'.$this->fields_arr['blog_audio'], 'members');
				$this->displayBlog_arr['ans_view_audio']['on_click'] = 'blog_audio_'.$this->fields_arr['blog_audio'];

				$this->displayBlog_arr['ext_ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blog_'.$this->fields_arr['blog_ext_video'], '?pg=Blog_'.$this->fields_arr['blog_ext_video'], 'members');
  				$this->displayBlog_arr['ext_ans_view_video']['on_click'] = 'external_video_blog_'.$this->fields_arr['blog_ext_video'];

				$this->displayBlog_arr['ext_ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blog_'.$this->fields_arr['blog_ext_audio'], '?pg=Blog_'.$this->fields_arr['blog_ext_audio'], 'members');
				$this->displayBlog_arr['ext_ans_view_audio']['on_click'] = 'external_audio_blog_'.$this->fields_arr['blog_ext_audio'];

				return $this->displayBlog_arr;
			}

		/**
		 * blogList::incrementViews()
		 *
		 * @return
		 */
		public function incrementViews()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].
						' SET total_views=total_views+1'.
						' WHERE blog_id='.$this->dbObj->Param('blog_id');

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

		/**
		 * ViewVideo::getComment()
		 *
		 * @return
		 **/
		public function getComment()
			{
				$sql = 'SELECT comment FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE comment_id='.$this->dbObj->Param('comment_id');

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

				if($row = $rs->FetchRow())
					{
						return $row['comment'];
					}
			}

		/**
		 * ViewVideo::updateCommentAndVideoTable()
		 *
		 * @return
		 **/
		public function updateCommentAndVideoTable()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].
						' SET comment='.$this->dbObj->Param('comment').
						' WHERE comment_id='.$this->dbObj->Param('comment_id');

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

				echo $this->getFormField('comment_id');
				echo '***--***!!!';
				echo $this->getFormField('f');
			}

		/**
		 * Publish the comment
		 */
		public function publishComment()
			{
				$comment_ids = addslashes($this->fields_arr['comment_id']);
				$comment_ids = explode(',', $comment_ids);
				$comment_ids = '\''.implode('\',\'', $comment_ids).'\'';
				//COMMENT SENTER MAIL
				$sql = 'SELECT `user_id`,comment,blog_id
						FROM `blog_comment`
						WHERE `comment_id` IN('.$comment_ids.') AND status=\'Toactivate\' ';

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

				if ($rs->PO_RecordCount())
					{
						while($row = $rs->FetchRow())
							{
								$this->sendPublishedMailAlert($row['user_id'],'Comment',$row['blog_id'],$row['comment']);
							}
					}
				//die();
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].
						' SET status=\'Active\' WHERE comment_id IN ('.$comment_ids.') AND status=\'Toactivate\' ';

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

				if($this->dbObj->Affected_Rows())
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].
								' SET total_comments=total_comments+'.$this->dbObj->Affected_Rows().
								' WHERE blog_id='.$this->dbObj->Param('blog_id').
								' AND accept_comments=\'Yes\'';

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

			}

		/**
		 * publish sent mail;
		 */
		 public function sendPublishedMailAlert($userid, $mailtype, $blog_id, $content_description)
			{
				$select_query = 'SELECT `user_id`,subject,seo_title,message
						         FROM `blogs`
								 WHERE `blog_id`=('.$blog_id.')';

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

				if ($rs->PO_RecordCount())
					{
						while($row = $rs->FetchRow())
							{
								$mail_content_seo = $row['seo_title'];
								$mail_content = $row['subject'];
								$owner_id = $row['user_id'];
							}
					}
				$toDetails = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $userid);
				$site_link = "<a target=\"_blank\" href=\"".URL($this->CFG['site']['url'])."\">".URL($this->CFG['site']['url'])."</a>";
				if($mailtype == 'Comment')
					{
						$url_link = getUrl('blogcomment','?title='.$mail_content_seo, $mail_content_seo.'/', 'root');
						$comment_link = "<a target=\"_blank\" href=\"".$url_link."\">".$mail_content."</a>";
						$subject = $this->getMailContent($this->LANG['comment_publish_subject'], array('VAR_USERNAME'=>$toDetails['display_name'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_DESCRIPTION_OF_COMMENT'=>$content_description, 'VAR_COMMENT'=>$comment_link, 'VAR_LINK'=>$site_link));
						$content = $this->getMailContent($this->LANG['comment_publish_message'], array('VAR_USERNAME'=>$toDetails['display_name'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_DESCRIPTION_OF_COMMENT'=>$content_description, 'VAR_COMMENT'=>$comment_link, 'VAR_LINK'=>$site_link));
					}
				$this->_sendMail($toDetails['email'],
							$subject,
							$content,
							$this->CFG['site']['noreply_name'],
							$this->CFG['site']['noreply_email']);
				if($owner_id !='')
					{
						$toDetails = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $owner_id);
						$site_link = "<a target=\"_blank\" href=\"".URL($this->CFG['site']['url'])."\">".URL($this->CFG['site']['url'])."</a>";
						$url_link = getUrl('blogcomment', '?title='.$mail_content_seo,$mail_content_seo.'/','root');
						$comment_link = "<a target=\"_blank\" href=\"".$url_link."\">".$mail_content."</a>";
						$subject = $this->getMailContent($this->LANG['comment_publish_subject_owner'], array('VAR_USERNAME'=>$toDetails['display_name'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_DESCRIPTION_OF_COMMENT'=>$content_description, 'VAR_COMMENT'=>$comment_link, 'VAR_LINK'=>$site_link));
						$content = $this->getMailContent($this->LANG['comment_publish_message_owner'], array('VAR_USERNAME'=>$toDetails['display_name'], 'VAR_SITENAME'=>$this->CFG['site']['name'], 'VAR_DESCRIPTION_OF_COMMENT'=>$content_description, 'VAR_COMMENT'=>$comment_link, 'VAR_LINK'=>$site_link));
					}
					$this->_sendMail($toDetails['email'],
							$subject,
							$content,
							$this->CFG['site']['noreply_name'],
							$this->CFG['site']['noreply_email']);
			}

		/**
		 * blogList::_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());
			}

		/**
		 * blogList::deleteComment()
		 *
		 * @return
		 */
		public function deleteComment()
			{
				$del_count = 0;
				//deleting replies
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE main_comment_id IN('.$this->dbObj->Param('comment_id').')';

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

				$dsql = 'SELECT count(comment_id)as dcnt FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE comment_id IN('.$this->fields_arr['comment_id'].') AND status=\'ToActivate\'';

				$dstmt = $this->dbObj->Prepare($dsql);
				$drs = $this->dbObj->Execute($dstmt);
			    if (!$drs)
				    trigger_db_error($this->dbObj);

				$row = $drs->FetchRow();
				$del_count = $row['dcnt'];
				//deleting comments
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE comment_id IN('.$this->fields_arr['comment_id'].')';

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

				#echo $this->dbObj->Affected_Rows();die();
				if($this->dbObj->Affected_Rows())
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].
								' SET total_comments = total_comments-'.$this->dbObj->Affected_Rows().'+'.$del_count.
								' WHERE blog_id='.$this->dbObj->Param('blog_id');
						//die($sql);

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

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function deleteReply()
			{
				//deleting comments
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE comment_id IN('.$this->dbObj->Param('comment_id').')';

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

		/**
		 * blogList::deleteBlog()
		 *
		 * @return
		 */
		public function deleteBlog()
			{
				$blog_ids = $this->fields_arr['blog_id'];
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['blogs'].
						' WHERE blog_id IN('.$blog_ids.')';

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

				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['blog_comment'].
						' WHERE blog_id IN('.$blog_ids.')';

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

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function updateBlogAbuseCount()
		    {
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].' SET abuse_count=abuse_count+1'.
						' WHERE blog_id='.$this->dbObj->Param('blog_id');

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

		/**
		 * blogList::updateAbuseBlog()
		 *
		 * @return
		 */
		public function updateAbuseBlog()
			{
				$sql = 'SELECT abuse_id FROM '.$this->CFG['db']['tbl']['abuse_blog'].
						' WHERE blog_id='.$this->dbObj->Param('blog_id').
						' AND reported_by='.$this->dbObj->Param('reported_by');

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

				if (!$rs->PO_RecordCount())
					{
						$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['abuse_blog'].
								' SET blog_id='.$this->dbObj->Param('blog_id').
								', reported_by='.$this->dbObj->Param('reported_by').
								', reason='.$this->dbObj->Param('reason').
								', date_abused=NOW()';

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

						if ($this->dbObj->Affected_Rows())
							{
								$this->updateBlogAbuseCount();
							}
					}
			}

		/**
		 * blogList::fetchReply()
		 *
		 * @param mixed $mainComment_id
		 * @return
		 */
		public function fetchReply($mainComment_id)
			{
				$this->fetchReply_arr	=	array();
				if (!$this->repliesArray)
					return ;
				$inc	=	0;
				foreach($this->repliesArray as $row)
					{
						$this->fetchReply_arr[$inc]['record']	=	$row;
						$this->fetchReply_arr[$inc]['anchor']	=	$anchor = 'reply'.$row['comment_id'];
						$this->fetchReply_arr[$inc]['date_added']	=	($row['date_added'] != '') ? $this->getTimeDiffernceFormat($row['date_added']) : '';
						$this->fetchReply_arr[$inc]['userDetails']	=	$userDetails = $this->getUserDetailsFromUsersTable($this->CFG['db']['tbl']['users'], $row['user_id']);
						$this->fetchReply_arr[$inc]['name']	=	$name = $userDetails['display_name'];
						$this->fetchReply_arr[$inc]['wordWrapManual_comment']	=	wordWrapManual($row['comment'], $this->CFG['admin']['blog']['line_length']);
						$this->fetchReply_arr[$inc]['stripString_name']	=	stripString($name, $this->CFG['username']['short_length']);
						if ($this->CFG['user']['user_id']==$row['user_id'] OR $this->CFG['user']['user_id']==$this->fields_arr['user_id'])
						{
							$this->fetchReply_arr[$inc]['deletereplies_Confirmation']	=	'return Confirmation(\'selDelMsgConfirm\', \'msgConfirmform\', Array(\'act\',\'comment_id\', \'main_comment_id\',\'msgConfirmText\'), Array(\'deletereplies\',\''.$row['comment_id'].'\', \''.$row['main_comment_id'].'\', \''.$this->LANG['blogcomment_confirm_reply_message'].'\'), Array(\'value\',\'value\',\'value\',\'innerHTML\'));';
	                    }
						$inc++;
					}
				return $this->fetchReply_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function hasReply($mainComment_id)
			{
				$sql = 'SELECT comment_id, blog_id, user_id, comment, main_comment_id, UNIX_TIMESTAMP(date_added) AS date_added'.
						' FROM '.$this->CFG['db']['tbl']['blog_comment'].' AS bc'.
						' WHERE main_comment_id='.$this->dbObj->Param('comment_id');
				if ($this->CFG['admin']['ignore_user'])
					{
						$sql .= ' AND NOT EXISTS (SELECT 1 FROM '.$this->CFG['db']['tbl']['user_ignored'].' AS ui WHERE ui.user_id='.$this->CFG['user']['user_id'].' AND ui.ignored_id=bc.user_id)';
					}
				$sql .= ' ORDER BY comment_id DESC';

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

				$this->repliesArray = array();
				while($row = $rs->FetchRow())
					{
						$this->repliesArray[] = $row;
					}
				return $this->repliesArray;
			}
	}
//--------------------Code begins-------------->>>>>//
//print_r($_POST);die();
$blogList = new blogList();
if(!chkIsAllowedModule(array('blog')))
	Redirect2URL(getUrl($CFG['redirect']['dsabled_module_url']['file_name']));

$blogList->left_navigation_div = 'blogBlog';

//set blocks
$blogList->setPageBlockNames(array('msg_form_info','selCommentListBlock', 'selAcceptCommentBlock', 'selBlogDetails'));
$blogList->setFormField('f', '');
$blogList->setFormField('msg', '');
$blogList->setFormField('type', '');
$blogList->setFormField('page', '');
$blogList->setFormField('comment_id', '');
$blogList->setFormField('main_comment_id', '');
$blogList->setFormField('liFormId', '');
$blogList->setFormField('liAnsId', '');
$blogList->setFormField('blog_id', '');
$blogList->setFormField('answer', '');
/*********** Page Navigation Start *********/
$blogList->setFormField('orderby_field', 'comment_id');
$blogList->setFormField('orderby', 'DESC');
$blogList->setFormField('start', '0');
$blogList->setFormField('numpg', $CFG['data_tbl']['numpg']);
$blogList->setMinRecordSelectLimit(2);
$blogList->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$blogList->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
$blogList->setTableNames(array($CFG['db']['tbl']['blog_comment'].' AS bc'));
$blogList->setReturnColumns(array('comment_id', 'blog_id', 'bc.user_id', 'comment','status', 'TIME_TO_SEC(TIMEDIFF(NOW(), date_added)) AS date_edit', 'UNIX_TIMESTAMP(date_added) AS date_added', 'video_id', 'audio_id', 'external_video_id', 'external_audio_id'));
/************ page Navigation stop *************/
//set Form Fields
$blogList->resetFieldsArray();
$blogList->sanitizeFormInputs($_REQUEST);
//show default pages
if($blogList->populateBlogsValue())
	{
		if($blogList->isFormPOSTed($_POST, 'confirm'))
			{
				if($blogList->getFormField('act') == 'publish')
					{
						$blogList->publishComment();
						Redirect2Url ('blogComment.php?blog_id='.$blogList->getFormField('blog_id').'&msg=pub');
						die();
					}
				if($blogList->getFormField('act') == 'delete')
					{
						$blogList->deleteComment();
						Redirect2Url ('blogComment.php?blog_id='.$blogList->getFormField('blog_id').'&msg=del');
						die();
					}
				elseif($blogList->getFormField('act') == 'deletereplies')
					{
						$blogList->deleteReply();
						Redirect2Url ('blogComment.php?blog_id='.$blogList->getFormField('blog_id').'&msg=delreply');
					}
				else
					{
						$blogList->deleteBlog();
						Redirect2Url ('manageBlogs.php?del=succ');
						die();
					}
			}
		if($blogList->isFormGETed($_GET, 'msg'))
			{
				if($blogList->getFormField('msg')=='pub' AND $blogList->isValidFormInputs())
					{
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonSuccessMsg($LANG['comment_publish_success']);
					}
				if($blogList->getFormField('msg')=='add' AND $blogList->isValidFormInputs())
					{
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonSuccessMsg($LANG['blog_cmd_comment_post_success']);
					}
				if($blogList->getFormField('msg')=='del' AND $blogList->isValidFormInputs())
					{
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonSuccessMsg($LANG['blog_cmd_comment_deleted_successfully']);
					}
				if($blogList->getFormField('msg')=='delreply' AND $blogList->isValidFormInputs())
					{
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonSuccessMsg($LANG['reply_deleted_successfully']);
					}
				if($blogList->getFormField('msg')=='edited' AND $blogList->isValidFormInputs())
					{
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonSuccessMsg($LANG['blog_edited_successfully']);
					}
			}
		$blogList->incrementViews();
		$blogList->setPageBlockShow('selBlogDetails');
		$blogList->setPageBlockShow('selCommentListBlock');
		$LANG['page_title'] = wordWrapManual($blogList->getFormField('subject'), $CFG['admin']['blog']['line_length']);
		if($blogList->isFormPOSTed($_POST, 'confirm_action'))
			{
				$blogList->chkIsNotEmpty('reason', $LANG['err_tip_compulsory']);
				if ($blogList->isValidFormInputs())
					{
						$blogList->updateAbuseBlog();
						$blogList->setPageBlockShow('block_msg_form_success');
						$blogList->setCommonErrorMsg($LANG['blog_cmd_abused_successfully']);
					}
					else
						{
							$blogList->setCommonErrorMsg($LANG['blog_cmd_err_msg_reason_empty']);
							$blogList->setPageBlockShow('block_msg_form_error');
						}
			}
	}
else
	{
		$blogList->setCommonErrorMsg($LANG['blog_cmd_invalid_blogs_id']);
		$blogList->setPageBlockShow('block_msg_form_error');
	}
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
?>
<?php
	if($blogList->isShowPageBlock('selBlogDetails'))
		{
	    	$blogList->selBlogDetails['displayBlog']	=	$blogList->displayBlog();
			if(isMember())
				{
					$blogList->selBlogDetails['anchor']	=	$anchor = 'abuseContent';
				}
		}
	if($blogList->isShowPageBlock('selCommentListBlock'))
		{
			$blogList->selCommentListBlock['hidden_array']	=	array('start', 'blog_id');
			$blogList->selCommentListBlock['showCommentsList']	=	$blogList->showCommentsList();
		}
//include the header file
$blogList->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['blog_module']);
$smartyObj->display('blogComment.tpl');
//includ the footer of the page
?>
<script language="javascript" type="text/javascript" src="<?php echo $CFG['site']['url'];?>js/blogs.js"></script>
<script language="javascript" type="text/javascript">
	var site_url = '<?php echo $CFG['site']['url'];?>';
	var block_arr = new Array('selEditPhotoComments','selMsgAbuseConfirm', 'selMsgConfirm', 'selDelMsgConfirm');
	var replace_url = '<?php echo $CFG['site']['url'].'login.php';?>';
	var deleteConfirmation = "<?php echo $LANG['blogcomment_confirm_reply_message'];?>";
	var total_comments = 10;
	var minimum_counts = 10000;
</script>
<script language="javascript" type="text/javascript">
	function showdiv(divid){
		document.getElementById[divid].style.display = '';
	}
</script>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$blogList->includeFooter();
?>