39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php
|
|
class Stories {
|
|
private static $storiesDir = __DIR__ . '/../stories/';
|
|
|
|
public static function getAll() {
|
|
$stories = [];
|
|
foreach (glob(self::$storiesDir . '*.json') as $file) {
|
|
$story = json_decode(file_get_contents($file), true);
|
|
$stories[] = $story;
|
|
}
|
|
return $stories;
|
|
}
|
|
|
|
public static function get($id) {
|
|
$file = self::$storiesDir . $id . '.json';
|
|
if (!file_exists($file)) {
|
|
return null;
|
|
}
|
|
return json_decode(file_get_contents($file), true);
|
|
}
|
|
|
|
public static function save($story) {
|
|
// Ajout de l'heure à la date de mise à jour
|
|
$story['updated'] = date('Y-m-d H:i:s');
|
|
|
|
$file = self::$storiesDir . $story['id'] . '.json';
|
|
return file_put_contents($file, json_encode($story, JSON_PRETTY_PRINT));
|
|
}
|
|
|
|
public static function formatDate($date) {
|
|
if (strlen($date) > 10) { // Si la date contient aussi l'heure
|
|
$datetime = DateTime::createFromFormat('Y-m-d H:i:s', $date);
|
|
return $datetime ? $datetime->format('d/m/Y H:i') : '';
|
|
} else { // Pour les anciennes dates sans heure
|
|
$datetime = DateTime::createFromFormat('Y-m-d', $date);
|
|
return $datetime ? $datetime->format('d/m/Y') : '';
|
|
}
|
|
}
|
|
} |