2020-01-21 13:33:39 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Web.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.TestData
|
|
|
|
|
|
{
|
|
|
|
|
|
public class SegmentTestController : SurfaceController
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public ActionResult EnableDocTypeSegments(string alias, string propertyTypeAlias)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ConfigurationManager.AppSettings["Umbraco.TestData.Enabled"] != "true")
|
|
|
|
|
|
return HttpNotFound();
|
|
|
|
|
|
|
|
|
|
|
|
var ct = Services.ContentTypeService.Get(alias);
|
|
|
|
|
|
if (ct == null)
|
|
|
|
|
|
return Content($"No document type found by alias {alias}");
|
|
|
|
|
|
|
|
|
|
|
|
var propType = ct.PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
|
|
|
|
|
|
if (propType == null)
|
|
|
|
|
|
return Content($"The document type {alias} does not have a property type {propertyTypeAlias ?? "null"}");
|
|
|
|
|
|
|
|
|
|
|
|
if (ct.Variations.VariesBySegment())
|
|
|
|
|
|
return Content($"The document type {alias} already allows segments, nothing has been changed");
|
|
|
|
|
|
|
2020-01-22 11:10:56 +01:00
|
|
|
|
ct.SetVariesBy(ContentVariation.Segment);
|
|
|
|
|
|
propType.SetVariesBy(ContentVariation.Segment);
|
2020-01-21 13:33:39 +11:00
|
|
|
|
|
|
|
|
|
|
Services.ContentTypeService.Save(ct);
|
|
|
|
|
|
return Content($"The document type {alias} and property type {propertyTypeAlias} now allows segments");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionResult DisableDocTypeSegments(string alias)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ConfigurationManager.AppSettings["Umbraco.TestData.Enabled"] != "true")
|
|
|
|
|
|
return HttpNotFound();
|
|
|
|
|
|
|
|
|
|
|
|
var ct = Services.ContentTypeService.Get(alias);
|
|
|
|
|
|
if (ct == null)
|
|
|
|
|
|
return Content($"No document type found by alias {alias}");
|
|
|
|
|
|
|
|
|
|
|
|
if (!ct.VariesBySegment())
|
|
|
|
|
|
return Content($"The document type {alias} does not allow segments, nothing has been changed");
|
|
|
|
|
|
|
2020-01-22 11:10:56 +01:00
|
|
|
|
ct.SetVariesBy(ContentVariation.Segment, false);
|
2020-01-21 13:33:39 +11:00
|
|
|
|
|
|
|
|
|
|
Services.ContentTypeService.Save(ct);
|
|
|
|
|
|
return Content($"The document type {alias} no longer allows segments");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionResult AddSegmentData(int contentId, string propertyAlias, string value, string segment, string culture = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = Services.ContentService.GetById(contentId);
|
|
|
|
|
|
if (content == null)
|
|
|
|
|
|
return Content($"No content found by id {contentId}");
|
|
|
|
|
|
|
2020-01-21 13:52:53 +11:00
|
|
|
|
if (propertyAlias.IsNullOrWhiteSpace() || !content.HasProperty(propertyAlias))
|
|
|
|
|
|
return Content($"The content by id {contentId} does not contain a property with alias {propertyAlias ?? "null"}");
|
2020-01-21 13:33:39 +11:00
|
|
|
|
|
|
|
|
|
|
if (content.ContentType.VariesByCulture() && culture.IsNullOrWhiteSpace())
|
|
|
|
|
|
return Content($"The content by id {contentId} varies by culture but no culture was specified");
|
|
|
|
|
|
|
2020-01-21 13:52:53 +11:00
|
|
|
|
if (value.IsNullOrWhiteSpace())
|
|
|
|
|
|
return Content("'value' cannot be null");
|
|
|
|
|
|
|
|
|
|
|
|
if (segment.IsNullOrWhiteSpace())
|
|
|
|
|
|
return Content("'segment' cannot be null");
|
|
|
|
|
|
|
2020-01-21 13:33:39 +11:00
|
|
|
|
content.SetValue(propertyAlias, value, culture, segment);
|
|
|
|
|
|
Services.ContentService.Save(content);
|
|
|
|
|
|
|
|
|
|
|
|
return Content($"Segment value has been set on content {contentId} for property {propertyAlias}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|