From 6a633d90cad65ccf0b1ce6836ea017f2fc2edac0 Mon Sep 17 00:00:00 2001 From: mgbaybay Date: Thu, 17 Oct 2024 15:51:54 +0800 Subject: [PATCH] Layouts: Update canvas-only layout duration --- lib/Controller/Widget.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Controller/Widget.php b/lib/Controller/Widget.php index abc8940a15..919a546e2b 100644 --- a/lib/Controller/Widget.php +++ b/lib/Controller/Widget.php @@ -1782,6 +1782,9 @@ public function saveElements(Request $request, Response $response, $id) // Parse the element JSON to see if we need to set `itemsPerPage` $slots = []; $uniqueSlots = 0; + $isMediaOnlyWidget = true; + $maxDuration = 1; + foreach ($elementJson as $widgetElement) { foreach ($widgetElement['elements'] ?? [] as $element) { $slotNo = 'slot_' . ($element['slot'] ?? 0); @@ -1796,10 +1799,13 @@ public function saveElements(Request $request, Response $response, $id) if (!in_array($mediaId, $existingMediaIds)) { // Make sure it exists, and we have permission to use it. - $this->mediaFactory->getById($mediaId, false); + $media = $this->mediaFactory->getById($mediaId, false); + $maxDuration = $media->duration ?? 10; } $widget->assignMedia($mediaId); $newMediaIds[] = $mediaId; + } else { + $isMediaOnlyWidget = false; } } } @@ -1829,6 +1835,21 @@ public function saveElements(Request $request, Response $response, $id) } } + // Canvas-only layout without a custom duration + if ($widget->type == 'global' && $isMediaOnlyWidget && $widget->useDuration == 0) { + // Do we need to recalculate the duration? + if (count($newMediaIds) < count($existingMediaIds)) { + foreach ($newMediaIds as $newMediaId) { + $media = $this->mediaFactory->getById($newMediaId, false); + $maxDuration = max($media->duration, $maxDuration); + } + } else { + $maxDuration = max($widget->calculatedDuration, $maxDuration); + } + + $widget->calculatedDuration = $maxDuration; + } + // Save, without auditing widget options. $widget->save([ 'saveWidgetOptions' => true, @@ -1837,7 +1858,8 @@ public function saveElements(Request $request, Response $response, $id) 'notifyDisplays' => false, 'audit' => true, 'auditWidgetOptions' => false, - 'auditMessage' => 'Elements Updated', + 'auditMessage' => 'Widget Duration: ' . $widget->duration . ', Widget Calculated Duration: ' . $widget->calculatedDuration + . ' maxDuration: ' . $maxDuration ]); // Successful