Files
Umbraco-CMS/src/Umbraco.Core/Models/Template.cs
Kenn Jacobsen 01224fce89 Template API for new backoffice (#13642)
* Template query builder API

* Create a dedicated template service (copy template operations from file service)

* CRUD API for templates

* Make file service consume the template service (remove duplicated code)

* Use the template service in the old template controller so we can track changes better (note: this is breaking, but it doesn't matter as the controller will be deleted)

* Add scaffolding to the template API

* Make the route differ between query settings and execution

* Get rid of ugly string constants

* Refactor query execution a little to improve code health

* Fix build checks (compat)

* Deduce the master template from the template contents

* Make template service async, move master template parsing into template service

* Fix open API test

* Make sure the unit tests use new template parsing

* Add FIXME for SetMasterTemplate

* added obsolete attributes

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2023-01-11 14:40:41 +01:00

88 lines
2.8 KiB
C#

using System.Runtime.Serialization;
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Models;
/// <summary>
/// Represents a Template file.
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public class Template : File, ITemplate
{
private readonly IShortStringHelper _shortStringHelper;
private string _alias;
private string? _masterTemplateAlias;
private Lazy<int>? _masterTemplateId;
private string? _name;
public Template(IShortStringHelper shortStringHelper, string? name, string? alias)
: this(shortStringHelper, name, alias, null)
{
}
public Template(IShortStringHelper shortStringHelper, string? name, string? alias, Func<File, string?>? getFileContent)
: base(string.Empty, getFileContent)
{
_shortStringHelper = shortStringHelper;
_name = name;
_alias = alias?.ToCleanString(shortStringHelper, CleanStringType.UnderscoreAlias) ?? string.Empty;
_masterTemplateId = new Lazy<int>(() => -1);
}
[DataMember]
public Lazy<int>? MasterTemplateId
{
get => _masterTemplateId;
set => SetPropertyValueAndDetectChanges(value, ref _masterTemplateId, nameof(MasterTemplateId));
}
public string? MasterTemplateAlias
{
get => _masterTemplateAlias;
set => SetPropertyValueAndDetectChanges(value, ref _masterTemplateAlias, nameof(MasterTemplateAlias));
}
[DataMember]
public new string? Name
{
get => _name;
set => SetPropertyValueAndDetectChanges(value, ref _name, nameof(Name));
}
[DataMember]
public new string Alias
{
get => _alias;
set => SetPropertyValueAndDetectChanges(
value.ToCleanString(_shortStringHelper, CleanStringType.UnderscoreAlias), ref _alias!, nameof(Alias));
}
/// <summary>
/// Returns true if the template is used as a layout for other templates (i.e. it has 'children')
/// </summary>
public bool IsMasterTemplate { get; set; }
// FIXME: moving forward the master template is calculated from the actual template content; figure out how to get rid of this method, or at least *only* use it from TemplateService
[Obsolete("MasterTemplate is now calculated from the content. This will be removed in Umbraco 15.")]
public void SetMasterTemplate(ITemplate? masterTemplate)
{
if (masterTemplate == null)
{
MasterTemplateId = new Lazy<int>(() => -1);
MasterTemplateAlias = null;
}
else
{
MasterTemplateId = new Lazy<int>(() => masterTemplate.Id);
MasterTemplateAlias = masterTemplate.Alias;
}
}
protected override void DeepCloneNameAndAlias(File clone)
{
// do nothing - prevents File from doing its stuff
}
}