From 8d180df5de1a3695457efd5d254506ed76424498 Mon Sep 17 00:00:00 2001 From: Jim Mason Date: Wed, 16 Oct 2024 12:46:32 +0100 Subject: [PATCH 1/3] migrate home page recently played date and time pickers to twig --- js/home.js | 4 ++-- ui/Home.php | 40 ++++++++++++++++----------------- ui/templates/default/onnow.html | 24 ++++++++++++++++++-- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/js/home.js b/js/home.js index caf52ffc..5513c774 100644 --- a/js/home.js +++ b/js/home.js @@ -2,7 +2,7 @@ // Zookeeper Online // // @author Jim Mason -// @copyright Copyright (C) 1997-2023 Jim Mason +// @copyright Copyright (C) 1997-2024 Jim Mason // @link https://zookeeper.ibinx.com/ // @license GPL-3.0 // @@ -220,7 +220,7 @@ $().ready(function(){ accept: 'application/json; charset=utf-8', url: url }).done(function (response) { - $("#time").empty().append(response.times).selectmenu('refresh'); + $("#time").empty().append(response).selectmenu('refresh'); var time = $("#time").val(); populateCards(true, time == 'now' ? null : (date + " " + time)); }); diff --git a/ui/Home.php b/ui/Home.php index 6a069d51..89232a77 100644 --- a/ui/Home.php +++ b/ui/Home.php @@ -3,7 +3,7 @@ * Zookeeper Online * * @author Jim Mason - * @copyright Copyright (C) 1997-2022 Jim Mason + * @copyright Copyright (C) 1997-2024 Jim Mason * @link https://zookeeper.ibinx.com/ * @license GPL-3.0 * @@ -47,46 +47,44 @@ public function recentSpins() { } protected function makeDatePicker() { + $result = []; + $now = new \DateTime(); - $result = ""; - $now->modify("-1 days"); - $result .= ""; - for($i=0; $i<5; $i++) { + $result[] = clone $now; + + for($i=0; $i<6; $i++) { $now->modify("-1 days"); - $result .= ""; + $result[] = clone $now; } - return $result; + $this->addVar("dates", $result); } protected function makeTimePicker($date=null) { + $result = []; + $now = new \DateTime(); if(!$date || $now->format("Y-m-d") == $date) { // today - $result = ""; $hour = (int)$now->format("H"); + $result[] = -1; } else { - $result = ""; $hour = 23; + $result[] = $hour; } do { if($hour % 3) continue; - $h = sprintf("%02d", $hour); - $ampm = $h >= 12?"pm":"am"; - $hx = $h > 12?$hour-12:$hour; - $dh = $h == 12?"noon":($hx.$ampm); - $result .= ""; + $result[] = $hour; } while(--$hour > 0); - return $result; + $this->addVar("times", $result); } public function getTimes() { - $retVal = []; - $retVal['times'] = $this->makeTimePicker($_REQUEST["date"] ?? null); - echo json_encode($retVal); + $this->setTemplate('onnow.html'); + $this->makeTimePicker($_REQUEST["date"] ?? null); + echo json_encode($this->render('time')); } public function emitHome() { @@ -102,8 +100,8 @@ public function emitHome() { private function emitRecentlyPlayed() { $this->addVar('discogs', true); - $this->addVar('datepicker', $this->makeDatePicker()); - $this->addVar('timepicker', $this->makeTimePicker()); + $this->makeDatePicker(); + $this->makeTimePicker(); } private function emitTopPlays($numweeks=1, $limit=10) { diff --git a/ui/templates/default/onnow.html b/ui/templates/default/onnow.html index f3dbb6a4..7835766d 100644 --- a/ui/templates/default/onnow.html +++ b/ui/templates/default/onnow.html @@ -36,8 +36,28 @@

Recently Played on {{ app.station }}

- - + +
From 1871ac3fdd1d387d3c6db6b026b9f4ddb55954ba Mon Sep 17 00:00:00 2001 From: Jim Mason Date: Wed, 16 Oct 2024 17:23:03 +0100 Subject: [PATCH 2/3] ensure ajax response is valid json --- js/home.js | 4 ++-- ui/Home.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/js/home.js b/js/home.js index 5513c774..caf52ffc 100644 --- a/js/home.js +++ b/js/home.js @@ -2,7 +2,7 @@ // Zookeeper Online // // @author Jim Mason -// @copyright Copyright (C) 1997-2024 Jim Mason +// @copyright Copyright (C) 1997-2023 Jim Mason // @link https://zookeeper.ibinx.com/ // @license GPL-3.0 // @@ -220,7 +220,7 @@ $().ready(function(){ accept: 'application/json; charset=utf-8', url: url }).done(function (response) { - $("#time").empty().append(response).selectmenu('refresh'); + $("#time").empty().append(response.times).selectmenu('refresh'); var time = $("#time").val(); populateCards(true, time == 'now' ? null : (date + " " + time)); }); diff --git a/ui/Home.php b/ui/Home.php index 89232a77..a5fa948b 100644 --- a/ui/Home.php +++ b/ui/Home.php @@ -84,7 +84,7 @@ protected function makeTimePicker($date=null) { public function getTimes() { $this->setTemplate('onnow.html'); $this->makeTimePicker($_REQUEST["date"] ?? null); - echo json_encode($this->render('time')); + echo json_encode([ "times" => $this->render('time') ]); } public function emitHome() { From b1ba2f0f51b3aa62573930d4c398f29e214b8e5a Mon Sep 17 00:00:00 2001 From: Jim Mason Date: Wed, 16 Oct 2024 19:34:53 +0100 Subject: [PATCH 3/3] finessed date and time combo generation --- ui/Home.php | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/ui/Home.php b/ui/Home.php index a5fa948b..e836d224 100644 --- a/ui/Home.php +++ b/ui/Home.php @@ -47,38 +47,29 @@ public function recentSpins() { } protected function makeDatePicker() { - $result = []; - - $now = new \DateTime(); - $result[] = clone $now; - - for($i=0; $i<6; $i++) { - $now->modify("-1 days"); - $result[] = clone $now; - } + $result = array_map(function($i) { + return (new \DateTime())->modify("-$i days"); + }, range(0, 6)); $this->addVar("dates", $result); } protected function makeTimePicker($date=null) { - $result = []; - $now = new \DateTime(); if(!$date || $now->format("Y-m-d") == $date) { // today $hour = (int)$now->format("H"); - $result[] = -1; - } else { - $hour = 23; - $result[] = $hour; - } + $initial = -1; + } else + $initial = $hour = 23; + + $result = array_filter(range(1, $hour), function($i) { + return $i % 3 === 0; + }); - do { - if($hour % 3) continue; - $result[] = $hour; - } while(--$hour > 0); + $result[] = $initial; - $this->addVar("times", $result); + $this->addVar("times", array_reverse($result)); } public function getTimes() {