<?php

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

define('IN_ADMIN',true);

$CALL_SYSTEM			        =	array();
$CALL_SYSTEM['ADSENSE']           =   true;

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBAdsenseMOD');

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

		if ($PowerBB->_CONF['member_permission'])
		{
			$PowerBB->template->display('header');


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


			if ($PowerBB->_GET['control'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_ControlMain();
				}
			}
			elseif ($PowerBB->_GET['add'])
			{
              	if ($PowerBB->_GET['main'])
				{
					$this->_AddAdsenseMain();
				}
                elseif ($PowerBB->_GET['start'])
				{
					$this->_AddAdsenseStart();
				}
			}
			elseif ($PowerBB->_GET['edit'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_EditMain();
				}
				elseif ($PowerBB->_GET['start'])
				{
					$this->_EditStart();
				}
			}
			elseif ($PowerBB->_GET['del'])
			{
                if ($PowerBB->_GET['start'])
				{
					$this->_DelStart();
				}
			}

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

	}


	/**
	 * add Adsense Main
	 */

	function _AddAdsenseMain()
	{
		global $PowerBB;

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

    }

	/**
	 * add Adsense Start
	 */
	function _AddAdsenseStart()
	{
		global $PowerBB;

		if (empty($PowerBB->_POST['name'])
			or empty($PowerBB->_POST['adsense']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_fill_in_all_the_information']);
		}


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

			$AdsenseArr['field']['name']            = 	 $PowerBB->_POST['name'];
			$AdsenseArr['field']['adsense'] 		= 	$PowerBB->_POST['adsense'];
			$AdsenseArr['field']['home'] 		    = 	$PowerBB->_POST['home'];
			$AdsenseArr['field']['forum'] 	    	= 	$PowerBB->_POST['forum'];
			$AdsenseArr['field']['topic'] 		    = 	$PowerBB->_POST['topic'];

			$insert = $PowerBB->adsense->InsertAdsense($AdsenseArr);

			if ($insert)
			{
	          $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Add_the_declaration_successfully']);
               $PowerBB->functions->redirect('admin.php?page=adsense&amp;control=1&amp;main=1');
			}

	}

	function _ControlMain()
	{
		global $PowerBB;

        // show Adsense List
		$AdsenseArr 					= 	array();
		$AdsenseArr['order']			=	array();
		$AdsenseArr['order']['field']	=	'id';
		$AdsenseArr['order']['type']	=	'DESC';
		$AdsenseArr['proc'] 			= 	array();
		$AdsenseArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['AdsensesList'] = $PowerBB->adsense->GetAdsenseList($AdsenseArr);

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




	function _EditMain()
	{
		global $PowerBB;

			if (empty($PowerBB->_GET['id']))
			{
				$PowerBB->functions->error('الإعلان المطلوب غير موجود');
			}

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

			$AdsenseEdit = $PowerBB->adsense->GetAdsenseInfo($AdsenseEditArr);

			$PowerBB->template->assign('AdsenseEdit',$AdsenseEdit);


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

	function _EditStart()
	{
		global $PowerBB;

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

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

		$AdsenseArr['field']['name']            = 	 $PowerBB->_POST['name'];
		$AdsenseArr['field']['adsense'] 		= 	$PowerBB->_POST['adsense'];
		$AdsenseArr['field']['home'] 		    = 	$PowerBB->_POST['home'];
		$AdsenseArr['field']['forum'] 	    	= 	$PowerBB->_POST['forum'];
		$AdsenseArr['field']['topic'] 		    = 	$PowerBB->_POST['topic'];
		$AdsenseArr['where'] 				= 	array('id',$PowerBB->_GET['id']);

		$update = $PowerBB->adsense->UpdateAdsense($AdsenseArr);

		if ($update)
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Announcement_has_been_updated_successfully']);
			$PowerBB->functions->redirect('admin.php?page=adsense&amp;control=1&amp;main=1');
		}
	}

	function _DelStart()
	{
		global $PowerBB;

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

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

			$del = $PowerBB->adsense->DeleteAdsense($DelArr);

		if ($del)
		{
				$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Ad_Deleted_successfully']);
				$PowerBB->functions->redirect('admin.php?page=adsense&amp;control=1&amp;main=1');

		}
	}

}

?>
