<?php

class regiaoDAO {

    function __construct() {
        
    }

    function __destruct() {
        //$this->banco = NULL;
    }

    public function __set($campo, $valor) {
        $this->$campo = $valor;
    }

    public function __get($campo) {
        return $this->$campo;
    }

    static function salvar($data, $table, $dbk = NULL) {
        try {
            $fields = $data;

            $tmp = array();
            foreach ($fields as $k => $v) {
                if ($k != $dbk) {
                    $tmp[] = $k . ' = :' . $k;
                }
            }
            if (isset($dbk)) {
                $fields['id'] = $dbk;
                $SQL = 'UPDATE ' . $table . ' SET ' . implode(', ', $tmp) . ' WHERE (id=:id)';
            } else {
                $SQL = 'INSERT INTO ' . $table . ' SET ' . implode(', ', $tmp);
            }
            $db = new DB();
            $id = $db->InsertData($SQL, $fields);
            return $id;
        } catch (Exception $ex) {
            throw new Exception($ex->getMessage() . ': ' . __FUNCTION__);
        }
    }

    static function getZone($id) {
        try {
            $sql = "SELECT timezone FROM regiao "
                    . "WHERE id_pais=?";
            $db = new DB();
            return $db->executeReturnFetch($sql , array($id));
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __FUNCTION__);
        }
    }

    public static function deletarUsuario($id) {
        try {
            $sql = 'DELETE FROM usuario'
                    . ' WHERE id =' . $id;
            $db = new DB();
            $db->query($sql);
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __CLASS__ . '*' . __FUNCTION__);
        }
    }
    
    static function getSigla($id_pais) {
        try {
            $sql = "SELECT a.moeda_sigla FROM regiao a"
                    . " WHERE a.id_pais = ?"
                    . " AND a.status = 1";
            $db = new DB();
            return $db->executeReturnFetch($sql , array($id_pais));
        } catch (Exception $err) {
            throw new Exception($err->getMessage() . ': ' . __FUNCTION__);
        }
    }
   
}
