<?php

$CALL_SYSTEM = array();

$CALL_SYSTEM['PM'] 	= 	true;
$CALL_SYSTEM['WARNLOG'] 	= 	true;
$CALL_SYSTEM['SUPERMEMBERLOGS'] 			= 	true;

(!defined('IN_PowerBB')) ? die() : '';

include('common.php');

define('CLASS_NAME','PowerBBWarnMOD');

class PowerBBWarnMOD
{
	function run()
	{
		global $PowerBB;


			if ($PowerBB->_GET['index'])
			{
				$this->_MemberWarnIndex();
			}
			elseif($PowerBB->_GET['start'])
			{
				$this->_MemberWarnStart();
			}



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



	function _MemberWarnIndex()
	{
		global $PowerBB;
		$PowerBB->_GET['id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['id'],'intval');

     			if (empty($PowerBB->_GET['id']))
     		{
     			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
     		}
		$PowerBB->functions->ShowHeader();
		// Getting member info
		$MemArr = array();
		$MemArr['where'] = array('id',$PowerBB->_GET['id']);
		$PowerBB->_CONF['member_row'] = $PowerBB->member->GetMemberInfo($MemArr);
		$PowerBB->_CONF['template']['MemberInfo'] = $PowerBB->_CONF['member_row'];
		//////////

		// Getting member group
		$GroupInfo = array();
		$GroupInfo['where'] = array('id',$PowerBB->_CONF['member_row']['usergroup']);
		$PowerBB->_CONF['group_info'] = $PowerBB->group->GetGroupInfo($GroupInfo);
		//////////

		if (!$PowerBB->_CONF['member_permission'])
     	{
     		$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Visitors_can_not_use_your_warning']);
     	}
		elseif(!$PowerBB->_CONF['rows']['group_info']['send_warning'])
	{
		$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_have_no_powers_to_use_this_system']);
	}	elseif(!$PowerBB->_CONF['group_info']['can_warned'])
	{
		$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_notice_this_member']);
	}

     	$PowerBB->template->display('warn_send');
     }


 function _MemberWarnStart()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();

 			if (empty($PowerBB->_GET['id']))
     		{
     			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
     		}
		if (empty($PowerBB->_POST['text']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_enter_the_reason_for_warning_this_member']);
		}
		// Getting member info
		$MemArr = array();
		$MemArr['where'] = array('id',$PowerBB->_GET['id']);
		$PowerBB->_CONF['member_row'] = $PowerBB->member->GetMemberInfo($MemArr);
		///////
		$StartArr = array();
		$StartArr['field'] = array();

		$StartArr['field']['warnings'] 	= 	$PowerBB->_CONF['member_row']['warnings']+1;
		$StartArr['where'] =	array('id',$PowerBB->_GET['id']);

		$Warn = $PowerBB->member->UpdateMember($StartArr);


		if ($Warn)
		{
					$PowerBB->_POST['text'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'sql');
				$StartArr = array();
				$StartArr['field'] = array();
				$StartArr['field']['unread_pm'] 	= 	$PowerBB->_CONF['member_row']['unread_pm']+1;
				$StartArr['where'] =	array('id',$PowerBB->_GET['id']);
				$Update = $PowerBB->member->UpdateMember($StartArr);
				//////////
				$MsgArr 	= 	array();
				$MsgArr['get_id']=	true;
				$MsgArr['field']	=	array();
				$MsgArr['field']['user_from'] 	= 	$PowerBB->_CONF['rows']['member_row']['username'];
				$MsgArr['field']['user_to'] 	= 	$PowerBB->_CONF['member_row']['username'];
				$MsgArr['field']['title'] 		= 	$PowerBB->_CONF['template']['lang']['Warning'];
				$MsgArr['field']['text'] 		= 	$PowerBB->_POST['text'];
				$MsgArr['field']['date'] 		= 	date('Y-m-d', $PowerBB->_CONF['now']);
				$MsgArr['field']['icon'] 		= 	'look/images/icons/i1.gif';
				$MsgArr['field']['folder'] 		= 	'inbox';
				$Send = $PowerBB->pm->InsertMassege($MsgArr);
				$LogArr = array();
				$LogArr['field'] = array();
				$LogArr['field']['warn_from'] = $PowerBB->_CONF['rows']['member_row']['username'];
				$LogArr['field']['warn_to'] = $PowerBB->_CONF['member_row']['username'];
				$LogArr['field']['warn_text'] = $PowerBB->_POST['text'];
				$LogArr['field'] ['warn_date'] = date('Y-m-d', $PowerBB->_CONF['now']);
				$LogInsert = $PowerBB->warnlog->Insert($LogArr);

                 //  if warning number More The number of warnings suspended Member
				if ($PowerBB->_CONF['member_row']['warnings'] >= $PowerBB->_CONF['info_row']['warning_number_to_ban']-1)
				{
					$GrpArr 			= 	array();
					$GrpArr['where'] 	= 	array('id','6');

					$GroupInfo = $PowerBB->group->GetGroupInfo($GrpArr);

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

					$MemInfo = $PowerBB->member->GetMemberInfo($MemArr);

					$style = $GroupInfo['username_style'];
					$username_style_cache = str_replace('[username]',$MemInfo['username'],$style);


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

					$BandArr['field']['usergroup'] 	= 	'6';
		            $BandArr['field']['username_style_cache'] 		= 	$username_style_cache;
					$BandArr['field']['user_title'] 	= 	$PowerBB->_CONF['template']['lang']['suspended'];
					$BandArr['where'] =	array('id',$PowerBB->_GET['id']);

					$Band = $PowerBB->member->UpdateMember($BandArr);

				 if ($Band)
				 {
			        $SmLogsArr 			= 	array();
					$SmLogsArr['field']	=	array();

					$SmLogsArr['field']['username'] 	= 	$PowerBB->_CONF['rows']['member_row']['username'];
					$SmLogsArr['field']['edit_action'] 	= 	$PowerBB->_CONF['template']['lang']['Warning_by'] . $PowerBB->_CONF['member_row']['username'];
					$SmLogsArr['field']['subject_title']= 	$PowerBB->_CONF['template']['lang']['suspended_by'];
					$SmLogsArr['field']['subject_id'] 	= 	$PowerBB->_GET['id'];
					$SmLogsArr['field']['edit_date'] 	= 	date("d/m/Y", $PowerBB->_CONF['now']);

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

			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Was_warning_member_successfully']);
			$PowerBB->functions->redirect('index.php');
		}
		else
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['There_was_an_error_no_warning_Member']);
		}
	}
}

?>
