<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class control extends adminbase {
	private $sitemap = null;

    public function action_run() {
        TPL::display($this->cptpl . 'sitemap.tpl');
    }

    
    public function action_makemap() {
        $response = 0;
        $counts = 0;
        $mapData = "";
        $product = XRequest::getInt("product");
        $product_num = XRequest::getInt("product_num");
        if ($product_num < 1) {
            $product_num = 50;
        }
        if ($product == 1) {
            $product_data = $this->_getDataList("product", $product_num);
            if (!empty($product_data)) {
                if (empty($mapData)) {
                    $mapData = $product_data;
                }
                else {
                    $mapData = array_merge($mapData, $product_data);
                }
            }
        }

        $article = XRequest::getInt("article");
        $article_num = XRequest::getInt("article_num");
        if ($article_num < 1) {
            $article_num = 50;
        }
        if ($article == 1) {
            $article_data = $this->_getDataList("article", $article_num);
            if (!empty($article_data)) {
                if (empty($mapData)) {
                    $mapData = $article_data;
                }
                else {
                    $mapData = array_merge($mapData, $article_data);
                }
            }
        }

        $photo = XRequest::getInt("photo");
        $photo_num = XRequest::getInt("photo_num");
        if ($photo_num < 1) {
            $photo_num = 50;
        }
        if ($photo == 1) {
            $photo_data = $this->_getDataList("photo", $photo_num);
            if (!empty($photo_data)) {
                if (empty($mapData)) {
                    $mapData = $photo_data;
                }
                else {
                    $mapData = array_merge($mapData, $photo_data);
                }
            }
        }

        $download = XRequest::getInt("download");
        $download_num = XRequest::getInt("download_num");
        if ($download_num < 1) {
            $download_num = 50;
        }
        if ($download == 1) {
            $download_data = $this->_getDataList("download", $download_num);
            if (!empty($download_data)) {
                if (empty($mapData)) {
                    $mapData = $download_data;
                }
                else {
                    $mapData = array_merge($mapData, $download_data);
                }
            }
        }

        $hr = XRequest::getInt("hr");
        $hr_num = XRequest::getInt("hr_num");
        if ($hr_num < 1) {
            $hr_num = 50;
        }
        if ($hr == 1) {
            $hr_data = $this->_getDataList("hr", $hr_num);
            if (!empty($hr_data)) {
                if (empty($mapData)) {
                    $mapData = $hr_data;
                }
                else {
                    $mapData = array_merge($mapData, $hr_data);
                }
            }
        }

        if (!empty($mapData)) {
            $mapData = XHandle::sysSortArray($mapData, "addtime", "SORT_DESC");
            $result = $this->_create_sitemap($mapData);
            if (true === $result) {
                $response = 1;
                $counts = count($mapData);
            }
        }
        $msg = "共生成{$counts}条sitemap数据";
        echo json_encode(array("response"=>$response, "result"=>$msg));
    }

    private function _getDataList($mod, $num) {
        $dt = array();
        $m_sitemap = parent::model("sitemap", "am");
        $dt = $m_sitemap->getLastList($mod, $num);
        unset($m_sitemap);
        return $dt;
    }

    private function _create_sitemap($url_data) {
        $res = false;
        @include BASE_ROOT."source/core/util/class.sitemap.php";
        $this->sitemap = new SitemapGenerator(parent::$cfg['siteurl']);
        if (!empty($url_data)) {
            foreach ($url_data as $k=>$val) {
                
                $this->sitemap->addUrl($val['url'], date('c', $val['addtime']));
            }
            $this->sitemap->createSitemap();
            $this->sitemap->writeSitemap();
            $this->sitemap->updateRobots(); 
            $res = true;
        }
        unset($this->sitemap);
        return $res;
    }
         public function action_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }
}

?>
