<?php
session_start();
include_once("class/class.MySQL.php");
$db	=new MySQL();

$action = $_GET['action'];

if($action == "listList"){

	$rssList = $db->fetchAll("news_updates",'','id DESC');
	echo '{"news":'.json_encode($rssList).'}';


}elseif($action == "listAllNews"){

	$rssList = $db->fetchAll("news_updates",'','id DESC');
	
	
		//////////////////////////////user rights checking//////////////////////////////////////

$sub_rights=mysql_fetch_array(mysql_query("select * from menu  where  menu_name='Organization News'  "));

//////////////////////////////user rights checking//////////////////////////////////////
	$count = count($rssList);
	
	$userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['m_menu_id']	= $sub_rights['menu_id'];
	if($count > 0)
	{
	
	   if($_SESSION['usertype']=='Superadmin' || $_SESSION['branchadmin']=='Y')
	   {
	   $rssList[count($rssList)-1]['permissions'][0] = array("user_id"=>$userid);
	   }
	   else
	   {
	   $rssList[count($rssList)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	   }
	}
	else
	{
		   if($_SESSION['usertype']=='Superadmin' || $_SESSION['branchadmin']=='Y')
	   {
	   $rssList[0]['permissions'][0] = array("user_id"=>$userid);
	   }
	   else
	   {
	   $rssList[0]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	   }
	}
	
	echo '{"news":'.json_encode($rssList).'}';


}elseif($action == "addNews"){


	$data['news_title'] 		= $_POST['news_title'];
	$data['news_description'] 	= $_POST['news_description'];
	
	if(!isset($_POST['news_id'])){	
		$data['created_time']		= date("Y-m-d H:i:s");
		$db->Insert($data,"news_updates");
	}else{
		$where['id'] = $_POST['news_id'];
		$db->Update('news_updates',$data,$where);
	}

}

elseif($action == "getNews"){
	$where['id'] = $_GET['id'];
	$news = $db->Select("news_updates",$where);
	echo json_encode($news);
}
elseif($action == "del"){
	$where['id'] = $_POST['id'];
	$db->Delete("news_updates",$where);

}


?>