131 lines
5.3 KiB
PHP
131 lines
5.3 KiB
PHP
<?php
|
|
// =====================================================================
|
|
// Database Helper Functions
|
|
// Provides PDO Connection, JSON Response Helpers, Category Definitions
|
|
// and shared miscellaneous Functions for all API Endpoints.
|
|
// =====================================================================
|
|
|
|
require_once __DIR__ . '/init.php';
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
// JSON Response
|
|
// Creates JSON Response including HTTP Status Code and HTTP Header
|
|
// for every API Endpoint and terminates the Script.
|
|
// ---------------------------------------------------------------------
|
|
function json_response($data, $status_code = 200) {
|
|
// Defines HTTP Status Code and HTTP Header
|
|
// 1XX Informational, 2XX Successful, 3XX Redirection,
|
|
// 4XX Client Error, 5XX Server Error
|
|
http_response_code($status_code);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
// Converts PHP-Array to JSON-String
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Error Response
|
|
// Creates standardized Error Responses with Error Message and HTTP Status
|
|
// Code. Uses json_response() for consistent Formatting.
|
|
// ---------------------------------------------------------------------
|
|
function error_response($message, $status_code = 400) {
|
|
json_response(['error' => $message], $status_code);
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Validate Required Fields
|
|
// Checks if specified Fields exist in the given Data Array and are
|
|
// non-empty. Returns an Array of missing Field Names, or an empty
|
|
// Array if all Fields are present.
|
|
// ---------------------------------------------------------------------
|
|
function validate_required($data, $fields) {
|
|
$missing = [];
|
|
|
|
foreach ($fields as $field) {
|
|
// Checks if Fields exists in Data Array and are not empty
|
|
if (!isset($data[$field]) || trim($data[$field]) === '') {
|
|
$missing[] = $field;
|
|
}
|
|
}
|
|
// Returns Array of missing Fields or emty Array
|
|
return $missing;
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Get POST Input
|
|
// Reads POST Parameters. Returns an associative Array.
|
|
// Fallback to JSON Request Body if no POST Data is present.
|
|
// ---------------------------------------------------------------------
|
|
function get_input() {
|
|
// Checks for standard POST Requests
|
|
if (!empty($_POST)) {
|
|
return array_map('trim', $_POST);
|
|
}
|
|
|
|
// Fall back for JSON POST Requests
|
|
$json = file_get_contents('php://input');
|
|
$data = json_decode($json, true);
|
|
|
|
if (is_array($data)) {
|
|
return array_map('trim', $data);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Get PDO Connection
|
|
// Returns PDO Instance wrapped in a Function to prevent global
|
|
// Variable Dependencies in Endpoint Files.
|
|
// ---------------------------------------------------------------------
|
|
function get_db() {
|
|
global $pdo;
|
|
|
|
if (!$pdo) {
|
|
error_response('Database Connection failed.', 500);
|
|
}
|
|
|
|
return $pdo;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Category Definitions
|
|
// Returns associative Array of Category Keys to Labels, Icons,
|
|
// and Colors. Shared between Citizen Participation Portal and
|
|
// Moderation Page.
|
|
// ToDo: Move to Database Table.
|
|
// ---------------------------------------------------------------------
|
|
function get_categories() {
|
|
return [
|
|
'consumption' => ['label' => 'Geschäfte', 'faIcon' => 'fa-cart-shopping', 'color' => '#C00000'],
|
|
'building' => ['label' => 'Bauen', 'faIcon' => 'fa-building', 'color' => '#E65100'],
|
|
'energy' => ['label' => 'Energie', 'faIcon' => 'fa-bolt', 'color' => '#FFC000'],
|
|
'environment' => ['label' => 'Umwelt', 'faIcon' => 'fa-seedling', 'color' => '#92D050'],
|
|
'mobility' => ['label' => 'Mobilität', 'faIcon' => 'fa-bus', 'color' => '#0070C0'],
|
|
'industry' => ['label' => 'Industrie', 'faIcon' => 'fa-industry', 'color' => '#7030A0'],
|
|
'other' => ['label' => 'Sonstiges', 'faIcon' => 'fa-thumbtack', 'color' => '#7F7F7F'],
|
|
];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Task Category Definitions
|
|
// Returns associative Array of Task Category Keys to Labels, Icons,
|
|
// and Colors. Shared between Citizen Participation Portal and
|
|
// Moderation Page.
|
|
// ToDo: Move to Database Table.
|
|
// ---------------------------------------------------------------------
|
|
function get_task_categories() {
|
|
return [
|
|
'repair' => ['label' => 'Reparatur', 'faIcon' => 'fa-wrench', 'color' => '#C00000'],
|
|
'social' => ['label' => 'Nachbarschaft', 'faIcon' => 'fa-people-group', 'color' => '#E65100'],
|
|
'safety' => ['label' => 'Sicherheit', 'faIcon' => 'fa-shield-halved', 'color' => '#FFC000'],
|
|
'greenery' => ['label' => 'Grünpflege', 'faIcon' => 'fa-leaf', 'color' => '#92D050'],
|
|
'cleanup' => ['label' => 'Sauberkeit', 'faIcon' => 'fa-broom', 'color' => '#0070C0'],
|
|
'other_task' => ['label' => 'Sonstiges', 'faIcon' => 'fa-clipboard-check','color' => '#7F7F7F'],
|
|
];
|
|
} |