Exemple #1
0
        public ActionResult RenderLatestMessages()
        {
            //Instantiate variables
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            List <latestUpdates> lstLatestUpdates = new List <latestUpdates>();


            try
            {
                //Get all prayers
                BaseSearchProvider mySearcher = ExamineManager.Instance.SearchProviderCollection[Common.searchProviders.MessagesSearcher];
                ISearchCriteria    criteria   = mySearcher.CreateSearchCriteria(IndexTypes.Content);
                IBooleanOperation  query      = criteria.Field(Common.NodeProperties.indexType, Common.NodeProperties.content); //gets all items
                query.And().OrderByDescending(Common.NodeProperties.publishDate);
                query.And().OrderBy(Common.NodeProperties.nodeName);
                query.And().OrderBy(Common.miscellaneous.Path);
                ISearchResults isResults = mySearcher.Search(query.Compile());


                if (isResults.Any())
                {
                    //Instantiate variables
                    DateTime          msgDate      = new DateTime(1900, 1, 1);
                    DateTime          prevDate     = new DateTime(1900, 1, 1);
                    latestUpdates     latestUpdate = new latestUpdates();
                    visionary         visionary    = new visionary();
                    message           message;
                    IPublishedContent ipMsg;
                    IPublishedContent ipVisionary;


                    //Get top 'n' results and determine link structure
                    foreach (SearchResult srResult in isResults.Take(20))
                    {
                        //Obtain message's node
                        ipMsg = Umbraco.TypedContent(Convert.ToInt32(srResult.Id));
                        if (ipMsg != null)
                        {
                            //Obtain date of message
                            msgDate = ipMsg.GetPropertyValue <DateTime>(Common.NodeProperties.publishDate);

                            //Create a new date for messages
                            if (msgDate != prevDate)
                            {
                                //Update current date
                                prevDate = msgDate;

                                //Create new instances for updates and add to list of all updates.
                                latestUpdate = new latestUpdates();
                                latestUpdate.datePublished = msgDate;
                                lstLatestUpdates.Add(latestUpdate);

                                //Reset the visionary class on every new date change.
                                visionary = new visionary();
                            }

                            //Obtain current visionary or webmaster
                            if (ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.Visionary) != null)
                            {
                                if (visionary.id != ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.Visionary).Id)
                                {
                                    //Obtain visionary node
                                    ipVisionary = ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.Visionary);

                                    //Create new visionary class and add to latest update class
                                    visionary      = new visionary();
                                    visionary.id   = ipVisionary.Id;
                                    visionary.name = ipVisionary.Name;
                                    visionary.url  = ipVisionary.Url;
                                    latestUpdate.lstVisionaries.Add(visionary);
                                }
                            }
                            else if (ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.WebmasterMessageList) != null)
                            {
                                if (visionary.id != ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.WebmasterMessageList).Id)
                                {
                                    //Obtain visionary node
                                    ipVisionary = ipMsg.AncestorsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == Common.docType.WebmasterMessageList);

                                    //Create new visionary class and add to latest update class
                                    visionary      = new visionary();
                                    visionary.id   = ipVisionary.Id;
                                    visionary.name = ipVisionary.Name;
                                    visionary.url  = ipVisionary.Url;
                                    latestUpdate.lstVisionaries.Add(visionary);
                                }
                            }

                            //Create new message and add to existing visionary class.
                            message       = new message();
                            message.id    = ipMsg.Id;
                            message.title = ipMsg.Name;
                            message.url   = ipMsg.Url;
                            visionary.lstMessages.Add(message);
                        }
                    }
                }


                //Return data to partialview
                return(PartialView("~/Views/Partials/Common/LatestUpdates.cshtml", lstLatestUpdates));
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(@"MessageController.cs : RenderLatestMessages()");
                sb.AppendLine("model:" + Newtonsoft.Json.JsonConvert.SerializeObject(lstLatestUpdates));
                Common.SaveErrorMessage(ex, sb, typeof(MessageController));


                ModelState.AddModelError("", "*An error occured while creating the latest message list.");
                return(CurrentUmbracoPage());
            }
        }