From 7e11fdd6fcde435e1467451e5e988d7facdcdbcd Mon Sep 17 00:00:00 2001 From: Dan Garner Date: Sat, 5 Aug 2023 12:34:43 +0100 Subject: [PATCH] Widget: fix RSS/Mastodon/Notification duration calculation. --- lib/Entity/Module.php | 3 ++ lib/Widget/MastodonProvider.php | 8 +-- lib/Widget/NotificationProvider.php | 10 ++-- .../DurationProviderNumItemsTrait.php | 50 +++++++++++++++++++ lib/Widget/RssProvider.php | 10 ++-- 5 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 lib/Widget/Provider/DurationProviderNumItemsTrait.php diff --git a/lib/Entity/Module.php b/lib/Entity/Module.php index 105924c410..4dac4a35d7 100644 --- a/lib/Entity/Module.php +++ b/lib/Entity/Module.php @@ -393,6 +393,9 @@ public function calculateDuration(Widget $widget): ?int } else if ($this->widgetProvider === null) { return null; } + + $this->getLog()->debug('calculateDuration: using widget provider'); + $durationProvider = $this->moduleFactory->createDurationProvider($this, $widget); $this->widgetProvider->fetchDuration($durationProvider); diff --git a/lib/Widget/MastodonProvider.php b/lib/Widget/MastodonProvider.php index ed4047dd14..786044f1c7 100644 --- a/lib/Widget/MastodonProvider.php +++ b/lib/Widget/MastodonProvider.php @@ -26,7 +26,7 @@ use GuzzleHttp\Exception\RequestException; use Xibo\Widget\DataType\SocialMedia; use Xibo\Widget\Provider\DataProviderInterface; -use Xibo\Widget\Provider\DurationProviderInterface; +use Xibo\Widget\Provider\DurationProviderNumItemsTrait; use Xibo\Widget\Provider\WidgetProviderInterface; use Xibo\Widget\Provider\WidgetProviderTrait; @@ -36,6 +36,7 @@ class MastodonProvider implements WidgetProviderInterface { use WidgetProviderTrait; + use DurationProviderNumItemsTrait; public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface { @@ -146,11 +147,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn return $this; } - public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface - { - return $this; - } - public function getDataCacheKey(DataProviderInterface $dataProvider): ?string { // No special cache key requirements. diff --git a/lib/Widget/NotificationProvider.php b/lib/Widget/NotificationProvider.php index f9e9d08ef6..ad38c45012 100644 --- a/lib/Widget/NotificationProvider.php +++ b/lib/Widget/NotificationProvider.php @@ -2,7 +2,7 @@ /* * Copyright (C) 2023 Xibo Signage Ltd * - * Xibo - Digital Signage - http://www.xibo.org.uk + * Xibo - Digital Signage - https://xibosignage.com * * This file is part of Xibo. * @@ -25,13 +25,14 @@ use Carbon\Carbon; use Xibo\Event\NotificationDataRequestEvent; use Xibo\Widget\Provider\DataProviderInterface; -use Xibo\Widget\Provider\DurationProviderInterface; +use Xibo\Widget\Provider\DurationProviderNumItemsTrait; use Xibo\Widget\Provider\WidgetProviderInterface; use Xibo\Widget\Provider\WidgetProviderTrait; class NotificationProvider implements WidgetProviderInterface { use WidgetProviderTrait; + use DurationProviderNumItemsTrait; public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface { @@ -43,11 +44,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn return $this; } - public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface - { - return $this; - } - public function getDataCacheKey(DataProviderInterface $dataProvider): ?string { return $dataProvider->getWidgetId() . '_' . $dataProvider->getDisplayId(); diff --git a/lib/Widget/Provider/DurationProviderNumItemsTrait.php b/lib/Widget/Provider/DurationProviderNumItemsTrait.php new file mode 100644 index 0000000000..5840bc9c69 --- /dev/null +++ b/lib/Widget/Provider/DurationProviderNumItemsTrait.php @@ -0,0 +1,50 @@ +. + */ + +namespace Xibo\Widget\Provider; + +/** + * A trait providing the duration for widgets using numItems, durationIsPerItem and itemsPerPage + */ +trait DurationProviderNumItemsTrait +{ + public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface + { + $this->getLog()->debug('fetchDuration: DurationProviderNumItemsTrait'); + + // Take some default action to cover the majourity of region specific widgets + // Duration can depend on the number of items per page for some widgets + // this is a legacy way of working, and our preference is to use elements + $numItems = $durationProvider->getWidget()->getOptionValue('numItems', 15); + + if ($durationProvider->getWidget()->getOptionValue('durationIsPerItem', 0) == 1 && $numItems > 1) { + // If we have paging involved then work out the page count. + $itemsPerPage = $durationProvider->getWidget()->getOptionValue('itemsPerPage', 0); + if ($itemsPerPage > 0) { + $numItems = ceil($numItems / $itemsPerPage); + } + + $durationProvider->setDuration($durationProvider->getWidget()->calculatedDuration * $numItems); + } + return $this; + } +} diff --git a/lib/Widget/RssProvider.php b/lib/Widget/RssProvider.php index efee996ee6..313b372d7a 100644 --- a/lib/Widget/RssProvider.php +++ b/lib/Widget/RssProvider.php @@ -33,16 +33,17 @@ use Xibo\Support\Exception\InvalidArgumentException; use Xibo\Widget\DataType\Article; use Xibo\Widget\Provider\DataProviderInterface; -use Xibo\Widget\Provider\DurationProviderInterface; +use Xibo\Widget\Provider\DurationProviderNumItemsTrait; use Xibo\Widget\Provider\WidgetProviderInterface; use Xibo\Widget\Provider\WidgetProviderTrait; /** - * Downloads a RSS feed and returns Article data types + * Downloads an RSS feed and returns Article data types */ class RssProvider implements WidgetProviderInterface { use WidgetProviderTrait; + use DurationProviderNumItemsTrait; public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface { @@ -225,11 +226,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn return $this; } - public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface - { - return $this; - } - public function getDataCacheKey(DataProviderInterface $dataProvider): ?string { // No special cache key requirements.