<?php

	//-------------------------------------------------------------------------------------
	// Class DBGeneric
	//-------------------------------------------------------------------------------------

	class DBGeneric {

    //-------------------------------------------------------------------------------------
    // Function DBGeneric::getFilterSql()
    //
    // $parameterList['filterValue']
    // $parameterList['filterValueType']
    // $parameterList['filterOperator']
    // $parameterList['filterList']
    //-------------------------------------------------------------------------------------

		function getFilterSql($parameterList)
		{			
      global $dbFunction;

			$filterOperator = '';
      
      extract($parameterList);

			$filterSql = "";
			
      if ((($filterValueType == "string")&($filterValue != ""))|
					(($filterValueType == "integer")&($filterValue >0))|
					(($filterValueType == "list")&($filterValue != ""))|
					($filterValueType == "boolean")|
					(($filterValueType == "booleanInteger")&($filterValue < 2))|
					(($filterValueType == "booleanDate")&($filterValue < 2))|
					(($filterValueType == "date")&($filterValue !=""))|
					(($filterValueType == "time")&($filterValue !=""))) { 

				// Determine if string value seperated by OR  					
				$operator = sprintf(" AND ");
				if ($filterValueType=="string") {
					if (strpos($filterValue, 'OR') !== false) { 
						$operator = " OR ";
						$filterValue = trim(str_replace("OR", "", $filterValue));
					};
				}

				// Split the filter value by any number of space characters
				$filterValueList = preg_split("/[\s]+/", $filterValue);

				$filterSql = sprintf(" and ("); 
				$filterSqlList = [];
								
				foreach ($filterValueList as $filterValue) {
					$filterSqlListValue = [];
  				foreach ($filterList as $filterName) { 
  					switch ($filterValueType) { 
  						case "string" : 
								$sql = sprintf("(%s like '%%%s%%')", $filterName, $filterValue);
								if ($filterOperator=="=") { $sql = sprintf("(%s = '%s')", $filterName, $filterValue); }
								break;
  						case "list" : 
								$sql = sprintf("(%s in (%s))", $filterName, $filterValue); 
								break;
  						case "date" : 
								$filterValue = $dbFunction->getMysqlDate($filterValue);
  						case "time" : 
								$sql = sprintf("(%s %s '%s')", $filterName, $filterOperator, $filterValue); 
								break;								
  						case "boolean" : 
								$sql = ($filterValue==1) ? sprintf("(%s = 'true')", $filterName, $filterValue) : sprintf("(%s = 'false')", $filterName); 
								break;
  						case "booleanDate" : 
								$sql = ($filterValue==1) ? sprintf("(%s != '1000-01-01')", $filterName) : sprintf("(%s = '1000-01-01')", $filterName); 
								break;
  						default : 
								$sql = sprintf("(%s %s %s)", $filterName, $filterOperator, $filterValue); 
								break;
  					}
  					array_push($filterSqlListValue,$sql);
  				}
					$filterSql = sprintf("(%s)", implode(" or ", $filterSqlListValue));
					array_push($filterSqlList, $filterSql);
				}
				// Reduce number of filter values to two
				$filterSqlList = array_slice($filterSqlList, 0, 2);
				$filterSql = sprintf(" and (%s)", implode($operator, $filterSqlList)); 
			}
						
			return $filterSql;
		}
    
    //-------------------------------------------------------------------------------------

  }

?>