Add folderkeys to package xml. (#11172)
* Add folderkeys to package xml. * Cleanup * Use folderKeys to find child * Fix potential issue with when no folder * Fixed logic issue. Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
@@ -78,7 +78,7 @@ namespace Umbraco.Cms.Core.Services
|
||||
/// <returns></returns>
|
||||
bool HasContainerInPath(params int[] ids);
|
||||
|
||||
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentContainerId, string name, int userId = Constants.Security.SuperUserId);
|
||||
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentContainerId, Guid key, string name, int userId = Constants.Security.SuperUserId);
|
||||
Attempt<OperationResult> SaveContainer(EntityContainer container, int userId = Constants.Security.SuperUserId);
|
||||
EntityContainer GetContainer(int containerId);
|
||||
EntityContainer GetContainer(Guid containerId);
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Umbraco.Cms.Core.Services
|
||||
/// <returns></returns>
|
||||
IReadOnlyDictionary<Udi, IEnumerable<string>> GetReferences(int id);
|
||||
|
||||
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Constants.Security.SuperUserId);
|
||||
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Constants.Security.SuperUserId);
|
||||
Attempt<OperationResult> SaveContainer(EntityContainer container, int userId = Constants.Security.SuperUserId);
|
||||
EntityContainer GetContainer(int containerId);
|
||||
EntityContainer GetContainer(Guid containerId);
|
||||
@@ -68,7 +68,7 @@ namespace Umbraco.Cms.Core.Services
|
||||
/// <param name="dataTypeDefinitions"><see cref="IDataType"/> to save</param>
|
||||
/// <param name="userId">Id of the user issuing the save</param>
|
||||
void Save(IEnumerable<IDataType> dataTypeDefinitions, int userId = Constants.Security.SuperUserId);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an <see cref="IDataType"/>
|
||||
/// </summary>
|
||||
|
||||
@@ -546,13 +546,33 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
{
|
||||
var alias = documentType.Element("Info").Element("Alias").Value;
|
||||
var folders = foldersAttribute.Value.Split(Constants.CharArrays.ForwardSlash);
|
||||
|
||||
var folderKeysAttribute = documentType.Attribute("FolderKeys");
|
||||
|
||||
var folderKeys = Array.Empty<Guid>();
|
||||
if (folderKeysAttribute != null)
|
||||
{
|
||||
folderKeys = folderKeysAttribute.Value.Split(Constants.CharArrays.ForwardSlash).Select(x=>Guid.Parse(x)).ToArray();
|
||||
}
|
||||
|
||||
var rootFolder = WebUtility.UrlDecode(folders[0]);
|
||||
//level 1 = root level folders, there can only be one with the same name
|
||||
var current = _contentTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
|
||||
|
||||
EntityContainer current;
|
||||
Guid? rootFolderKey = null;
|
||||
if (folderKeys.Length == folders.Length && folderKeys.Length > 0)
|
||||
{
|
||||
rootFolderKey = folderKeys[0];
|
||||
current = _contentTypeService.GetContainer(rootFolderKey.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
//level 1 = root level folders, there can only be one with the same name
|
||||
current = _contentTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
{
|
||||
var tryCreateFolder = _contentTypeService.CreateContainer(-1, rootFolder);
|
||||
var tryCreateFolder = _contentTypeService.CreateContainer(-1, rootFolderKey ?? Guid.NewGuid(), rootFolder);
|
||||
if (tryCreateFolder == false)
|
||||
{
|
||||
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", rootFolder);
|
||||
@@ -567,7 +587,8 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
for (var i = 1; i < folders.Length; i++)
|
||||
{
|
||||
var folderName = WebUtility.UrlDecode(folders[i]);
|
||||
current = CreateContentTypeChildFolder(folderName, current);
|
||||
Guid? folderKey = (folderKeys.Length == folders.Length) ? folderKeys[i] : null;
|
||||
current = CreateContentTypeChildFolder(folderName, folderKey ?? Guid.NewGuid(), current);
|
||||
importedFolders[alias] = current.Id;
|
||||
}
|
||||
}
|
||||
@@ -576,17 +597,17 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
return importedFolders;
|
||||
}
|
||||
|
||||
private EntityContainer CreateContentTypeChildFolder(string folderName, IUmbracoEntity current)
|
||||
private EntityContainer CreateContentTypeChildFolder(string folderName, Guid folderKey, IUmbracoEntity current)
|
||||
{
|
||||
var children = _entityService.GetChildren(current.Id).ToArray();
|
||||
var found = children.Any(x => x.Name.InvariantEquals(folderName));
|
||||
var found = children.Any(x => x.Name.InvariantEquals(folderName) ||x.Key.Equals(folderKey));
|
||||
if (found)
|
||||
{
|
||||
var containerId = children.Single(x => x.Name.InvariantEquals(folderName)).Id;
|
||||
return _contentTypeService.GetContainer(containerId);
|
||||
}
|
||||
|
||||
var tryCreateFolder = _contentTypeService.CreateContainer(current.Id, folderName);
|
||||
var tryCreateFolder = _contentTypeService.CreateContainer(current.Id, folderKey, folderName);
|
||||
if (tryCreateFolder == false)
|
||||
{
|
||||
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", folderName);
|
||||
@@ -1057,17 +1078,26 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
foreach (var datatypeElement in datatypeElements)
|
||||
{
|
||||
var foldersAttribute = datatypeElement.Attribute("Folders");
|
||||
|
||||
if (foldersAttribute != null)
|
||||
{
|
||||
var name = datatypeElement.Attribute("Name").Value;
|
||||
var folders = foldersAttribute.Value.Split(Constants.CharArrays.ForwardSlash);
|
||||
var folderKeysAttribute = datatypeElement.Attribute("FolderKeys");
|
||||
|
||||
var folderKeys = Array.Empty<Guid>();
|
||||
if (folderKeysAttribute != null)
|
||||
{
|
||||
folderKeys = folderKeysAttribute.Value.Split(Constants.CharArrays.ForwardSlash).Select(x=>Guid.Parse(x)).ToArray();
|
||||
}
|
||||
var rootFolder = WebUtility.UrlDecode(folders[0]);
|
||||
var rootFolderKey = folderKeys.Length > 0 ? folderKeys[0] : Guid.NewGuid();
|
||||
//there will only be a single result by name for level 1 (root) containers
|
||||
var current = _dataTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
|
||||
|
||||
if (current == null)
|
||||
{
|
||||
var tryCreateFolder = _dataTypeService.CreateContainer(-1, rootFolder);
|
||||
var tryCreateFolder = _dataTypeService.CreateContainer(-1, rootFolderKey, rootFolder);
|
||||
if (tryCreateFolder == false)
|
||||
{
|
||||
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", rootFolder);
|
||||
@@ -1081,7 +1111,8 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
for (var i = 1; i < folders.Length; i++)
|
||||
{
|
||||
var folderName = WebUtility.UrlDecode(folders[i]);
|
||||
current = CreateDataTypeChildFolder(folderName, current);
|
||||
Guid? folderKey = (folderKeys.Length == folders.Length) ? folderKeys[i] : null;
|
||||
current = CreateDataTypeChildFolder(folderName, folderKey ?? Guid.NewGuid(), current);
|
||||
importedFolders[name] = current.Id;
|
||||
}
|
||||
}
|
||||
@@ -1090,17 +1121,17 @@ namespace Umbraco.Cms.Infrastructure.Packaging
|
||||
return importedFolders;
|
||||
}
|
||||
|
||||
private EntityContainer CreateDataTypeChildFolder(string folderName, IUmbracoEntity current)
|
||||
private EntityContainer CreateDataTypeChildFolder(string folderName, Guid folderKey, IUmbracoEntity current)
|
||||
{
|
||||
var children = _entityService.GetChildren(current.Id).ToArray();
|
||||
var found = children.Any(x => x.Name.InvariantEquals(folderName));
|
||||
var found = children.Any(x => x.Name.InvariantEquals(folderName) ||x.Key.Equals(folderKey));
|
||||
if (found)
|
||||
{
|
||||
var containerId = children.Single(x => x.Name.InvariantEquals(folderName)).Id;
|
||||
return _dataTypeService.GetContainer(containerId);
|
||||
}
|
||||
|
||||
var tryCreateFolder = _dataTypeService.CreateContainer(current.Id, folderName);
|
||||
var tryCreateFolder = _dataTypeService.CreateContainer(current.Id, folderKey,folderName);
|
||||
if (tryCreateFolder == false)
|
||||
{
|
||||
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", folderName);
|
||||
|
||||
@@ -830,7 +830,7 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
|
||||
protected Guid ContainerObjectType => EntityContainer.GetContainerObjectType(ContainedObjectType);
|
||||
|
||||
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
|
||||
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
|
||||
{
|
||||
EventMessages eventMessages = EventMessagesFactory.Get();
|
||||
using (IScope scope = ScopeProvider.CreateScope())
|
||||
@@ -843,7 +843,8 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
{
|
||||
Name = name,
|
||||
ParentId = parentId,
|
||||
CreatorId = userId
|
||||
CreatorId = userId,
|
||||
Key = key
|
||||
};
|
||||
|
||||
var savingNotification = new EntityContainerSavingNotification(container, eventMessages);
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
|
||||
#region Containers
|
||||
|
||||
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
|
||||
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
|
||||
{
|
||||
var evtMsgs = EventMessagesFactory.Get();
|
||||
using (var scope = ScopeProvider.CreateScope())
|
||||
@@ -70,7 +70,8 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
{
|
||||
Name = name,
|
||||
ParentId = parentId,
|
||||
CreatorId = userId
|
||||
CreatorId = userId,
|
||||
Key = key
|
||||
};
|
||||
|
||||
var savingEntityContainerNotification = new EntityContainerSavingNotification(container, evtMsgs);
|
||||
|
||||
@@ -191,18 +191,23 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
xml.Add(new XAttribute("Configuration", _configurationEditorJsonSerializer.Serialize(dataType.Configuration)));
|
||||
|
||||
var folderNames = string.Empty;
|
||||
var folderKeys = string.Empty;
|
||||
if (dataType.Level != 1)
|
||||
{
|
||||
//get URL encoded folder names
|
||||
var folders = _dataTypeService.GetContainers(dataType)
|
||||
.OrderBy(x => x.Level)
|
||||
.Select(x => WebUtility.UrlEncode(x.Name));
|
||||
.OrderBy(x => x.Level);
|
||||
|
||||
folderNames = string.Join("/", folders.ToArray());
|
||||
folderNames = string.Join("/", folders.Select(x => WebUtility.UrlEncode(x.Name)).ToArray());
|
||||
folderKeys = string.Join("/", folders.Select(x => x.Key).ToArray());
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(folderNames) == false)
|
||||
{
|
||||
xml.Add(new XAttribute("Folders", folderNames));
|
||||
xml.Add(new XAttribute("FolderKeys", folderKeys));
|
||||
}
|
||||
|
||||
|
||||
return xml;
|
||||
}
|
||||
@@ -490,19 +495,24 @@ namespace Umbraco.Cms.Core.Services.Implement
|
||||
tabs);
|
||||
|
||||
var folderNames = string.Empty;
|
||||
var folderKeys = string.Empty;
|
||||
//don't add folders if this is a child doc type
|
||||
if (contentType.Level != 1 && masterContentType == null)
|
||||
{
|
||||
//get URL encoded folder names
|
||||
var folders = _contentTypeService.GetContainers(contentType)
|
||||
.OrderBy(x => x.Level)
|
||||
.Select(x => WebUtility.UrlEncode(x.Name));
|
||||
.OrderBy(x => x.Level);
|
||||
|
||||
folderNames = string.Join("/", folders.ToArray());
|
||||
folderNames = string.Join("/", folders.Select(x => WebUtility.UrlEncode(x.Name)).ToArray());
|
||||
folderKeys = string.Join("/", folders.Select(x => x.Key).ToArray());
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(folderNames) == false)
|
||||
{
|
||||
xml.Add(new XAttribute("Folders", folderNames));
|
||||
xml.Add(new XAttribute("FolderKeys", folderKeys));
|
||||
}
|
||||
|
||||
|
||||
return xml;
|
||||
}
|
||||
|
||||
@@ -292,7 +292,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentTypes)]
|
||||
public IActionResult PostCreateContainer(int parentId, string name)
|
||||
{
|
||||
var result = _contentTypeService.CreateContainer(parentId, name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
|
||||
var result = _contentTypeService.CreateContainer(parentId, Guid.NewGuid(), name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
|
||||
|
||||
if (result.Success)
|
||||
return Ok(result.Result); //return the id
|
||||
|
||||
@@ -260,7 +260,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
public IActionResult PostCreateContainer(int parentId, string name)
|
||||
{
|
||||
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
|
||||
var result = _dataTypeService.CreateContainer(parentId, name, currentUser.Id);
|
||||
var result = _dataTypeService.CreateContainer(parentId, Guid.NewGuid(), name, currentUser.Id);
|
||||
|
||||
if (result.Success)
|
||||
return Ok(result.Result); //return the id
|
||||
|
||||
@@ -261,7 +261,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
[Authorize(Policy = AuthorizationPolicies.TreeAccessMediaTypes)]
|
||||
public IActionResult PostCreateContainer(int parentId, string name)
|
||||
{
|
||||
var result = _mediaTypeService.CreateContainer(parentId, name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
|
||||
var result = _mediaTypeService.CreateContainer(parentId, Guid.NewGuid(), name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
|
||||
|
||||
if (result.Success)
|
||||
return Ok(result.Result); //return the id
|
||||
|
||||
Reference in New Issue
Block a user