Ejemplo n.º 1
0
        public GlossaryPlaceholderData getGlossary(CmsPage page, int identifier, CmsLanguage language, bool createNewIfDoesNotExist)
        {
            if (page.ID < 0 || identifier < 0)
            {
                return(new GlossaryPlaceholderData());
            }

            string  sql = "select glossaryid, sortOrder, viewMode from glossary c where c.pageid = " + page.ID.ToString() + " and c.identifier = " + identifier.ToString() + " and langShortCode like '" + dbEncode(language.shortCode) + "'  and deleted is null;";
            DataSet ds  = this.RunSelectQuery(sql);

            if (this.hasSingleRow(ds))
            {
                DataRow dr = ds.Tables[0].Rows[0];
                GlossaryPlaceholderData data = new GlossaryPlaceholderData();
                data.GlossaryId = Convert.ToInt32(dr["glossaryid"]);
                data.SortOrder  = (GlossaryPlaceholderData.GlossarySortOrder)Enum.Parse(typeof(GlossaryPlaceholderData.GlossarySortOrder), dr["sortOrder"].ToString());
                data.ViewMode   = (GlossaryPlaceholderData.GlossaryViewMode)Enum.Parse(typeof(GlossaryPlaceholderData.GlossaryViewMode), dr["viewMode"].ToString());
                return(data);
            }
            else
            {
                if (createNewIfDoesNotExist)
                {
                    return(createNewGlossary(page, identifier, language));
                }
                else
                {
                    throw new Exception("getGlossary database error: placeholder does not exist");
                }
            }
            return(new GlossaryPlaceholderData());
        } // getGlossary
Ejemplo n.º 2
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
Ejemplo n.º 3
0
        public GlossaryData[] getGlossaryData(GlossaryPlaceholderData placeholderData, string letterToDisplay)
        {
            string sql = "select * from glossarydata where Deleted IS NULL ";

            switch (placeholderData.ViewMode)
            {
            case GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter:
                if (letterToDisplay.Length != 1)
                {
                    // throw new ArgumentException("letterToDisplay must be specified!!!");
                }
                else
                {
                    sql += " AND LEFT(word,1) in ('" + dbEncode(letterToDisplay.ToString().ToLower()) + "','" + dbEncode(letterToDisplay.ToString().ToUpper()) + "') ";
                }
                break;

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

            default:
                throw new ArgumentException("invalid ViewMode");
            } // switch ViewMode
            sql += " AND phGlossaryId = " + placeholderData.GlossaryId + " ";
            sql += " ORDER BY left(word,1) "; // always order by the first character of the name

            switch (placeholderData.SortOrder)
            {
            case GlossaryPlaceholderData.GlossarySortOrder.byWord:
                sql += ", word ";
                break;

            case GlossaryPlaceholderData.GlossarySortOrder.byDescription:
                sql += ", Description ";
                break;

            case GlossaryPlaceholderData.GlossarySortOrder.byId:
                sql += ", GlossaryDataId ";
                break;

            default:
                throw new ArgumentException("invalid sortOrder");
            } // switch SortOrder

            // -- run the query
            List <GlossaryData> ret = new List <GlossaryData>();
            DataSet             ds  = this.RunSelectQuery(sql);

            if (this.hasRows(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ret.Add(dataFromRow(dr));
                } // foreach
            }

            return(ret.ToArray());
        } // getGlossaryData
