<?php

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

define('IN_ADMIN',true);

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBPortalMOD');

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

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

			if ($PowerBB->_GET['control'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_ControlMain();
				}
                elseif ($PowerBB->_GET['start'])
				{
					$this->_ControlStart();
				}
			}
			elseif ($PowerBB->_GET['add_block'])
			{
              	if ($PowerBB->_GET['main'])
				{
					$this->_AddBlockMain();
				}
                elseif ($PowerBB->_GET['start'])
				{
					$this->_AddBlockStart();
				}
			}
			elseif ($PowerBB->_GET['control_blocks'])
			{
              	if ($PowerBB->_GET['main'])
				{
					$this->_ControlBlocksMain();
				}
                elseif ($PowerBB->_GET['start'])
				{
					$this->_ControlBlocksStart();
				}
			}
			elseif ($PowerBB->_GET['edit_block'])
			{
				if ($PowerBB->_GET['main'])
				{
					$this->_EditMain();
				}
				elseif ($PowerBB->_GET['start'])
				{
					$this->_EditStart();
				}
			}
			elseif ($PowerBB->_GET['del_block'])
			{
                if ($PowerBB->_GET['start'])
				{
					$this->_DelStart();
				}
			}

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

	}


	/**
	 * add Portal Main
	 */

	function _AddBlockMain()
	{
		global $PowerBB;

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

    }

	/**
	 * add Portal Start
	 */
	function _AddBlockStart()
	{
		global $PowerBB;

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

			$title         = 	 $PowerBB->_POST['title'];
			$text 		   = 	$PowerBB->_POST['text'];
			$place_block   = 	$PowerBB->_POST['place_block'];

		$sort = 0;
		if ($PowerBB->_POST['order_type'] == 'auto')
		{
          $blocks_info = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->prefix."blocks" . " ORDER BY sort DESC");
           $get_blocks_row = $PowerBB->DB->sql_fetch_array($blocks_info);

			// No section
			if (!$get_blocks_row)
			{
				$sort = 1;
			}
			// There is a section
			else
			{
				$sort = $get_blocks_row['sort'] + 1;
			}
		}
		else
		{
			$sort = $PowerBB->_POST['sort'];
		}

			$insert = $PowerBB->DB->sql_query('INSERT INTO ' . $PowerBB->prefix."blocks" . " SET title='$title',text='$text',place_block='$place_block',sort='$sort' ");

			if ($insert)
			{
	          $PowerBB->functions->msg('تمت اضافة البلوك بنجاح ..');
               $PowerBB->functions->redirect('admin.php?page=portal&amp;control_blocks=1&amp;main=1');
			}
	}

	function _ControlMain()
	{
		global $PowerBB;

        // show Portal List

		$SecArr 						= 	array();
		$SecArr['get_from']				=	'db';

		$SecArr['proc'] 				= 	array();
		$SecArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$SecArr['order']				=	array();
		$SecArr['order']['field']		=	'sort';
		$SecArr['order']['type']		=	'ASC';

		$SecArr['where']				=	array();
		$SecArr['where'][0]['name']		= 	'parent';
		$SecArr['where'][0]['oper']		= 	'=';
		$SecArr['where'][0]['value']	= 	'0';

		// Get main sections
		$cats = $PowerBB->section->GetSectionsList($SecArr);

		// We will use forums_list to store list of forums which will view in main page
		$PowerBB->_CONF['template']['foreach']['forums_list'] = array();

		// Loop to read the information of main sections
       foreach ($cats as $cat)
		{
	               // foreach main sections
				   $PowerBB->_CONF['template']['foreach']['forums_list'][$cat['id'] . '_m'] = $cat;
                    unset($sectiongroup);

	                   // Get main Forums
						$ForumArr 						= 	array();
						$ForumArr['get_from']				=	'db';

						$ForumArr['proc'] 				= 	array();
						$ForumArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

						$ForumArr['order']				=	array();
						$ForumArr['order']['field']		=	'sort';
						$ForumArr['order']['type']		=	'ASC';

						$ForumArr['where']				=	array();
						$ForumArr['where'][0]['name']		= 	'parent';
						$ForumArr['where'][0]['oper']		= 	'=';
						$ForumArr['where'][0]['value']	= 	$cat['id'];

						// Get parent sections
						$forums = $PowerBB->section->GetSectionsList($ForumArr);

						foreach ($forums as $forum)
						{
							$forum['sub'] .= ('<option value="' .$forum['id'] . '" selected="selected">---'  . $forum['title'] . '</option>');

               				$PowerBB->_CONF['template']['foreach']['forums_list'][$forum['id'] . '_f'] = $forum;

						}

						  // end foreach ($forums)
						unset($ForumArr);
			            $ForumArr = @mysql_free_result($ForumArr);
          }

    		 unset($SecArr);
			 $SecArr = @mysql_free_result($SecArr);

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

	function _ControlStart()
	{
		global $PowerBB;

        // show Portal List

		$update = array();
		$update[0] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['title_portal'],'var_name'=>'title_portal'));
		$update[1] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['active_portal'],'var_name'=>'active_portal'));
		$update[2] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['portal_section_news'],'var_name'=>'portal_section_news'));
		$update[3] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['portal_news_num'],'var_name'=>'portal_news_num'));
	    $update[4] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['portal_columns'],'var_name'=>'portal_columns'));
	    $update[5] = $PowerBB->info->UpdateInfo(array('value'=>$PowerBB->_POST['portal_news_along'],'var_name'=>'portal_news_along'));
		if ($update[0]
		    and $update[1]
			and $update[2]
			and $update[3]
            and $update[4]
            and $update[5])
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['updated_successfully_Please_wait']);
			$PowerBB->functions->redirect('admin.php?page=portal&amp;control=1&amp;main=1');
		}

	}

	function _ControlBlocksMain()
	{
		global $PowerBB;

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

	function _ControlBlocksStart()
	{
		global $PowerBB;

      $blocks_info = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->prefix."blocks" . " ORDER BY id ASC");
       while ($get_blocks_row = $PowerBB->DB->sql_fetch_array($blocks_info))
      {
			$title = 'title-' . $get_blocks_row['id'];
			$active = 'active-' . $get_blocks_row['id'];
			$place_block = 'place_block-' . $get_blocks_row['id'];
			$sort = 'sort-' . $get_blocks_row['id'];
				$t 	= 	$PowerBB->_POST[$title];
				$a 	= 	$PowerBB->_POST[$active];
				$p 	= 	$PowerBB->_POST[$place_block];
				$s 	= 	$PowerBB->_POST[$sort];
                $id = $get_blocks_row['id'];
               $update = $PowerBB->DB->sql_query('UPDATE ' . $PowerBB->prefix."blocks" . " SET title='$t',active='$a',place_block='$p',sort='$s'  WHERE " . $PowerBB->prefix."blocks" . ".id = '$id'");
     }
		if ($update)
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['updated_successfully_Please_wait']);
			$PowerBB->functions->redirect('admin.php?page=portal&amp;control_blocks=1&amp;main=1');
		}


	}

	function _EditMain()
	{
		global $PowerBB;

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

           $PowerBB->template->assign('id',$PowerBB->_GET['id']);

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

	function _EditStart()
	{
		global $PowerBB;

			if (empty($PowerBB->_GET['id']))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['The_declaration_does_not_exist']);
			}
            $id = $PowerBB->_GET['id'];
			$text 		   = 	$PowerBB->_POST['text'];
          $update = $PowerBB->DB->sql_query('UPDATE ' . $PowerBB->prefix."blocks" . " SET text='$text' WHERE " . $PowerBB->prefix."blocks" . ".id = '$id'");

		if ($update)
		{
			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['updated_successfully_Please_wait']);
			$PowerBB->functions->redirect('admin.php?page=portal&amp;control_blocks=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']);
			}
            $id = $PowerBB->_GET['id'];

			$del = $PowerBB->DB->sql_query('DELETE FROM ' . $PowerBB->prefix."blocks" . " WHERE id='$id'");

		if ($del)
		{
			$PowerBB->functions->msg('تم حذف البلوك بنجاح');
			$PowerBB->functions->redirect('admin.php?page=portal&amp;control_blocks=1&amp;main=1');
		}
	}

}

?>
