105 lines
4.3 KiB
PHP
105 lines
4.3 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.php';
|
|
|
|
$id = (int)$data['id'];
|
|
|
|
try {
|
|
/** 1. FILE HANDLING **/
|
|
$uploader = new FileUploader($include_url . "uploads/contact/");
|
|
|
|
// Cleanup deleted files
|
|
if ($id > 0) {
|
|
$sql = "SELECT `contact_image` FROM md_contact WHERE company_id = :company_id AND id = :id";
|
|
$sth = $pdo2->prepare($sql);
|
|
$sth->execute([":company_id" => $company_id, ":id" => $id]);
|
|
$uploader->cleanup($sth->fetchColumn(), $data['keep_files'] ?? '');
|
|
}
|
|
|
|
// Upload new files
|
|
$errors = $uploader->upload('contact_image');
|
|
if (!empty($errors)) {
|
|
$answer["success"] = 0;
|
|
$answer["message"] = $errors[0];
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
$db_image_string = $uploader->buildFileString($data['keep_files'] ?? '');
|
|
|
|
/** 2. DATABASE **/
|
|
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $id, $db_image_string) {
|
|
|
|
$sql = "SELECT `log` FROM md_contact
|
|
WHERE company_id = :company_id AND id = :id";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([
|
|
":company_id" => $company_id,
|
|
":id" => $id
|
|
]);
|
|
|
|
$table_log = json_decode($sth->fetchColumn() ?: '[]', true);
|
|
if (!is_array($table_log)) $table_log = [];
|
|
$table_log[] = $logging;
|
|
|
|
$params = [
|
|
":company_id" => $company_id,
|
|
":contact_name" => $data["contact_name"],
|
|
":tax_id" => $data["tax_id"],
|
|
":organization" => $data["organization"],
|
|
":branch" => $data["branch"],
|
|
":contact_type" => (int)$data["contact_type"],
|
|
":billing_address" => $data["billing_address"],
|
|
":shipping_location"=> $data["shipping_location"],
|
|
":shipping_address" => $data["shipping_address"],
|
|
":remark" => $data["remark"],
|
|
":contact_image" => $db_image_string,
|
|
":status" => (int)$data["status"],
|
|
":log" => json_encode($table_log),
|
|
];
|
|
|
|
if ($id > 0) {
|
|
$sql = "UPDATE md_contact SET
|
|
`contact_name` = :contact_name,
|
|
`tax_id` = :tax_id,
|
|
`organization` = :organization,
|
|
`branch` = :branch,
|
|
`contact_type` = :contact_type,
|
|
`billing_address` = :billing_address,
|
|
`shipping_location` = :shipping_location,
|
|
`shipping_address` = :shipping_address,
|
|
`remark` = :remark,
|
|
`contact_image` = :contact_image,
|
|
`status` = :status,
|
|
`log` = :log
|
|
WHERE id = :id AND company_id = :company_id";
|
|
$params[":id"] = $id;
|
|
} else {
|
|
$sql = "INSERT INTO md_contact
|
|
(company_id, contact_name, tax_id, organization, branch, contact_type,
|
|
billing_address, shipping_location, shipping_address, remark, contact_image, status, log)
|
|
VALUES
|
|
(:company_id, :contact_name, :tax_id, :organization, :branch, :contact_type,
|
|
:billing_address, :shipping_location, :shipping_address, :remark, :contact_image, :status, :log)";
|
|
}
|
|
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute($params);
|
|
|
|
});
|
|
|
|
$answer["success"] = 1;
|
|
|
|
} catch (PDOException $e) {
|
|
// DB failed — rollback uploaded files
|
|
if (isset($uploader)) $uploader->rollbackUploads();
|
|
$answer["success"] = 0;
|
|
$answer["message"] = "Database error, please try again";
|
|
|
|
} catch (Exception $e) {
|
|
if (isset($uploader)) $uploader->rollbackUploads();
|
|
$answer["success"] = 0;
|
|
$answer["message"] = $e->getMessage();
|
|
}
|
|
|
|
exit(json_encode($answer));
|
|
?>
|