<?php 
class pages {
	function getAllRecords($all='*',$where='',$orderby='',$limit=''){
		$sql ="select $all FROM tbl_pages WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		if($orderby!=''){
			$sql .= " $orderby ";
		}
		if($limit!=''){
			$sql .= " $limit ";
		}
		return connection::execute($sql,'select-all');
	}
	function getAllpagesFromIds($ids=null){
		if($ids!=null){
			$sql ="select * FROM tbl_pages WHERE FIND_IN_SET(page_id,'$ids')";
			return connection::execute($sql,'select-all');
		}	
	}
	function fromId($id){
		if($id && !is_null($id)){
			$sql ="select * FROM tbl_pages WHERE page_id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	function getLastOrder(){
		$sql ="select * FROM tbl_pages ORDER BY page_order desc LIMIT 0,1 "; 
		$pagesDetails = connection::execute($sql,'select');
		if($pagesDetails && count($pagesDetails) > 0){
			return $pagesDetails->page_order;
		}
		return 0;
	}
	function getpagesDetailsFromName($name){
		if($name && !is_null($name)){
			$sql ="select * FROM tbl_pages WHERE page_slug = '$name' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	function addRecords($data){
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_pages(page_title,page_slug,page_desc,on_header,on_home,on_footer,page_meta_title,page_meta_keywords,page_meta_desc,page_order,page_banner,page_status,page_parent,page_short_desc)  VALUES ('".$data->page_title."', '".$data->page_slug."', '".$data->page_desc."', '".$data->on_header."','".$data->on_home."', '".$data->on_footer."', '".$data->page_meta_title."', '".$data->page_meta_keywords."', '".$data->page_meta_desc."', '".$data->page_order."', '".$data->page_banner."', '".$data->page_status."','".$data->page_parent."','".$data->page_short_desc."' )";
			return connection::execute($sql,'insert');
		}
		return false;
	}
	function updateRecords($data){
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_pages SET page_title = '".$data->page_title."',page_slug = '".$data->page_slug."', page_desc = '".$data->page_desc."', on_header = '".$data->on_header."',  on_home = '".$data->on_home."', on_footer = '".$data->on_footer."',  page_meta_title = '".$data->page_meta_title."', page_meta_keywords = '".$data->page_meta_keywords."', page_meta_desc = '".$data->page_meta_desc."', page_banner = '".$data->page_banner."', page_status = '".$data->page_status."', page_parent = '".$data->page_parent."', page_short_desc = '".$data->page_short_desc."' WHERE page_id = ".$data->page_id;
			return connection::execute($sql,'update');
		}
		return false;
	}
	function changeStatus($page_id,$status){
		if($status==0){
			$new_status = 1;
		}else{
			$new_status = 0;
		}
		$sql ="UPDATE tbl_pages SET page_status = '".$new_status."' WHERE page_id = ".$page_id;
		return connection::execute($sql,'update');
	}
	function deleteRecord($page_id){
		$sql ="DELETE FROM tbl_pages  WHERE page_id = ".$page_id;
		return connection::execute($sql,'delete');
	}
	function allAction($act_val,$checkArr){
		if($act_val=='delete_all'  && $checkArr && count($checkArr) > 0){
				$sql ="DELETE FROM tbl_pages  WHERE page_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'delete');
		}else if($act_val=='activate_all' && $checkArr && count($checkArr) > 0){
				$sql ="UPDATE tbl_pages SET page_status = '1'  WHERE page_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}else if($act_val=='inactivate_all' && $checkArr && count($checkArr) > 0){
				$sql ="UPDATE tbl_pages SET page_status = '0'  WHERE page_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}
	}
	
	function changeOrder($page_id,$page_order,$position){
		if($page_id!='' && $page_order!='' && $position!=''){
			$pagesDetails = $this->fromId($page_id);
	
			if($position=='Dn'){
				$qr="select page_order,page_id from tbl_pages where page_order > '".$page_order."' order by page_order asc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->page_id;
					$NewOrder = $result1->page_order;
					$qry = "UPDATE tbl_pages SET `page_order`= $page_order WHERE page_id =".$Id;
					connection::execute($qry,'update');
					$qry1 = "UPDATE tbl_pages SET `page_order`= $NewOrder WHERE page_id =".$page_id;
					connection::execute($qry1,'update');
				}
			}
			
			if($position=='Up'){
				$qr="select page_order,page_id from tbl_pages where page_order < '".$page_order."' order by page_order desc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->page_id;
					$NewOrder = $result1->page_order;
					$qry = "UPDATE tbl_pages SET `page_order`= $page_order WHERE page_id =".$Id;
					connection::execute($qry,'update');
					$qry1 = "UPDATE tbl_pages SET `page_order`= $NewOrder WHERE page_id =".$page_id;
					connection::execute($qry1,'update');
				}
			}
		}		
	}
}
?>