Пример #1
0
        public override void RenderInViewMode(HtmlTextWriter writer, CmsPage page, int identifier, CmsLanguage langToRenderFor, string[] paramList)
        {
            GlossaryDb db = new GlossaryDb();
            GlossaryPlaceholderData placeholderData = db.getGlossary(page, identifier, langToRenderFor, true);

            string letterToDisplay = getLetterToDisplay(placeholderData);

            GlossaryData[] items;
            if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.RssFeed)
            {
                items = db.FetchRssFeedGlossaryDataFromDatabase();
            }
            else
            {
                items = db.getGlossaryData(placeholderData, letterToDisplay);
            }

            string[] charactersWithData = db.getAllCharactersWithData(items);

            StringBuilder html = new StringBuilder();

            if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.RssFeed)
            {
                html.Append("<!-- Glossary data pulled from RSS: " + GlossaryPlaceholderData.getRssDataSourceUrl() + " -->");
            }
            else
            {
                html.Append("<!-- Glossary data pulled from HatCMS database -->");
            }

            html.Append(GetHtmlDisplay(page, items, placeholderData, charactersWithData, letterToDisplay));

            writer.Write(html.ToString());
        } // RenderView
        public override string Render()
        {
            StringBuilder html = new StringBuilder();

            if (GlossaryPlaceholderData.DataSource != GlossaryPlaceholderData.GlossaryDataSource.RssFeed)
            {
                html.Append(base.formatErrorMsg("The Glossary data is currently hosted from the Local Database. To update the glossary, edit the page with the glossary placeholder in it"));
            }
            else
            {
                html.Append("<p>");
                string url = GlossaryPlaceholderData.getRssDataSourceUrl();
                html.Append(base.formatNormalMsg("Fetching updated glossary from <a href=\"" + url + "\" target=\"_blank\">" + url + "</a>"));

                try
                {
                    // -- run the background task in the foreground thread to update the database.
                    HatCMS.Modules.Glossary.BackgroundTasks.FetchUpdatedRSSGlossaryItems backgroundFetcher = new HatCMS.Modules.Glossary.BackgroundTasks.FetchUpdatedRSSGlossaryItems();
                    if (backgroundFetcher.FetchAndSaveRemoteRSSGlossaryData())
                    {
                        // -- fetch the data from the database.
                        GlossaryDb     db    = new GlossaryDb();
                        GlossaryData[] items = db.FetchRssFeedGlossaryDataFromDatabase();
                        html.Append(base.formatNormalMsg(items.Length + " glossary entries are now available."));
                    }
                    else
                    {
                        html.Append(base.formatErrorMsg("Exception: could not update the glossary from the external URL."));
                    }
                }
                catch (Exception ex)
                {
                    html.Append(base.formatErrorMsg("Exception: could not update the glossary from the external URL: " + ex.Message));
                }

                html.Append("</p>");
            }

            return(html.ToString());
        }
Пример #3
0
        } // RenderView

        public override Rss.RssItem[] GetRssFeedItems(CmsPage page, CmsPlaceholderDefinition placeholderDefinition, CmsLanguage langToRenderFor)
        {
            GlossaryDb db = new GlossaryDb();
            GlossaryPlaceholderData placeholderData = db.getGlossary(page, placeholderDefinition.Identifier, langToRenderFor, true);

            GlossaryPlaceholderData.GlossaryViewMode origViewMode = placeholderData.ViewMode;

            // -- gets all glossary items (regardless of the ViewMode)
            GlossaryData[] items = db.getGlossaryData(placeholderData.GlossaryId);

            // -- each glossary item gets its own rssItem
            List <Rss.RssItem> ret = new List <Rss.RssItem>();

            foreach (GlossaryData glData in items)
            {
                Rss.RssItem rssItem = new Rss.RssItem();
                rssItem = base.InitRssItem(rssItem, page, langToRenderFor);

                rssItem.Description = glData.description;
                // -- setup the proper link
                switch (placeholderData.ViewMode)
                {
                case GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter:

                    Dictionary <string, string> urlParams = new Dictionary <string, string>();
                    urlParams.Add("l", glData.word.ToUpper()[0].ToString());
                    rssItem.Link = new Uri(page.getUrl(urlParams, langToRenderFor));
                    break;

                case GlossaryPlaceholderData.GlossaryViewMode.SinglePageWithJumpList:
                    // nothing to do
                    break;

                default:
                    throw new Exception("Error: invalid GlossaryViewMode");
                } // switch
            }
            return(ret.ToArray());
        }
