<?php 

require_once "connection.php";

class GetModel{

	static public function getData($table,$select){

		$selectArray = explode(",",$select);

		if(empty(Connection::getColumnsData($table,$selectArray))){

			return null;

		}

		$sql = "SELECT $select FROM $table";

		$stmt = Connection::connect()->prepare($sql);

		$stmt->execute();

		return $stmt->fetchAll(PDO::FETCH_CLASS);

	}

	static public function getDataFilter($table,$select,$linkTo,$equalTo){

		$selectArray = explode(",",$select);

		if(empty(Connection::getColumnsData($table,$selectArray))){

			return null;

		}

        $linkToArray = explode(",",$linkTo);
        $equalToArray = explode("_",$equalTo);
        
        $linkToText = "";
        
        if(count($linkToArray) > 1){
            
            foreach( $linkToArray as $key => $value){
                
                if($key > 0){
                    
                    $linkToText .= "AND ".$value." = :".$value." ";
                    
                }
                
            }
        }
        
        
		$sql = "SELECT $select FROM $table WHERE $linkToArray[0] = :$linkToArray[0] $linkToText";

		$stmt = Connection::connect()->prepare($sql);
		
		foreach ($linkToArray as $key => $value){
		    
		    $stmt->bindParam(":".$value,$equalToArray[$key],PDO::PARAM_STR);
		    
		}

		$stmt->bindParam(":".$linkTo,$equalTo,PDO::PARAM_STR);

		$stmt->execute();

		return $stmt->fetchAll(PDO::FETCH_CLASS);

	}

	static public function getDataRange($table,$select,$linkTo,$equalTo,$filterTo,$inTo){
	    
	    $filter = "";
	    
	    if($filterTo != null && $inTo != null){
	        
	       $filter = 'AND '.$filterTo.' IN ('.$inTo.')'; 
	        
	    }
	    
		$selectArray = explode(",",$select);
		
		if(empty(Connection::getColumnsData($table,$selectArray))){

			return null;

		}

		$sql = "SELECT $select FROM $table WHERE $linkTo = :$linkTo $filter";

		$stmt = Connection::connect()->prepare($sql);
		
		$stmt->bindParam(":".$linkTo,$equalTo,PDO::PARAM_STR);

		$stmt->execute();

		return $stmt->fetchAll(PDO::FETCH_CLASS);

	}
    static public function getDataFilterStatus($table,$select,$linkTo,$equalTo){

		$selectArray = explode(",",$select);

		if(empty(Connection::getColumnsData($table,$selectArray))){

			return null;

		}

		$sql = "SELECT $select FROM $table WHERE $linkTo = :$linkTo and Estado = 0";

		$stmt = Connection::connect()->prepare($sql);

		$stmt->bindParam(":".$linkTo,$equalTo,PDO::PARAM_STR);

		$stmt->execute();

		return $stmt->fetchAll(PDO::FETCH_CLASS);

	}

}

?>