Sunteți pe pagina 1din 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Forms.Data;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.web;
using Umbraco.Forms.Core;

namespace ContourExtensions {
public class NodePrevaluesNodeFactory : FieldPreValueSourceType {

public NodePrevaluesNodeFactory()
{
this.Id = new Guid("4F711F20-D5A6-4A2E-B678-4FBFFB3CDB2F");
this.Name = "Umbraco Documents (published)";
this.Description = "Uses published nodes from a specific source as
prevalues";
}

[Umbraco.Forms.Core.Attributes.Setting("Root node", description = "Source


to fetch nodes from", control =
"Umbraco.Forms.Core.FieldSetting.Pickers.ContentWithXpath")]
public string RootNode { get; set; }

[Umbraco.Forms.Core.Attributes.Setting("Use current page as root",


description = "Does not work in preview mode", control =
"Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string UseCurrentPage { get; set; }

[Umbraco.Forms.Core.Attributes.Setting("Document type", description = "The


type of nodes you would like to use", control =
"Umbraco.Forms.Core.FieldSetting.Pickers.DocumentType")]
public string DocType { get; set; }

public override List<PreValue> GetPreValues(Field field) {


List<PreValue> retval = new List<PreValue>();
Node root = null;
int _root = 0;

if (DocType != string.Empty)
{
int dtid = 0;

if (int.TryParse(DocType, out dtid) && dtid > 0)


{
DocumentType dt = new DocumentType(dtid);
DocType = dt.Alias;
}
}

if (UseCurrentPage == true.ToString() &&


umbraco.presentation.UmbracoContext.Current != null &&
umbraco.presentation.UmbracoContext.Current.PageId.HasValue &&
umbraco.presentation.UmbracoContext.Current.PageId.Value > 0)
root = new
Node(umbraco.presentation.UmbracoContext.Current.PageId.Value);
else
{
if (int.TryParse(RootNode, out _root) && _root > 0)
root = new Node(_root);
else
{
if (umbraco.presentation.UmbracoContext.Current != null &&
umbraco.presentation.UmbracoContext.Current.PageId.HasValue &&
umbraco.presentation.UmbracoContext.Current.PageId.Value > 0)
{
string transval = XpathExecutionHelper.GetResult(RootNode);

if (int.TryParse(transval, out _root) && _root > 0)


root = new Node(_root);
}
}

if (root != null)
{
foreach (Node n in root.Children)
{
if (n.NodeTypeAlias == DocType || DocType == string.Empty)
{
PreValue pv = new PreValue();
pv.Id = n.Id;
pv.Value = n.Name;
retval.Add(pv);
}
}
}
return retval;
}

public override List<Exception> ValidateSettings() {


return new List<Exception>();
}
}
}

S-ar putea să vă placă și