<?php
class sitemapAction extends BackendAction{
    public function _initialize() {
        parent::_initialize();
		$config_file = CONF_PATH . 'url.php';
        $this->_config = require $config_file;	
    }

    
    public function index() {        
        $this->display();
    }
    
    public function getename($cid){
		$ename = D('article_cate')->where(array('id'=>$cid))->getField('ename');
		return $ename;
	}
		
	public function share(){				
		$p   = I('p');	
		$str  = "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
		if($p ==1){
		$shareindex = C('ftx_site_url');		
		$str .= '<url>';        
        $str .= '<loc>'.htmlspecialchars($shareindex).'</loc>';
		$str .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
        $str .= '<changefreq>weekly</changefreq>';        
        $str .= '<priority>1</priority>';
        $str .= '</url>';	
		$cate_list = D('article_cate')->where(array('status'=>1))->field('id,ename')->order('id desc')->select(); 
        if($cate_list){            
            foreach($cate_list as $k=>$v){				
				$cateurl = C('ftx_site_url').$v['ename'];				
                $str .= '<url>';        
                $str .= '<loc>'.htmlspecialchars($cateurl).'</loc>';
                $str .= '<changefreq>daily</changefreq>';
                $str .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
                $str .= '<priority>0.9</priority>';
                $str .= '</url>';
            }
        }										
		}		
		$c_name = C('ftx_c_name');			
		if(!$c_name){
		$c_name = 'wen';	
		}
		$page_size = 10000;								
		$start = $page_size * ($p - 1) ;
		$where['status'] = 1;		
		$where['fabu'] = 1;	
		$slist = D('article')->field('id,mid,add_time,cate_id')->where($where)->limit($start . ',' . $page_size)->order('id asc')->select();	
		if(!$slist){
		$this->ajaxReturn(0, 'sitemap更新完成！');		
		}		
		foreach($slist as $k=>$v){		
			if(C('ftx_a_link')=='md5'){
			$vid = $v['mid'];	
			}else{
			$vid = $v['id'];	
			}
			if(C('ftx_url_set')==1){				
			$upt = $this->getename($v['cate_id']);	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}		
		    $url = C('ftx_site_url').$upt.'/'.$vid.$this->_config['URL_HTML_SUFFIX'];			
			$str .= '<url>';        
			$str .= '<loc>'.htmlspecialchars($url).'</loc>';
			$str .= '<lastmod>'.date(DATE_ATOM,$v['add_time']).'</lastmod>';
			$str .= '<changefreq>daily</changefreq>';			
			$str .= '<priority>0.8</priority>';
			$str .= '</url>';
		}		
		$str.= '</urlset>';
		$this->updata('article'.$p,$str);
		$this->ajaxReturn(1, '');	
	}		
    
	public function updata($name,$str){
		file_put_contents($_SERVER['DOCUMENT_ROOT'].'/'.$name.'.xml',$str);
	}
	
	
}
?>
