<?php
/**
 * Insert repository
 *
 * @author Lucas Dantas <lucas@caju.nu>
 * @package classes
 * @subpackage insert
 * @version 1.0
 * @since 2018/04/09 19:25
 */
Class InsertRepositoryDBR{

	/**
	 * Connection's object
	 *
	 * @access private
	 * @var PDO
	 */
	private $connection;

	/**
	 * Showing or not the exception
	 *
	 * @access private
	 * @var boolean
	 */
	private $exception	= true;

	/**
	 * Instance
	 *
	 * @access private
	 * @static
	 * @var ListRepositoryDBR
	 */
	private static $instance = null;

	/**
	 * Class constructor
	 *
	 * @access public
	 * @uses ConnectionDBR::getInstance Defining the instance
	 */
	public function __construct(){

		$this->connection	= ConnectionDBR::getInstance()->getConnection();

	}

	/**
	 * Defining the instance
	 *
	 * @access public
	 * @return InsertRepositoryDBR
	 */
	public static function getInstance(){

		//Checking
		if( is_null( self::$instance ) )
			//Defining
			self::$instance	= new self();

		//Returning
		return self::$instance;

	}

	/**
	 * Inserting the proposal
	 *
	 * @access public
	 * @param array $fields Fields
	 * @throws Exception
	 * @return integer
	 */
	public function proposal( $fields ){

		try{

			//Preparing
			$sql	= $this->connection->prepare( 'INSERT INTO proposal(source,name,email,phone,choice,message,date) VALUES(:source,:name,:email,:phone,:choice,:message,NOW())' );

			//Defining
			$sql->bindValue( ':source', $fields[ 'origem' ], PDO::PARAM_INT );
			$sql->bindValue( ':name', $fields[ 'nome' ] );
			$sql->bindValue( ':email', $fields[ 'email' ] );
			$sql->bindValue( ':phone', $fields[ 'telefone' ] );
			$sql->bindValue( ':choice', $fields[ 'produto-servico' ] );
			$sql->bindValue( ':message', $fields[ 'mensagem' ] );

			//Executing
			$sql->execute();

			//Returning
			return $this->connection->lastInsertId();

		}catch( PDOException $e ){

			//Throwing the exception
			throw new Exception( 'Ocorreu um erro durante a inserção do contato da proposta. Por favor, tente novamente mais tarde.' );

		}

	}

	/**
	 * Inserting the contact
	 *
	 * @access public
	 * @param array $fields Fields
	 * @throws Exception
	 * @return integer
	 */
	public function contact( $fields ){

		try{

			//Preparing
			$sql	= $this->connection->prepare( 'INSERT INTO contact(source,name,email,phone,company,message,date) VALUES(:source,:name,:email,:phone,:company,:message,NOW())' );

			//Defining
			$sql->bindValue( ':source', SOURCE_SITE_HOME, PDO::PARAM_INT );
			$sql->bindValue( ':name', $fields[ 'nome' ] );
			$sql->bindValue( ':email', $fields[ 'email' ] );
			$sql->bindValue( ':phone', $fields[ 'telefone' ] );
			$sql->bindValue( ':company', $fields[ 'empresa' ] );
			$sql->bindValue( ':message', $fields[ 'mensagem' ] );

			//Executing
			$sql->execute();

			//Returning
			return $this->connection->lastInsertId();

		}catch( PDOException $e ){

			//Throwing the exception
			throw new Exception( 'Ocorreu um erro durante a inserção do contato do fale conosco. Por favor, tente novamente mais tarde.' );

		}

	}

	/**
	 * Inserting the order
	 *
	 * @access public
	 * @param array $fields Fields
	 * @throws Exception
	 * @return integer
	 */
	public function order( $fields ){

		try{

			//Preparing
			$sql	= $this->connection->prepare( 'INSERT INTO orders(source,name,email,phone,cpf_cnpj,type,zip_code,address,neighborhood,number,message,date) VALUES(:source,:name,:email,:phone,:cpfCnpj,:type,:zipCode,:address,:neighborhood,:number,:message,NOW())' );

			//Defining
			$sql->bindValue( ':source', SOURCE_SITE_INTERNAL, PDO::PARAM_INT );
			$sql->bindValue( ':name', $fields[ 'empresa' ] );
			$sql->bindValue( ':email', $fields[ 'email' ] );
			$sql->bindValue( ':phone', $fields[ 'telefone' ] );
			$sql->bindValue( ':cpfCnpj', $fields[ 'cpf-cnpj' ] );
			$sql->bindValue( ':type', $fields[ 'tipo' ] );
			$sql->bindValue( ':zipCode', $fields[ 'cep' ] );
			$sql->bindValue( ':address', $fields[ 'endereco' ] );
			$sql->bindValue( ':neighborhood', $fields[ 'bairro' ] );
			$sql->bindValue( ':number', $fields[ 'numero' ] );
			$sql->bindValue( ':message', $fields[ 'mensagem' ] );

			//Executing
			$sql->execute();

			//Returning
			return $this->connection->lastInsertId();

		}catch( PDOException $e ){

			//Throwing the exception
			throw new Exception( 'Ocorreu um erro durante a inserção do contato da ordem de serviço. Por favor, tente novamente mais tarde.' );

		}

	}

	/**
	 * Inserting the newsletter
	 *
	 * @access public
	 * @param array $fields Fields
	 * @throws Exception
	 */
	public function newsletter( $fields ){

		try{

			//Preparing
			$sql	= $this->connection->prepare( 'INSERT INTO newsletter(name,email,date) VALUES(:name,:email,NOW())' );

			//Defining
			$sql->bindValue( ':name', $fields[ 'nome' ] );
			$sql->bindValue( ':email', $fields[ 'email' ] );

			//Executing
			$sql->execute();

		}catch( PDOException $e ){

			//Throwing the exception
			throw new Exception( 'Ocorreu um erro durante a inserção do contato da newsletter. Por favor, tente novamente mais tarde.' );

		}

	}

	/**
	 * Inserting the central
	 *
	 * @access public
	 * @param integer $id Identifier
	 * @param integer $destination Destination
	 * @param string $message Mensagem
	 * @uses Util::defineDestinationOptions Defining the destination's options
	 * @throws Exception
	 */
	public function central( $id, $destination, $message ){

		try{

			//Defining the object
			$object		= Util::defineDestinationOptions( $destination );

			//Preparing
			$resource	= $this->connection->prepare( 'INSERT INTO ' . $object->answer_table . '(lead,type,message,date) VALUES(:lead,:type,:message,NOW())' );

			//Defining
			$resource->bindValue( ':lead', $id, PDO::PARAM_INT );
			$resource->bindValue( ':type', ANSWER_CLIENT, PDO::PARAM_INT );
			$resource->bindValue( ':message', $message );

			//Executing
			$resource->execute();

		}catch( PDOException $e ){

			//Throwing the exception
			throw new Exception( 'Ocorreu um erro durante a inserção da resposta do cliente. Por favor, tente novamente mais tarde.' );

		}

	}

}