Sorting a set of custom items by a field

Topics: Customizing Orchard, Troubleshooting, Writing modules
Feb 26, 2014 at 5:49 PM
Hi,

I'm a .NET/MVC developer that's brand new to Orchard.

However, I have inherited some code from a previous developer and need to make what should be a relatively easy change. The code in question is an extension of the Orchard Search module that allows for some custom filtering of searches by tag/category. My basic goal is to change the code so that instead of filtering/sorting by the "PublishedUtc" that's on the "CommonPart" it instead sorts and filters by "ArticleDate", a field on the "BlogPost" part.

I've bumbled around and not managed to move forward, I'm wondering if someone out there can help me fill in the gaps in my knowledge. Code for the controller action below, thanks in advance for any help you can give.

Sam
  public ActionResult Full(PagerParameters pagerParameters, string q = "", string startDate = "", string endDate = "", string[] tags = null, string category = "")
        {
            var list = Shape.List();
            var listAsContentItems = new List<ContentItemViewModel>();
            var itemsToAdd = new List<dynamic>();
            var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
            // Had Image in here
            List<ContentItem> foundItems;
            if (string.IsNullOrEmpty(q))
                foundItems = _contentManager.Query().ForType(new string[] { "BlogPost" })
                    .List().OrderByDescending(c=> c.As<CommonPart>().PublishedUtc)
                    .ToList();
            else
               foundItems = _contentManager.Query().ForType(new string[] {"BlogPost", "MediaImage"}).List().ToList();

            List<int> matchingIds = null;
            if (!string.IsNullOrEmpty(q)) matchingIds = fetchMatchingIds(q, 0, 999); // We actually don't want any paging here


            foreach (var contentItem in foundItems)
            {   
                // Search Query Filter
                if (contentItem.TypeDefinition.DisplayName == "News Item" && matchingIds != null && !matchingIds.Contains(contentItem.Id))
                    continue;
                if (!string.IsNullOrEmpty(q)) 
                {
                    var asTitle = contentItem.As<TitlePart>();
                    var asBody = contentItem.As<BodyPart>();
                    var loweredQuery = q.ToLower();

                    var titleContains = (asTitle != null && asTitle.Title != null && asTitle.Title.ToLower().Contains(loweredQuery));
                    var bodyContains = (asBody != null && asBody.Text != null && asBody.Text.ToLower().Contains(loweredQuery));

                    if (!titleContains && !bodyContains) continue;
                }
                

                // Date filtering
                DateTime startDateAsDate;
                DateTime endDateAsDate;

                var cp = contentItem.As<CommonPart>();
                if (cp == null ||
                    cp.PublishedUtc.HasValue && !string.IsNullOrEmpty(startDate) &&
                    DateTime.TryParse(startDate, out startDateAsDate) && cp.PublishedUtc <= startDateAsDate)
                    continue;
                if (cp == null ||
                    cp.PublishedUtc.HasValue && !string.IsNullOrEmpty(endDate) &&
                    DateTime.TryParse(endDate, out endDateAsDate) && cp.PublishedUtc >= endDateAsDate) continue;
 
                // Tag Filtering
                if (tags != null)
                {
                    var tp = contentItem.As<TagsPart>();
                    if (tp == null || tp.CurrentTags == null) continue; // we don't even care, it cant match the passed tags now
                    var currentTags = tp.CurrentTags.ToList();
                    var tagFound = false;
                    foreach (var tag in currentTags)
                    {
                        var name = tag.TagName;
                        if (tags.Contains(name)) tagFound = true;
                    }
                    if (!tagFound) continue;
                }

                // Category Filtering
                if (!string.IsNullOrEmpty(category))
                {
                    var tp = contentItem.As<TermsPart>();
                    if (tp == null || !tp.TermParts.Any(x => x.TermPart.Name == category)) continue;
                }

                itemsToAdd.Add(_contentManager.BuildDisplay(contentItem, "Summary"));
            }

            foreach (var contentItem in itemsToAdd.Skip((pager.Page-1) * pager.PageSize).Take(pager.PageSize))
            {
                list.Add(contentItem);
                listAsContentItems.Add(ContentItemViewModel.FromContentItem(contentItem.ContentItem));
            }


            var pagerShape = Shape.Pager(pager).TotalItemCount(itemsToAdd.Count);

            var searchViewModel = new SearchViewModel
            {
                Query = q,
                TotalItemCount = list.Items.Count,
                StartPosition = (pager.Page - 1) * pager.PageSize + 1,
                EndPosition = pager.Page * pager.PageSize > itemsToAdd.Count ? itemsToAdd.Count : pager.Page * pager.PageSize,
                ContentItems = list,
                Pager = pagerShape,
                ContentItemsAsContentItems = listAsContentItems
            };

            return View(searchViewModel);
        }
Feb 27, 2014 at 8:27 PM
Anyone have thoughts?