<?php
(!defined('IN_PowerBB')) ? die() : '';
include('common.php');
define('CLASS_NAME','PowerBBManagementMOD');
class PowerBBManagementMOD
{
	function run()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();

		$PowerBB->_GET['section'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['section'],'intval');

			if ($PowerBB->_GET['do_replys'])
			{
				if ($PowerBB->_POST['do'] == 'moveposts')
				{
					$this->_Moveposts();
				}
				elseif ($PowerBB->_GET['startmoveposts'])
				{
					$this->_StartMovePosts();
				}
				elseif ($PowerBB->_POST['do'] == 'deleteposts')
				{
					$this->_Deleteposts();
				}
				elseif ($PowerBB->_GET['startdeleteposts'])
				{
					$this->_StartDeleteposts();
				}
				elseif ($PowerBB->_POST['do'] == 'approveposts')
				{
					$this->_Approveposts();
				}
				elseif ($PowerBB->_POST['do'] == 'unapproveposts')
				{
					$this->_Unapproveposts();
				}
				elseif ($PowerBB->_GET['operator'] == 'delete')
				{
					$this->__ReplyDelete();
				}
				elseif ($PowerBB->_GET['start_del_reply'])
				{
					$this->__ReplyDeleteStart();
				}

			}



		$PowerBB->functions->GetFooter();
	}


	function _Moveposts()
	{
		global $PowerBB;

			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');

		if (empty($PowerBB->_POST['check']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_select_any_posts']);
		}

		if (empty($PowerBB->_GET['subject_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

	 $PowerBB->template->assign('subject_id',$PowerBB->_GET['subject_id']);
      $PowerBB->template->display('replys_move_index');
	 $Replys_M = $PowerBB->_POST['check'];
      	$x = 0;
       foreach ($Replys_M as $GetReplys)
       {
		$Replys =intval($GetReplys);
		$PowerBB->template->assign('replys',$Replys);
			$x += 1;
	      $PowerBB->template->assign('x',$x);
       $PowerBB->template->display('replys_move_index2');
		}

	}

	function _StartMovePosts()
	{
		global $PowerBB;

        $PowerBB->_POST['subject_id'] = str_replace($PowerBB->functions->GetForumAdress()."index.php?page=topic&show=1&id=","", $PowerBB->_POST['subject_id'] );
        $PowerBB->_POST['subject_id'] = str_replace($PowerBB->functions->GetForumAdress()."t","", $PowerBB->_POST['subject_id'] );
        $urlhtml = ".html";
        $PowerBB->_POST['subject_id'] = str_replace($urlhtml,'', $PowerBB->_POST['subject_id'] );
		$PowerBB->_POST['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['subject_id'],'intval');

		if (empty($PowerBB->_POST['check']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_select_any_posts']);
		}

		if (empty($PowerBB->_POST['subject_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_enter_the_link_to_the_topic']);
		}


        $SubjectArr = array();
		$SubjectArr['where'] = array('id',$PowerBB->_POST['subject_id']);

		$SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);

        $SubjectOldArr = array();
		$SubjectOldArr['where'] = array('id',$PowerBB->_POST['subject_old']);

		$SubjectInfoOld = $PowerBB->subject->GetSubjectInfo($SubjectOldArr);

				$SecArr 			= 	array();
		        $SecArr['where'] 	= 	array('id',$SubjectInfoOld['section']);

		        $this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);


		if (!$SubjectInfo)
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_requested_topic_does_not_exist']);
		}

		$Reply_M = $PowerBB->_POST['check'];
       foreach ($Reply_M as $GetReply)
       {

            /////
			$ReplyArr = array();
			$ReplyArr['where'] = array('id',intval($GetReply));

			$ReplyInfo = $PowerBB->reply->GetReplyInfo($ReplyArr);

           ///////
			$UpdateArr 					= 	array();
			$UpdateArr['subject_id']	=	$PowerBB->_POST['subject_id'];
			$UpdateArr['where'] 		= 	array('id',intval($GetReply));

			$update = $PowerBB->reply->MoveReply($UpdateArr);
            //////
			$UpdateArr 				= 	array();
			$UpdateArr['field']		= 	array();

			$UpdateArr['field']['section'] 		= 	$SubjectInfo['section'];
			$UpdateArr['field']['subject_id'] 		= 	$PowerBB->_POST['subject_id'];

			$UpdateArr['where'] 				= 	array('id',intval($GetReply));

			$update = $PowerBB->reply->UpdateReply($UpdateArr);
            /////

				if ($this->SectionInfo['last_subjectid'] == $PowerBB->_POST['subject_old'])
				{
				 	/**
				 	 *Update Section Cache ;)
				 	 */
                    $SectionCache = $SubjectInfoOld['section'];
					// The number of section's subjects number
					$reply_num = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['reply'] . " WHERE section = '$SectionCache' "));

					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();
					$UpdateArr['field']['reply_num'] 	= 	$reply_num;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateReplyNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$reply_num));


					$subject_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['subject'] . " WHERE section = '$SectionCache' "));

					// The number of section's subjects number
					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();

					$UpdateArr['field']['subject_num'] 	= 	$subject_nm;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateSubjectNumber(array('subject_num'	=>	$subject_nm));


                    $subject_id = $PowerBB->_POST['subject_old'];
					$GetLastqueryReplyForm = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['reply'] . " WHERE subject_id = '$subject_id' AND delete_topic<>1 AND review_reply<>1 ORDER by write_time DESC");
					$GetLastReplyForm = $PowerBB->DB->sql_fetch_array($GetLastqueryReplyForm);

					if (!$GetLastReplyForm)
					{
					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$SubjectInfo['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$SubjectInfo['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$PowerBB->_POST['subject_old'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$SubjectInfo['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
					}
					else
					{

					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$GetLastReplyForm['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$GetLastReplyForm['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$GetLastReplyForm['subject_id'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$GetLastReplyForm['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
				   }

					// Get Section Info
					$SecaArr 			= 	array();
					$SecaArr['where'] 	= 	array('id',$SectionCache);

					$this->SecInfo = $PowerBB->section->GetSectionInfo($SecaArr);

					// Update section's cache
					$UpdateArr 				= 	array();
					$UpdateArr['parent'] 	= 	$this->SecInfo['parent'];

					$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);


					unset($UpdateArr);
				}


	     }


            $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Specific_posts_have_been_moved_successfully']);
			$PowerBB->functions->redirect('index.php?page=topic&show=1&id=' . $PowerBB->_POST['subject_id']);



	}

	function _Deleteposts()
	{
		global $PowerBB;

			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');

		if (empty($PowerBB->_POST['check']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_select_any_posts']);
		}

		if (empty($PowerBB->_GET['subject_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}


	 	 $SubjectInfoArr				=	array();
	     $SubjectInfoArr['where'] 	= 	array('id',$PowerBB->_GET['subject_id']);

		 $SubjectDelInfo = $PowerBB->subject->GetSubjectInfo($SubjectInfoArr);

	     $PowerBB->template->assign('SubjectDelInfo',$SubjectDelInfo);
	     $PowerBB->template->assign('subject',$PowerBB->_GET['subject_id']);

	  $PowerBB->template->assign('subject_id',$PowerBB->_GET['subject_id']);
	  $PowerBB->template->assign('section_id',$SubjectDelInfo['section']);
      $PowerBB->template->display('replys_delet_index');
	  $Replys_D = $PowerBB->_POST['check'];
       foreach ($Replys_D as $GetReplys)
       {
		$PowerBB->template->assign('replys',$GetReplys);
       $PowerBB->template->display('replys_delet_index2');
		}

	}


	function _StartDeleteposts()
	{
		global $PowerBB;

		$Reply_D = $PowerBB->_POST['check'];
       foreach ($Reply_D as $GetReply)
       {


            /////

		    if ($PowerBB->_POST['deletetype'] == 1)
		     {
			   $UpdateArr 			= array();
			   $UpdateArr['where'] = array('id',intval($GetReply));

		     	$update = $PowerBB->reply->MoveReplyToTrash($UpdateArr);
			 }
		   else
		     {
				  $DeleteReplyArr				=	array();
		          $DeleteReplyArr['where'] 	= 	array('id',intval($GetReply));
				  $delReply = $PowerBB->reply->DeleteReply($DeleteReplyArr);
		     }


				$SecArr 			= 	array();
		        $SecArr['where'] 	= 	array('id',$PowerBB->_POST['section_id']);

		        $this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

		        $SubjectArr = array();
				$SubjectArr['where'] = array('id',$PowerBB->_POST['subject_id']);

				$SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);

				$UpdateSubjectNumber = $PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$PowerBB->_CONF['info_row']['reply_number']));

                    $subject_id = $PowerBB->_POST['subject_id'];
					$Getlast_replier = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['reply'] . " WHERE subject_id = '$subject_id' AND delete_topic<>1 AND review_reply<>1 ORDER by write_time DESC");
					$GetLast_replierForm = $PowerBB->DB->sql_fetch_array($Getlast_replier);
					if (!$GetLast_replierForm)
					{
					$last_replier = '';
					}
					else
					{
					$last_replier = $GetLast_replierForm['writer'];
					}

		     		$UpdateArr 					= 	array();
		     		$UpdateArr['field']			=	array();

		     		$UpdateArr['field']['reply_num'] 	= 	$this->SectionInfo['reply_num'] - 1;
		     		$UpdateArr['field']['last_replier'] 	= 	$last_replier;
		     		$UpdateArr['where']					= 	array('id',$this->SectionInfo['id']);

 				if ($this->SectionInfo['last_subjectid'] == $PowerBB->_POST['subject_id'])
				{
				 	/**
				 	 *Update Section Cache ;)
				 	 */
                    $SectionCache = $PowerBB->_POST['section_id'];
					// The number of section's subjects number
					$reply_num = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['reply'] . " WHERE section = '$SectionCache' "));

					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();
					$UpdateArr['field']['reply_num'] 	= 	$reply_num;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateReplyNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$reply_num));


					$subject_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['subject'] . " WHERE section = '$SectionCache' "));

					// The number of section's subjects number
					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();

					$UpdateArr['field']['subject_num'] 	= 	$subject_nm;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateSubjectNumber(array('subject_num'	=>	$subject_nm));


                    $subject_id = $PowerBB->_POST['subject_id'];
					$GetLastqueryReplyForm = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['reply'] . " WHERE subject_id = '$subject_id' AND delete_topic<>1 AND review_reply<>1 ORDER by write_time DESC");
					$GetLastReplyForm = $PowerBB->DB->sql_fetch_array($GetLastqueryReplyForm);

					if (!$GetLastReplyForm)
					{
					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$SubjectInfo['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$SubjectInfo['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$PowerBB->_POST['subject_id'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$SubjectInfo['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
					}
					else
					{

					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$GetLastReplyForm['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$GetLastReplyForm['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$GetLastReplyForm['subject_id'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$GetLastReplyForm['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
				   }

					// Get Section Info
					$SecaArr 			= 	array();
					$SecaArr['where'] 	= 	array('id',$SectionCache);

					$this->SecInfo = $PowerBB->section->GetSectionInfo($SecaArr);

					// Update section's cache
					$UpdateArr 				= 	array();
					$UpdateArr['parent'] 	= 	$this->SecInfo['parent'];

					$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);


					unset($UpdateArr);
				}
		  }



            $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Has_been_deleted_successfully_identified_Posts']);
			$PowerBB->functions->redirect('index.php?page=topic&show=1&id=' . $PowerBB->_POST['subject_id']);

	}

	function _Approveposts()
	{
		global $PowerBB;

			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');

		if (empty($PowerBB->_POST['check']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_select_any_posts']);
		}

		$Reply_M = $PowerBB->_POST['check'];
       foreach ($Reply_M as $GetReply)
       {
 			$UpdateArr 				= 	array();
			$UpdateArr['field']		= 	array();
			$UpdateArr['field']['review_reply'] 		= 	'0';
			$UpdateArr['where'] 				= 	array('id',intval($GetReply));

			$update = $PowerBB->reply->UpdateReply($UpdateArr);


				$ReplyArr 			= 	array();
		        $ReplyArr['where'] 	= 	array('id',intval($GetReply));

		        $this->ReplyInfo = $PowerBB->reply->GetReplyInfo($ReplyArr);


				$SecArr 			= 	array();
		        $SecArr['where'] 	= 	array('id',$this->ReplyInfo['section']);

		        $this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

		      $UpdateSectionCache = $PowerBB->functions->UpdateSectionCache($this->ReplyInfo['section']);



	    }


            $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Approved_posts_specific_successfully']);
			$PowerBB->functions->redirect('index.php?page=topic&show=1&id=' . $PowerBB->_GET['subject_id']);
   }

	function _Unapproveposts()
	{
		global $PowerBB;

			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');

		if (empty($PowerBB->_POST['check']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_select_any_posts']);
		}

		$Reply_M = $PowerBB->_POST['check'];
       foreach ($Reply_M as $GetReply)
       {
 			$UpdateArr 				= 	array();
			$UpdateArr['field']		= 	array();
			$UpdateArr['field']['review_reply'] 		= 	'1';
			$UpdateArr['where'] 				= 	array('id',intval($GetReply));

			$update = $PowerBB->reply->UpdateReply($UpdateArr);

				$ReplyArr 			= 	array();
		        $ReplyArr['where'] 	= 	array('id',intval($GetReply));

		        $this->ReplyInfo = $PowerBB->reply->GetReplyInfo($ReplyArr);


				$SecArr 			= 	array();
		        $SecArr['where'] 	= 	array('id',$this->ReplyInfo['section']);

		        $this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

		      $UpdateSectionCache = $PowerBB->functions->UpdateSectionCache($this->ReplyInfo['section']);


	     }

            $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Was_not_to_approve_the_posts_identified_successfully']);
			$PowerBB->functions->redirect('index.php?page=topic&show=1&id=' . $PowerBB->_GET['subject_id']);
   }

	function __ReplyDelete()
	{
		global $PowerBB;
			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');
			$PowerBB->_GET['section'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['section'],'intval');
			$PowerBB->_GET['reply_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['reply_id'],'intval');


		if (empty($PowerBB->_GET['subject_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}
		if (empty($PowerBB->_GET['reply_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}
		if (empty($PowerBB->_GET['section']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

		if ($PowerBB->_CONF['group_info']['group_mod'] == '1')
		{
			if ($PowerBB->_CONF['group_info']['del_reply'] == '0')
			{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['error_permission']);
			}
		}
        else
		{
			if ($PowerBB->_CONF['group_info']['id'] != '1'
			   AND  $PowerBB->_CONF['group_info']['id'] != '2')
			{
			    $SubjectArr = array();
			    $SubjectArr['where'] = array('id',$PowerBB->_GET['subject_id']);
			    $SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);
				if ($SubjectInfo['close'] == '1')
				{
				 $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['This_topic_is_locked']);
				}
			}
		}


       $SecGroupArr                    =    array();
       $SecGroupArr['where']              =    array();

       $SecGroupArr['where'][0]          =    array();
       $SecGroupArr['where'][0]['name']    =    'section_id';
       $SecGroupArr['where'][0]['oper']    =    '=';
       $SecGroupArr['where'][0]['value']    =    $PowerBB->_GET['section'];

       $SecGroupArr['where'][1]          =    array();
       $SecGroupArr['where'][1]['con']       =    'AND';
       $SecGroupArr['where'][1]['name']    =    'group_id';
       $SecGroupArr['where'][1]['oper']    =    '=';
       $SecGroupArr['where'][1]['value']    =    $PowerBB->_CONF['group_info']['id'];

       // Finally get the permissions of group
       $this->SectionGroup = $PowerBB->group->GetSectionGroupInfo($SecGroupArr);
       if (!$this->SectionGroup['del_own_reply'])
       {
		 $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['error_permission']);
       }


	 	 $SubjectInfoArr				=	array();
	     $SubjectInfoArr['where'] 	= 	array('id',$PowerBB->_GET['subject_id']);

		 $SubjectDelInfo = $PowerBB->subject->GetSubjectInfo($SubjectInfoArr);

	     $PowerBB->template->assign('SubjectDelInfo',$SubjectDelInfo);
	     $PowerBB->template->assign('subject_id',$PowerBB->_GET['subject_id']);
	     $PowerBB->template->assign('reply_id',$PowerBB->_GET['reply_id']);
	     $PowerBB->template->assign('section_id',$PowerBB->_GET['section']);


	     $PowerBB->template->display('reply_delete_index');


    }

	function __ReplyDeleteStart()
	{
		global $PowerBB;

		if ($PowerBB->_CONF['group_info']['group_mod'] == '1')
		{
			if ($PowerBB->_CONF['group_info']['del_reply'] == '0')
			{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['error_permission']);
			}
		}

			$PowerBB->_GET['subject_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['subject_id'],'intval');
			$PowerBB->_GET['section'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['section'],'intval');
			$PowerBB->_GET['reply_id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['reply_id'],'intval');



		if (empty($PowerBB->_GET['subject_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}
		if (empty($PowerBB->_GET['reply_id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}
		if (empty($PowerBB->_GET['section']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

				$SecArr 			= 	array();
		        $SecArr['where'] 	= 	array('id',$PowerBB->_GET['section']);

		        $this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);


        $SubjectArr = array();
		$SubjectArr['where'] = array('id',$PowerBB->_GET['subject_id']);

		$SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);

	  if ($PowerBB->_POST['deletetype'] == 1)
	  {
		$UpdateArr 			= array();
		$UpdateArr['where'] = array('id',$PowerBB->_GET['reply_id']);

		$update = $PowerBB->reply->MoveReplyToTrash($UpdateArr);

      }
      else
      {
       			  $DeleteReplyArr				=	array();
		          $DeleteReplyArr['where'] 	= 	array('id',$PowerBB->_GET['reply_id']);
				  $delReply = $PowerBB->reply->DeleteReply($DeleteReplyArr);


					$UpdateSubjectNumber = $PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$PowerBB->_CONF['info_row']['reply_number']));

		     		$UpdateArr 					= 	array();
		     		$UpdateArr['field']			=	array();

		     		$UpdateArr['field']['reply_num'] 	= 	$this->SectionInfo['reply_num'] - 1;
		     		$UpdateArr['where']					= 	array('id',$this->SectionInfo['id']);

		     		$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);

                    $subject_id = $PowerBB->_GET['subject_id'];
					$Getlast_replier = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['reply'] . " WHERE subject_id = '$subject_id' AND delete_topic<>1 AND review_reply<>1 ORDER by write_time DESC");
					$GetLast_replierForm = $PowerBB->DB->sql_fetch_array($Getlast_replier);
					if (!$GetLast_replierForm)
					{
					$last_replier = '';
					}
					else
					{
					$last_replier = $GetLast_replierForm['writer'];
					}
			   //////////
					 // Update Subject
					$SubjectArr = array();
					$SubjectArr['where'] = array('id',$PowerBB->_GET['subject_id']);

					$SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);

                    $SubjectArr 							= 	array();
					$SubjectArr['field'] 					= 	array();
					$SubjectArr['field']['reply_number'] 	= 	$SubjectInfo['reply_number'] -1;
					$SubjectArr['field']['last_replier'] 	= 	$last_replier;
					$SubjectArr['where'] 					= 	array('id',$PowerBB->_GET['subject_id']);

					$update = $PowerBB->subject->UpdateSubject($SubjectArr);
       }

  				if ($this->SectionInfo['last_subjectid'] == $PowerBB->_GET['subject_id'])
				{
				 	/**
				 	 *Update Section Cache ;)
				 	 */
                    $SectionCache = $PowerBB->_GET['section'];
					// The number of section's subjects number
					$reply_num = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['reply'] . " WHERE section = '$SectionCache' "));

					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();
					$UpdateArr['field']['reply_num'] 	= 	$reply_num;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateReplyNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$reply_num));


					$subject_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['subject'] . " WHERE section = '$SectionCache' "));

					// The number of section's subjects number
					$UpdateArr 					= 	array();
					$UpdateArr['field']			=	array();

					$UpdateArr['field']['subject_num'] 	= 	$subject_nm;
					$UpdateArr['where']					= 	array('id',$SectionCache);

					$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);


					$PowerBB->cache->UpdateSubjectNumber(array('subject_num'	=>	$subject_nm));


                    $subject_id = $PowerBB->_GET['subject_id'];
					$GetLastqueryReplyForm = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['reply'] . " WHERE subject_id = '$subject_id' AND delete_topic<>1 AND review_reply<>1 ORDER by write_time DESC");
					$GetLastReplyForm = $PowerBB->DB->sql_fetch_array($GetLastqueryReplyForm);

					if (!$GetLastReplyForm)
					{
					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$SubjectInfo['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$SubjectInfo['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$PowerBB->_GET['subject_id'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$SubjectInfo['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
					}
					else
					{

					// Update Last subject's information in Section Form
					$UpdateLastFormSecArr = array();
					$UpdateLastFormSecArr['field']			=	array();

					$UpdateLastFormSecArr['field']['last_writer'] 		= 	$GetLastReplyForm['writer'];
					$UpdateLastFormSecArr['field']['last_subject'] 		= 	$GetLastReplyForm['title'];
					$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$GetLastReplyForm['subject_id'];
					$UpdateLastFormSecArr['field']['last_date'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['last_time'] 	= 	$GetLastReplyForm['write_time'];
					$UpdateLastFormSecArr['field']['icon'] 		    = 	$GetLastReplyForm['icon'];
					$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
					$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

					$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

					// Update Last Form Sec subject's information
					$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
				   }

					// Get Section Info
					$SecaArr 			= 	array();
					$SecaArr['where'] 	= 	array('id',$SectionCache);

					$this->SecInfo = $PowerBB->section->GetSectionInfo($SecaArr);

					// Update section's cache
					$UpdateArr 				= 	array();
					$UpdateArr['parent'] 	= 	$this->SecInfo['parent'];

					$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);


					unset($UpdateArr);
				}
			// INSERT moderators Action
			$EditAction				=	array();
		    $EditAction['where'] 	= 	array('id',$PowerBB->_GET['subject_id']);

			$action = $PowerBB->subject->GetSubjectInfo($EditAction);

		    $subject_title = $action['title'];
		    $time=time()+$PowerBB->_CONF['info_row']['timestamp'];

	        $SmLogsArr 			= 	array();
			$SmLogsArr['field']	=	array();

			$SmLogsArr['field']['username'] 	= 	$PowerBB->_CONF['member_row']['username'];
			$SmLogsArr['field']['edit_action'] 	= 	$PowerBB->_CONF['template']['lang']['Delete_reply_ID'] . $PowerBB->_GET['reply_id'];
			$SmLogsArr['field']['subject_title']= 	$PowerBB->functions->CleanVariable($subject_title,'sql');
			$SmLogsArr['field']['subject_id'] 	= 	$PowerBB->_GET['subject_id'];
			$SmLogsArr['field']['edit_date'] 	= 	date("d/m/Y", $time);

			$insert = $PowerBB->supermemberlogs->InsertSupermemberlogs($SmLogsArr);

			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Deleted_reply_successfully']);
			$PowerBB->functions->redirect('index.php?page=topic&amp;show=1&amp;id=' . $PowerBB->_GET['subject_id']);

	}

}
?>
