<?php
class Gerar_DAO_salvar 
{
	function montarArquivo($_tabela_bd, $nome_classe, $buscar_coluna_primary)
	{
		$buscar_coluna_primary = new Buscar_coluna_primary();
		//$coluna_primary = $buscar_coluna_primary->buscar($_tabela_bd)->getColuna();
		//$nome_coluna_pk = strtoupper(substr($coluna_primary,0,1)).substr($coluna_primary,1);
		$dao = "";
		$dao .= "	public function salvar(\$".$_tabela_bd->getTabela().")\n";
		$dao .= "	{\n";
		$dao .= "		\$inputs = array();\n";
		$colunas = $_tabela_bd->getLista_colunas();
		$colunasInsert = "";
		$valuesInsert = "";
		for ($i=0; $i <count($colunas); $i++)
		{	
			//if ($colunas[$i]->getIndice() != "PRIMARY")
			//{
				$nome_coluna = strtoupper(substr($colunas[$i]->getColuna(),0,1)).substr($colunas[$i]->getColuna(),1);
				switch ($colunas[$i]->getTipo())
				{
					case ("md5"):
						$dao .= "		array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>\$".$_tabela_bd->getTabela()."->get".$nome_coluna."()));\n";
						$valuesInsert .= "md5(:".$colunas[$i]->getColuna().")";
					break;
					case ("data"):
						$dao .= "		if(\$".$_tabela_bd->getTabela()."->get".$nome_coluna."() instanceof DateTime)\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>\$".$_tabela_bd->getTabela()."->get".$nome_coluna."()->format('Y-m-d H:i:s')));\n";
						$dao .= "		}\n";
						$dao .= "		else\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>null));\n";
						$dao .= "		}\n";
						$valuesInsert .= ":".$colunas[$i]->getColuna()."";
					break;	
					case ("tabela"):
						$dao .= "		if(\$".$_tabela_bd->getTabela()."->get".$nome_coluna."() != \"\")\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>\$".$_tabela_bd->getTabela()."->get".$nome_coluna."()));\n";
						$dao .= "		}\n";
						$dao .= "		else\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>null));\n";
						$dao .= "		}\n";	
						$valuesInsert .= ":".$colunas[$i]->getColuna()."";
					break;	
					case ("float"):
						$dao .= "		array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>str_replace(',','.',str_replace('.','',\$".$_tabela_bd->getTabela()."->get".$nome_coluna."()))));\n";
						$valuesInsert .= ":".$colunas[$i]->getColuna()."";
					break;
					default:
						$dao .= "		if(\$".$_tabela_bd->getTabela()."->get".$nome_coluna."() != \"\")\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>\$".$_tabela_bd->getTabela()."->get".$nome_coluna."()));\n";
						$dao .= "		}\n";
						$dao .= "		else\n";
						$dao .= "		{\n";
						$dao .= "			array_push(\$inputs, array('campo'=>':".$colunas[$i]->getColuna()."', 'valor'=>null));\n";
						$dao .= "		}\n";
						$valuesInsert .= ":".$colunas[$i]->getColuna()."";
				}	
				
				$colunasInsert .= "".$colunas[$i]->getColuna()."";
				
				if ($i < count($colunas)-1)
				{
					//if (  $colunas[$i+1]->getIndice() != "PRIMARY")//nao adicionar virgula no md5 pois cria em cima e nao atualiza data de criação como diz o nome
					//{	
						$colunasInsert .= ", ";
						$valuesInsert .= ", ";
					//}	
				}	
			//}	
		}
		$dao .= "		try\n";
		$dao .= "		{\n";
		$dao .= "			\$sql = \"INSERT INTO ".$_tabela_bd->getTabela()." (".$colunasInsert.") VALUES (".$valuesInsert.")  \";\n";
		$dao .= "			return \$this->_funcoes_bd->executarSql(\$sql,\$inputs);\n";
		$dao .= "		}\n";
		$dao .= "		catch (Exception \$e)\n";
		$dao .= "		{\n";
		$dao .= "			throw new Exception(\$e->getMessage());\n";
		$dao .= "		}\n";
		$dao .= "	}\n";
				
		return $dao;	
	}	
}	