Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media Element #2147

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions lib/Controller/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,6 @@ public function editWidget(Request $request, Response $response, $id)
$template = $this->moduleTemplateFactory->getByDataTypeAndId($module->dataType, $existingTemplateId);
}

// Before we start, clean out any cached media
$widget->clearCachedMedia();

// We're expecting all of our properties to be supplied for editing.
foreach ($module->properties as $property) {
if ($property->type === 'message') {
Expand Down Expand Up @@ -561,6 +558,8 @@ public function editWidget(Request $request, Response $response, $id)
}
}

// TODO: remove media which is no longer referenced, without removing primary media and/or media in elements

// If we have a validator interface, then use it now
foreach ($widgetValidators as $widgetValidator) {
$widgetValidator->validate($module, $widget, 'save');
Expand Down Expand Up @@ -1622,6 +1621,19 @@ public function saveElements(Request $request, Response $response, $id)
// Store the target regionId
$widget->load();

// Get a list of elements that already exist and their mediaId's
$newMediaIds = [];
$existingMediaIds = [];
foreach (json_decode($widget->getOptionValue('elements', '[]'), true) as $widgetElement) {
foreach ($widgetElement['elements'] ?? [] as $element) {
if (!empty($element['mediaId'])) {
$existingMediaIds[] = intval($element['mediaId']);
}
}
}

$this->getLog()->debug('saveElements: there are ' . count($existingMediaIds) . ' existing mediaIds');

// Pull out elements directly from the request body
$elements = $request->getBody()->getContents();
$elementJson = json_decode($elements, true);
Expand All @@ -1639,6 +1651,18 @@ public function saveElements(Request $request, Response $response, $id)
$slots[] = $slotNo;
$uniqueSlots++;
}

// Handle elements with the mediaId property so that media is linked and unlinked correctly.
if (!empty($element['mediaId'])) {
$mediaId = intval($element['mediaId']);

if (!in_array($mediaId, $existingMediaIds)) {
// Make sure it exists, and we have permission to use it.
$this->mediaFactory->getById($mediaId, false);
}
$widget->assignMedia($mediaId);
$newMediaIds[] = $mediaId;
}
}
}

Expand All @@ -1649,11 +1673,18 @@ public function saveElements(Request $request, Response $response, $id)
// Save elements
$widget->setOptionValue('elements', 'raw', $elements);

// Unassign any mediaIds from elements which are no longer used.
foreach ($existingMediaIds as $existingMediaId) {
if (!in_array($existingMediaId, $newMediaIds)) {
$widget->unassignMedia($existingMediaId);
}
}

// Save
$widget->save([
'saveWidgetOptions' => true,
'saveWidgetAudio' => false,
'saveWidgetMedia' => false,
'saveWidgetMedia' => true,
'notifyDisplays' => false,
'audit' => true
]);
Expand Down
16 changes: 1 addition & 15 deletions lib/Entity/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ class Widget implements \JsonSerializable
public $folderId;
public $permissionsFolderId;

/** @var int[] Original Module Media Ids */
private $originalModuleMediaIds = [];

/** @var array[int] Original Media IDs */
/** @var int[] Original Media IDs */
private $originalMediaIds = [];

/** @var array[WidgetAudio] Original Widget Audio */
Expand Down Expand Up @@ -598,16 +595,6 @@ public function getPrimaryMediaPath(): string
return $this->widgetMediaFactory->getPathForMediaId($this->getPrimaryMediaId());
}

/**
* Clear Media
* this must only clear module media, not "primary" media
*/
public function clearCachedMedia()
{
$this->load();
$this->mediaIds = array_values(array_diff($this->mediaIds, $this->originalModuleMediaIds));
}

