Exemplo n.º 1
0
        /// <summary>
        /// Pre-load categories into DataSet.
        /// </summary>
        private void PreLoadCategories()
        {
            var doCategory = new DOCategory(this.context.Connection);

            this.dsCategories = doCategory.EnumCategories();
            var doRule = new DORule(this.context.Connection);

            this.dsRules = doRule.EnumAll();
            var doMapping = new DOMapping(this.context.Connection);

            this.dsMappings = doMapping.EnumAll();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Execute re-counting of categories.
        /// </summary>
        private void RecountCategories()
        {
            this.oLogger.Output(CAT("<br/>", EOL, "Recount categories ... "));
            var doCategory   = new DOCategory(this.context.Connection);
            var doItem       = new DOItem(this.context.Connection);
            var dsCategories = doCategory.EnumCategories();

            for (int n = 0; n < dsCategories.GetSize(); n++)
            {
                var oCategory  = dsCategories.GetRow(n);
                var categoryId = STR(oCategory["s_CatId"]);
                var oldCounter = INT(oCategory["i_Counter"]);

                //String filter = STR(oCategory["s_Filter"]);
                //String sqlFilter = DOItem.BuildSqlByFilter(filter);

                var categoryName = STR(oCategory["s_Name"]);
                var sqlFilter    = DOItem.BuildSqlByCategory(categoryName);

                var dsCounters = doItem.EnumIds(CAT("_this.b_Counted = 0 AND ", sqlFilter));
                if (dsCounters.GetSize() == 0)
                {
                    continue;
                }

                var newCounter = INT(dsCounters.GetSize());

                //Update category
                var categoryFields = new THashtable();
                categoryFields["i_Counter"] = oldCounter + newCounter;
                doCategory.UpdateById(categoryId, categoryFields);
            }

            doItem.Update("_this.b_Counted = 1", "_this.b_Counted = 0");

            this.oLogger.Output(CAT(" ... Done<br/>", EOL));
        }
Exemplo n.º 3
0
        /// Execute main logic for Items block.
        public override void Execute()
        {
            var pars = this.Check();

            if (pars == null)
            {
                return;
            }

            var list       = (String)pars["list"];
            var listNumber = list == null ? 1 : INT(list);
            var sourceName = (String)pars["source_name"];
            var filterName = (String)pars["filter_name"];

            var errorMessage = "";
            var filter       = (String)null;
            var category     = (String)null;

            if (!NUL(filterName))
            {
                var          doCategory = new DOCategory(this.context.Connection);
                THashtable[] oCategory  =
                { new THashtable() };
                if (!doCategory.CheckFilterName(filterName, oCategory))
                {
                    errorMessage += "Non-existing filter name!";
                }
                else
                {
                    category = STR(oCategory[0]["s_Name"]);
                    filter   = STR(oCategory[0]["s_Filter"]);
                }
            }

            var sourceId = -1;

            if (!NUL(sourceName))
            {
                var          doSource = new DOSource(this.context.Connection);
                THashtable[] oSource  =
                { new THashtable() };
                if (!doSource.CheckSourceName(sourceName, oSource))
                {
                    if (errorMessage.Length > 0)
                    {
                        errorMessage += "<br/>";
                    }
                    errorMessage += "Non-existing source name!";
                }
                else
                {
                    sourceId = INT(oSource[0]["i_SourceId"]);
                }
            }

            var engine = this.context.GetEngine();

            var prepare = new THashtable();

            if (errorMessage.Length > 0)
            {
                prepare["[#ErrMessage]"] = errorMessage;
                this.Write("error", prepare);
                return;
            }

            if (Config.SHOW_IMAGES)
            {
                prepare["[#Show_Images]"] = 1;
            }
            prepare["[#ColSpan]"] = Config.SHOW_IMAGES ? 4 : 3;

            // Uncomment to enable filtering by source and/or category
            prepare["[#FilterItems]"] = engine.IncludeTemplate("Pages/FilterItems");

            var s_Title = CAT(
                "Browse ",
                Config.NAME_ITEMS,
                (this.context.IsMobile ? "<br/>" : null),
                (!BLANK(sourceName) ? CAT(" ... from '", sourceName, "'") : null),
                (!BLANK(filter) ? CAT(" ... for '", category, "'") : null)
                );

            prepare["[#Title]"] = s_Title;

            var maxRows = Config.DB_ITEMS_ROWS;

            var doItem = new DOItem(this.context.Connection);
            //String realFilter = DOItem.BuildSqlByFilter(filter);
            var realFilter = DOItem.BuildSqlByCategory(category);
            var dsItems    = doItem.EnumItems(sourceName, realFilter, listNumber, maxRows);

            var listTotal = dsItems.GetTotalPages();

            if (listNumber > listTotal)
            {
                if (listTotal > 0)
                {
                    prepare["[#ErrMessage]"] = "List number is too large!";
                    this.Write("error", prepare);
                    return;
                }
                else
                {
                    prepare["[#ErrMessage]"] = "Empty list!";
                    this.Write("error", prepare);
                    return;
                }
            }
            if (listTotal > 1)
            {
                prepare["[#List_Total]"] = listTotal;
                prepare["[#List]"]       = listNumber;
            }

            var count = 1;
            var rows  = new TArrayList();

            for (int n = 0; n < dsItems.GetSize(); n++)
            {
                var oItem = dsItems.GetRow(n);
                var row   = FillItemRow(oItem, doItem.GetIdField(), count);
                count++;
                rows.Add(row);
            }
            prepare["[#Rows]"] = rows;

            if (listTotal > 1)
            {
                var chunk  = 2;
                var before = false;
                var after  = false;

                var pages = new TArrayList();
                for (int n = 1; n <= listTotal; n++)
                {
                    var page = new THashtable();
                    if (n < listNumber - chunk)
                    {
                        if (!before)
                        {
                            before          = true;
                            page["[#Text]"] = "1";
                            page["[#Link]"] = GetPageLink(1);
                            pages.Add(page);
                            page            = new THashtable();
                            page["[#Text]"] = " ... ";
                            //row.Remove("[#Link]");
                            pages.Add(page);
                        }
                        continue;
                    }
                    if (n > listNumber + chunk)
                    {
                        if (!after)
                        {
                            after           = true;
                            page["[#Text]"] = " ... ";
                            pages.Add(page);
                            page            = new THashtable();
                            page["[#Text]"] = listTotal;
                            page["[#Link]"] = GetPageLink(listTotal);
                            pages.Add(page);
                        }
                        continue;
                    }
                    if (listNumber == n)
                    {
                        page["[#Text]"] = CAT("=", n, "=");
                        pages.Add(page);
                    }
                    else
                    {
                        if (n == 1)
                        {
                            page["[#Link]"] = GetPageLink(1);
                            page["[#Text]"] = 1;
                        }
                        else
                        {
                            page["[#Link]"] = GetPageLink(n);
                            page["[#Text]"] = n;
                        }
                        pages.Add(page);
                    }
                }
                prepare["[#Pages]"] = pages;
            }

            this.Write("Pages/items", prepare);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Execute main logic for generating RSS-feeds.
        /// </summary>
        public override void Execute()
        {
            //this.context.Request.Initialize();
            this.context.Request.ExtractAllVars();

            var errorMessage = "";

            // Check source
            var source = this.context.Request["source"];

            if (!NUL(source))
            {
                if (BLANK(source))
                {
                    errorMessage += "Empty source!";
                }
                else
                {
                    var          doSource = new DOSource(this.context.Connection);
                    THashtable[] oSource  =
                    { new THashtable() };
                    if (!doSource.CheckSourceName(source, oSource))
                    {
                        errorMessage += CAT("Incorrect source '", source, "'!");
                    }
                }
            }

            var anyFilter = false;

            if (this.context.Request.Contains("code"))
            {
                if (EQ(this.context.Request["code"], Config.SECURITY_CODE))
                {
                    anyFilter = true;
                }
            }

            // Check filter
            var filter       = (String)null;
            var filterName   = (String)null;
            var categoryName = (String)null;
            var doCategory   = new DOCategory(this.context.Connection);
            var dsCategories = doCategory.EnumCategories();

            if (dsCategories.GetSize() > 0)
            {
                filterName = this.context.Request["filter"];
                if (!NUL(filterName))
                {
                    if (BLANK(filterName))
                    {
                        if (errorMessage.Length > 0)
                        {
                            errorMessage += " ";
                        }
                        errorMessage += "Empty filter!";
                    }
                    else
                    {
                        THashtable[] oCategory =
                        { new THashtable() };
                        if (doCategory.CheckFilterName(filterName, oCategory))
                        {
                            categoryName = STR(oCategory[0]["s_Name"]);
                            filter       = STR(oCategory[0]["s_Filter"]);
                        }
                        else
                        {
                            if (anyFilter)
                            {
                                filter = filterName;
                            }
                            else
                            {
                                if (errorMessage.Length > 0)
                                {
                                    errorMessage += " ";
                                }
                                errorMessage += CAT("Incorrect filter '", filterName, "'!");
                            }
                        }
                    }
                }
            }

            // Check that parameters contain only 'source' or/and 'filter'
            var keys = this.context.Request.GetKeys();

            while (keys.MoveNext())
            {
                var key = (String)keys.GetCurrent();
                if (EQ(key, "source") || EQ(key, "filter") || EQ(key, "code") || EQ(key, "count"))
                {
                    //OK
                }
                else
                {
                    //Not OK
                    if (errorMessage.Length > 0)
                    {
                        errorMessage += " ";
                    }
                    errorMessage += CAT("Incorrect parameter '", key, "'!");
                }
            }

            if (errorMessage.Length > 0)
            {
                this.WriteErrorMessage(errorMessage);
                return;
            }

            var fullTitle = false;

            if (this.context.Request.Contains("title") && STR(this.context.Request["title"]) == "full")
            {
                fullTitle = true;
            }

            var count    = Config.MAX_RSS_ITEMS;
            var countSet = false;

            if (this.context.Request.Contains("count"))
            {
                if (INT(this.context.Request["count"]) > 0)
                {
                    count = INT(this.context.Request["count"]);
                    if (count < Config.MIN_RSS_ITEMS)
                    {
                        count = Config.MIN_RSS_ITEMS;
                    }
                    if (count > Config.MAX_RSS_ITEMS)
                    {
                        count = Config.MAX_RSS_ITEMS;
                    }
                    countSet = true;
                }
            }

            // Get content from cache (if enabled and cache data exists)
            var cachedFile = "";

            if (Config.CACHE_RSS && !countSet)
            {
                cachedFile = Strings.Concat(
                    this.context.RssFolder, "/rss",
                    (BLANK(source) ? null : CAT("-s=", source)),
                    (BLANK(filterName) ? null : CAT("-f=", filterName)),
                    (fullTitle ? "-full" : null), ".xml");
                if (Helper.FileExists(cachedFile))
                {
                    this.context.Response.WriteHeader("Content-type", "text/xml; charset=UTF-8");
                    var tempContent = Helper.ReadAllText(cachedFile);
                    //this.context.Response.Write(tempContent.Substring(3)); //TODO -- BOM?
                    this.context.Response.Write(tempContent); //TODO -- BOM?
                    return;
                }
            }

            var doItem = new DOItem(this.context.Connection);

            // 0 - item url
            // 1 - item title
            // 2 - marketplace url
            // 3 - marketplace name
            // 4 - date
            // 5 - description
            // 6 - category

            var pubDate  = DateTimes.Format(DateTimes.XML_DTS);
            var nowDate  = DateTimes.Format(DateTimes.SQL_DTS);
            var nowTime  = DateTimes.GetTime(nowDate);
            var fromDate = DateTimes.GmtFormat(DateTimes.SQL_DTS, nowTime - 6 * 60 * 60);
            //String search = DOItem.BuildSqlByFilter(filter);
            var search  = DOItem.BuildSqlByCategory(categoryName);
            var dsItems = doItem.EnumItemsFromSource(fromDate, source, search, count);
            var current = 0;

            var contentToCache = "";

            if (dsItems.GetSize() == 0)
            {
                contentToCache = this.WriteStart(source, categoryName, pubDate);
            }

            for (int n = 0; n < dsItems.GetSize(); n++)
            {
                var oItem = dsItems.GetRow(n);
                var date  = STR(oItem["d_Date"]);
                if (DateTimes.GetTime(date) > nowTime)
                {
                    continue;
                }

                if (current == 0)
                {
                    // Get puDate from the first item and write starting block
                    pubDate        = DateTimes.Format(DateTimes.XML_DTS, DateTimes.GetTime(date));
                    contentToCache = this.WriteStart(source, categoryName, pubDate);
                }

                var    category = this.context.Contains("Name_Category") ? STR(oItem["s_Category"]) : null;
                var    creator  = this.context.Contains("Name_Creator") ? STR(oItem["s_Creator"]) : null;
                String custom1  = this.context.Contains("Name_Custom1") ? STR(oItem["s_Custom1"]) : null;
                String custom2  = this.context.Contains("Name_Custom2") ? STR(oItem["s_Custom2"]) : null;

                var sourceName  = STR(oItem["s_SourceName"]);
                var description = STR(oItem["t_Description"]);
                if (!BLANK(description))
                {
                    description = Regex.Replace(description, "<br/>", " ", RegexOptions.IgnoreCase);
                    description = Regex.Replace(description, "&nbsp;", " ");
                    description = Regex.Replace(description, "[ \r\n\t]+", " ");
                    if (description.Length > 512)
                    {
                        description = description.Substring(0, 511);
                        var lastSpaceIndex = description.LastIndexOf(" ");
                        description = Strings.Concat(description.Substring(0, lastSpaceIndex), " ...");
                    }
                    //Boolean utfIsValid = Mb_check_encoding(description, "UTF-8");
                    //if (utfIsValid == false)
                    //    description = ""; //TODO
                }
                var itemTitle = CAT(
                    (fullTitle == true && !BLANK(custom2) ? CAT(custom2, " | ") : null),
                    Strings.RemoveTags(Strings.StripSlashes(STR(oItem["s_Title"]))),
                    (fullTitle == true ? CAT(" [", sourceName, "]") : null)
                    );

                var link = (String)null;
                if (this.context.ImmediateRedirect)
                {
                    link = STR(oItem["s_Link"]);
                }
                else
                {
                    var url     = STR(oItem["s_Url"]);
                    var idField = doItem.GetIdField();
                    link = this.GetAbsoluteLink(Config.INDEX_PAGE, "?p=view_item&amp;id=", "item/", oItem[idField]);
                    if (!BLANK(url))
                    {
                        link = this.AppendLink(link, "&amp;title=", "/", url);
                    }
                }

                Object[] args = ARR(7);
                args[0] = link;
                args[1] = itemTitle;
                args[2] = this.GetAbsoluteLink(Config.ACTION_PAGE, "?p=do_redirect_source&amp;source=", "redirect/source/", sourceName);
                args[3] = sourceName;
                args[4] = DateTimes.Format(DateTimes.XML_DTS, DateTimes.GetTime(date));
                var additional = (String)null;
                if (!BLANK(creator))
                {
                    if (additional != null)
                    {
                        additional = CAT(additional, "<br/>");
                    }
                    additional = CAT(additional, this.context["Name_Creator"], ": ", creator);
                }
                if (!BLANK(category))
                {
                    if (additional != null)
                    {
                        additional = CAT(additional, "<br/>");
                    }
                    additional = CAT(additional, this.context["Name_Categories"], ": ", category);
                }
                if (!BLANK(custom2))
                {
                    if (additional != null)
                    {
                        additional = CAT(additional, "<br/>");
                    }
                    additional = CAT(additional, this.context["Name_Custom2"], ": ", custom2);
                }
                if (!BLANK(custom1))
                {
                    if (additional != null)
                    {
                        additional = CAT(additional, "<br/>");
                    }
                    additional = CAT(additional, this.context["Name_Custom1"], ": ", custom1);
                }

                var extendedDescription = (String)null;
                if (!BLANK(description))
                {
                    if (BLANK(additional))
                    {
                        extendedDescription = description;
                    }
                    else
                    {
                        extendedDescription = CAT(description, "<br/><br/>", additional);
                    }
                }
                else if (!BLANK(additional))
                {
                    extendedDescription = additional;
                }
                args[5] = extendedDescription;
                args[6] = category;

                var itemContent = this.WriteItem(args);
                if (!BLANK(itemContent))
                {
                    contentToCache += itemContent;
                }

                current++;
            }

            var endContent = this.WriteEnd();

            if (!BLANK(endContent))
            {
                contentToCache += endContent;
            }

            // Save content to cache (if applicable)
            if (Config.CACHE_RSS && !countSet)
            {
                Helper.TestFileFolder(cachedFile);
                //Helper.WriteText(cachedFile, Strings.Concat("\xEF\xBB\xBF", xmlContent));
                Helper.WriteText(cachedFile, contentToCache);
            }
            this.context.Response.WriteHeader("Content-type", "text/xml; charset=UTF-8");
            this.context.Response.Write(contentToCache); //TODO -- BOM?
        }
Exemplo n.º 5
0
        /// Execute main logic for Bottom block
        public override void Execute()
        {
            var prepare = new THashtable();

            prepare.Put("[#Items_By_Category]",
                        CAT(Config.NAME_ITEMS, "_by_", this.context["Name_Category"]));

            var doCategory = new DOCategory(this.context.Connection);
            var dsCategory = doCategory.EnumAll(Config.SHOW_EMPTY ? null : "_this.i_Counter <> 0",
                                                Config.SORT_CATEGORIES == null ? null : CAT("_this.", Config.SORT_CATEGORIES));
            var size  = dsCategory.GetSize();
            int size3 = size % 3;
            int n1    = INT(size / 3) + (size3 == 0 ? 0 : 1);
            int n2    = n1 * 2;

            Object[] nn           = ARR(0, n1, n2, size);
            var      filterBlocks = new TArrayList();

            for (int td = 0; td < 3; td++)
            {
                var filterBlock = new THashtable();
                var rows        = new TArrayList();
                for (int n = INT(nn[td]); n < INT(nn[td + 1]); n++)
                {
                    var oCategory = dsCategory.GetRow(n);
                    if (NUL(oCategory))
                    {
                        continue;
                    }
                    var counter = INT(oCategory["i_Counter"]);
                    if (Config.SHOW_EMPTY == false && INT(counter) == 0)
                    {
                        continue;
                    }
                    var key  = STR(oCategory["s_CatId"]);
                    var name = STR(oCategory["s_Name"]);
                    var row  = new THashtable();
                    row["[#Link]"]     = this.GetLink(Config.INDEX_PAGE, "?p=items&filter=", "items/filter/", key);
                    row["[#LinkText]"] = name;
                    //if (counter > 0)
                    row["[#Counter]"] = counter;
                    rows.Add(row);
                }
                filterBlock["[#Rows]"] = rows;
                filterBlocks.Add(filterBlock);
            }
            prepare["[#FilterBlocks]"] = filterBlocks;

            if (!this.context.IsMobile)
            {
                //dsCategory = doCategory.EnumAll(null, Config.SORT_CATEGORIES == null ? null : CAT("_this.", Config.SORT_CATEGORIES));
                size  = dsCategory.GetSize();                 //50
                size3 = size % 3;                             //2
                n1    = INT(size / 3) + (size3 == 0 ? 0 : 1); //17.3
                n2    = n1 * 2;                               //34.6
                nn    = ARR(0, n1, n2, size);
                var rssBlocks = new TArrayList();
                for (int td = 0; td < 3; td++)
                {
                    var rssBlock = new THashtable();
                    var rows     = new TArrayList();
                    for (int n = INT(nn[td]); n < INT(nn[td + 1]); n++)
                    {
                        var oCategory = dsCategory.GetRow(n);
                        if (NUL(oCategory))
                        {
                            continue;
                        }
                        var key  = STR(oCategory["s_CatId"]);
                        var name = STR(oCategory["s_Name"]);
                        //counter = INT(oCategory["i_Counter"]);
                        var row = new THashtable();
                        row["[#Link]"]     = this.GetLink(Config.RSS_PAGE, "?filter=", "rss/", CAT(key, (this.context.FineUrls ? ".xml" : null)));
                        row["[#LinkText]"] = name;
                        rows.Add(row);
                    }
                    rssBlock["[#Rows]"] = rows;
                    rssBlocks.Add(rssBlock);
                }
                prepare["[#RssBlocks]"] = rssBlocks;
            }
            this.Write("bottom", prepare);
        }