Ejemplo n.º 4
0
        } // saveUpdatedGlossary

        public GlossaryData[] FetchRssFeedGlossaryDataFromDatabase()
        {
            // "http://www.sadcwaterhub.org/glossary/feed?lang_tid[0]=2"
            string dataCacheKey = GlossaryPlaceholderData.getRssDataPersistentVariableName();
            CmsPersistentVariable persistedData = CmsPersistentVariable.Fetch(dataCacheKey);

            if (persistedData.Name != "")
            {
                List <GlossaryData> list = (List <GlossaryData>)persistedData.PersistedValue;
                return(list.ToArray());
            }
            else
            {
                return(new GlossaryData[0]);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// returns String.Empty if no specific letter is to be displayed.
        /// </summary>
        /// <param name="placeholderData"></param>
        /// <returns></returns>
        public static string getLetterToDisplay(GlossaryPlaceholderData placeholderData)
        {
            string letterToDisplay = "";

            if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter)
            {
                string l = PageUtils.getFromForm("l", "");
                if (l.Length == 1)
                {
                    letterToDisplay = l[0].ToString();
                }
                else
                {
                    // placeholderData.ViewMode = GlossaryPlaceholderData.GlossaryViewMode.SinglePageWithJumpList;
                }
            }
            return(letterToDisplay);
        }
Ejemplo n.º 6
0
        private bool RemoveAllExistingGlossaryDataAndInsertNew(GlossaryPlaceholderData placeholder, GlossaryData[] allNewGlossaryData)
        {
            string delSql     = "delete from glossarydata where phGlossaryId = " + placeholder.GlossaryId + "  ; ";
            int    numUpdated = this.RunUpdateQuery(delSql);

            if (numUpdated >= 0)
            {
                if (allNewGlossaryData.Length == 0)
                {
                    return(true);
                }

                string        sql     = "INSERT INTO glossarydata (phGlossaryId,isAcronym,word,description) VALUES ";
                List <string> sqlVals = new List <string>();
                foreach (GlossaryData g in allNewGlossaryData)
                {
                    string Vals = ("(");
                    Vals += "" + placeholder.GlossaryId.ToString() + ", ";

                    if (g.isAcronym)
                    {
                        Vals += ("1, ");
                    }
                    else
                    {
                        Vals += ("0, ");
                    }

                    Vals += ("'" + dbEncode(g.word) + "', ");
                    Vals += ("'" + dbEncode(g.description) + "' ");
                    Vals += (")");

                    sqlVals.Add(Vals);
                } // foreach

                sql = sql + String.Join(",", sqlVals.ToArray()) + ";";
                int numInserted = this.RunUpdateQuery(sql);
                if (numInserted == allNewGlossaryData.Length)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 7
0
        } // getGlossary

        public GlossaryPlaceholderData createNewGlossary(CmsPage page, int identifier, CmsLanguage language)
        {
            GlossaryPlaceholderData data = new GlossaryPlaceholderData();

            string sql = "insert into glossary (pageid, identifier, langShortCode, sortOrder, ViewMode) values (";

            sql  = sql + page.ID.ToString() + "," + identifier.ToString() + ",";
            sql += "'" + dbEncode(language.shortCode) + "', ";
            sql  = sql + "'" + Enum.GetName(typeof(GlossaryPlaceholderData.GlossarySortOrder), data.SortOrder) + "', ";
            sql  = sql + "'" + Enum.GetName(typeof(GlossaryPlaceholderData.GlossaryViewMode), data.ViewMode) + "' ";
            sql += "); ";

            int newId = this.RunInsertQuery(sql);

            if (newId > -1)
            {
                page.setLastUpdatedDateTimeToNow();
            }

            return(data);
        } // createNewGlossary
        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());
        }
Ejemplo n.º 9
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());
        }
Ejemplo n.º 10
0
        } // createNewGlossary

        public bool saveUpdatedGlossary(CmsPage page, int identifier, CmsLanguage language, GlossaryPlaceholderData data, GlossaryData[] allGlossaryData)
        {
            string sql = "update glossary set ";

            sql += " sortOrder = '" + Enum.GetName(typeof(GlossaryPlaceholderData.GlossarySortOrder), data.SortOrder) + "', ";
            sql += " ViewMode = '" + Enum.GetName(typeof(GlossaryPlaceholderData.GlossaryViewMode), data.ViewMode) + "' ";
            sql += " where glossaryid = " + data.GlossaryId.ToString();

            int numAffected = this.RunUpdateQuery(sql);

            if (numAffected > 0)
            {
                if (RemoveAllExistingGlossaryDataAndInsertNew(data, allGlossaryData))
                {
                    return(page.setLastUpdatedDateTimeToNow());
                }
            }

            return(false);
        } // saveUpdatedGlossary
