/**********************************************************************************
 * ClassName:       SearchTags
 * Description:     搜索页标签处理
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2009-11-22     New          Ayong     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using Fm.Bussiness;
using Fm.DataAccess;
using System.Web;
using System.Collections;

namespace Fm.Tags
{
    public class SearchTags
    {
        //获取参数属性列表  
        GlobalTags gb = new GlobalTags();
        /// <summary>
        /// 分页标签解析
        /// </summary>
        /// <param name="content">解析内容</param>
        /// <param name="cid">栏目ID</param>
        /// <param name="pid">page页</param>
        /// <returns></returns>
        public string Analysis_Page(string content, int pid, string kw)
        {
            //实例化分页数据操作类
            Formpage page = new Formpage();
            //获取查询条件
            string where = "title like '%"+kw+"%'";
            //查询该栏目的信息
            public_select ps = new public_select();
            //正则获取标签信息
            Regex r = new Regex(@"(\{fm:page\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:page\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            //标签解析开始---

            string Attributes = "";//存储属性
            string Text = "";//存储标签Text
            string AllText = "";
            foreach (Match m in r.Matches(content))
            {
                //获取正则相关属性
                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();


                string count = null;
                count = gb.TagVal(Attributes, "count");
                if (count == null)
                {
                    count = "15";
                }

                /*分页Start*************************/
                //查询记录总数
                DataView total = page.PageRow("Fm_model_main", "docid", int.Parse(count), 1, where) as DataView;

                int rownum = int.Parse(total[0]["total"].ToString());
                int pagesize = int.Parse(count);
                int rowcount = 0;
                //判断总页码
                if (rownum % pagesize == 0)
                {
                    rowcount = rownum / pagesize;
                }
                else
                {
                    rowcount = rownum / pagesize + 1;
                }
                if (rowcount == 0)
                {
                    rowcount = 1;
                }
                //标签替换
                string txt = Text;
                txt = txt.Replace("{field.total}", rownum.ToString());
                txt = txt.Replace("{field.current}", pid.ToString());//当前页
                txt = txt.Replace("{field.amount}", rowcount.ToString());//总页数
                //获取URL 地址
                string url = "/search.aspx?page={page}&kw="+kw+"";
                //根据栏目选项属性替换首页链接
               
                txt = txt.Replace("{field.index}", url.Replace("{page}", "1"));

                //最后一页
                txt = txt.Replace("{field.last}", url.Replace("{page}", rowcount.ToString()));

                int front = 1;
                int next = 1;
                if (pid > 1)
                {
                    front = pid - 1;

                }
                if (pid == rowcount)
                {
                    next = pid;
                }
                else
                {
                    next = pid + 1;
                }
                txt = txt.Replace("{field.front}", url.Replace("{page}", front.ToString()));//上一页
                txt = txt.Replace("{field.next}", url.Replace("{page}", next.ToString()));//下一页
                //处理数字页标签
                //正则获取标签信息
                //实例化正则对象
                Regex reg = new Regex(@"(\{Fm:number\}(?<text>[\s\S]*?)\{/Fm:number\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                //存储新的内容
                StringBuilder str = new StringBuilder();
                str.Append("");
                string NewText = "";
                string NewAllText = "";
                foreach (Match ma in reg.Matches(txt))
                {
                    //存储新的内容
                    NewText = ma.Groups["text"].ToString();
                    NewAllText = ma.Groups[0].Value.ToString();
                    //替换首页链接
                    str.Append(NewText.Replace("{field.url}", url.Replace("{page}", "1")));
               
                    str = str.Replace("{field.count}", "1");
                    //循环生成分页连接
                    for (int i = 2; i < rowcount; i++)
                    {
                        string s = NewText;
                        if (pid > 9)
                        {
                            if (i < (pid - 4) && i > 1)
                            {
                                continue;
                            }
                            
                        }
                        if (i > (pid + 4) && i < rowcount)
                        {
                            break;
                        }
                        //替换标签
                        s = s.Replace("{field.url}", url.Replace("{page}", i.ToString()));
                        s = s.Replace("{field.count}", i.ToString());
                        str.Append(s);
                    }
                    str.Append(NewText.Replace("{field.url}", url.Replace("{page}", rowcount.ToString())).Replace("{field.count}", rowcount.ToString()));


                }
                if (NewAllText != "")
                {
                    txt = txt.Replace(NewAllText, str.ToString());
                }
                /*分页End***************************/

                content = content.Replace(AllText, txt);

                content = Analysis_Loop(content, (page.PageView_Bywhere("Fm_model_article_view", "docid", int.Parse(count), pid, where) as DataView).Table,kw);
            }
            return content;
        }
        public string Analysis_Loop(string content, DataTable dt,string kw)
        {
            //标签解析开始---
            string Text = "";//存储标签Text
            string AllText = "";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:loop\}(?<text>[\s\S]*?)\{/fm:loop\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {
                //存储新的内容
                StringBuilder str = new StringBuilder();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();

                int index = 1;
                foreach (DataRow row in dt.Rows)
                {
                    StringBuilder txt = new StringBuilder();
                    txt.Append(Text);
                    //标签解析
                    //索引标签
                    txt = txt.Replace("{field.index}", index.ToString());
                    index++;

                    txt = txt.Replace("{field.docid}", row["docid"].ToString());
                    txt = txt.Replace("{field.cid}", row["colid"].ToString());
                   // txt = txt.Replace("{field.cname}", row["cl_name"].ToString());
                    //txt = txt.Replace("{$title}",dv[num]["title"].ToString());
                    txt = txt.Replace("{field.click}", row["click"].ToString());
                    if (row["thumbnail"].ToString().Equals(""))
                    {
                        txt = txt.Replace("{field.thumbnail}", "/images/thumbnail.jpg");
                    }
                    else
                    {
                        txt = txt.Replace("{field.thumbnail}", row["thumbnail"].ToString());
                    }
                    txt = txt.Replace("{field.top}", row["topclick"].ToString());
                    txt = txt.Replace("{field.tread}", row["tread"].ToString());
                    //将搜索关键字高亮

                    txt = txt.Replace("{field.title}", row["title"].ToString().Replace(kw,"<font color=red>"+kw+"</font>"));

                    txt = txt.Replace("field.color", row["color"].ToString());

                    txt = txt.Replace("{field.keywords}", row["keyword"].ToString());
                    txt = txt.Replace("{field.intro}", row["intro"].ToString());
                    txt = txt.Replace("{field.username}", row["username"].ToString());
                    //txt = txt.Replace("{field.author}", row["author"].ToString());
                    //txt = txt.Replace("{field.source}", row["source"].ToString());
                    //处理连接地址
                    if (row["html"].ToString().ToLower() == "true")
                    {
                        txt = txt.Replace("{field.url}", row["path"].ToString());
                    }
                    else
                    {
                        txt = txt.Replace("{field.url}", "/article.aspx?docid=" + row["docid"].ToString());
                    }
                    //处理时间
                    Regex r1 = new Regex(@"(\{field.writertime\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    string att = "";//存储属性
                    string all = "";
                    string format = null;
                    foreach (Match m1 in r1.Matches(txt.ToString()))
                    {
                        //获取属性
                        att = m1.Groups["attributes"].ToString();
                        all = m1.Groups[0].Value.ToString();

                        format = gb.TagVal(att, "format");

                        DateTime time = DateTime.Parse(row["writertime"].ToString());

                        if (format != null)
                        {

                            format = format.Replace("yyyy", time.Year.ToString());

                            format = format.Replace("mm", time.Month.ToString());

                            format = format.Replace("dd", time.Day.ToString());
                        }
                        else
                        {
                            format = time.Date.ToString();
                        }
                        //替换时间标签
                        txt = txt.Replace(all, format);
                    }


                    str.Append(txt);
                }
                //替换整个标签的数据
                content = content.Replace(AllText, str.ToString());

            }
            return content;
        }
    }
}
