Path: C:\
<?php
// API Premium para gerenciamento de funcionários
header('Content-Type: application/json; charset=utf-8');
$dataFile = __DIR__ . '/data/funcionarios.json';
$txtFile = __DIR__ . '/_funcionarios.txt';
// Migração Automática (Apenas roda 1x)
if (!file_exists($dataFile)) {
if (!is_dir(__DIR__ . '/data')) {
mkdir(__DIR__ . '/data', 0777, true);
}
$funcionarios = [];
if (file_exists($txtFile)) {
$lines = file($txtFile);
$id = 1;
foreach($lines as $line) {
if (!mb_check_encoding($line, 'UTF-8')) {
$line = mb_convert_encoding($line, 'UTF-8', 'ISO-8859-1');
}
$parts = explode('|', trim($line));
if (count($parts) >= 2) {
$nome = trim($parts[0]);
$valor = trim($parts[1]);
$funcionarios[] = [
'id' => $id++,
'nome' => $nome,
'valor_cracha' => $valor
];
}
}
}
file_put_contents($dataFile, json_encode($funcionarios, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
$action = isset($_GET['action']) ? $_GET['action'] : '';
// Carrega os dados JSON
$jsonRaw = file_get_contents($dataFile);
$data = json_decode($jsonRaw, true) ?: [];
// ----------- ENDPOINTS -----------
if ($action === 'list') {
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
if ($action === 'save') {
$rawInput = file_get_contents("php://input");
$input = json_decode($rawInput, true);
if (!$input || empty($input['nome']) || empty($input['valor_cracha'])) {
echo json_encode(['success' => false, 'error' => 'Dados inválidos']);
exit;
}
$idSalvo = null;
if (isset($input['id']) && $input['id']) {
// Editar
$idSalvo = (int) $input['id'];
foreach ($data as &$f) {
if ($f['id'] == $input['id']) {
$f['nome'] = $input['nome'];
$f['valor_cracha'] = $input['valor_cracha'];
if(array_key_exists('funcao', $input)) $f['funcao'] = $input['funcao'];
if(array_key_exists('rg', $input)) $f['rg'] = $input['rg'];
if(array_key_exists('cpf', $input)) $f['cpf'] = $input['cpf'];
if(array_key_exists('pis', $input)) $f['pis'] = $input['pis'];
if(array_key_exists('vinculo', $input)) $f['vinculo'] = $input['vinculo'];
break;
}
}
unset($f); // encerra a referência do foreach
} else {
// Criar Novo
$newId = 1;
foreach ($data as $f) {
if ($f['id'] >= $newId) $newId = $f['id'] + 1;
}
$idSalvo = $newId;
$data[] = [
'id' => $newId,
'nome' => $input['nome'],
'valor_cracha' => $input['valor_cracha'],
'funcao' => $input['funcao'] ?? '',
'rg' => $input['rg'] ?? '',
'cpf' => $input['cpf'] ?? '',
'pis' => $input['pis'] ?? '',
'vinculo' => $input['vinculo'] ?? ''
];
}
if(file_put_contents($dataFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
echo json_encode(['success' => true, 'id' => $idSalvo]);
} else {
echo json_encode(['success' => false, 'error' => 'Falha ao salvar no servidor']);
}
exit;
}
if ($action === 'delete') {
$rawInput = file_get_contents("php://input");
$input = json_decode($rawInput, true);
if (isset($input['id'])) {
$data = array_filter($data, function($f) use ($input) {
return $f['id'] != $input['id'];
});
$data = array_values($data); // recriar indices
file_put_contents($dataFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo json_encode(['success' => true]);
exit;
}
}
echo json_encode(['success' => false, 'error' => 'Ação não encontrada']);