173 lines
5.5 KiB
PHP
173 lines
5.5 KiB
PHP
<?php
|
||
use App\DevExtreme\LoadHelper;
|
||
//spl_autoload_register(array("DevExtreme\LoadHelper", "LoadModule"));
|
||
|
||
use App\DevExtreme\DbSet;
|
||
use App\DevExtreme\DataSourceLoader;
|
||
use App\DevExtreme\SummaryContext;
|
||
|
||
class DataController {
|
||
private $dbSet;
|
||
public function __construct() {
|
||
//TODO: use your database credentials
|
||
$connection = config('database.connections.mysql');
|
||
$host = $connection['host'];
|
||
$username = $connection['username'];
|
||
$password = $connection['password'];
|
||
$database = $connection['database'];
|
||
$mySQL = new mysqli($host, $username, $password, $database);
|
||
mysqli_set_charset($mySQL, "utf8");
|
||
|
||
$table = get("table");
|
||
if ($table === 'weld_logs' && get("module") === 'joint-release-log') {
|
||
$table = 'joint_release_view';
|
||
}
|
||
$this->dbSet = new DbSet($mySQL, $table);
|
||
}
|
||
|
||
public function Get($params) {
|
||
// Store module before unsetting (needed for employer view filtering)
|
||
$module = isset($params['module']) ? $params['module'] : null;
|
||
unset($params['table']);
|
||
unset($params['module']);
|
||
$excludeMechanicalSummary = false;
|
||
if(isset($params['excludeMechanicalSummary'])) {
|
||
$excludeMechanicalSummary = filter_var($params['excludeMechanicalSummary'], FILTER_VALIDATE_BOOLEAN) || $params['excludeMechanicalSummary'] === '1';
|
||
unset($params['excludeMechanicalSummary']);
|
||
}
|
||
if(isset($params['select'])) {
|
||
// $params['select'] = array_unique($params['select'] );
|
||
$uniqueArray = [];
|
||
|
||
foreach ($params['select'] as $value) {
|
||
// Anahtar olarak değeri kullanarak yinelenen öğeleri kontrol et
|
||
if (!in_array($value, $uniqueArray)) {
|
||
// Daha önce eşleşme bulunmadıysa, bu değeri ekle
|
||
$uniqueArray[] = $value;
|
||
}
|
||
}
|
||
$params['select'] = $uniqueArray;
|
||
}
|
||
|
||
if(!isset($params['sort'])) {
|
||
|
||
$params['sort'][] = (object) [
|
||
'selector' => 'id',
|
||
'desc' => true
|
||
];
|
||
}
|
||
|
||
SummaryContext::setExcludeMechanical($excludeMechanicalSummary);
|
||
$result = DataSourceLoader::Load($this->dbSet, $params, $module);
|
||
if (!isset($result)) {
|
||
$result = $this->dbSet->GetLastError();
|
||
}
|
||
return $result;
|
||
}
|
||
public function Post($values) {
|
||
$result = $this->dbSet->Insert($values);
|
||
if (!isset($result)) {
|
||
$result = $this->dbSet->GetLastError();
|
||
}
|
||
return $result;
|
||
}
|
||
public function Put($key, $values) {
|
||
$result = NULL;
|
||
if (isset($key) && isset($values) && is_array($values)) {
|
||
if (!is_array($key)) {
|
||
$keyVal = $key;
|
||
$key = array();
|
||
$key["ID"] = $keyVal;
|
||
}
|
||
$result = $this->dbSet->Update($key, $values);
|
||
if (!isset($result)) {
|
||
$result = $this->dbSet->GetLastError();
|
||
}
|
||
}
|
||
else {
|
||
throw new Exeption("Invalid params");
|
||
}
|
||
return $result;
|
||
}
|
||
public function Delete($key) {
|
||
$result = NULL;
|
||
if (isset($key)) {
|
||
if (!is_array($key)) {
|
||
$keyVal = $key;
|
||
$key = array();
|
||
$key["ID"] = $keyVal;
|
||
}
|
||
$result = $this->dbSet->Delete($key);
|
||
if (!isset($result)) {
|
||
$result = $this->dbSet->GetLastError();
|
||
}
|
||
}
|
||
else {
|
||
throw new Exeption("Invalid params");
|
||
}
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
function GetParseParams($params, $assoc = false) {
|
||
$result = NULL;
|
||
if (is_array($params)) {
|
||
$result = array();
|
||
foreach ($params as $key => $value) {
|
||
$result[$key] = json_decode($params[$key], $assoc);
|
||
if ($result[$key] === NULL) {
|
||
$result[$key] = $params[$key];
|
||
}
|
||
}
|
||
}
|
||
else {
|
||
$result = $params;
|
||
}
|
||
return $result;
|
||
}
|
||
function GetParamsFromInput() {
|
||
$result = NULL;
|
||
$content = file_get_contents("php://input");
|
||
if ($content !== false) {
|
||
$params = array();
|
||
parse_str($content, $params);
|
||
$result = GetParseParams($params, true);
|
||
}
|
||
return $result;
|
||
}
|
||
$response = NULL;
|
||
$controller = new DataController();
|
||
switch($_SERVER["REQUEST_METHOD"]) {
|
||
case "GET": {
|
||
$params = GetParseParams($_GET);
|
||
$response = $controller->Get($params);
|
||
break;
|
||
}
|
||
case "POST": {
|
||
$params = GetParamsFromInput();
|
||
$response = $controller->Post($params["values"]);
|
||
break;
|
||
}
|
||
case "PUT": {
|
||
$params = GetParamsFromInput();
|
||
$response = $controller->Put($params["key"], $params["values"]);
|
||
break;
|
||
}
|
||
case "DELETE": {
|
||
$params = GetParamsFromInput();
|
||
$response = $controller->Delete($params["key"]);
|
||
break;
|
||
}
|
||
}
|
||
if (isset($response) && !is_string($response)) {
|
||
// $response = mb_convert_encoding($response, 'UTF-8', 'UTF-8');
|
||
|
||
echo json_encode_tr($response);
|
||
}
|
||
else {
|
||
header("HTTP/1.1 500 Internal Server Error");
|
||
header("Content-Type: application/json");
|
||
$errorMessage = is_string($response) ? $response : "Unknown error";
|
||
error_log("DevExtreme error: " . $errorMessage);
|
||
echo json_encode(array("message" => $errorMessage, "code" => 500));
|
||
} |