protected
override
void
InitializeControls(GenericContainer container, IContentViewDefinition definition)
{
var masterDefinition = definition
as
IContentViewMasterDefinition;
if
(masterDefinition !=
null
)
{
var query =
this
.Manager.GetProducts();
if
(masterDefinition.AllowUrlQueries.HasValue && masterDefinition.AllowUrlQueries.Value)
{
query =
this
.EvaluateUrl(query,
"Date"
,
"PublicationDate"
,
this
.Host.UrlEvaluationMode,
this
.Host.UrlKeyPrefix);
query =
this
.EvaluateUrl(query,
"Author"
,
"Owner"
,
this
.Host.UrlEvaluationMode,
this
.Host.UrlKeyPrefix);
query =
this
.EvaluateUrl(query,
"Taxonomy"
,
""
,
typeof
(ProductItem),
this
.Host.UrlEvaluationMode,
this
.Host.UrlKeyPrefix);
}
int
? totalCount = 0;
int
? itemsToSkip = 0;
if
(masterDefinition.AllowPaging.HasValue && masterDefinition.AllowPaging.Value)
{
itemsToSkip =
this
.GetItemsToSkipCount(masterDefinition.ItemsPerPage,
this
.Host.UrlEvaluationMode,
this
.Host.UrlKeyPrefix);
}
CultureInfo uiCulture =
null
;
if
(AppSettings.CurrentSettings.Multilingual)
{
uiCulture = System.Globalization.CultureInfo.CurrentUICulture;
}
//the filter is adapted to the implementation of ILifecycleDataItemGeneric, so the culture is taken in advance when filtering published items.
this
.FilterExpression = ContentHelper.AdaptMultilingualFilterExpression(
this
.FilterExpression);
var filterExpression = DefinitionsHelper.GetFilterExpression(
this
.FilterExpression,
this
.AdditionalFilter);
query = Telerik.Sitefinity.Data.DataProviderBase.SetExpressions(
query,
filterExpression,
masterDefinition.SortExpression,
uiCulture,
itemsToSkip,
masterDefinition.ItemsPerPage,
ref
totalCount);
this
.IsEmptyView = (totalCount == 0);
if
(totalCount == 0)
{
this
.NewsList.Visible =
false
;
}
else
{
this
.ConfigurePager(totalCount.Value, masterDefinition);
this
.NewsList.DataSource = query.ToList();
this
.NewsList.PreRender +=
new
EventHandler(NewsList_PreRender);
}
}
}
private
void
NewsList_PreRender(
object
sender, System.EventArgs e)
{
string
commentsControlName =
"itemCommentsLink"
;
//// In ItemDataBound NavigateUrl property of the link is still not set. That is the reason why this logic is implemented in PreRender.
foreach
(var item
in
this
.NewsList.Items)
{
if
(item.ItemType == RadListViewItemType.DataItem || item.ItemType == RadListViewItemType.AlternatingItem)
{
var itemCommentsLink = item.FindControl(
"itemCommentsLink"
)
as
CommentsBox;
if
(itemCommentsLink !=
null
)
{
var dataItem = item.DataItem
as
Telerik.Sitefinity.GenericContent.Model.Content;
if
(dataItem !=
null
)
{
var query =
this
.GetCommentsQuery(dataItem);
var commentsCount = query.Count();
itemCommentsLink.CommentsCount = commentsCount;
}
var commentsControl = item.FindControl(commentsControlName)
as
CommentsBox;
if
(commentsControl !=
null
)
{
var allowComments = dataItem.AllowComments ??
false
;
if
(!allowComments)
{
commentsControl.Visible =
false
;
}
}
}
}
}
}
#region Helper methods
private
IQueryable<Comment> GetCommentsQuery(Telerik.Sitefinity.GenericContent.Model.Content dataItem)
{
var id = dataItem.Id;
IQueryable<Comment> query =
null
;
var commentsSettings =
new
CommentsSettingsWrapper(dataItem,
this
.MasterViewDefinition.CommentsSettingsDefinition);
if
((
bool
)commentsSettings.HideCommentsAfterNumberOfDays)
{
var numberOfDaysToHideComments = (
int
)commentsSettings.NumberOfDaysToHideComments;
var duration =
new
TimeSpan(numberOfDaysToHideComments, 0, 0, 0);
query =
this
.Manager.GetComments().Where<Comment>(c => c.CommentedItemID == id &&
c.CommentStatus == CommentStatus.Published &&
c.DateCreated > DateTime.UtcNow.Subtract(duration));
}
else
{
query =
this
.Manager.GetComments().Where<Comment>(c => c.CommentedItemID == id && c.CommentStatus == CommentStatus.Published);
}
return
query;
}
#endregion