using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Hosting;
using System.IO;
using EPiServer;
using EPiServer.Web;
using EPiServer.Core;
using EPiServer.Security;
using EPiServer.SpecializedProperties;
namespace Extensions
{
public static class LinkItemCollectionExtension
{
///
/// Prepares all links in a LinkItemCollection for output
/// by filtering out inaccessible links and ensures all links are correct.
///
/// The collection of links to prepare.
/// True will set target to _blank if target is not specified for the LinkItem.
/// A prepared and filtered list of LinkItems
public static IEnumerable ToPreparedLinkItems(this LinkItemCollection linkItemCollection, bool targetExternalLinksToNewWindow)
{
if (linkItemCollection != null)
{
foreach (var linkItem in linkItemCollection)
{
var url = new UrlBuilder(linkItem.Href);
if (PermanentLinkMapStore.ToMapped(url))
{
var pr = PermanentLinkUtility.GetPageReference(url);
if (!PageReference.IsNullOrEmpty(pr))
{
// page
var page = DataFactory.Instance.GetPage(pr);
if (IsPageAccessible(page))
{
linkItem.Href = page.LinkURL;
yield return LinkItem;
}
}
else
{
// document
if (IsFileAccessible(linkItem.Href))
{
Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);
linkItem.Href = url.Path;
yield return linkItem;
}
}
}
else if (!linkItem.Href.StartsWith("~"))
{
// external
if (targetExternalLinksToNewWindow && string.IsNullOrEmpty(linkItem.Target))
{
linkItem.Target = "_blank";
}
if (linkItem.Href.StartsWith("mailto:") || linkItem.Target == "null")
{
linkItem.Target = string.Empty;
}
yield return linkItem;
}
}
}
}
///
/// Prepares all links in a LinkItemCollection for output
/// by filtering out inaccessible links and ensures all links are correct.
///
/// The collection of links to prepare.
/// A prepared and filtered list of LinkItems
public static IEnumerable ToPreparedLinkItems(this LinkItemCollection linkItemCollection)
{
return linkItemCollection.ToPreparedLinkItems(false);
}
///
/// Converts a LinkItemCollection to typed pages. Any non-pages will be filtered out. (Not compatible with PageList - Use ToPageDataList)
///
/// PageType
/// The collection of links to convert
/// An enumerable of typed PageData
public static IEnumerable ToPages(this LinkItemCollection linkItemCollection) where T : PageData
{
if (linkItemCollection != null)
{
foreach (var linkItem in linkItemCollection)
{
var url = new UrlBuilder(linkItem.Href);
if (PermanentLinkMapStore.ToMapped(url))
{
var pr = PermanentLinkUtility.GetPageReference(url);
if (!PageReference.IsNullOrEmpty(pr))
{
var page = DataFactory.Instance.GetPage(pr);
if (page is T && IsPageAccessible(page))
{
yield return (T)page;
}
}
}
}
}
}
///
/// Converts a LinkItemCollection to typed pages. Any non-pages will be filtered out. (PageList compatible)
///
/// PageType
///
/// A list of typed PageData
public static List ToPageDataList(this LinkItemCollection linkItemCollection) where T : PageData
{
return linkItemCollection.ToPages().ToList();
}
///
/// Converts a LinkItemCollection to a list of pages. Any non-pages will be filtered out. (PageList compatible)
///
///
/// A list of typed PageData
public static List ToPageDataList(this LinkItemCollection linkItemCollection)
{
return linkItemCollection.ToPages().ToList();
}
private static bool IsPageAccessible(PageData page)
{
return (page != null &&
!page.IsDeleted &&
page.Status == VersionStatus.Published &&
page.PendingPublish == false &&
page.StartPublish < DateTime.Now &&
page.StopPublish > DateTime.Now &&
page.ACL.QueryDistinctAccess(AccessLevel.Read));
}
// TODO: Possibly a better solution w/o try catch?
private static bool IsFileAccessible(string filePath)
{
try
{
HostingEnvironment.VirtualPathProvider.GetFile(filePath);
return true;
}
catch { }
return false;
}
}
}