<?php 
 
/**********************************************************************/
/*	insertSingleQuery($arr,$table);
/*	insertMultipleQuery($mainArr,$table);
/*	deleteQuery($table,$condArr="");
/*	updateQuery($arr,$table,$condArr="");
/*	retrieveSingleQuery($fieldArr,$table,$condArr="",$order="");
/*	retrieveMultipleQuery($fieldArr,$table,$condArr="",$order="");
/**********************************************************************/

class condbClass{
    private $host      = DB_HOST;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;
 
    private $dbh;
    private $error;
 
    public function __construct(){
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
        $options = array(
            PDO::ATTR_PERSISTENT    => false,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        ); 
        try{ 
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

	public function query($query){
		$this->stmt = $this->dbh->prepare($query);
	}
	
	public function oldquery($query){
		$this->stmt = $this->dbh->query($query);
	}
	
	

	public function insertSingleQuery($arr,$table){
		try{	
			$arrM = array();
			foreach($arr as $arrInd => $arrVal):
				$arrM[]  = $arrInd;
				//$arrMV[] = ":".$arrVal;
				$arrMV[] = ":".$arrInd;
			endforeach;
			
			$arM 	= implode(",",$arrM);
			$arMV 	= implode(",",$arrMV);
			self::query("INSERT INTO $table ($arM) VALUES ($arMV)");  
			foreach($arr as $arrInd => $arrVal):
				self::bind(':'.$arrInd, $arrVal);
			endforeach;
			return self::execute();
		} catch (PDOException $e){
			$this->error = $e->getMessage();
			return false;
		}
 
	}
	
	public function insertMultipleQuery($mainArr,$table){
		try{	
			$arrM = array();
			$arrMV = array(); 
			foreach($mainArr[0] as $arrInd => $arrVal):
				$arrM[]  = $arrInd;
				$arrMV[] = ":".$arrInd;
			endforeach;
			$ctrMain 	= count($mainArr);
			$arM 		= implode(",",$arrM);
			$arMV 		= implode(",",$arrMV);
			self::beginTransaction(); 
			self::query("INSERT INTO $table ($arM) VALUES ($arMV)");
			for($a=0; $a<$ctrMain; $a++){
				foreach($mainArr[$a] as $arrInd => $arrVal):
					self::bind(':'.$arrInd, $arrVal);
				endforeach;
				if(self::execute()==false){
					self::cancelTransaction();
				}
			}
			self::endTransaction();
			return true;
		} catch (PDOException $e){
			$this->error = $e->getMessage();
			self::cancelTransaction();
			return false;
		} 
	}
	
	public function deleteQuery($table,$condArr=""){
		try{
			$ctrCond 		= count($condArr);
			$delCond 		= array();
			$condIndex 		= "";
			$condIndex2 	= "";
			$deCond 		= "";
			$conDelCond 	= "";
			$cond 			= "";
			if(!empty($condArr)):
				foreach($condArr as $condInd => $condVal):
					$condIndex 	= $condInd;
					$condIndex2 = ":".$condInd;
					$condValue 	= $condVal;
					if($ctrCond==0):
					$deCond 	= $condIndex."=".$condIndex2;
					else:
					$delCond[] 	= $condIndex."=".$condIndex2;
					endif;				
				endforeach;
					$conDelCond = implode(" AND ",$delCond);
			endif; 
				$cond = (!empty($condArr)) ? " WHERE ".$deCond.$conDelCond : "";
				
			self::query("DELETE FROM $table $cond");
			if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				self::bind($condInd, $condVal);
			endforeach;
			endif;
			return self::execute(); 
		} catch (PDOException $e){
			$this->error = $e->getMessage();
			return false;
		}
	}
	
	public function updateQuery($arr,$table,$condArr=""){
		try{
			$arrM = array();
			foreach($arr as $arrInd => $arrVal):
				$arrM[]  = $arrInd."=".":".$arrInd; 
			endforeach;
			$arM 			= implode(",",$arrM);
			$condIndex 		= "";
			$condValue 		= "";
			$editCond  		= array();
			$edCond 		= "";
			$conEditCond 	= "";

			$ctrCond = count($condArr);
			if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				$condIndex 	= $condInd;
				$condIndex2 = ":".$condInd;
				$condValue 	= $condVal;
				if($ctrCond==0):
				$edCond = $condIndex."=".$condIndex2;
				else:
				$editCond[] = $condIndex."=".$condIndex2;
				endif;				
			endforeach;

				$conEditCond = implode(" AND ",$editCond);
			endif;

			$cond = (!empty($condArr)) ? " WHERE ".$edCond.$conEditCond : "";
 //echo "UPDATE $table SET $arM $cond";
			self::query("UPDATE $table SET $arM $cond"); 
			if(!empty($condArr)):
				foreach($condArr as $condInd => $condVal):
						self::bind($condInd, $condVal);
				endforeach; 
			endif;
			 
			foreach($arr as $arrInd => $arrVal):
				self::bind(':'.$arrInd, $arrVal);
			endforeach;
			return self::execute();
		} catch (PDOException $e){
			$this->error = $e->getMessage();
			return false;
		}
	
	}
	 
	
	public function retrieveSingleQuery($fieldArr,$table,$condArr="",$order=""){ 
		$fields = "";
		$edCond = "";
		$fields = implode(",",$fieldArr);
		$ctrCond = count($condArr);
		if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				$condIndex 	= $condInd;
				$condIndex2 = ":".$condInd;
				$condValue 	= $condVal;
				if($ctrCond==0):
				$edCond = $condIndex."=".$condIndex2;
				else:
				$editCond[] = $condIndex."=".$condIndex2;
				endif;				
			endforeach;
			
			$conEditCond = implode(" AND ",$editCond);
		endif;

		$cond = (!empty($condArr)) ? " WHERE ".$edCond.$conEditCond : ""; 
		$orders = (!empty($order)) ? "ORDER BY $order" : "";
		self::query("SELECT $fields FROM $table $cond $orders");
		if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				self::bind($condInd, $condVal);
			endforeach; 
		endif;
		$result = self::single(); 
		return $result;
	}
	
	public function retrieveMultipleQuery($fieldArr,$table,$condArr="",$order=""){ 
		$fields = "";
		$edCond = "";
		$fields = implode(",",$fieldArr);
		$ctrCond = count($condArr);
		if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				$condIndex 	= $condInd;
				$condIndex2 = ":".$condInd;
				$condValue 	= $condVal;
				if($ctrCond==0):
				$edCond = $condIndex."=".$condIndex2;
				else:
				$editCond[] = $condIndex."=".$condIndex2;
				endif;				
			endforeach;
			
			$conEditCond = implode(" AND ",$editCond);
		endif;

		$cond = (!empty($condArr)) ? " WHERE ".$edCond.$conEditCond : ""; 
		$orders = (!empty($order)) ? "ORDER BY $order" : "";
		self::query("SELECT $fields FROM $table $cond $orders");
		if(!empty($condArr)):
			foreach($condArr as $condInd => $condVal):
				self::bind($condInd, $condVal);
			endforeach; 
		endif;
		$result = self::resultset(); 
		return $result;
	}
	
	public function getNextId($fieldId,$table){
		self::query("SELECT $fieldId FROM $table ORDER BY $fieldId DESC LIMIT 1");
		$result = self::resultset();
			if(!empty($result)):
				$ret = $result[0][$fieldId]+1;
			else:
				$ret = 1;
			endif;			
			return $ret;
	}
	
	public function bind($param, $value, $type = null){
		if (is_null($type)) {
			switch (true) {
				case is_int($value):
					$type = PDO::PARAM_INT;
					break;
				case is_bool($value):
					$type = PDO::PARAM_BOOL;
					break;
				case is_null($value):
					$type = PDO::PARAM_NULL;
					break;
				default:
					$type = PDO::PARAM_STR;
			}
		}
		$this->stmt->bindValue($param, $value, $type);
	}

	public function execute(){
		return $this->stmt->execute();
	}
	
 
	public function resultset(){
		$this->execute();
		return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
	}	
	
	public function single(){
		$this->execute();
		return $this->stmt->fetch(PDO::FETCH_ASSOC);
	}
	
	public function rowCount(){
		return $this->stmt->rowCount();
	}
	
	public function lastInsertId(){
		return $this->dbh->lastInsertId();
	}
	
	public function beginTransaction(){
		return $this->dbh->beginTransaction();
	}
	
	public function endTransaction(){
		return $this->dbh->commit();
	}
	
	public function cancelTransaction(){
		return $this->dbh->rollBack();
	}

	public function debugDumpParams(){
		return $this->stmt->debugDumpParams();
	}
	 
}