2019-12-04 16:14:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Umbraco.Core.Composing;
|
2019-12-05 11:18:18 +00:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Editors;
|
2019-12-04 16:14:33 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.PropertyEditors
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DataValueReferenceFactoryCollection : BuilderCollectionBase<IDataValueReferenceFactory>
|
|
|
|
|
|
{
|
|
|
|
|
|
public DataValueReferenceFactoryCollection(IEnumerable<IDataValueReferenceFactory> items)
|
|
|
|
|
|
: base(items)
|
|
|
|
|
|
{ }
|
2019-12-05 11:18:18 +00:00
|
|
|
|
|
2020-02-19 16:37:00 +11:00
|
|
|
|
// TODO: We could further reduce circular dependencies with PropertyEditorCollection by not having IDataValueReference implemented
|
|
|
|
|
|
// by property editors and instead just use the already built in IDataValueReferenceFactory and/or refactor that into a more normal collection
|
|
|
|
|
|
|
2019-12-10 12:37:52 +01:00
|
|
|
|
public IEnumerable<UmbracoEntityReference> GetAllReferences(IPropertyCollection properties, PropertyEditorCollection propertyEditors)
|
2019-12-05 11:18:18 +00:00
|
|
|
|
{
|
2020-02-07 12:18:27 +11:00
|
|
|
|
var trackedRelations = new HashSet<UmbracoEntityReference>();
|
2019-12-05 11:18:18 +00:00
|
|
|
|
|
|
|
|
|
|
foreach (var p in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!propertyEditors.TryGet(p.PropertyType.PropertyEditorAlias, out var editor)) continue;
|
|
|
|
|
|
|
2020-02-07 12:18:27 +11:00
|
|
|
|
//TODO: We will need to change this once we support tracking via variants/segments
|
|
|
|
|
|
// for now, we are tracking values from ALL variants
|
2019-12-05 11:18:18 +00:00
|
|
|
|
|
2020-11-19 15:21:10 +01:00
|
|
|
|
foreach (var propertyVal in p.Values)
|
2019-12-05 11:18:18 +00:00
|
|
|
|
{
|
2020-02-07 12:18:27 +11:00
|
|
|
|
var val = propertyVal.EditedValue;
|
|
|
|
|
|
|
|
|
|
|
|
var valueEditor = editor.GetValueEditor();
|
|
|
|
|
|
if (valueEditor is IDataValueReference reference)
|
|
|
|
|
|
{
|
|
|
|
|
|
var refs = reference.GetReferences(val);
|
2020-11-19 15:21:10 +01:00
|
|
|
|
foreach (var r in refs)
|
2020-02-07 12:18:27 +11:00
|
|
|
|
trackedRelations.Add(r);
|
2020-11-19 15:21:10 +01:00
|
|
|
|
}
|
2019-12-05 11:18:18 +00:00
|
|
|
|
|
2020-02-07 12:18:27 +11:00
|
|
|
|
// Loop over collection that may be add to existing property editors
|
|
|
|
|
|
// implementation of GetReferences in IDataValueReference.
|
|
|
|
|
|
// Allows developers to add support for references by a
|
|
|
|
|
|
// package /property editor that did not implement IDataValueReference themselves
|
|
|
|
|
|
foreach (var item in this)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if this value reference is for this datatype/editor
|
|
|
|
|
|
// Then call it's GetReferences method - to see if the value stored
|
|
|
|
|
|
// in the dataeditor/property has referecnes to media/content items
|
|
|
|
|
|
if (item.IsForEditor(editor))
|
|
|
|
|
|
{
|
2020-11-19 15:21:10 +01:00
|
|
|
|
foreach (var r in item.GetDataValueReference().GetReferences(val))
|
2020-02-07 12:18:27 +11:00
|
|
|
|
trackedRelations.Add(r);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-12-05 11:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return trackedRelations;
|
|
|
|
|
|
}
|
2019-12-04 16:14:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|