Skip to content

Commit

Permalink
Update index.php
Browse files Browse the repository at this point in the history
+ Sort style is now by latest date and timestamp
  • Loading branch information
chrischivlog committed Jun 9, 2024
1 parent ad5ce70 commit 93c7b09
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,49 @@

#scan the /exports folder
$directory = "exports/";
$files = scandir($directory, SCANDIR_SORT_DESCENDING);
$files = scandir($directory);

# exclude subdirs from the scan
# exclude subdirs from the scan and filter JSON files
$jsonFiles = array_filter($files, function($file) use ($directory) {
return is_file($directory . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'json';
});

# print out the json files found in export folder
header('Content-Type: application/json');
# Function to extract the timestamp from the filename
function extractTimestamp($filename) {
preg_match('/(\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2})/', $filename, $matches);
if (!empty($matches)) {
return DateTime::createFromFormat('m-d-Y_H-i-s', $matches[1])->getTimestamp();
}
return 0;
}

# Sort the JSON files by the extracted timestamp, descending
usort($jsonFiles, function($a, $b) {
return extractTimestamp($b) - extractTimestamp($a);
});

# Prepare the response data
$data = [];
$counter = 0;

foreach ($jsonFiles as $file) {
$fileData = file_get_contents($directory . '/' . $file);
$obj = json_decode($fileData);

# structure as api

# decode the json file
$fileData = file_get_contents($directory . $file);
$obj = json_decode($fileData);

$title = $obj->title;
$text = $obj->text;
$image = $obj->image;
$title = $obj->title;
$text = $obj->text;
$image = $obj->image;
$timestamp = extractTimestamp($file);

$counter++;
$data[] = [
'number'=>''.$counter.'', # print out counted number
'path'=> 'https://api.ifheroes.de/v1/news/exports/' . $file, # path to file with current url
'date'=> date("d-m-Y", filectime($directory . '/' . $file)), # get creation date from json file
'file_id'=> date("dmYhis", filectime($directory . '/' . $file)), # create an file id out of the date and time
'title'=>''.$title.'',
'text'=>''.$text.'',
'image'=>''.$image.'',
'number' => '' . $counter . '',
'path' => 'https://api.ifheroes.de/v1/news/exports/' . $file,
'date' => date("d-m-Y", $timestamp),
'file_id' => date("dmYHis", $timestamp),
'title' => $title,
'text' => $text,
'image' => $image,
];
}

Expand Down

0 comments on commit 93c7b09

Please sign in to comment.