Example #1
0
 /**
  * @abstract Metodo que fara a persistencia no banco
  */
 public function salvar($grava_historico = true)
 {
     # Verifica se tem acesso a salvar
     //if($this->_login->temPermissao($_GET[ACAO],Sistema_Login::SALVAR)){
     $temp = $this->_dados;
     # Verifica se utiliza chave
     # incremental pelo sistema
     if (isset($_POST['acaoform'])) {
         $alterar = $_POST['acaoform'] == "alterar" ? true : false;
     } else {
         $alterar = $this->_dados[$this->_chavepk] > 0 ? true : false;
         unset($temp[$this->_chavepk]);
     }
     if (!$this->_msg->temErro()) {
         # Verifica se tem valor para chave primaria
         # UPDATE
         if ($alterar) {
             unset($temp[$this->_chavepk]);
             foreach ($temp as $k => $v) {
                 $sets[] = sprintf(" %s = %s ", $k, $v);
             }
             $set = implode(",", $sets);
             $sql = sprintf("UPDATE %s SET %s WHERE %s=%d", $this->_tabela, $set, $this->_chavepk, $this->_dados[$this->_chavepk]);
             $id = Sistema_Conecta::Execute($sql);
             if ($id > 0) {
                 if ($grava_historico) {
                     # Grava no Histórico
                     $hist = new Sistema_Historico($_SESSION['ACT_ATUAL']);
                     $hist->setHistorico(Sistema_Historico::$OP_ALTERAR, $this->_dados[$this->_chavepk], $this->_tabela);
                 }
                 $this->_msg->setSucesso("Dados alterados com sucesso");
                 return array("id" => array("campoid" => $this->_chavepk, "valorid" => $this->_dados[$this->_chavepk]));
             } else {
                 $this->_msg->setErro("Não foi possível alterar os dados");
                 return null;
             }
         } else {
             # INSERT
             $temp1 = array_keys($temp);
             $campos = implode(",", $temp1);
             $valores = implode(",", $temp);
             $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->_tabela, $campos, $valores);
             $id = Sistema_Conecta::Execute($sql);
             if ($id > 0) {
                 if ($grava_historico) {
                     # Grava no Histórico
                     $hist = new Sistema_Historico($_SESSION['ACT_ATUAL']);
                     $hist->setHistorico(Sistema_Historico::$OP_INSERIR, $id, $this->_tabela);
                 }
                 $this->_msg->setSucesso("Dados inseridos com sucesso");
                 return array("id" => array("campoid" => $this->_chavepk, "valorid" => $id));
             } else {
                 $this->_msg->setErro("Não foi possível inserir os dados");
                 return null;
             }
         }
     }
     //}else{
     //$this->_msg->setErro("Você não tem permissão para alterar ou inserir neste formulário");
     //}
 }