first commit; dx 1.0 beta

This commit is contained in:
2025-11-09 22:35:16 +03:00
commit 4d01725955
153 changed files with 6504 additions and 0 deletions
+370
View File
@@ -0,0 +1,370 @@
<?php
include 'k-api.php';
function db()
{
$pdo = new PDO('sqlite:./data.db');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
};
function findClosestDateSort($array, $targetDate)
{
$targetTimestamp = DateTime::createFromFormat('d-m-y', $targetDate)->getTimestamp();
usort($array, function ($a, $b) use ($targetTimestamp) {
$aTimestamp = DateTime::createFromFormat('d-m-y', $a['fromDate'])->getTimestamp();
$bTimestamp = DateTime::createFromFormat('d-m-y', $b['fromDate'])->getTimestamp();
$aDiff = abs($aTimestamp - $targetTimestamp);
$bDiff = abs($bTimestamp - $targetTimestamp);
return $aDiff - $bDiff;
});
return $array[0];
}
function getDayOfWeek(string $date)
{
$p = explode('-', $date);
if (count($p) !== 3) throw new InvalidArgumentException('DD-MM-YY|YYYY');
[$d, $m, $y] = $p;
if (strlen($y) === 2) $y = '20' . $y;
$dt = DateTime::createFromFormat('Y-m-d', "$y-$m-$d");
if (!$dt) throw new InvalidArgumentException('Invalid date');
return (int)$dt->format('N') - 1; // 0=Mon ... 6=Sun
}
function joinArraysByKey(array $primaryArray, array $secondaryArray, bool $doNotOverwrite = false, string $key = 'num'): array
{
// Index secondary array once for O(1) lookups
$indexedArray = array_column($secondaryArray, null, $key);
$result = [];
foreach ($primaryArray as $item) {
if (!isset($item[$key])) {
$result[] = $item;
continue;
}
$currentKey = $item[$key];
if (isset($indexedArray[$currentKey])) {
// Merge based on $doNotOverwrite flag
$result[] = $doNotOverwrite
? array_merge($indexedArray[$currentKey], $item) // Primary values take precedence
: array_merge($item, $indexedArray[$currentKey]); // Secondary values take precedence
} else {
$result[] = $item;
}
}
return $result;
}
function FastDBReq(PDO $db, string $sql, array $params = []): array
{
// ivan
// pickme love
// <3 <3 <3 <3
$stmt = $db->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
CreateApi(function ($api) {
$api->on('/', 'GET', function () {
return 'dengi';
});
$api->on('/login', 'POST', function ($req) {
if (!isset($req->data['classId'])) {
$req->res->code(400);
return "class is empty";
}
$classId = substr(strtoupper($req->data['classId']), 0, 10);
// according docs (UPPERCASE, MIN 3 MAX 10)
$db = db();
$results = FastDBReq($db, "SELECT * FROM classes WHERE classid = ?", [$classId]);
$db = null;
if (count($results) != 0) {
return $results[0];
} else {
$req->res->code(400);
return "класс $classId не найден";
}
});
$api->on('/get_class_user_f', 'POST', function ($req) {
if (!isset($req->data['classId'])) {
$req->res->code(400);
return "";
}
$classId = substr(strtoupper($req->data['classId']), 0, 10);
// according docs (UPPERCASE, MIN 3 MAX 10)
$db = db();
$results = FastDBReq($db, "SELECT userclassname FROM classes WHERE classid = ?", [$classId]);
$db = null;
if (count($results) != 0) {
return $results[0];
} else {
$req->res->code(400);
return;
}
});
$api->on('/get_admin_user_f', 'POST', function ($req) {
if (!isset($req->data['adminId'])) {
$req->res->code(400);
return "";
}
$adminId = $req->data['adminId'];
$db = db();
$results = FastDBReq($db, "SELECT adminName FROM admins where adminid = ?", [$adminId]);
$db = null;
if (count($results) != 0) {
return $results[0];
} else {
$req->res->code(400);
return;
}
});
$api->on('/admin_login', 'POST', function ($req) {
if (!isset($req->data['adminId'])) {
$req->res->code(400);
return "admin is empty";
}
$adminId = $req->data['adminId'];
$db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminid = ?", [$adminId]);
$db = null;
if (count($results) != 0) {
return $results[0];
} else {
$req->res->code(400);
return "админ не найден";
}
});
$api->on('/req_client_schedule', 'POST', function ($req) {
// NOTE: THIS IS SCHEDULE FOR USER (SHOULD BE WITH CHANGES AND CALLS)
// types = [0 - default schedule, 1 - with schedule changes, 2 - with schedule and rings]
if (!$req->hasData(['classId', 'day'])) {
$req->res->code(400);
return "net dannih";
}
$classId = substr(strtoupper($req->data['classId']), 0, 10);
$day = $req->data['day'];
$db = db();
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) {
// return only changes
$content = json_decode($results[0]['content'], true);
$has_full_rings = true;
foreach ($content as $idx => $item) {
if (!isset($item['t-begin']) || !isset($item['t-end'])) {
$has_full_rings = false;
// optionally record $idx
break;
}
}
if ($has_full_rings) {
$db = null;
// with rings
return ['data' => $content, 'type' => 2];
} else {
// without rings
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]);
$rings = json_decode(findClosestDateSort($results, $day)['content'], true);
$out = joinArraysByKey($content, $rings, true);
$db = null;
return ['data' => $out, 'type' => 1];
}
} else {
// return shedules + calls
$dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
if (count($results) > 0) {
$out = [];
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]);
$rings = json_decode(findClosestDateSort($results, $day)['content'], true);
$out = joinArraysByKey($schedule, $rings);
$db = null;
return ['data' => $out, 'type' => 0];
} else {
$db = null;
return ['data' => []];
}
}
});
$api->on('/editor_data', "POST", function ($req) {
if (!$req->hasData(['tab', 'day', 'adminId'])) {
$req->res->code(400);
return "bad data";
}
$adminId = $req->data['adminId'];
$db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) {
$db = null;
$req->res->code(400);
return "админ не найден";
# end
}
$classId = $results[0]['adminClass'];
$day = $req->data['day'];
$tab = intval($req->data['tab']);
$changes = null;
$schedule = [];
$rings = [];
if ($tab != 2) {
if ($tab == 0) {
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) {
$changes = json_decode($results[0]['content'], true);
}
}
$dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
if (count($results) > 0) {
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
}
} else {
$results = FastDBReq($db, "SELECT * FROM RINGS WHERE classId = ?", [$classId]);
if (count($results) > 0) {
$rings = json_decode(findClosestDateSort($results, $day)['content'], true);
}
}
$db = null;
switch ($tab) {
case 0:
if ($changes === null) { // type check!!!
return $schedule;
} else {
return $changes;
}
case 1:
return $schedule;
case 2:
return $rings;
default:
return "tab not found dengi dengi";
}
});
$api->on(
"/editor_publish",
"POST",
function ($req) {
if (!$req->hasData(['adminId', 'tab', 'day', 'newData'])) {
$req->res->code(400);
return "bad data";
}
$adminId = $req->data['adminId'];
$db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) {
$db = null;
$req->res->code(400);
return "админ не найден";
# end
}
$classId = $results[0]['adminClass'];
$day = $req->data['day'];
$tab = intval($req->data['tab']);
$newDataRaw = $req->data['newData'];
$newData = json_decode($newDataRaw, true);
// im too lazy to close db
switch ($tab) {
case 0: // changes
// first get rasp and compare
// if no chnages skip, else publish
$dayOfWeek = getDayOfWeek($day);
$results = FastDBReq($db, "SELECT * FROM schedules WHERE classId = ? AND dayOfWeek = ?", [$classId, $dayOfWeek]);
$publish = true;
if (count($results) > 0) {
$schedule = json_decode(findClosestDateSort($results, $day)['content'], true);
if ($newData === $schedule) { // position sentensive
$publish = false;
}
}
if ($publish) {
// removing changes for today if they are exists
$results = FastDBReq($db, "SELECT * FROM changes WHERE classId = ? AND forDay = ?", [$classId, $day]);
if (count($results) > 0) {
$changes = json_decode($results[0]['content'], true);
if ($newData === $changes) {
// already present
return true;
}
FastDBReq($db, "DELETE FROM changes WHERE forDay = ? AND classId = ?", [$day, $classId]); // SCARY 💀💀💀
}
// creating new
FastDBReq($db, 'INSERT INTO changes VALUES (?, ?, ?)', [$classId, $day, $newDataRaw]);
return true;
}
return false;
case 1: // rasp
$dayOfWeek = getDayOfWeek($day);
// FIXME: im to lazy to find if closest rasp are the same
FastDBReq($db, 'DELETE FROM schedules WHERE classId=? AND fromDate=?', [$classId, $day]);
FastDBReq($db, 'INSERT INTO schedules VALUES (?,?,?,?)', [$classId, $day, $dayOfWeek, $newDataRaw]);
return true;
case 2: // rigns
//same
FastDBReq($db, 'DELETE FROM rings WHERE classId=? AND fromDate=?', [$classId, $day]);
FastDBReq($db, 'INSERT INTO rings VALUES (?,?,?)', [$classId, $day, $newDataRaw]);
return true;
default:
return "tab not found dengi dengi";
}
}
);
$api->on('/editor_fullitems_del', "POST", function ($req) {
// SCARY!!!!!!
// LEVEL 10000000
// 💀💀💀💀💀
if (!$req->hasData(['tab', 'day', 'adminId'])) {
$req->res->code(400);
return "bad data";
}
$adminId = $req->data['adminId'];
$db = db();
$results = FastDBReq($db, "SELECT * FROM admins WHERE adminId=?", [$adminId]);
if (count($results) == 0) {
$db = null;
$req->res->code(400);
return "админ не найден";
# end
}
$classId = $results[0]['adminClass'];
$day = $req->data['day'];
$tab = intval($req->data['tab']);
switch ($tab) {
case 0:
FastDBReq($db, "DELETE FROM changes WHERE forDay = ? AND classId = ?", [$day, $classId]);
break;
case 1:
FastDBReq($db, 'DELETE FROM schedules WHERE classId=? AND fromDate=?', [$classId, $day]);
break;
case 2:
FastDBReq($db, 'DELETE FROM rings WHERE classId=? AND fromDate=?', [$classId, $day]);
break;
default:
return "you saved your live because you did'n know what tabs are here";
}
$db = null;
return true;
});
});