MySC/includes/views/index.php
2024-09-21 17:07:52 +02:00

161 lines
4.9 KiB
PHP

<?php
use EesyPHP\Auth;
use EesyPHP\Db;
use EesyPHP\Log;
use EesyPHP\Tpl;
use EesyPHP\Url;
use MySC\Db\Category;
use MySC\Db\SCase;
use MySC\Db\Thing;
use function EesyPHP\vardump;
use function EesyPHP\generate_uuid;
if (php_sapi_name() == "cli")
return true;
/**
* Redirect to homepage
* @param EesyPHP\UrlRequest $request
* @return void
*/
function handle_redirect_homepage($request) {
Url::redirect("home");
}
Url :: add_url_handler(null, 'handle_redirect_homepage', null, false);
/**
* Homepage
* @param EesyPHP\UrlRequest $request
* @return void
*/
function handle_homepage($request) {
Tpl :: display("index.tpl");
}
Url :: add_url_handler("#^home$#", 'handle_homepage', null, false);
function _list_static_directory_files($root_dir, $dir, &$result, &$last_updated) {
foreach (array_diff(scandir($dir), array('.','..')) as $file) {
$path = "$dir/$file";
if (is_dir($path)) {
_list_static_directory_files($root_dir, $path, $result, $last_updated);
}
else {
$result[] = Tpl::static_url(substr($path, strlen($root_dir)+1));
$file_last_updated = filemtime($path);
if ($file_last_updated > $last_updated)
$last_updated = $file_last_updated;
}
}
}
/**
* Cache manifest file
* @param EesyPHP\UrlRequest $request
* @return void
*/
function handle_cache_manifest($request) {
$cache = ["home"];
$last_updated = filemtime(Tpl::resolve_templates_path("index.tpl"));
foreach (Tpl::static_directories() as $static_directory)
_list_static_directory_files($static_directory, $static_directory, $cache, $last_updated);
Tpl::assign("cache", $cache);
Tpl::assign("last_updated", date("Y/m/d H:i:s", $last_updated));
header("Content-type: text/plain");
$content = Tpl::fetch("cache_manifest.tpl");
$etag = md5($content);
header("Cache-Control: max-age=3000, must-revalidate");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_updated)." GMT");
header("Etag: $etag");
print($content);
exit();
}
Url :: add_url_handler("#^cache\.manifest$#", 'handle_cache_manifest');
/**
* Sync data
* @param EesyPHP\UrlRequest $request
* @return void
*/
function handle_sync($request) {
$data = json_decode($_POST["data"], true);
Log::debug("Sync scases data: %s", vardump($data["scases"]));
$user = Auth::user();
$updated = false;
$db_scases = SCase :: list(['username' => $user -> username]);
foreach($data["scases"] as $scase_uuid => $scase_data) {
Log::debug("sync(): scase %s", $scase_uuid);
$scase = Scase :: from_json($scase_data, $user);
if (array_key_exists($scase_uuid, $db_scases)) {
Log::debug("sync(): scase %s exist in DB", $scase_uuid);
$db_scases[$scase_uuid] -> sync($scase, $updated);
// @phpstan-ignore-next-line
$db_categories = $db_scases[$scase_uuid] -> categories();
}
else {
Log::debug("sync(): scase %s does not exist in DB, create it", $scase_uuid);
$scase -> save();
$updated = true;
$db_categories = [];
}
foreach($scase_data['cats'] as $category_uuid => $category_data) {
Log::debug("sync(): scase %s / category %s", $scase_uuid, $category_uuid);
$category = Category :: from_json($category_data, $scase);
if (array_key_exists($category_uuid, $db_categories)) {
Log::debug("sync(): scase %s / category %s exists in DB, sync it", $scase_uuid, $category_uuid);
$db_categories[$category_uuid] -> sync($category, $updated);
$db_things = $db_categories[$category_uuid] ->things();
}
else {
Log::debug("sync(): scase %s / category %s does not exists in DB, create it", $scase_uuid, $category_uuid);
$category -> save();
$updated = true;
$db_things = [];
}
foreach($category_data['things'] as $thing_uuid => $thing_data) {
$thing = Thing :: from_json($thing_data, $category);
if (array_key_exists($thing_uuid, $db_things)) {
$db_things[$thing_uuid] -> sync($thing, $updated);
}
else {
$thing -> save();
$updated = true;
}
}
}
}
$data = [
"scases" => [],
"updated" => $updated,
];
foreach (SCase :: list(['username' => $user -> username]) as $uuid => $scase) {
$data["scases"][$uuid] = $scase -> to_json();
}
Tpl::display_ajax_return($data);
}
Url :: add_url_handler("#^sync$#", 'handle_sync', null, true, true, true);
/**
* Support info page
* @param EesyPHP\UrlRequest $request
* @return void
*/
function handle_support_info($request) {
if (isset($_REQUEST['download'])) {
header('Content-Type: text/plain');
header('Content-disposition: attachment; filename="'.date('Ymd-His-').Auth::user()->username.'-aide-support.txt"');
}
Tpl :: display(
isset($_REQUEST['download'])?
'support_info_content.tpl':"support_info.tpl",
"Page d'aide à l'assistance utilisateurs"
);
}
Url :: add_url_handler('|^support/?$|', 'handle_support_info');
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab