<?php 
class packages {
	function getAllRecords($all='*',$where='',$orderby='',$limit=''){
		$sql ="select $all FROM tbl_packages WHERE 1=1 "; 
		if($where!=''){
			$sql .= " AND $where ";
		}
		if($orderby!=''){
			$sql .= " $orderby ";
		}
		if($limit!=''){
			$sql .= " $limit ";
		}
		return connection::execute($sql,'select-all');
	}
	function getAllpackagesFromIds($ids=null){
		if($ids!=null){
			$sql ="select * FROM tbl_packages WHERE FIND_IN_SET(package_id,'$ids')";
			return connection::execute($sql,'select-all');
		}	
	}
	function fromId($id){
		if($id && !is_null($id)){
			$sql ="select * FROM tbl_packages WHERE package_id = $id"; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	function getLastOrder(){
		$sql ="select * FROM tbl_packages ORDER BY package_order desc LIMIT 0,1 "; 
		$packagesDetails = connection::execute($sql,'select');
		if($packagesDetails && count($packagesDetails) > 0){
			return $packagesDetails->package_order;
		}
		return 0;
	}
	function getpackagesDetailsFromName($name){
		if($name && !is_null($name)){
			$sql ="select * FROM tbl_packages WHERE package_slug = '$name' "; 
			return connection::execute($sql,'select');
		}
		return false;
	}
	function addRecords($data){
		if($data && count($data) > 0){
			$sql ="INSERT INTO tbl_packages(package_title,package_slug,package_desc,package_short_desc,package_meta_title,package_meta_keywords,package_meta_desc,package_image,package_status)  VALUES ('".$data->package_title."', '".$data->package_slug."', '".$data->package_desc."', '".$data->package_short_desc."', '".$data->package_meta_title."', '".$data->package_meta_keywords."', '".$data->package_meta_desc."', '".$data->package_image."', '".$data->package_status."' )";
			
			return connection::execute($sql,'insert');
		}
		return false;
	}
	function updateRecords($data){
		if($data && !is_null($data)){
			$sql ="UPDATE tbl_packages SET package_title = '".$data->package_title."',package_slug = '".$data->package_slug."', package_desc = '".$data->package_desc."', package_short_desc = '".$data->package_short_desc."',  package_meta_title = '".$data->package_meta_title."', package_meta_keywords = '".$data->package_meta_keywords."', package_meta_desc = '".$data->package_meta_desc."', package_image = '".$data->package_image."', package_status = '".$data->package_status."' WHERE package_id = ".$data->package_id;
			return connection::execute($sql,'update');
		}
		return false;
	}
	function changeStatus($package_id,$status){
		if($status==0){
			$new_status = 1;
		}else{
			$new_status = 0;
		}
		$sql ="UPDATE tbl_packages SET package_status = '".$new_status."' WHERE package_id = ".$package_id;
		return connection::execute($sql,'update');
	}
	function deleteRecord($package_id){
		$sql ="DELETE FROM tbl_packages  WHERE package_id = ".$package_id;
		return connection::execute($sql,'delete');
	}
	function allAction($act_val,$checkArr){
		if($act_val=='delete_all'  && $checkArr && count($checkArr) > 0){
				$sql ="DELETE FROM tbl_packages  WHERE package_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'delete');
		}else if($act_val=='activate_all' && $checkArr && count($checkArr) > 0){
				$sql ="UPDATE tbl_packages SET package_status = '1'  WHERE package_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}else if($act_val=='inactivate_all' && $checkArr && count($checkArr) > 0){
				$sql ="UPDATE tbl_packages SET package_status = '0'  WHERE package_id IN(".implode(',',$checkArr).") " ;
				return connection::execute($sql,'update');
		}
	}
	
	function changeOrder($package_id,$package_order,$position){
		if($package_id!='' && $package_order!='' && $position!=''){
			$packagesDetails = $this->fromId($package_id);
	
			if($position=='Dn'){
				$qr="select package_order,package_id from tbl_packages where package_order > '".$package_order."' order by package_order asc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->package_id;
					$NewOrder = $result1->package_order;
					$qry = "UPDATE tbl_packages SET `package_order`= $package_order WHERE package_id =".$Id;
					connection::execute($qry,'update');
					$qry1 = "UPDATE tbl_packages SET `package_order`= $NewOrder WHERE package_id =".$package_id;
					connection::execute($qry1,'update');
				}
			}
			
			if($position=='Up'){
				$qr="select package_order,package_id from tbl_packages where package_order < '".$package_order."' order by package_order desc limit 0,1";
				$result1 = connection::execute($qr,'select');
				if($result1 && count($result1)>0){
					$Id=$result1->package_id;
					$NewOrder = $result1->package_order;
					$qry = "UPDATE tbl_packages SET `package_order`= $package_order WHERE package_id =".$Id;
					connection::execute($qry,'update');
					$qry1 = "UPDATE tbl_packages SET `package_order`= $NewOrder WHERE package_id =".$package_id;
					connection::execute($qry1,'update');
				}
			}
		}		
	}
}
?>