mirror of
https://github.com/itflow-org/itflow
synced 2026-09-09 16:25:11 +00:00
API - Add location update, archive and delete
This commit is contained in:
45
api/v1/locations/archive.php
Normal file
45
api/v1/locations/archive.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$location_id = intval($_POST['location_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($location_id)) {
|
||||
|
||||
// Fetch location info
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "
|
||||
SELECT location_name
|
||||
FROM locations
|
||||
WHERE location_id = $location_id AND location_client_id = $client_id AND location_archived_at IS NULL
|
||||
LIMIT 1
|
||||
"));
|
||||
|
||||
if ($row) {
|
||||
|
||||
$location_name = escapeSql($row['location_name']);
|
||||
|
||||
// Archive location
|
||||
$update_sql = mysqli_query($mysqli, "
|
||||
UPDATE locations SET
|
||||
location_primary = 0,
|
||||
location_archived_at = NOW()
|
||||
WHERE location_id = $location_id AND location_client_id = $client_id
|
||||
");
|
||||
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Location", "Archive", "$location_name archived via API ($api_key_name)", $client_id, $location_id);
|
||||
logAudit("API", "Success", "Archived location $location_name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
30
api/v1/locations/delete.php
Normal file
30
api/v1/locations/delete.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
|
||||
// Parse ID
|
||||
$location_id = intval($_POST['location_id']);
|
||||
|
||||
// Default
|
||||
$delete_count = false;
|
||||
|
||||
if (!empty($location_id)) {
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT location_name FROM locations WHERE location_id = $location_id AND location_client_id = $client_id AND location_archived_at IS NOT NULL LIMIT 1"));
|
||||
$location_name = escapeSql($row['location_name'] ?? '');
|
||||
|
||||
$delete_sql = mysqli_query($mysqli, "DELETE FROM locations WHERE location_id = $location_id AND location_client_id = $client_id AND location_archived_at IS NOT NULL LIMIT 1");
|
||||
|
||||
// Check delete & get affected rows
|
||||
if ($delete_sql && !empty($location_name)) {
|
||||
$delete_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Location", "Delete", "$location_name via API ($api_key_name)", $client_id, $location_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../delete_output.php';
|
||||
43
api/v1/locations/unarchive.php
Normal file
43
api/v1/locations/unarchive.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$location_id = intval($_POST['location_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($location_id)) {
|
||||
|
||||
// Fetch location info
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "
|
||||
SELECT location_name
|
||||
FROM locations
|
||||
WHERE location_id = $location_id AND location_client_id = $client_id AND location_archived_at IS NOT NULL
|
||||
LIMIT 1
|
||||
"));
|
||||
|
||||
if ($row) {
|
||||
|
||||
$location_name = escapeSql($row['location_name']);
|
||||
|
||||
// Unarchive location
|
||||
$update_sql = mysqli_query($mysqli, "
|
||||
UPDATE locations SET location_archived_at = NULL
|
||||
WHERE location_id = $location_id AND location_client_id = $client_id
|
||||
");
|
||||
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Location", "Unarchive", "$location_name unarchived via API ($api_key_name)", $client_id, $location_id);
|
||||
logAudit("API", "Success", "Unarchived location $location_name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
42
api/v1/locations/update.php
Normal file
42
api/v1/locations/update.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
|
||||
// Parse ID
|
||||
$location_id = intval($_POST['location_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($location_id)) {
|
||||
|
||||
$location_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM locations WHERE location_id = '$location_id' AND location_client_id = $client_id AND location_archived_at IS NULL LIMIT 1"));
|
||||
|
||||
// Variable assignment from POST - assigning the current database value if a value is not provided
|
||||
require_once 'location_model.php';
|
||||
|
||||
if ($location_row) {
|
||||
|
||||
// Reset primary location
|
||||
if ($primary == 1) {
|
||||
mysqli_query($mysqli, "UPDATE locations SET location_primary = 0 WHERE location_client_id = $client_id");
|
||||
}
|
||||
|
||||
$update_sql = mysqli_query($mysqli, "UPDATE locations SET location_name = '$name', location_description = '$description', location_country = '$country', location_address = '$address', location_city = '$city', location_state = '$state', location_zip = '$zip', location_hours = '$hours', location_notes = '$notes', location_primary = '$primary' WHERE location_id = $location_id AND location_client_id = $client_id AND location_archived_at IS NULL LIMIT 1");
|
||||
|
||||
// Check update & get affected rows
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Location", "Edit", "$name via API ($api_key_name)", $client_id, $location_id);
|
||||
logAudit("API", "Success", "Edited location $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
Reference in New Issue
Block a user