<?php 

class gallerys {
	
	function getAllRecords($all='*',$where='',$orderby='',$limit=''){
		
		$sql ="select $all FROM tbl_gallery WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		
		if($orderby!=''){
			$sql .= " $orderby ";
		}
		
		if($limit!=''){
			$sql .= " $limit ";
		}

		return connection::execute($sql,'select-all');
	}
	
	function getAllgallerysFromIds($ids=null){
		if($ids!=null){
			$sql ="select * FROM tbl_gallery WHERE FIND_IN_SET(gallery_id,'$ids')";
			return connection::execute($sql,'select-all');
		}	
	}
	
	function fromId($id){
		
		if($id && !is_null($id)){
			$sql ="select * FROM tbl_gallery WHERE gallery_id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	
	function getLastOrder(){
		$sql ="select * FROM tbl_gallery ORDER BY gallery_order desc LIMIT 0,1 "; 
		$gallerysDetails = connection::execute($sql,'select');
		if($gallerysDetails && count($gallerysDetails) > 0){
			return $gallerysDetails->gallery_order;
		}
		return 0;
	}
	
		
	
	function addRecords($data){
		
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_gallery(gallery_title,gallery_order,gallery_banner,on_sponcer,gallery_status,on_gallery,on_client_logo)  VALUES ('".$data->gallery_title."','".$data->gallery_order."', '".$data->gallery_banner."','".$data->on_sponcer."', '".$data->gallery_status."', '".$data->on_gallery."', '".$data->on_client_logo."' )";
			return connection::execute($sql,'insert');
		}
		return false;
	}
	
	function updateRecords($data){
		
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_gallery SET gallery_title = '".$data->gallery_title."', gallery_banner = '".$data->gallery_banner."',on_sponcer = '".$data->on_sponcer."', gallery_status = '".$data->gallery_status."', on_gallery = '".$data->on_gallery."', on_client_logo = '".$data->on_client_logo."' WHERE gallery_id = ".$data->gallery_id;
			return connection::execute($sql,'update');
		}
		return false;
	}
	
	function changeStatus($gallery_id,$status){

		if($status==0){
			$new_status = 1;
		}else{
			$new_status = 0;
		}
		$sql ="UPDATE tbl_gallery SET gallery_status = '".$new_status."' WHERE gallery_id = ".$gallery_id;
		return connection::execute($sql,'update');
	}
	
	function deleteRecord($gallery_id){
		$sql ="DELETE FROM tbl_gallery  WHERE gallery_id = ".$gallery_id;
		return connection::execute($sql,'delete');
	}
	
	function allAction($act_val,$checkArr){
		
		if($act_val=='delete_all'  && $checkArr && count($checkArr) > 0){

				$sql ="DELETE FROM tbl_gallery  WHERE gallery_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'delete');
				
		}else if($act_val=='activate_all' && $checkArr && count($checkArr) > 0){
				
				$sql ="UPDATE tbl_gallery SET gallery_status = '1'  WHERE gallery_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		
		}else if($act_val=='inactivate_all' && $checkArr && count($checkArr) > 0){

				$sql ="UPDATE tbl_gallery SET gallery_status = '0'  WHERE gallery_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}
	}
	
	function changeOrder($gallery_id,$gallery_order,$position){

		if($gallery_id!='' && $gallery_order!='' && $position!=''){
			$gallerysDetails = $this->fromId($gallery_id);
	
			if($position=='Dn'){
				$qr="select gallery_order,gallery_id from tbl_gallery where gallery_order > '".$gallery_order."' order by gallery_order asc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->gallery_id;
					$NewOrder = $result1->gallery_order;
					$qry = "UPDATE tbl_gallery SET `gallery_order`= $gallery_order WHERE gallery_id =".$Id;
					connection::execute($qry,'update');

					$qry1 = "UPDATE tbl_gallery SET `gallery_order`= $NewOrder WHERE gallery_id =".$gallery_id;
					connection::execute($qry1,'update');
				}
			}
			
			if($position=='Up'){
				$qr="select gallery_order,gallery_id from tbl_gallery where gallery_order < '".$gallery_order."' order by gallery_order desc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->gallery_id;
					$NewOrder = $result1->gallery_order;
					$qry = "UPDATE tbl_gallery SET `gallery_order`= $gallery_order WHERE gallery_id =".$Id;
					connection::execute($qry,'update');

					$qry1 = "UPDATE tbl_gallery SET `gallery_order`= $NewOrder WHERE gallery_id =".$gallery_id;
					connection::execute($qry1,'update');
				}
			}
		}		
	}
}

?>