示例#1
0
        public ActionResult Index(SearchPage currentPage, string keyword, int?page)
        {
            keyword = keyword == null ? string.Empty : keyword.Trim();

            // Paging
            var pageNumber = 1;

            if (page.HasValue && page > 1)
            {
                pageNumber = page.Value;
            }

            var pageSize = currentPage.PageSize;

            if (pageSize <= 0)
            {
                pageSize = 20;
            }

            var sortFields = new Collection <SortField>();

            sortFields.Add(SortFieldFactory.CreateSortField(Field.Title, true));

            int totalItems;
            var pages = new List <SitePageData>();

            pages = CmsSearchHandler.Search <SitePageData>(keyword, (ContentReference)null, pageNumber, pageSize,
                                                           sortFields, out totalItems).ToList();
            var searchItems = new List <SearchItemModel>();

            foreach (var pageData in pages)
            {
                if (pageData != null)
                {
                    searchItems.Add(ConvertPageToSearchItem(pageData));
                }
                else
                {
                    totalItems--;
                }
            }
            var searchPageLink = SiteSettingsHandler.Instance.SiteSettings.SearchPageLink;
            var model          = new SearchContentModel(currentPage)
            {
                Items         = new PagedList <SearchItemModel>(searchItems, pageNumber - 1, pageSize, totalItems),
                TotalItems    = totalItems,
                Keyword       = keyword,
                SearchPageUrl = !ContentReference.IsNullOrEmpty(searchPageLink) ? urlResolver.GetUrl(searchPageLink) : string.Empty,
                Pagination    = new SimplePagination()
                {
                    TotalRecord = totalItems,
                    PageSession = pageSize
                }
            };

            return(View(model));
        }
示例#2
0
        private List <BlogDetailPage> GetBlogSearch(BlogListingPage currentPage, string querySearch)
        {
            querySearch = querySearch == null ? string.Empty : querySearch.Trim().ToLower();

            // Paging
            var pageNumber = 1;
            var pageSize   = 100;

            var sortFields = new Collection <SortField>();

            sortFields.Add(SortFieldFactory.CreateSortField(Field.Title, true));

            int totalItems;
            var pages = new List <BlogDetailPage>();

//            pages = CmsSearchHandler.Search<BlogDetailPage>(querySearch, (ContentReference)null, pageNumber, pageSize,
//                     sortFields, out totalItems).ToList();
//            if (pages.Count != 0) return pages;
            var listBlogDetail = _contentLoader.GetChildren <BlogDetailPage>(currentPage.ContentLink).Where(x => DateTime.Compare(x.StartPublish, DateTime.Now) < 0 &&
                                                                                                            DateTime.Compare(x.StopPublish, DateTime.Now) > 0).ToList();

            pages = listBlogDetail.Where(x => (x.Title + "").ToLower().Contains(querySearch) || (x.SummaryDes + "").ToLower().Contains(querySearch) || (x.Content.ToBlocksIncludedString() + "").ToLower().Contains(querySearch)).ToList();
            return(pages);
        }
示例#3
0
        public JsonResult GetSearchResult(string q, int?page)
        {
            q = q == null ? string.Empty : q.Trim();

            // Paging
            var pageNumber = 1;

            if (page.HasValue && page > 1)
            {
                pageNumber = page.Value;
            }

            var pageSize = 10;
            //if (pageSize <= 0)
            //{
            //    pageSize = 20;
            //}

            var sortFields = new Collection <SortField>();

//            sortFields.Add(SortFieldFactory.CreateSortField(Field.Default, true));
            sortFields.Add(SortFieldFactory.CreateSortField(Field.Id, false));
//            sortFields.Add(SortFieldFactory.CreateSortField(Field.Title, false));
            int totalItems;
            var pages = new List <SitePageData>();

            pages = CmsSearchHandler.Search <SitePageData>(q, (ContentReference)null, pageNumber, pageSize,
                                                           sortFields, out totalItems).ToList();
            pages = pages.OrderByDescending(x => (x.Heading + "").ToLower().Contains(q)).ToList();
            var listBlogDetail = CmsSearchHandler.Search <BlogDetailPage>(string.Empty, (ContentReference)null, 1, 100,
                                                                          sortFields, out totalItems).ToList();

            q = q.ToLower();
            var blogPages = listBlogDetail.Where(x => (x.Title + "").ToLower().Contains(q) || (x.SummaryDes + "").ToLower().Contains(q) || (x.Content.ToBlocksIncludedString() + "").ToLower().Contains(q)).ToList();

            pages.AddRange(blogPages);
            pages = pages.Distinct().ToList();
            try
            {
                var resultsObj = pages.Select(x => new
                {
                    title       = x.DisplayName,
                    description = x.ShortDescription != null ? x.ShortDescription.ToString().GetRawContentFromHtmlSource().GetShortContent(59) : string.Empty,
                    url         = GetUrl(x)
                });
                var md = new
                {
                    data        = resultsObj,
                    success     = 1,
                    currentPage = pageNumber,
                    hasShowMore = totalItems > pageSize * pageNumber,
                    message     = resultsObj.Count() == 0 ? "The search returns no results" : ""
                };
                return(Json(md, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                var md = new
                {
                    data      = "error",
                    success   = 1,
                    message   = "Something went wrong!",
                    exception = ex.Message
                };
                return(Json(md, JsonRequestBehavior.AllowGet));
            }
        }