Пример #4
0
        public string RunInlineGlossaryFilter(CmsPage pageBeingFiltered, string placeholderHtml)
        {
            try
            {
                bool enabled = CmsConfig.getConfigValue("GlossaryHighlightFilter:Enable", false); // disabled by default

                if (!enabled || CmsContext.currentEditMode == CmsEditMode.Edit)
                {
                    return(placeholderHtml);
                }

#if DEBUG
                CmsContext.currentPage.HeadSection.AddCSSStyleStatements("span.InlineGlossaryTerm { border-bottom: 1px dotted red; }");
#endif

                // -- get the glossaryID to get data for (language specific)
                int    glossaryId  = 1;
                string glossaryIds = CmsConfig.getConfigValue("GlossaryHighlightFilter:GlossaryId", "");
                try
                {
                    string[] glossaryIdsParts = glossaryIds.Split(new char[] { CmsConfig.PerLanguageConfigSplitter }, StringSplitOptions.RemoveEmptyEntries);
                    if (glossaryIdsParts.Length >= CmsConfig.Languages.Length)
                    {
                        int index = CmsLanguage.IndexOf(CmsContext.currentLanguage.shortCode, CmsConfig.Languages);
                        if (index >= 0)
                        {
                            glossaryId = Convert.ToInt32(glossaryIdsParts[index]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error: GlossaryHighlightFilter is incorrectly configured!");
                }

                // -- get the glossary data from the database. The data is cached so that we don't hit the database for this info every time.
                GlossaryData[] gData;
                string         cacheKey = "GlossaryHighlightFilter_Data_" + glossaryId;
                if (!CmsContext.currentUserIsLoggedIn && System.Web.Hosting.HostingEnvironment.Cache[cacheKey] != null)
                {
                    gData = (GlossaryData[])System.Web.Hosting.HostingEnvironment.Cache[cacheKey];
                }
                else
                {
                    GlossaryDb db = new GlossaryDb();

                    if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.RssFeed)
                    {
                        gData = db.FetchRssFeedGlossaryDataFromDatabase();
                    }
                    else
                    {
                        gData = db.getGlossaryData(glossaryId);
                    }

                    if (!CmsContext.currentUserIsLoggedIn)
                    {
                        System.Web.Hosting.HostingEnvironment.Cache.Insert(cacheKey, gData, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
                    }

                    // go through longer words first (longer words/phrases are usually more specific than shorter ones)
                    gData = GlossaryData.SortByWordLength(gData, SortDirection.Descending);
                }

                // -- short-circuit processing if there aren't any glossary terms in the system.
                if (gData.Length == 0)
                {
                    return(placeholderHtml);
                }

                // -- process the placeholderHTML
                string html = placeholderHtml;

                List <string> toSurround = new List <string>();
                List <string> prefixs    = new List <string>();
                List <string> suffixs    = new List <string>();

                foreach (GlossaryData d in gData)
                {
                    int index = html.IndexOf(d.word.Trim(), StringComparison.CurrentCultureIgnoreCase);
                    if (index >= 0 && d.word.Trim().Length > 0)
                    {
                        // string safeDesc = StringUtils.AddSlashes(d.description);
                        string safeDesc = HttpUtility.HtmlEncode(d.word + ": " + d.description);
                        safeDesc = safeDesc.Replace("\n", " ");
                        safeDesc = safeDesc.Replace("\r", " ");
                        safeDesc = safeDesc.Replace("\t", " ");
                        safeDesc = safeDesc.Replace("  ", " ");

                        string prefix = "<span title=\"" + safeDesc + "\" class=\"InlineGlossaryTerm\">";
                        string suffix = "</span>";

                        toSurround.Add(d.word.Trim());
                        prefixs.Add(prefix);
                        suffixs.Add(suffix);
                    }
                } // foreach word

                html = SurroundInHtml(toSurround.ToArray(), prefixs.ToArray(), suffixs.ToArray(), html);

                return(html.ToString());
            }
            catch (Exception ex)
            {
                placeholderHtml += ("<!-- GlossaryHighlightingFilter Error: " + ex.Message + " -->");
            }

            return(placeholderHtml);
        }
Пример #5
0
        public override void RenderInEditMode(HtmlTextWriter writer, CmsPage page, int identifier, CmsLanguage langToRenderFor, string[] paramList)
        {
            string ControlId           = "Glossary_" + page.ID.ToString() + "_" + identifier.ToString() + "_" + langToRenderFor.shortCode;
            string renderEditorToDivId = ControlId + "RenderArea";
            string renderJsonToFormId  = ControlId + "JsonFormData";
            string jsonDataVarName     = ControlId + "_GlossaryJsonData";


            GlossaryDb db = new GlossaryDb();
            GlossaryPlaceholderData placeholderData = db.getGlossary(page, identifier, langToRenderFor, true);

            string action = PageUtils.getFromForm(ControlId + "_action", "");

            if (String.Compare(action, "updateGlossary", true) == 0)
            {
                string receivedJSONData = PageUtils.getFromForm(renderJsonToFormId, "");


                GlossaryData[] newData = fromJSON(receivedJSONData);

                placeholderData.SortOrder = (GlossaryPlaceholderData.GlossarySortOrder)PageUtils.getFromForm(ControlId + "SortOrder", typeof(GlossaryPlaceholderData.GlossarySortOrder), placeholderData.SortOrder);
                placeholderData.ViewMode  = (GlossaryPlaceholderData.GlossaryViewMode)PageUtils.getFromForm(ControlId + "ViewMode", typeof(GlossaryPlaceholderData.GlossaryViewMode), placeholderData.ViewMode);
                bool b = db.saveUpdatedGlossary(page, identifier, langToRenderFor, placeholderData, newData);
                if (b)
                {
                    // -- get the glossary data again so that all IDs are updated.
                    GlossaryData[] dataToSave = db.getGlossaryData(placeholderData, "");
                    string         jsonToSave = getJSVariableStatement(jsonDataVarName, dataToSave);
                    try
                    {
                        string jsonFilename = System.Web.Hosting.HostingEnvironment.MapPath(CmsContext.ApplicationPath + "_system/writable/Glossary/" + jsonDataVarName + ".js");
                        System.IO.File.Delete(jsonFilename);
                        System.IO.File.WriteAllText(jsonFilename, jsonToSave);
                    }
                    catch
                    { }
                }
            } // if updateGlossary

            StringBuilder html = new StringBuilder();

            html.Append(PageUtils.getHiddenInputHtml(ControlId + "_action", "updateGlossary"));
            string EOL = Environment.NewLine;

            html.Append("<p><strong>Glossary Display Configuration:</strong></p>");

            html.Append("<table>");
            if (CmsConfig.Languages.Length > 1)
            {
                html.Append("<tr>");
                html.Append("<td>Language:</td><td>" + langToRenderFor.shortCode + "</td>");
                html.Append("</tr>");
            }
            html.Append("<tr>");
            html.Append("<td>Display Mode: </td>");
            html.Append("<td>");
            html.Append(PageUtils.getDropDownHtml(ControlId + "ViewMode", ControlId + "ViewMode", Enum.GetNames(typeof(GlossaryPlaceholderData.GlossaryViewMode)), Enum.GetName(typeof(GlossaryPlaceholderData.GlossaryViewMode), placeholderData.ViewMode)));
            html.Append("</td>");
            html.Append("</tr>");
            html.Append("<tr>");
            html.Append("<td>Output Sorting: </td>");
            html.Append("<td>");
            html.Append(PageUtils.getDropDownHtml(ControlId + "SortOrder", ControlId + "SortOrder", Enum.GetNames(typeof(GlossaryPlaceholderData.GlossarySortOrder)), Enum.GetName(typeof(GlossaryPlaceholderData.GlossarySortOrder), placeholderData.SortOrder)));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("</table>" + EOL);

            // -- glossary data
            // note: the form is linked to GlossaryEditor.js
            GlossaryData[] items = db.getGlossaryData(placeholderData, "");
            html.Append("<p><strong>Glossary Entries:</strong></p>");

            if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.LocalDatabase)
            {
                html.Append("<div id=\"" + renderEditorToDivId + "\"></div>");

                html.Append("<input type=\"button\" onclick=\"AddGlossaryElement('" + renderEditorToDivId + "');\" value=\"add new glossary entry\">" + Environment.NewLine);


                // -- the JSON data is passed to the server through this hidden form element
                html.Append(PageUtils.getHiddenInputHtml(renderJsonToFormId, renderJsonToFormId, ""));


                page.HeadSection.AddEmbeddedJavascriptFile(JavascriptGroup.ControlOrPlaceholder, typeof(Glossary).Assembly, "json2.js");
                page.HeadSection.AddEmbeddedJavascriptFile(JavascriptGroup.ControlOrPlaceholder, typeof(Glossary).Assembly, "GlossaryEditor.js");

                page.HeadSection.AddJSStatements(getJSVariableStatement(jsonDataVarName, items));


                page.HeadSection.AddJSOnReady("var " + ControlId + "Instance = new GlossaryEditor('" + renderEditorToDivId + "', '" + renderJsonToFormId + "', JSON.parse(" + jsonDataVarName + ")); ");
                page.HeadSection.AddJSOnReady("GlossaryEditorInstances[GlossaryEditorInstances.length] = " + ControlId + "Instance; ");
                page.HeadSection.AddJSOnReady("" + ControlId + "Instance.updateDisplay();");
            }
            else if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.RssFeed)
            {
                html.Append("<div class=\"Glossary RSS Edit\">");
                html.Append("The Glossary is being driven from the following RSS feed: ");
                html.Append("<a href=\"" + GlossaryPlaceholderData.getRssDataSourceUrl() + "\">" + GlossaryPlaceholderData.getRssDataSourceUrl() + "</a><br />");
                html.Append("<em>This URL is configured by the system administrator</em>");
                html.Append("</div>");
            }


            writer.Write(html.ToString());
        } // RenderEdit