api_monitor/vendor/action/client/client_save.php

142 lines
5.4 KiB
PHP

<?php
/** Importação de classes */
use vendor\model\Client;
use vendor\controller\client\ClientValidate;
try{
/** Verifica se o token de acesso é válido */
if($Main->verifyToken()){
/** Instânciamento de classes */
$Client = new Client();
$ClientValidate = new ClientValidate();
/** Parametros de entrada */
$clientId = isset($_POST['client_id']) ? (int)filter_input(INPUT_POST, 'client_id', FILTER_SANITIZE_SPECIAL_CHARS) : 0 ;
$cns = isset($_POST['cns']) ? (string)filter_input(INPUT_POST, 'cns', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$name = isset($_POST['name']) ? (string)filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$state = isset($_POST['state']) ? (string)filter_input(INPUT_POST, 'state', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$city = isset($_POST['city']) ? (string)filter_input(INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$responsible = isset($_POST['responsible']) ? (string)filter_input(INPUT_POST, 'responsible', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$consultant = isset($_POST['consultant']) ? (string)filter_input(INPUT_POST, 'consultant', FILTER_SANITIZE_SPECIAL_CHARS) : '';
$typeContract = isset($_POST['type_contract']) ? (string)filter_input(INPUT_POST, 'type_contract', FILTER_SANITIZE_SPECIAL_CHARS) : '';
/** Validando os campos de entrada */
$ClientValidate->setClientId($clientId);
$ClientValidate->setCns($cns);
$ClientValidate->setName($name);
$ClientValidate->setState($state);
$ClientValidate->setCity($city);
$ClientValidate->setResponsible($responsible);
$ClientValidate->setConsultant($consultant);
$ClientValidate->setTypeContract($typeContract);
/** Verifico a existência de erros */
if (!empty($ClientValidate->getErrors())) {
/** Preparo o formulario para retorno **/
$result = [
'cod' => 0,
'title' => 'Atenção',
'message' => '<div class="alert alert-danger" role="alert">'.$ClientValidate->getErrors().'</div>',
];
} else {
/** Efetua um novo cadastro ou salva os novos dados */
if ($Client->Save($ClientValidate->getClientId(),
$ClientValidate->getCns(),
$ClientValidate->getName(),
$ClientValidate->getState(),
$ClientValidate->getCity(),
$ClientValidate->getResponsible(),
$ClientValidate->getConsultant(),
$ClientValidate->getTypeContract())){
/** Adição de elementos na array */
$message = '<div class="alert alert-success" role="alert"> <b>'.($ClientValidate->getClientId() > 0 ? 'Cadastro atualizado com sucesso' : 'Cadastro efetuado com sucesso').' </b></div>';
/** Result **/
$result = [
'cod' => 200,
'title' => 'Atenção',
'message' => $message,
'redirect' => '',
];
} else {
/** Verifica se houve erros no SQL */
if($Client->getErrors()){
/** Adição de elementos na array */
$message = '<div class="alert alert-success" role="alert">' . $Client->getErrors() .'</div>';
} else {
/** Adição de elementos na array */
$message = '<div class="alert alert-success" role="alert">' . ($ClientValidate->getClientid() > 0 ? 'Não foi possível atualizar o cadastro' : 'Não foi possível efetuar o cadastro') .'</div>';
}
/** Result **/
$result = [
'cod' => 0,
'title' => 'Atenção',
'message' => $message,
];
}
}
/** Envio **/
echo json_encode($result);
/** Paro o procedimento **/
exit;
/** Caso o token de acesso seja inválido, informo */
}else{
/** Informa que o usuário precisa efetuar autenticação junto ao sistema */
$authenticate = true;
/** Informo */
throw new InvalidArgumentException('Sua sessão expirou é necessário efetuar nova autenticação junto ao sistema', 0);
}
} catch (Exception $exception) {
/** Controle de mensagens */
/*$message = '<span class="badge badge-primary">Detalhes.:</span> ' . 'código = ' . $exception->getCode() . ' - linha = ' . $exception->getLine() . ' - arquivo = ' . $exception->getFile() . '</br>';
$message .= '<span class="badge badge-primary">Mensagem.:</span> ' . $exception->getMessage();*/
/** Preparo o formulario para retorno **/
$result = [
'cod' => 500,
'message' => '<div class="alert alert-danger" role="alert">'.$exception->getMessage().'</div>',
'title' => 'Atenção',
'type' => 'exception',
'authenticate' => $authenticate
];
/** Envio **/
echo json_encode($result);
/** Paro o procedimento **/
exit;
}