From e47ea2882a329aa3d7c0933141c3e244afce2eca Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Mon, 16 Dec 2024 18:47:22 +0100 Subject: [PATCH 1/6] [EdfColorDay Bridge] add new bridge --- bridges/EdfColorDayBridge.php | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 bridges/EdfColorDayBridge.php diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php new file mode 100644 index 00000000000..708260ceb93 --- /dev/null +++ b/bridges/EdfColorDayBridge.php @@ -0,0 +1,116 @@ + [ + 'name' => 'Choisir un contrat', + 'type' => 'list', + // we can add later more option prices like EJP + 'values' => [ + 'Tempo' => 'tempo' + ], + ] + ] + ]; + const CACHE_TIMEOUT = 7200; // 2h + + /** + * @param simple_html_dom $html + * @param string $contractUri + * @return void + */ + private function tempo(string $json): void + { + $jsonDecoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR); + + $values = [ + $this->formatFrenchDate('now') => date('Y-m-d'), + 'Demain ' . $this->formatFrenchDate('tomorrow') => date('Y-m-d', strtotime('+1 day')) + ]; + + foreach ($values as $key => $value) { + $i++; + $item = []; + + $text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]); + $item['uri'] = self::URI . $contractUri; + $item['title'] = $text; + $item['author'] = self::MAINTAINER; + $item['content'] = $text; + $item['uid'] = hash('sha256', $item['title']); + + $this->items[] = $item; + } + } + + private function formatFrenchDate(string $datetime): string + { + // Set the locale to French + setlocale(LC_TIME, 'fr_FR.UTF-8'); + + // Create a DateTime object for the desired date + $now = new DateTime($datetime); + + // Format the date + return strftime('%A %d %B %Y', $now->getTimestamp()); + } + + private function getDisplayableColor(string $color): string + { + $displayableColor = null; + switch($color) { + case 'BLUE': + $displayableColor = '🟦 TEMPO_BLEU'; + break; + case 'WHITE': + $displayableColor = '⬜ TEMPO_BLANC'; + break; + case 'RED': + $displayableColor = '🟥 TEMPO_ROUGE'; + break; + default: + $displayableColor = '⬛ NON_DEFINI'; + break; + } + + return $displayableColor; + } + + private function getTempoYear(): string + { + $month = date('n'); // Current month as a number (1-12) + $year = date('Y'); // Current year + + // Assuming the tempo year starts in September + if ($month >= 9) { + return $year . '-' . ($year + 1); // e.g., 2024-2025 + } + + return ($year - 1) . '-' . $year; // e.g., 2023-2024 + } + + public function collectData() + { + $contract = $this->getKey('contract'); + + $header = [ + 'Content-type: application/json', + ]; + $opts = [ + CURLOPT_HTTPGET => 1, + ]; + + $json = getContents(self::URI . '?season=' . $this->getTempoYear(), $header, $opts); + + if ($contract === 'Tempo') { + $this->tempo($json); + } + } +} From 934c5be96326433c5a84245dcef267d800bb0af2 Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Mon, 16 Dec 2024 18:51:30 +0100 Subject: [PATCH 2/6] [EdfColorDay Bridge] lint --- bridges/EdfColorDayBridge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php index 708260ceb93..1b4e45c40b7 100644 --- a/bridges/EdfColorDayBridge.php +++ b/bridges/EdfColorDayBridge.php @@ -65,7 +65,7 @@ private function formatFrenchDate(string $datetime): string private function getDisplayableColor(string $color): string { $displayableColor = null; - switch($color) { + switch ($color) { case 'BLUE': $displayableColor = '🟦 TEMPO_BLEU'; break; @@ -87,7 +87,7 @@ private function getTempoYear(): string { $month = date('n'); // Current month as a number (1-12) $year = date('Y'); // Current year - + // Assuming the tempo year starts in September if ($month >= 9) { return $year . '-' . ($year + 1); // e.g., 2024-2025 From 23834c92db7dcf63be7ad58f74f79b813bcb6d4f Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Sun, 23 Nov 2025 01:28:24 +0100 Subject: [PATCH 3/6] [EdfColorDayBridge] PR review --- bridges/EdfColorDayBridge.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php index 1b4e45c40b7..f54a1c1064a 100644 --- a/bridges/EdfColorDayBridge.php +++ b/bridges/EdfColorDayBridge.php @@ -19,7 +19,7 @@ class EdfColorDayBridge extends BridgeAbstract ] ] ]; - const CACHE_TIMEOUT = 7200; // 2h + const CACHE_TIMEOUT = 0; //7200; // 2h /** * @param simple_html_dom $html @@ -28,7 +28,7 @@ class EdfColorDayBridge extends BridgeAbstract */ private function tempo(string $json): void { - $jsonDecoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR); + $jsonDecoded = Json::decode($json); $values = [ $this->formatFrenchDate('now') => date('Y-m-d'), @@ -52,17 +52,25 @@ private function tempo(string $json): void private function formatFrenchDate(string $datetime): string { - // Set the locale to French - setlocale(LC_TIME, 'fr_FR.UTF-8'); + // Set the locale and date format + $locale = 'fr_FR'; + $formatter = new IntlDateFormatter( + $locale, + IntlDateFormatter::FULL, // Full date format + IntlDateFormatter::NONE, // No time + null, // Default timezone + IntlDateFormatter::GREGORIAN, // Gregorian calendar + 'EEEE dd MMMM yyyy' // Custom pattern + ); // Create a DateTime object for the desired date $now = new DateTime($datetime); // Format the date - return strftime('%A %d %B %Y', $now->getTimestamp()); + return $formatter->format($now); } - private function getDisplayableColor(string $color): string + private function getDisplayableColor(?string $color): string { $displayableColor = null; switch ($color) { @@ -101,7 +109,7 @@ public function collectData() $contract = $this->getKey('contract'); $header = [ - 'Content-type: application/json', + 'Accept: application/json', ]; $opts = [ CURLOPT_HTTPGET => 1, From 4bacbfaf0c574b5e0e93275b8d58589fe40781a2 Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Sun, 23 Nov 2025 01:31:54 +0100 Subject: [PATCH 4/6] [EdfColorDayBridge] Set default cache to 2hr --- bridges/EdfColorDayBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php index f54a1c1064a..a92714b8857 100644 --- a/bridges/EdfColorDayBridge.php +++ b/bridges/EdfColorDayBridge.php @@ -19,7 +19,7 @@ class EdfColorDayBridge extends BridgeAbstract ] ] ]; - const CACHE_TIMEOUT = 0; //7200; // 2h + const CACHE_TIMEOUT = 7200; // 2h /** * @param simple_html_dom $html From 9f867d2a7ba98ce8736bad71635117f595a2194b Mon Sep 17 00:00:00 2001 From: "Florent V." Date: Mon, 24 Nov 2025 11:24:03 +0100 Subject: [PATCH 5/6] [EdfColorDayBridge] Remove unused increment of variable $i --- bridges/EdfColorDayBridge.php | 1 - 1 file changed, 1 deletion(-) diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php index a92714b8857..1c3f26e2cfd 100644 --- a/bridges/EdfColorDayBridge.php +++ b/bridges/EdfColorDayBridge.php @@ -36,7 +36,6 @@ private function tempo(string $json): void ]; foreach ($values as $key => $value) { - $i++; $item = []; $text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]); From 8a9809d87d752e0197bbb74cb54bc58943be35ae Mon Sep 17 00:00:00 2001 From: "Florent V." Date: Mon, 24 Nov 2025 14:26:50 +0100 Subject: [PATCH 6/6] [EdfColorDayBridge] Remove unused variable + fix URI --- bridges/EdfColorDayBridge.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php index 1c3f26e2cfd..9f82db6562d 100644 --- a/bridges/EdfColorDayBridge.php +++ b/bridges/EdfColorDayBridge.php @@ -22,8 +22,7 @@ class EdfColorDayBridge extends BridgeAbstract const CACHE_TIMEOUT = 7200; // 2h /** - * @param simple_html_dom $html - * @param string $contractUri + * @param string $json * @return void */ private function tempo(string $json): void @@ -39,7 +38,7 @@ private function tempo(string $json): void $item = []; $text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]); - $item['uri'] = self::URI . $contractUri; + $item['uri'] = self::URI . '?season=' . $this->getTempoYear(); $item['title'] = $text; $item['author'] = self::MAINTAINER; $item['content'] = $text;