예제 #1
0
        protected override void Render(HtmlTextWriter writer)
        {
            int maxResultsToShow = 5;


            StringBuilder html = new StringBuilder();

            CmsPage currentPage = CmsContext.currentPage;
            string  title       = currentPage.Title;
            string  bodyText    = getPageBodyText(currentPage, CmsContext.currentLanguage);

            string keywordIndexDir  = SearchResults.IndexStorageDirectory;
            string spellingIndexDir = SearchResults.SpellCheckIndexStorageDirectory;

            LuceneKeywordSearch search = new LuceneKeywordSearch(keywordIndexDir, spellingIndexDir);

            IndexableFileInfo[] related = search.getRelatedFiles(title, maxResultsToShow + 1); // always returns the current page

            if (related.Length > 1)
            {
                html.Append("<div class=\"SimilarPages\">");
                html.Append("Related Pages:");
                html.Append("<ul>");
                foreach (IndexableFileInfo f in related)
                {
                    if (String.Compare(currentPage.Path, f.Filename, true) == 0) // skip current page.
                    {
                        continue;
                    }
                    string url = CmsContext.getUrlByPagePath(f.Filename);
                    html.Append("<li>");
                    html.Append("<a href=\"" + url + "\">");
                    html.Append(f.Title);
                    html.Append("</a>");
                    html.Append("</li>");
                } // foreach
                html.Append("</ul>");
                html.Append("</div>");
            }


            writer.Write(html.ToString());
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            CmsPage currentPage = CmsContext.currentPage;

            /*
             * Item Output Template String Format:
             * {0} = Page's Title
             * {1} = Page's MenuTitle
             * {2} = Page's relative URL
             * {3} = the search string snippet to display for this page
             * {4} = Page's breadcrumb created using MenuTitle's, concatenated using ' > ' spacers
             * {5} = "odd" or "even"
             *
             */
            string defaultItemTemplate = "<div class=\"SearchResultItem {5}\"><a class=\"SearchResultItemLink\" href=\"{2}\">{0}</a><br/><blockquote class=\"SearchResultInfo\"><div class=\"Snippet\">{3}</div><div class=\"SearchResultItemBreadcrumb\">{4}</div></blockquote></div>";
            string itemOutputTemplate  = CmsControlUtils.getControlParameterKeyValue(CmsContext.currentPage, this, "ItemTemplate", defaultItemTemplate);

            string        action = PageUtils.getFromForm("action", "");
            StringBuilder html   = new StringBuilder();

            html.Append("<div class=\"SearchResults\">");

            if (action == "re-Index All Pages" || action.ToLower() == "reindexall")
            {
                ReIndexAllPages();
                html.Append("<p><strong>All Pages have been re-indexed</strong><p>");
            }

            string noQuery = Guid.NewGuid().ToString();
            string query   = PageUtils.getFromForm("q", noQuery);

            html.Append("<div class=\"SearchBox\">");
            // -- output search box
            string formId = "SearchResults";

            html.Append(currentPage.getFormStartHtml(formId));
            if (query == noQuery)
            {
                html.Append(PageUtils.getInputTextHtml("q", "queryBox", "", 20, 255));
            }
            else
            {
                html.Append(PageUtils.getInputTextHtml("q", "queryBox", query, 20, 255));
            }
            html.Append("<input type=\"submit\" value=\"search\">");

            // -- output doIndex button
            if (CmsContext.currentUserIsSuperAdmin)
            {
                html.Append(" Admin:<input type=\"submit\" name=\"action\" value=\"re-Index All Pages\">");
            }
            html.Append(currentPage.getFormCloseHtml(formId));
            html.Append("</div>");
            html.Append("<p>");

            LuceneKeywordSearch search = new LuceneKeywordSearch(IndexStorageDirectory, SpellCheckIndexStorageDirectory);

            if (query != noQuery)
            {
                // -- do the search with keyword highlighting
                IndexableFileInfo[] fileInfos = search.doSearch(query, query);

                fileInfos = getFileInfosForCurrentLanguage(fileInfos);

                // -- output the results
                if (fileInfos.Length < 1)
                {
                    // -- no results
                    html.Append("<strong>Your search for \"" + query + "\" returned no results</strong>");
                    // -- get spelling suggestion
                    string spellingSuggestion = search.getSpellingSuggestion(query);
                    if (spellingSuggestion.Trim() != query.Trim())
                    {
                        NameValueCollection p = new NameValueCollection();
                        p.Add("q", spellingSuggestion);
                        string newSearchUrl = CmsContext.getUrlByPagePath(CmsContext.currentPage.Path, p);
                        html.Append("<p><font color=\"#cc0000\">Did you mean:</font> \"<a href=\"" + newSearchUrl + "\">" + spellingSuggestion + "</a>\" <font color=\"#cc0000\">?</font></p>");
                    }
                }
                else
                {
                    html.Append("<strong>Your search for \"" + query + "\" returned " + fileInfos.Length + " results</strong><p>");
                    html.Append("<p>" + getPagerOutput(fileInfos) + "</p>");

                    int startAtItemNumber = PageUtils.getFromForm("num", 0);
                    if (startAtItemNumber >= fileInfos.Length)
                    {
                        startAtItemNumber = fileInfos.Length - 1;
                    }
                    int endAt = Math.Min(startAtItemNumber + numItemsPerPage - 1, fileInfos.Length - 1);

                    if (startAtItemNumber == 0 && endAt == 0 && fileInfos.Length == 1)
                    {
                        html.Append(getItemDisplay(fileInfos[0], itemOutputTemplate));
                    }
                    else
                    {
                        for (int i = startAtItemNumber; i <= endAt; i++)
                        {
                            if (endAt <= 0)
                            {
                                break;
                            }
                            IndexableFileInfo fileInfo = fileInfos[i];
                            html.Append(getItemDisplay(fileInfo, itemOutputTemplate));
                        } // for

                        html.Append("<p>" + getPagerOutput(fileInfos) + "</p>");
                    } // else
                }
            }             // if query != noQuery

            html.Append("</div>");

            writer.WriteLine(html.ToString());
        }         // Render