From 98d246f0216fd95ac0a9cb8e76577e408025b30e Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Thu, 28 Sep 2023 21:17:07 +0300 Subject: [PATCH] All done. Maybe. --- .../Finna/AjaxHandler/GetOrganisationInfo.php | 98 ++++++++++++++-- .../OrganisationInfo/Provider/Kirkanta.php | 3 +- themes/finna2/js/finna-organisation-info.js | 63 ++++++++-- .../finna2/less/finna/organisation-info.less | 17 +-- .../elements/location/open-status.phtml | 5 + .../location/schedule-week-navi.phtml | 17 +++ .../elements/location/widget-details.phtml | 95 +++++++++++++++ .../organisationinfo/elements/widget.phtml | 110 ++---------------- 8 files changed, 278 insertions(+), 130 deletions(-) create mode 100644 themes/finna2/templates/organisationinfo/elements/location/open-status.phtml create mode 100644 themes/finna2/templates/organisationinfo/elements/location/schedule-week-navi.phtml create mode 100644 themes/finna2/templates/organisationinfo/elements/location/widget-details.phtml diff --git a/module/Finna/src/Finna/AjaxHandler/GetOrganisationInfo.php b/module/Finna/src/Finna/AjaxHandler/GetOrganisationInfo.php index 547e50e54c4d..16ac164120c7 100644 --- a/module/Finna/src/Finna/AjaxHandler/GetOrganisationInfo.php +++ b/module/Finna/src/Finna/AjaxHandler/GetOrganisationInfo.php @@ -204,6 +204,24 @@ public function handleRequest(Params $params) ); break; + case 'widget-location': + if (!($id = $params->fromQuery('id'))) { + return $this->handleError('getOrganisationInfo: missing id'); + } + if (!($locationId = $params->fromQuery('locationId') ?: null)) { + return $this->handleError('getOrganisationInfo: missing location id'); + } + $this->setLocationIdCookie($locationId); + + $result = $this->getWidgetLocationData( + $id, + $locationId, + $buildings, + $sectors, + (bool)$params->fromQuery('details', true), + ); + break; + case 'organisation-page-link': if (!($id = $params->fromQuery('id'))) { return $this->handleError('getOrganisationInfo: missing id'); @@ -399,16 +417,7 @@ protected function getWidget( } $orgInfo['list'] = $consortiumInfo['list']; - $locationName = ''; - if (null !== $locationId) { - $locationId = (string)$locationId; - foreach ($consortiumInfo['list'] ?? [] as $location) { - if ((string)$location['id'] === $locationId) { - $locationName = $location['name']; - break; - } - } - } + $locationName = $this->getLocationName($locationId, $orgInfo); $widget = $this->renderer->render( 'organisationinfo/elements/widget.phtml', @@ -417,6 +426,54 @@ protected function getWidget( return compact('widget', 'locationId'); } + /** + * Get widget data for a location + * + * @param string $id Organisation id + * @param string $locationId Location id + * @param array $buildings Buildings + * @param array $sectors Sectors + * @param bool $showDetails Whether details are shown + * + * @return array + */ + protected function getWidgetLocationData( + string $id, + string $locationId, + array $buildings, + array $sectors, + bool $showDetails + ): array { + $consortiumInfo = $this->organisationInfo->getConsortiumInfo($sectors, $id, $buildings); + $defaultLocationId = $consortiumInfo['consortium']['finna']['servicePoint'] ?? null; + if (null === $locationId) { + $locationId = $defaultLocationId; + } + + $orgInfo = $this->organisationInfo->getDetails($sectors, $id, $locationId); + if (!$orgInfo) { + return []; + } + $orgInfo['list'] = $consortiumInfo['list']; + + $locationName = $this->getLocationName($locationId, $orgInfo); + + $openStatus = $this->renderer->render( + 'organisationinfo/elements/location/open-status.phtml', + compact('orgInfo') + ); + $schedule = $this->renderer->render( + 'organisationinfo/elements/location/schedule.phtml', + compact('orgInfo') + ); + $details = $showDetails + ? $this->renderer->render( + 'organisationinfo/elements/location/widget-details.phtml', + compact('orgInfo') + ) : ''; + return compact('openStatus', 'schedule', 'details', 'locationId', 'locationName'); + } + /** * Get organisation page image and link * @@ -440,6 +497,27 @@ protected function getOrganisationPageLink(string $id, array $sectors, string $p return compact('found', 'html'); } + /** + * Get location name from organisation list + * + * @param ?string $locationId Location ID + * @param array $orgInfo Organisation info + * + * @return string + */ + protected function getLocationName(?string $locationId, array $orgInfo): string + { + if (null !== $locationId) { + $locationId = (string)$locationId; + foreach ($orgInfo['list'] ?? [] as $location) { + if ((string)$location['id'] === $locationId) { + return $location['name']; + } + } + } + return ''; + } + /** * Return an error response in JSON format and log the error message. * diff --git a/module/Finna/src/Finna/OrganisationInfo/Provider/Kirkanta.php b/module/Finna/src/Finna/OrganisationInfo/Provider/Kirkanta.php index 86fabb9be0d3..dabd05547830 100644 --- a/module/Finna/src/Finna/OrganisationInfo/Provider/Kirkanta.php +++ b/module/Finna/src/Finna/OrganisationInfo/Provider/Kirkanta.php @@ -162,11 +162,10 @@ public function getConsortiumInfo(string $language, string $id, array $locationF if (!$servicePointResponse) { return []; } - $result = [ + return [ 'consortium' => $consortium, 'list' => $this->parseList($servicePointResponse), ]; - return $this->processDetails($result); } /** diff --git a/themes/finna2/js/finna-organisation-info.js b/themes/finna2/js/finna-organisation-info.js index eb9a1c5eb0ce..038d123bade8 100644 --- a/themes/finna2/js/finna-organisation-info.js +++ b/themes/finna2/js/finna-organisation-info.js @@ -385,11 +385,58 @@ finna.organisationInfo = (function finnaOrganisationInfo() { function loadWidgetLocation(locationId) { + let openStatusEl = container.querySelector('.js-open-status'); + let scheduleEl = container.querySelector('.js-opening-times'); + let selectedLocationEl = container.querySelector('.js-location-dropdown .js-selected'); + if (!openStatusEl || !scheduleEl || !selectedLocationEl) { + console.error('Organisation info widget open status, schedule or selected location element not found'); + return; + } + const loadIndicatorEl = container.querySelector('.js-loader'); + if (!loadIndicatorEl) { + console.error('Organisation info widget load indicator element not found'); + return; + } + + loadIndicatorEl.classList.remove('hidden'); + fetch(VuFind.path + '/AJAX/JSON?' + new URLSearchParams({ + method: 'getOrganisationInfo', + element: 'widget-location', + id: params.id, + locationId: locationId, + buildings: params.buildings || '', + details: params.details || '1' + })) + .then((response) => { + loadIndicatorEl.classList.add('hidden'); + if (!response.ok) { + scheduleEl.textContent = VuFind.translate('error_occurred'); + } else { + response.json().then((result) => { + selectedLocationEl.textContent = result.data.locationName; + openStatusEl.innerHTML = result.data.openStatus; + scheduleEl.innerHTML = result.data.schedule; + if (result.data.details) { + if (detailsEl) { + detailsEl.innerHTML = result.data.details; + } + } + initWeekNavi(result.data.locationId); + finna.layout.initToolTips($(detailsEl)); + }); + } + }); + } + + function initWidget(_params) + { + params = _params; container = document.querySelector('.js-organisation-info-widget'); if (!container) { console.error('Organisation info widget element not found'); return; } + let contentEl = container.querySelector('.js-content'); if (!contentEl) { console.error('Organisation info widget content element not found'); @@ -406,7 +453,6 @@ finna.organisationInfo = (function finnaOrganisationInfo() { method: 'getOrganisationInfo', element: 'widget', id: params.id, - locationId: locationId || '', buildings: params.buildings || '', details: params.details || '1' })) @@ -417,6 +463,7 @@ finna.organisationInfo = (function finnaOrganisationInfo() { } else { response.json().then((result) => { contentEl.innerHTML = result.data.widget; + detailsEl = container.querySelector('.js-details'); initWeekNavi(result.data.locationId); finna.layout.initToolTips($(contentEl)); contentEl.querySelectorAll('.js-location-dropdown ul.dropdown-menu li').forEach((el) => { @@ -424,18 +471,18 @@ finna.organisationInfo = (function finnaOrganisationInfo() { loadWidgetLocation(el.dataset.id); }); }); + contentEl.querySelectorAll('.js-location-dropdown li').forEach((el) => { + el.addEventListener('keydown', (ev) => { + if (ev.keyCode === 13) { + el.click(); + } + }) + }); }); } }); } - function initWidget(_params) - { - params = _params; - - loadWidgetLocation(null); - } - return { init: init, initWidget: initWidget diff --git a/themes/finna2/less/finna/organisation-info.less b/themes/finna2/less/finna/organisation-info.less index c1090399af3f..9c7d69f3c4be 100644 --- a/themes/finna2/less/finna/organisation-info.less +++ b/themes/finna2/less/finna/organisation-info.less @@ -44,7 +44,7 @@ } } - .opening-times { + .js-opening-times { padding-bottom: 20px; h4 { margin: 0; @@ -563,7 +563,7 @@ } } - .opening-times { + .js-opening-times { padding-bottom: 0; } @@ -727,10 +727,13 @@ padding-bottom: 20px; width: 100%; - .location-is-open, .location-is-closed { - font-size: 1.45em; - font-weight: bold; - margin-left: 1rem; + .js-open-status { + display: inline; + .location-is-open, .location-is-closed { + font-size: 1.45em; + font-weight: bold; + margin-left: 1rem; + } } .btn { @@ -829,7 +832,7 @@ color: #000; } } - .selected { + .js-selected { display: inline-block; line-height: 1; max-width: 100%; diff --git a/themes/finna2/templates/organisationinfo/elements/location/open-status.phtml b/themes/finna2/templates/organisationinfo/elements/location/open-status.phtml new file mode 100644 index 000000000000..0b0b54222880 --- /dev/null +++ b/themes/finna2/templates/organisationinfo/elements/location/open-status.phtml @@ -0,0 +1,5 @@ + + transEsc('organisation_info_is_open')?> + + transEsc('organisation_info_is_closed')?> + diff --git a/themes/finna2/templates/organisationinfo/elements/location/schedule-week-navi.phtml b/themes/finna2/templates/organisationinfo/elements/location/schedule-week-navi.phtml new file mode 100644 index 000000000000..87581b9279ac --- /dev/null +++ b/themes/finna2/templates/organisationinfo/elements/location/schedule-week-navi.phtml @@ -0,0 +1,17 @@ + + +
+ + + transEsc('organisation_info_schedule_week')?> + escapeHtml(date('W', $firstDate))?> + + + +
+ + diff --git a/themes/finna2/templates/organisationinfo/elements/location/widget-details.phtml b/themes/finna2/templates/organisationinfo/elements/location/widget-details.phtml new file mode 100644 index 000000000000..ba4034786f37 --- /dev/null +++ b/themes/finna2/templates/organisationinfo/elements/location/widget-details.phtml @@ -0,0 +1,95 @@ +
+ + + + + +
diff --git a/themes/finna2/templates/organisationinfo/elements/widget.phtml b/themes/finna2/templates/organisationinfo/elements/widget.phtml index 585d3dd0e68a..73c57b912bd4 100644 --- a/themes/finna2/templates/organisationinfo/elements/widget.phtml +++ b/themes/finna2/templates/organisationinfo/elements/widget.phtml @@ -4,7 +4,7 @@ - - transEsc('organisation_info_is_open')?> - - transEsc('organisation_info_is_closed')?> - +
+ render('organisationinfo/elements/location/open-status.phtml')?> +
-
+
render('organisationinfo/elements/location/schedule.phtml')?>
-
-
- - - - - -
+
+ render('organisationinfo/elements/location/widget-details.phtml')?>