top.window.location = '../../index.php'"; exit; } require_once __DIR__ . "/" . "../../includes/include_basics_only.php"; require_once __DIR__ . "/" . "../../includes/classes/ConnectPDO.php"; use includes\classes\ConnectPDO; $conn = ConnectPDO::getInstance(); $post = $_POST; $exception = ""; $screenNotification = ""; $data = []; $data['success'] = true; $data['message'] = ""; $data['cod'] = (isset($post['cod']) ? intval($post['cod']) : ""); $data['action'] = $post['action']; $data['field_id'] = ""; $data['building_name'] = (isset($post['building_name']) ? noHtml($post['building_name']) : ""); $data['unit'] = (isset($post['unit']) ? noHtml($post['unit']) : ""); /* Validações */ if ($data['action'] == "new" || $data['action'] == "edit") { if (empty($data['building_name'])) { $data['success'] = false; $data['field_id'] = "building_name"; } if ($data['success'] == false) { $data['message'] = message('warning', 'Ooops!', TRANS('MSG_EMPTY_DATA'),''); echo json_encode($data); return false; } } if ($data['action'] == 'new') { $terms = (!empty($data['unit']) ? " pred_unit = {$data['unit']} " : " pred_unit IS NULL "); /* verifica se um registro com esse nome já existe para a unidade informada */ $sql = "SELECT pred_cod FROM predios WHERE pred_desc = '" . $data['building_name'] . "' AND {$terms} "; $res = $conn->query($sql); if ($res->rowCount()) { $data['success'] = false; $data['field_id'] = "building_name"; $data['message'] = message('warning', '', TRANS('MSG_RECORD_EXISTS_SAME_UNIT'), ''); echo json_encode($data); return false; } if (!csrf_verify($post)) { $data['success'] = false; $data['message'] = message('warning', 'Ooops!', TRANS('FORM_ALREADY_SENT'),''); echo json_encode($data); return false; } $sql = "INSERT INTO predios ( pred_desc, pred_unit ) VALUES ( '" . $data['building_name'] . "', ".dbField($data['unit'])." )"; try { $conn->exec($sql); $data['success'] = true; $data['message'] = TRANS('MSG_SUCCESS_INSERT'); $_SESSION['flash'] = message('success', '', $data['message'], ''); echo json_encode($data); return false; } catch (Exception $e) { $exception .= "
" . $e->getMessage(); $data['success'] = false; $data['message'] = TRANS('MSG_ERR_SAVE_RECORD') . $exception; $_SESSION['flash'] = message('danger', '', $data['message'], ''); echo json_encode($data); return false; } } elseif ($data['action'] == 'edit') { $terms = (!empty($data['unit']) ? " pred_unit = {$data['unit']} " : " pred_unit IS NULL "); /* verifica se um registro com esse nome e unidade já existe para outro código */ $sql = "SELECT pred_cod FROM predios WHERE pred_desc = '" . $data['building_name'] . "' AND pred_cod <> '" . $data['cod'] . "' AND {$terms}"; $res = $conn->query($sql); if ($res->rowCount()) { $data['success'] = false; $data['field_id'] = "building_name"; $data['message'] = message('warning', '', TRANS('MSG_RECORD_EXISTS_SAME_UNIT'), ''); echo json_encode($data); return false; } if (!csrf_verify($post)) { $data['success'] = false; $data['message'] = message('warning', 'Ooops!', TRANS('FORM_ALREADY_SENT'),''); echo json_encode($data); return false; } $sql = "UPDATE predios SET pred_desc = '" . $data['building_name'] . "', pred_unit = ".dbField($data['unit'])." WHERE pred_cod = '" . $data['cod'] . "'"; try { $conn->exec($sql); $data['success'] = true; $data['message'] = TRANS('MSG_SUCCESS_EDIT'); $_SESSION['flash'] = message('success', '', $data['message'], ''); echo json_encode($data); return false; } catch (Exception $e) { $exception .= "
" . $e->getMessage(); $data['success'] = false; $data['message'] = TRANS('MSG_ERR_DATA_UPDATE') . $exception; $_SESSION['flash'] = message('danger', '', $data['message'], ''); echo json_encode($data); return false; } } elseif ($data['action'] == 'delete') { $sqlFindPrevention = "SELECT loc_id FROM localizacao WHERE loc_predio = '" . $data['cod'] . "' "; $resFindPrevention = $conn->query($sqlFindPrevention); $foundPrevention = $resFindPrevention->rowCount(); if ($foundPrevention) { $data['success'] = false; $data['message'] = TRANS('MSG_CANT_DEL'); $_SESSION['flash'] = message('danger', '', $data['message'], ''); echo json_encode($data); return false; } $sql = "DELETE FROM predios WHERE pred_cod = '" . $data['cod'] . "'"; try { $conn->exec($sql); $data['success'] = true; $data['message'] = TRANS('OK_DEL'); $_SESSION['flash'] = message('success', '', $data['message'], ''); echo json_encode($data); return false; } catch (Exception $e) { $exception .= "
" . $e->getMessage(); $data['success'] = false; $data['message'] = TRANS('MSG_ERR_DATA_REMOVE'); $_SESSION['flash'] = message('danger', '', $data['message'] . $exception, ''); echo json_encode($data); return false; } } echo json_encode($data);