|
using System; |
|
using System.Collections.Generic; |
|
using System.ComponentModel; |
|
using System.Linq; |
|
using Telerik.Sitefinity.Data; |
|
using Telerik.Sitefinity.DynamicModules.Model; |
|
using Telerik.Sitefinity.Modules; |
|
using Telerik.Sitefinity.Modules.GenericContent.Archive; |
|
using Telerik.Sitefinity.Web.UI.PublicControls; |
|
using Telerik.Sitefinity.Web.UrlEvaluation; |
|
|
|
namespace SitefinityWebApp.Controls |
|
{ |
|
public class CustomArchiveWidget : ArchiveControl |
|
{ |
|
public override void ConstructArchiveItems() |
|
{ |
|
if (this.ContentType != null) |
|
{ |
|
var contentType = this.ResolveContentType(); |
|
|
|
var items = this.GetArchieveItems(contentType, this.Provider, this.DateBuildOptions, this.DateEvaluatorPropertyName); |
|
DataBindArchiveRepeater(items); |
|
} |
|
} |
|
|
|
public virtual List<ArchiveItem> GetArchieveItems(Type contentType, string providerName, DateBuildOptions dateBuildOptions, string propertyName) |
|
{ |
|
if (propertyName.IsNullOrEmpty()) |
|
throw new ArgumentException("propertName cannot be empty."); |
|
|
|
var manager = ManagerBase.GetMappedManager(contentType, providerName); |
|
|
|
var queryMinOrderExpr = string.Concat(propertyName, " ASC"); |
|
var queryMaxOrderExpr = string.Concat(propertyName, " DESC"); |
|
var queryMin = manager.GetItems(contentType, DefinitionsHelper.PublishedFilterExpression, queryMinOrderExpr, 0, 1); |
|
var queryMax = manager.GetItems(contentType, DefinitionsHelper.PublishedFilterExpression, queryMaxOrderExpr, 0, 1); |
|
|
|
var archieves = new List<ArchiveItem>(); |
|
|
|
DynamicContent firstContent = queryMin.Cast<DynamicContent>().FirstOrDefault(); |
|
DynamicContent lastContent = queryMax.Cast<DynamicContent>().FirstOrDefault(); |
|
|
|
if (firstContent != null && lastContent != null) |
|
{ |
|
DateTime? startDate = null; |
|
DateTime? endDate = null; |
|
var prop = TypeDescriptor.GetProperties(contentType).Find(propertyName, true); |
|
if (prop != null) |
|
{ |
|
if (!(prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(DateTime?))) |
|
throw new ArgumentException(string.Format("{0} property is not of type DateTime.", propertyName)); |
|
|
|
startDate = (DateTime)prop.GetValue(firstContent); |
|
endDate = (DateTime)prop.GetValue(lastContent); |
|
} |
|
if (startDate.HasValue && endDate.HasValue) |
|
{ |
|
SplitterResolver splitter = new SplitterResolver(); |
|
var trackedIntervals = splitter.GetIntervals(startDate.Value, endDate.Value, dateBuildOptions); |
|
foreach (var interval in trackedIntervals) |
|
{ |
|
var dateExpression = this.GetArchiveFilterExpression(interval.StartDate, interval.EndDate, propertyName); |
|
int? itemsCount = 0; |
|
manager.GetItems(contentType, dateExpression, null, 0, 0, ref itemsCount); |
|
if (itemsCount > 0) |
|
{ |
|
archieves.Add(new ArchiveItem(interval.StartDate, (int)itemsCount)); |
|
} |
|
} |
|
archieves.Sort(DateDescendingComparison); |
|
} |
|
} |
|
return archieves; |
|
} |
|
|
|
public static Comparison<ArchiveItem> DateDescendingComparison = delegate(ArchiveItem x, ArchiveItem y) |
|
{ |
|
return y.Date.CompareTo(x.Date); |
|
}; |
|
|
|
private string GetArchiveFilterExpression(DateTime startDate, DateTime endDate, string propertyName) |
|
{ |
|
return string.Format("{0} >= ({1}) AND {2} < ({3}) AND {4}", |
|
propertyName, |
|
startDate.ToString("yyyy-MM-dd HH:mm:ss"), |
|
propertyName, |
|
endDate.ToString("yyyy-MM-dd HH:mm:ss"), |
|
DefinitionsHelper.PublishedFilterExpression); |
|
} |
|
} |
|
} |