/**
* Assign Audio Media
* @param WidgetAudio $audio
Expand Down Expand Up @@ -830,7 +817,6 @@ public function load(bool $loadActions = true): Widget
// Load any media assignments for this widget
$this->mediaIds = $this->widgetMediaFactory->getByWidgetId($this->widgetId);
$this->originalMediaIds = $this->mediaIds;
$this->originalModuleMediaIds = $this->widgetMediaFactory->getModuleOnlyByWidgetId($this->widgetId);

// Load any widget audio assignments
$this->audio = $this->widgetAudioFactory->getByWidgetId($this->widgetId);
Expand Down
8 changes: 6 additions & 2 deletions lib/Factory/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,13 @@ public function processDownloads($success = null, $failure = null, $rejected = n
* @return Media
* @throws NotFoundException
*/
public function getById($mediaId)
public function getById($mediaId, bool $isDisableUserCheck = true)
{
$media = $this->query(null, array('disableUserCheck' => 1, 'mediaId' => $mediaId, 'allModules' => 1));
$media = $this->query(null, [
'disableUserCheck' => $isDisableUserCheck ? 1 : 0,
'mediaId' => $mediaId,
'allModules' => 1,
]);

if (count($media) <= 0) {
throw new NotFoundException(__('Cannot find media'));
Expand Down
10 changes: 0 additions & 10 deletions lib/Factory/WidgetMediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ public function getByWidgetId($widgetId)
return $this->query(null, array('widgetId' => $widgetId));
}

/**
* Media Linked to Widgets by WidgetId
* @param int $widgetId
* @return array[int]
*/
public function getModuleOnlyByWidgetId($widgetId)
{
return $this->query(null, ['widgetId' => $widgetId, 'moduleOnly' => 1]);
}

/**
* @param int $mediaId
* @return int
Expand Down
12 changes: 11 additions & 1 deletion lib/Widget/Render/WidgetHtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private function render(

// Extend any elements which need to be extended.
foreach ($moduleTemplates as $moduleTemplate) {
if ($moduleTemplate->type === 'element' && $moduleTemplate->dataType !== 'global'
if ($moduleTemplate->type === 'element'
&& !empty($moduleTemplate->extends)
&& array_key_exists($moduleTemplate->extends->template, $globalElements)
) {
Expand Down Expand Up @@ -614,6 +614,14 @@ private function render(
);
}
}

// Check the element for a mediaId property and set it to
// [[mediaId=the_id_from_the_mediaId_property]]
if (!empty($element['mediaId'])) {
// Update the element so we output the mediaId replacement
$widgetElements[$widgetIndex]['elements'][$elementIndex]['properties']['mediaId']
= '[[mediaId=' . $element['mediaId'] . ']]';
}
}
}

Expand All @@ -624,6 +632,8 @@ private function render(
// Render out HBS/style from templates
// we do not render Twig here
foreach ($moduleTemplates as $moduleTemplate) {
$this->getLog()->debug('render: outputting module template ' . $moduleTemplate->templateId);

// Handle extends.
$extension = $moduleTemplate->getUnmatchedProperty('extends');
$isExtensionHasHead = false;
Expand Down
3 changes: 0 additions & 3 deletions modules/templates/global-elements.xml
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,6 @@ $(target).find('.date').each(function(_idx, dateEl){
<canRotate>true</canRotate>
<startWidth>250</startWidth>
<startHeight>250</startHeight>
<properties>
<property id="mediaId" type="text"></property>
</properties>
</template>
<template>
<id>line</id>
Expand Down
9 changes: 0 additions & 9 deletions ui/src/editor-core/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,6 @@ Widget.prototype.saveElements = function(
// Save only id and value for element properties if they are not empty
if (element.properties != undefined) {
element.properties = Object.values(element.properties).map((property) => {
// If property is mediaId and it's null, use element mediaId
if (
property.id === 'mediaId' &&
element.mediaId !== undefined &&
property.value === null
) {
property.value = element.mediaId;
}

return {
id: property.id,
value: property.value,
Expand Down
2 changes: 1 addition & 1 deletion ui/src/layout-editor/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ Viewer.prototype.renderElementContent = function(
convertedProperties[extendOverrideKey] = meta[metaKey];
} else if (extendWithDataKey === 'mediaId') {
convertedProperties[extendOverrideKey] =
'[[mediaId=' + convertedProperties[extendWithDataKey] + ']]';
'[[mediaId=' + element.mediaId + ']]';
} else {
convertedProperties[extendOverrideKey] =
(elData) && elData[extendWithDataKey];
Expand Down