Ejemplo n.º 11
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
Ejemplo n.º 12
0
        public static string GetHtmlDisplay(CmsPage page, GlossaryData[] items, GlossaryPlaceholderData placeholderData, string[] charactersWithData, string letterToDisplay)
        {
            StringBuilder html = new StringBuilder();

            html.Append("<div class=\"Glossary\">" + Environment.NewLine);
            // -- output JumpLinks

            html.Append("<div class=\"JumpLinks\">");
            string        JumpLinksSeperator = " | ";
            List <string> jumpLinks          = new List <string>();

            if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter && letterToDisplay != "")
            {
                jumpLinks.Add("<a href=\"" + page.Url + "\" title=\"view entire glossary\">[all]</a>");
            }


            char startChar   = 'A';
            char lastChar    = 'Z'; lastChar++;
            char currentChar = startChar;

            do
            {
                string link    = "";
                bool   outputA = false;

                if (StringUtils.IndexOf(charactersWithData, currentChar.ToString(), StringComparison.CurrentCultureIgnoreCase) > -1)
                {
                    outputA = true;

                    string url = "";
                    if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter)
                    {
                        NameValueCollection pageParams = new NameValueCollection();
                        pageParams.Add("l", currentChar.ToString());
                        url = CmsContext.getUrlByPagePath(page.Path, pageParams);
                    }
                    else if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.SinglePageWithJumpList)
                    {
                        url = "#letter_" + currentChar.ToString();
                    }

                    link += "<a href=\"" + url + "\">";
                }
                link += currentChar.ToString();

                if (outputA)
                {
                    link += "</a>";
                }

                jumpLinks.Add(link);
                currentChar++;
            }while (currentChar != lastChar);

            string jumpLinksHtml = String.Join(JumpLinksSeperator, jumpLinks.ToArray());

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

            // -- output terms
            switch (placeholderData.ViewMode)
            {
            case GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter:
                if (letterToDisplay == "")
                {
                    startChar = 'A';
                    lastChar  = 'Z'; lastChar++;
                }
                else
                {
                    startChar = letterToDisplay[0];
                    lastChar  = letterToDisplay[0]; lastChar++;
                }
                break;

            case GlossaryPlaceholderData.GlossaryViewMode.SinglePageWithJumpList:
                startChar = 'A';
                lastChar  = 'Z'; lastChar++;
                break;

            default:
                throw new ArgumentException("invalid GlossaryViewMode");
            } // switch viewmode


            currentChar = startChar;
            html.Append("<table class=\"glossaryitems\">");
            do
            {
                html.Append("<tr><td class=\"LetterHeading\" colspan=\"2\">");
                html.Append("<a name=\"letter_" + currentChar.ToString() + "\"></a>");
                html.Append("<h2>" + currentChar + "</h2>");
                html.Append("</td></tr>" + Environment.NewLine);

                GlossaryData[] itemsToDisplay = new GlossaryData[0];
                if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.SinglePageWithJumpList || letterToDisplay == "")
                {
                    itemsToDisplay = GlossaryData.getItemsStartingWithChar(items, currentChar);
                }
                else if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter && letterToDisplay != "")
                {
                    itemsToDisplay = GlossaryData.getItemsStartingWithChar(items, currentChar);
                }
                else if (placeholderData.ViewMode == GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter)
                {
                    itemsToDisplay = items;
                }


                bool oddRow = true;
                foreach (GlossaryData item in itemsToDisplay)
                {
                    string cssClass = "even";
                    if (oddRow)
                    {
                        cssClass = "odd";
                    }
                    html.Append("<tr class=\"" + cssClass + "\">");
                    html.Append("<td class=\"word " + cssClass + "\">" + item.word + "</td>");
                    html.Append("<td class=\"description " + cssClass + "\">" + item.description + "</td>");
                    html.Append("</tr>" + Environment.NewLine);
                    oddRow = !oddRow;
                } // foreach glossarydata item

                currentChar++;
            } while (currentChar != lastChar);

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

            return(html.ToString());
        }