/**
  * API Method para inserir um array de participantes vindos da handsontable
  */
 public function UpdateAll()
 {
     $this->RequirePermission(Usuario::$P_ADMIN, 'SecureExample.LoginForm', 'Autentique-se para acessar esta página', 'Você não possui permissão para acessar essa página ou sua sessão expirou');
     //$someJSON = $_GET['dados'];
     require_once "Model/PalestraParticipante.php";
     try {
         $json = json_decode(RequestUtil::GetBody());
         //$json = $_GET['dados'];
         //FILTRA OBJETOS DUPLICADOS NA TABELA DE PARTICIPANTES HANDSONTABLE
         $unique = array();
         //throw new Exception($json);
         foreach ($json->data as $object) {
             if (isset($unique[$object->idParticipante])) {
                 continue;
             }
             $unique[$object->idParticipante] = $object;
         }
         //$json = json_decode($someJSON);
         if (!$json) {
             throw new Exception('The request body does not contain valid JSON');
         }
         $errors_p = array();
         $row = 0;
         foreach ($json->data as $json) {
             //trocar $unique por $json->data se nao for usa-lo
             if ($json->idParticipante == '' && $json->nome == '' && $json->email == '' && $json->nome == '') {
                 //se linha estiver em branco
             } else {
                 $pk = $this->SafeGetVal($json, 'idParticipante', null);
                 $cpf = $this->SafeGetVal($json, 'cpf', null);
                 $jatem = true;
                 if ($pk != '') {
                     //SE TIVER ID É EDIÇÃOO SENÃO CRIA UM NOVO PARTICIPANTE
                     try {
                         $participante = $this->Phreezer->Get('Participante', $pk);
                     } catch (NotFoundException $ex) {
                         throw new Exception('Participante não encontrado');
                     }
                 } elseif ($pk == '' && $cpf != '') {
                     //SE TIVER CPF É EDIÇÃO SENÃO CRIA UM NOVO PARTICIPANTE
                     // try {
                     // $participante = $this->Phreezer->Get('Participante',$pk);
                     // } catch (NotFoundException $ex){
                     // throw new Exception('Participante não encontrado');
                     // }
                     $criteria = new ParticipanteCriteria();
                     $criteria->Cpf_Equals = $cpf;
                     $jatem = true;
                     try {
                         $participante = $this->Phreezer->GetByCriteria("Participante", $criteria);
                         //throw new Exception('MAQU');
                     } catch (NotFoundException $nfex) {
                         //throw new Exception($nfex);
                         $participante = new Participante($this->Phreezer);
                         //$this->RenderExceptionJSON($nfex);
                     }
                 } else {
                     $participante = new Participante($this->Phreezer);
                 }
                 //se existir id, mas tudo estiver em branco o sistema exclui do banco
                 if ($json->idParticipante != '' && $json->nome == '' && $json->email == '' && $json->nome == '') {
                     $participante->Delete();
                 } else {
                     $participante->Nome = $this->SafeGetVal($json, 'nome', $participante->Nome);
                     $participante->Email = $this->SafeGetVal($json, 'email', $participante->Email);
                     $participante->Cpf = $this->SafeGetVal($json, 'cpf', $participante->Cpf);
                     $participante->Validate();
                     $errors = $participante->GetValidationErrors();
                     //if(!$participante->Cpf) echo $participante->Nome .'--' . $participante->Cpf.'mmmm';
                     if (count($errors) > 0) {
                         $errors_p[$participante->IdParticipante]['message'] = $participante->GetValidationErrors();
                         $errors_p[$participante->IdParticipante]['success'] = false;
                         $errors_p[$participante->IdParticipante]['row'] = $row;
                     } else {
                         //Salva o participante no banco
                         $participante->Save();
                         //para fazer a associaçãoo na tabela palestra_participante
                         $table2 = new PalestraParticipante($this->Phreezer);
                         $table2->IdParticipante = $participante->IdParticipante;
                         $table2->IdPalestra = RequestUtil::Get('idPalestra');
                         $table2->Presenca = 0;
                         $table2->IdCertificado = 0;
                         $table2->Save(false, true);
                         if ($pk == '' or $jatem == true) {
                             $dados = array('idParticipante' => $participante->IdParticipante, 'row' => $row);
                             $sucesso['novo'][] = $dados;
                         }
                     }
                     // if (count($errors) > 0)
                     // {
                     // $this->RenderErrorJSON('Verifique erros no preenchimento do formulario',$errors);
                     // }
                     // else
                     // {
                     // $participante->Save();
                     // $this->RenderJSON($participante, $this->JSONPCallback(), true, $this->SimpleObjectParams());
                     // }
                 }
                 //fim do cadastro se nao for exclusao
             }
             //fim do cadastro caso nada esteja em branco
             $row++;
         }
         if (count($errors_p) > 0) {
             $this->RenderErrorJSON('Verifique erros no preenchimento do formulário', $errors_p);
         } else {
             $sucesso['success'] = true;
             $sucesso['message'] = 'Participantes salvos com sucesso';
             $this->RenderJSON($sucesso);
         }
     } catch (Exception $ex) {
         $this->RenderExceptionJSON($ex);
     }
 }
 /**
  * API Method inserts a new PalestraParticipante record and render response as JSON
  */
 public function Create()
 {
     try {
         $json = json_decode(RequestUtil::GetBody());
         if (!$json) {
             throw new Exception('The request body does not contain valid JSON');
         }
         $palestraparticipante = new PalestraParticipante($this->Phreezer);
         // TODO: any fields that should not be inserted by the user should be commented out
         // this is an auto-increment.  uncomment if updating is allowed
         // $palestraparticipante->Id = $this->SafeGetVal($json, 'id');
         $palestraparticipante->Presenca = $this->SafeGetVal($json, 'presenca');
         $palestraparticipante->IdParticipante = $this->SafeGetVal($json, 'idParticipante');
         $palestraparticipante->IdPalestra = $this->SafeGetVal($json, 'idPalestra');
         $palestraparticipante->IdCertificado = $this->SafeGetVal($json, 'idCertificado');
         $palestraparticipante->Validate();
         $errors = $palestraparticipante->GetValidationErrors();
         if (count($errors) > 0) {
             $this->RenderErrorJSON('Verifique erros no preenchimento do formulário', $errors);
         } else {
             $palestraparticipante->Save(false, true);
             //$force_insert = false, $ignore_duplicate = false
             $this->RenderJSON($palestraparticipante, $this->JSONPCallback(), true, $this->SimpleObjectParams());
         }
     } catch (Exception $ex) {
         $this->RenderExceptionJSON($ex);
     }
 }