/**********************************************************************************
 * ClassName:       ArticleTags
 * Description:     内容页标签处理
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2009-11-17     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 ArticleTags
    {
        public string Analysis_Content(string cont,int docid,int pid,string src)
        {
            Tags_sql sql = new Tags_sql();
            GlobalTags gb = new GlobalTags();
            DataTable dt = (sql.GetContent(docid) as DataView).Table;
            StringBuilder content = new StringBuilder();
            content.Append(cont);
            foreach (DataRow row in dt.Rows)
            {
                //文档ID
                content = content.Replace("{fm:field.docid}",row["docid"].ToString());
                //标题
                content = content.Replace("{fm:field.title}",row["title"].ToString());
                //栏目ID
                content = content.Replace("{fm:field.cid}",row["colid"].ToString());
                //发布人
                content = content.Replace("{fm:field.username}",row["username"].ToString());
                //浏览次数
                content = content.Replace("{fm:field.click}","<script language=javascript> PostPage('docid="+row["docid"].ToString()+"&page="+pid+"','/js/browse.aspx');document.write(GetValue()); </script>");
                //标题颜色
                content = content.Replace("{fm:field.color}",row["color"].ToString());
                //关键字
                content = content.Replace("{fm:field.keyword}",row["keyword"].ToString());
                //描述
                content = content.Replace("{fm:field.intro}",row["intro"].ToString());
                //被顶次数
                content = content.Replace("{fm:field.top}",row["topclick"].ToString());
                
                //被踩次数
                content = content.Replace("{fm:field.tread}",row["tread"].ToString());
                //缩略图
                content = content.Replace("{fm:field.thumbnail}",row["thumbnail"].ToString());
                //内容
                //content = content.Replace("{fm:field.content}",row["content"].ToString());
                //作者
                content = content.Replace("{fm:field.author}",row["author"].ToString());
                //来源
                content = content.Replace("{fm:field.source}",row["source"].ToString());
                //来源
                content = content.Replace("{fm:field.cname}", row["cl_name"].ToString());
                //上一篇
                //查询上一页信息
                DataView front = sql.GetNextOrFront(docid,"front") as DataView;
                if (front.Count > 0)
                {
                    //判断文档路径
                    string url = "";
                    if (front[0]["html"].ToString().ToLower() == "true")
                    {
                        url = front[0]["path"].ToString();
                    }
                    else
                    {
                        url = "/article.aspx?docid=" + front[0]["docid"].ToString();
                    }
                    content = content.Replace("{fm:field.front}", "<a href='" + url + "' title='" + front[0]["title"].ToString() + "'>" + front[0]["title"].ToString() + "</a>");
                }
                else
                {
                    content = content.Replace("{fm:field.front}","");
                }
                //下一篇
                DataView next = sql.GetNextOrFront(docid,"next") as DataView;
                if (next.Count > 0)
                {
                    string href = "";
                    if (next[0]["html"].ToString().ToLower() == "true")
                    {
                        href = next[0]["path"].ToString();
                    }
                    else
                    {
                        href = "/article.aspx?docid="+next[0]["docid"].ToString();
                    }
                    content = content.Replace("{fm:field.next}", "<a href='" + href + "' title='" + next[0]["title"].ToString() + "'>" + next[0]["title"].ToString() + "</a>");
                
                }
                else
                {
                    content = content.Replace("{fm:field.next}","");
                }
               // content = content.Replace("{fm:field.content}",row["content"].ToString());
                //处理时间
                Regex r = new Regex(@"(\{fm:field.writertime\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                string att = "";//存储属性
                string all = "";
                string format = null;
                string txt = "";
                foreach (Match m in r.Matches(content.ToString()))
                {
                    //获取属性
                    att = m.Groups["attributes"].ToString();
                    all = m.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 = format; 
                }
                content = content.Replace(all,txt);

                //调用分页信息
                content = Analysis_Paging(content.ToString(),row["content"].ToString(),pid,docid,src,bool.Parse(row["html"].ToString().ToLower()));
            }
            return content.ToString();
        }
        
        private StringBuilder Analysis_Paging(string content,string art,int pid,int docid,string src,bool html)
        {
                //ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                /*分页Start*************************/
                Regex r = new Regex(@"(#p#)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                int number = r.Matches(art).Count;
                //处理分页
                //标签解析开始---
                string Text = "";//存储标签Text
                string AllText = "";
                //实例化正则对象
                Regex p = new Regex(@"(\{fm:field.page\}(?<text>[\s\S]*?)\{/fm:field.page\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

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

                    //判断内容中是否有分页符
                    if (number > 0)
                    {
                        /************获取url地址Start**************/
                         string kzname="";
                         string filename="";
                        if (src != "")
                        {
                            kzname = Path.GetExtension(src);
                            filename= src.Replace(kzname, "");
                        }
                        string newurl = "";
                        if (html)
                        {
                            newurl = filename + "_{page}" + kzname;
                        }
                        else
                        {
                            newurl = "/article.aspx?docid="+docid+"&page={page}";
                        }
                        /************获取URL地址End****************/
                        number= number + 1;
                        //处理首页-下一页-上一页-最后一页 分页标签
                        StringBuilder txt = new StringBuilder();
                        txt.Append(Text);
                        //首页
                        txt = txt.Replace("{field.index}", src);

                        //最后一页
                        txt = txt.Replace("{field.last}", newurl.Replace("{page}",number.ToString()));
                        int front = 1;
                        int next = 1;
                        if (pid > 1)
                        {
                            front = pid - 1;

                        }
                        if (pid == number)
                        {
                            next = pid;
                        }
                        else
                        {
                            next = pid + 1;
                        }
                        //上一页
                        if (front == 1)
                        {
                            txt = txt.Replace("{field.front}", src);
                        }
                        else
                        {
                            txt = txt.Replace("{field.front}", newurl.Replace("{page}",front.ToString()));
                        }
                        //下一页

                        txt = txt.Replace("{field.next}", newurl.Replace("{page}",next.ToString()));
                        //处理数字页标签
                        //正则获取标签信息
                        //实例化正则对象
                        Regex reg = new Regex(@"(\{fm:number\}(?<text>[\s\S]*?)\{/fm:number\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        //存储新的内容
                        StringBuilder str = new StringBuilder();
                        string NewText = "";
                        string NewAllText = "";
                        foreach (Match ma in reg.Matches(txt.ToString()))
                        {
                            //存储新的内容
                            NewText = ma.Groups["text"].ToString();
                            NewAllText = ma.Groups[0].Value.ToString();


                            str.Append(NewText.Replace("{field.url}", src));
                            str = str.Replace("{field.count}", "1");
                            
                            //循环生成分页连接
                            for (int i = 2; i < number; i++)
                            {
                                string s = NewText;
                                //s.Append(NewText);
                                if (pid > 9)
                                {
                                    if (i < (pid - 4) && i > 1)
                                    {
                                        continue;
                                    }
                                   
                                }
                                if (i > (pid + 4) && i < number)
                                {
                                    break;
                                }
                                //替换标签
                                s = s.Replace("{field.url}", newurl.Replace("{page}",i.ToString()));
                                s = s.Replace("{field.count}", i.ToString());
                                str.Append(s);
                            }
                            str.Append(NewText.Replace("{field.url}", newurl.Replace("{page}",number.ToString())).Replace("{field.count}", number.ToString()));
                        }
                        if (NewAllText != "")
                        {
                            txt = txt.Replace(NewAllText, str.ToString());
                        }
                        /*分页End***************************/
                        /*************内容截取Start******************/
                        ArrayList substr = GetSubString(art, number);//存储已经截取的字符
                        //art=art.Replace("#p#","ん");
                        //string[] substr = art.Split('ん');
                       
                        //替换内容
                        content = content.Replace("{fm:field.content}",substr[(pid-1)].ToString());
                        /*************内容截取End********************/
                         content = content.Replace(AllText, txt.ToString());

                    }
                    else
                    {
                        content = content.Replace("{fm:field.content}",art);
                        content = content.Replace(AllText, "");
                    }
                   

                }
                
           
            builder.Append(content);
            return builder;
        }
        private ArrayList GetSubString(string str,int number)
        {
            ArrayList list = new ArrayList();
            string substr = str;
            //int length = 0;
            for (int i = 0; i < number; i++)
            {
                if (i<number-1)
                {
                    //截取字符串
                    list.Add(substr.Substring(0, substr.IndexOf("#p#")));
                    substr = substr.Remove(0,substr.IndexOf("#p#")+3);
                }    
                if(i==number-1)
                {
                    list.Add(substr);
                }
            }
            return list;
        }
    }
}
