<?php
class Model_html_Controller extends Base_Controller {
	function __construct(){
		parent::__construct();
		$this->html		=	new Html_Model();
		$this->channel	=	new Model_Channel_Model();
		$this->content	=	new Model_content_Model();
		$this->modelhtml=	new Model_html_Model();
		$this->cat		=	new Model_category_Model();
	}
	//更新全站
	function site(){
		if(@$this->input->post()){
			$index_template	=	$this->input->post("index_template");
			$db	=	new Database();
			$db->update("basecfg",array("index_template"=>$index_template));
			//更新主页
			$this->lmtpl->save($index_template,"index".site_html_suffix);
			//更新站点
		}
	}
	function index(){
		if(@$this->input->post()){
			$index_template	=	$this->input->post("index_template");
			$db	=	new Database();
			$db->update("basecfg",array("index_template"=>$index_template));
			$this->lmtpl->content->title			=	SITE_TITLE;
			$this->lmtpl->content->keywords			=	SITE_KEYWORDS;
			$this->lmtpl->content->description		=	SITE_DESCRIPTION;
			$this->lmtpl->save($index_template,"index".SITE_HTML_SUFFIX);
			$this->main->message="更新首页成功";
			$this->main->message_type="success";
			unset($_POST);
			self::index();
		}
		else{
			$this->view	=	new View("model_html/index");
			$this->main->formAction='model_html/index';
			$this->main->is_validator=true;
			$this->display();
		}
	}
	function category($_current_html_number=0,$html_sum_file=false,$max_html_number=false){
		//分类列表
		$current_html_number=	$_current_html_number;
		if($this->input->post()||$_current_html_number!=0){
			$cats				=	$this->input->post("cats");
			$posthtml			=	$this->input->post('html');
			$catd				=	$this->input->post("catd");
			if(is_array($cats)){
				foreach ($cats as $channel_id=>$tmpCat){
					foreach ($tmpCat as $tmpcatid){
						$resCat[$channel_id][]	=	array(
						"cat_id"			=>	$tmpcatid['cat_id'],
						"cat_index_tpl"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_index_tpl'],
						"cat_list_tpl"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_list_tpl'],
						"cat_preview_tpl"	=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_preview_tpl'],
						"cat_dir"			=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_dir'],
						"cat_name"			=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_name'],
						"cat_keywords"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_keywords'],
						"description"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['description']
						);
					}
				}
			}
			if(!$cats){
				$cats	=	$this->cat->allCats(false);
			}
			else{
				$cats	=	$resCat;
			}
			//流水号记录
			$i=0;
			$t=0;
			//每次生成最大文件量
			if($max_html_number===false){		$max_html_number	=	(int)$posthtml['max_numbers'];		}
			//计算生成文件总数
			if($html_sum_file==false){
				$html_sum_file		=	0;
				foreach ($cats as $channel_id=>$catids){
					$channel		=	$this->channel->getchannel($channel_id);	//站点数据
					$html_sum_file++;
					foreach($catids as $catid){
						$catobj			=	(object)$catid;	//分类数据
						if(!file_exists($catobj->cat_list_tpl)){
							$catobj->cat_list_tpl	=	$channel->channel_list_tpl;
						}
						$this->lmtpl->defineload($catobj->cat_list_tpl);
						$counts		=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>$catid['cat_id'],"page"=>false,"showline"=>false))->count();
						//计算分页
						$pagesize	=	$this->lmtpl->pagesize?$this->lmtpl->pagesize:30;
						$sumPage	=	$counts%$pagesize>=1?intval($counts/$pagesize)+1:intval($counts/$pagesize);
						//文件总数
						$html_sum_file	+=	$sumPage;
					}
				}
			}
			//计算生成文件总数结束。
			foreach ($cats as $channel_id=>$catids){
				$channel				=	$this->channel->getchannel($channel_id);	//站点数据
				$GLOBALS['channel_id']	=	$channel_id;
				//更新站点
				$this->lmtpl->content->title		=	$channel->channel_name;
				$this->lmtpl->content->keywords		=	$channel->channel_seo_keywords;
				$this->lmtpl->content->description	=	"";
				$this->lmtpl->channel_id			=	$channel_id;
				//封面模板为真时
				if(file_exists($channel->channel_index_tpl)){
					$this->lmtpl->save($channel->channel_index_tpl,$this->html->url($channel_id,0,0,0,0,"dir"));
				}
				else{
					if(file_exists($channel->channel_list_tpl)){
						$counts		=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>false,"page"=>false,"showline"=>false,"keywords"=>false))->count();
						$pagesize	=	$this->lmtpl->pagesize?$this->lmtpl->pagesize:30;
						$sumPage	=	($counts%$pagesize)>=1?(intval($counts/$pagesize)+1):intval($counts/$pagesize);
						$GLOBALS['dir']			=	$channel->channel_html_dir;
						$GLOBALS['sum']			=	$counts;
						$GLOBALS['pagesize']	=	$pagesize;
						$this->lmtpl->content->title		=	$channel->channel_name;
						$this->lmtpl->content->keywords		=	$channel->channel_seo_keywords;
						$this->lmtpl->content->description	=	@$channel->description;
						for($page=1;$page<=$sumPage;$page++){
							$GLOBALS['page']		=	$page;
							$savepath				=	$this->html->url($channel_id,0,0,$page,0,"dir");
							if($this->lmtpl->save($channel->channel_list_tpl,$savepath)){
								if(basename($savepath)=="index".SITE_HTML_SUFFIX){
									copy($savepath,dirname($savepath)."/list_1".SITE_HTML_SUFFIX);
								}
							}
						}
					}
				}
				$i++;
				foreach($catids as $catid){
					$catobj			=	(object)$catid;	//分类数据
					$this->lmtpl->content->title		=	$catobj->cat_name;
					$this->lmtpl->content->keywords		=	$catobj->cat_keywords;
					$this->lmtpl->content->description	=	$catobj->description;
					if(!file_exists($catobj->cat_list_tpl)){
						$catobj->cat_list_tpl	=	$channel->channel_list_tpl;
					}
					//计算分页
					$this->lmtpl->defineload($catobj->cat_list_tpl);
					$this->lmtpl->pagesize;
					$counts		=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>$catid['cat_id'],"keywords"=>false,"page"=>false,"showline"=>false))->count();
					$pagesize	=	$this->lmtpl->pagesize?$this->lmtpl->pagesize:30;
					$sumPage	=	$counts%$pagesize>=1?intval($counts/$pagesize)+1:intval($counts/$pagesize);
					$GLOBALS['dir']			=	$catid['cat_dir'];
					$GLOBALS['sum']			=	$counts;
					$GLOBALS['pagesize']	=	$pagesize;
					for($page=1;$page<=$sumPage;$page++){
						$i++;
						if($i<=$_current_html_number){	continue;	}
						$t++;
						$current_html_number++;
						$GLOBALS['page']		=	$page;
						$GLOBALS['catid']		=	$catid['cat_id'];
						if($this->lmtpl->save($catobj->cat_list_tpl,$this->html->url($channel_id,$catid['cat_id'],0,$page,0,"dir"))){
							if(basename($this->html->url($channel_id,$catid['cat_id'],0,$page,0,"dir"))=="index".SITE_HTML_SUFFIX){
								copy($this->html->url($channel_id,$catid['cat_id'],0,$page,0,"dir"),dirname($this->html->url($channel_id,$catid['cat_id'],0,$page,0,"dir"))."/list_1".SITE_HTML_SUFFIX);
							}
							if($t>=$max_html_number&&$current_html_number<$html_sum_file){
								$result['js']="publish_category_ajax($current_html_number,$html_sum_file,$max_html_number);";
								die(json_encode($result));
							}
						}
						else{
							$result['js']="alert('生成文件－".$this->html->url($channel_id,$catid['cat_id'],0,$page,0,"dir")."失败');";
							die(json_encode($result));
						}
					}
					unset($GLOBALS['dir']);
					unset($GLOBALS['sum']);
					unset($GLOBALS['catid']);
					unset($GLOBALS['pagesize']);
				}
			}
			$result['js']="publish_category_complete($current_html_number,$html_sum_file,$max_html_number)";
			die(json_encode($result));
		}
		else{
			$this->view			=	new View("model_html/category");

			$this->main->formAction="model_html/category";
			$this->display();
		}
	}
	function allcat(){
		$this->view			=	new View("model_html/allcats");
		$this->view->channels=	$this->modelhtml->getChannels();
		$this->view->treeCats=	$this->cat->allCats();
		$this->view->render(true);
	}
	function content($_current_html_number=0,$html_sum_file=false,$max_html_number=false){
		//分类列表
		$current_html_number=	$_current_html_number;
		if($this->input->post()||$_current_html_number!=0){
			$cats				=	$this->input->post("cats");
			$posthtml			=	$this->input->post('html');
			$catd				=	$this->input->post("catd");
			if(is_array($cats)){
				foreach ($cats as $channel_id=>$tmpCat){
					foreach ($tmpCat as $tmpcatid){
						$resCat[$channel_id][]	=	array(
						"cat_id"			=>	$tmpcatid['cat_id'],
						"cat_index_tpl"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_index_tpl'],
						"cat_list_tpl"		=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_list_tpl'],
						"cat_preview_tpl"	=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_preview_tpl'],
						"cat_dir"			=>	$catd[$channel_id][$tmpcatid['cat_id']]['cat_dir'],
						);
					}
				}
			}
			if(!$cats){	$cats	=	$this->cat->allCats(false);}
			else{	$cats	=	$resCat;	}
			//流水号记录
			$i=0;	$t=0;
			//每次生成最大文件量
			if($max_html_number===false){		$max_html_number	=	(int)$posthtml['max_numbers'];		}
			//计算生成文件总数
			if($html_sum_file==false){
				$html_sum_file		=	0;
				foreach ($cats as $channel_id=>$catids){
					$channel		=	$this->channel->getchannel($channel_id);	//站点数据
					/**
					*当只设定了站点列表模板
					*多用于单页面类文档
					*/
					if($channel->channel_list_tpl!=''&&!is_array($catids)){
						$this->UpdateNoneCategoryContent($channel_id);
					}
					foreach($catids as $catid){
						$catobj			=	(object)$catid;	//分类数据
						if(!file_exists($catobj->cat_preview_tpl)){
							$catobj->cat_preview_tpl	=	$channel->channel_content_tpl;
						}
						$sumPage	=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>$catid['cat_id'],"keywords"=>false,"page"=>false,"showline"=>false))->count();
						//文件总数
						$html_sum_file	+=	$sumPage;
					}
				}
			}
			//计算生成文件总数结束。
			foreach ($cats as $channel_id=>$catids){
				$channel				=	$this->channel->getchannel($channel_id);	//站点数据
				$GLOBALS['channel_id']	=	$channel_id;
				$this->lmtpl->channel_id=	$channel_id;
				//更新站点
				foreach($catids as $catid){
					$catobj			=	(object)$catid;	//分类数据
					if(!file_exists($catobj->cat_preview_tpl)){
						$catobj->cat_preview_tpl	=	$channel->channel_content_tpl;
					}
					//计算分页
					$selectR				=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>$catid['cat_id'],"page"=>false,"showline"=>false));
					$sumPage				=	$selectR->count();
					$cons					=	$selectR->result_array();
					$cons					=	$this->content->htmlcontent($cons);
					$GLOBALS['catid']		=	$catid['cat_id'];
					for($page=0;$page<$sumPage;$page++){
						$i++;
						$this->lmtpl->content			=	$cons[$page];	//取得当前id内容
						$this->lmtpl->content->keywords	=	$this->lmtpl->content->tags;
						$GLOBALS['content_id']			=	$this->lmtpl->content->content_id;
						if($i<=$_current_html_number){	continue;	}
						$this->lmtpl->defineload($catobj->cat_preview_tpl);
						if($this->lmtpl->pagesize){
							$counts		=	$this->content->getannexs($channel->model_value,$this->lmtpl->content->content_id)->count();
							$pagesize	=	$this->lmtpl->pagesize;
							$annexPages	=	$counts%$pagesize>=1?intval($counts/$pagesize)+1:intval($counts/$pagesize);
							$GLOBALS['sum']			=	$counts;
							$GLOBALS['pagesize']	=	$pagesize;
							$GLOBALS['dir']			=	$this->lmtpl->content->cat_dir;
							$GLOBALS['uri_segment']	=	$this->lmtpl->content->content_id."_";
							for($a=1;$a<=$annexPages;$a++){
								$GLOBALS['page']		=	$a;
								$contenturl				=	$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,$a,"dir");
								$this->lmtpl->save($catobj->cat_preview_tpl,$contenturl);
								if($a==1){
									$url	=	$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,0,"dir");
									@unlink($url);
									@copy($contenturl,$url);
								}
							}
						}
						else{
							$this->lmtpl->save($catobj->cat_preview_tpl,$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,0,"dir"));
						}
						$current_html_number++;
						$t++;
						if($t>=$max_html_number&&$current_html_number<=$html_sum_file){
							$result['js']	=	"publish_content_ajax($current_html_number,$html_sum_file,$max_html_number);";
							die(json_encode($result));
						}
						elseif($current_html_number>=$html_sum_file){
							$result['js']	=	"publish_content_complete($current_html_number,$html_sum_file,$max_html_number)";
							die(json_encode($result));
						}
					}
					unset($GLOBALS['dir']);
					unset($GLOBALS['catid']);
				}
			}
			$result['js']	=	"publish_content_complete($current_html_number,$html_sum_file,$max_html_number)";
			die(json_encode($result));
		}
		else{
			$this->view			=	new View("model_html/content");
			$this->main->formAction="model_html/content";
			$this->display();
		}
	}
	function updateIndexHtml(){
		$this->lmtpl->content->title		=	SITE_TITLE;
		$this->lmtpl->content->keywords		=	SITE_KEYWORDS;
		$this->lmtpl->content->description	=	SITE_DESCRIPTION;
		return $this->lmtpl->save(INDEX_TEMPLATE,"index".SITE_HTML_SUFFIX);
	}
	function updateCategoryHtml($channel_id,$cat_id){
		$this->lmtpl->pagesize		=	false;
		$channel					=	$this->channel->getchannel($channel_id);	//站点数据
		$GLOBALS['channel_id']		=	$channel_id;
		//更新站点
		$this->lmtpl->content->title		=	$channel->channel_name;
		$this->lmtpl->content->keywords		=	$channel->channel_seo_keywords;
		$this->lmtpl->content->description	=	"";
		//封面模板为真时
		if(file_exists($channel->channel_index_tpl)){
			$this->lmtpl->save($channel->channel_index_tpl,$this->html->url($channel_id,0,0,0,0,"dir"));
		}
		else{
			if(file_exists($channel->channel_list_tpl)){
				$counts		=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>false,"page"=>false,"showline"=>false,"keywords"=>false))->count();
				$pagesize	=	$this->lmtpl->pagesize?$this->lmtpl->pagesize:30;
				$sumPage	=	($counts%$pagesize)>=1?(intval($counts/$pagesize)+1):intval($counts/$pagesize);
				$GLOBALS['dir']			=	$channel->channel_html_dir;
				$GLOBALS['sum']			=	$counts;
				$GLOBALS['pagesize']	=	$pagesize;
				$this->lmtpl->channel_id			=	$channel_id;
				$this->lmtpl->content->title		=	$channel->channel_name;
				$this->lmtpl->content->keywords		=	$channel->channel_seo_keywords;
				$this->lmtpl->content->description	=	@$channel->description;
				for($page=1;$page<=$sumPage;$page++){
					$GLOBALS['page']		=	$page;
					$savepath				=	$this->html->url($channel_id,0,0,$page,0,"dir");
					if($this->lmtpl->save($channel->channel_list_tpl,$savepath)){
						if(basename($savepath)=="index".SITE_HTML_SUFFIX){
							copy($savepath,dirname($savepath)."/list_1".SITE_HTML_SUFFIX);
						}
					}
				}
				if(!$cat_id){	return false;	}
			}
		}
		$catobj			=	$this->cat->get_category_id($channel->model_value,$cat_id);	//分类数据
		if(!file_exists($catobj->cat_list_tpl)){
			$catobj->cat_list_tpl	=	$channel->channel_list_tpl;
		}
		//计算分页
		$this->lmtpl->defineload($catobj->cat_list_tpl);
		$this->lmtpl->pagesize;
		$counts		=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>$catobj->cat_id,"page"=>false,"showline"=>false,"keywords"=>false))->count();
		$pagesize	=	$this->lmtpl->pagesize?$this->lmtpl->pagesize:30;
		$sumPage	=	($counts%$pagesize)>=1?(intval($counts/$pagesize)+1):intval($counts/$pagesize);
		$GLOBALS['dir']			=	$catobj->cat_dir;
		$GLOBALS['sum']			=	$counts;
		$GLOBALS['pagesize']	=	$pagesize;
		$this->lmtpl->channel_id			=	$channel_id;
		$this->lmtpl->content->title		=	$catobj->cat_name;
		$this->lmtpl->content->keywords		=	$catobj->cat_keywords;
		$this->lmtpl->content->description	=	$catobj->description;
		for($page=1;$page<=$sumPage;$page++){
			$GLOBALS['page']		=	$page;
			$GLOBALS['catid']		=	$catobj->cat_id;
			if($this->lmtpl->save($catobj->cat_list_tpl,$this->html->url($channel_id,$catobj->cat_id,0,$page,0,"dir"))){
				if(basename($this->html->url($channel_id,$catobj->cat_id,0,$page,0,"dir"))=="index".SITE_HTML_SUFFIX){
					copy($this->html->url($channel_id,$catobj->cat_id,0,$page,0,"dir"),dirname($this->html->url($channel_id,$catobj->cat_id,0,$page,0,"dir"))."/list_1".SITE_HTML_SUFFIX);
				}
			}
		}
		unset($GLOBALS['dir']);
		unset($GLOBALS['sum']);
		unset($GLOBALS['pagesize']);
	}
	function updateContentHtml($channel_id,$cat_id,$content_id){
		$this->lmtpl->pagesize		=	false;
		$this->lmtpl->channel_id	=	$channel_id;
		if($cat_id>0){
			$this->UpdateCategoryContent($channel_id,$cat_id,$content_id);
		}
		else{
			$this->UpdateNoneCategoryContent($channel_id,$content_id);
		}
	}
	function UpdateCategoryContent($channel_id,$cat_id,$content_id){
		//计算生成文件总数结束。
		$channel				=	$this->channel->getchannel($channel_id);	//站点数据
		$GLOBALS['channel_id']	=	$channel_id;
		//更新站点
		$catobj					=	$this->cat->get_category_id($channel->model_value,$cat_id);	//分类数据
		if(!file_exists($catobj->cat_preview_tpl)){
			$catobj->cat_preview_tpl	=	$channel->channel_content_tpl;
		}
		//计算分页
		$cons[]					=	$this->html->conPrev($channel->model_value,$content_id,$channel_id);
		$cons[]					=	$this->html->conCurr($channel->model_value,$content_id,$channel_id);
		$cons[]					=	$this->html->conNext($channel->model_value,$content_id,$channel_id);
		$GLOBALS['dir']			=	$catobj->cat_dir;
		$GLOBALS['catid']		=	$catobj->cat_id;
		for($page=0;$page<count($cons);$page++){
			if(!is_object($cons[$page])){	continue;	}
			$this->lmtpl->channel_id		=	$channel_id;
			$this->lmtpl->content			=	$cons[$page];	//取得当前id内容
			$this->lmtpl->content->keywords	=	$this->lmtpl->content->tags;
			$GLOBALS['content_id']			=	$this->lmtpl->content->content_id;
			$this->lmtpl->defineload($catobj->cat_preview_tpl);
			if($this->lmtpl->pagesize){
				$counts		=	$this->content->getannexs($channel->model_value,$this->lmtpl->content->content_id)->count();
				$pagesize	=	$this->lmtpl->pagesize;
				$annexPages	=	$counts%$pagesize>=1?intval($counts/$pagesize)+1:intval($counts/$pagesize);
				$GLOBALS['sum']			=	$counts;
				$GLOBALS['pagesize']	=	$pagesize;
				$GLOBALS['dir']			=	$this->lmtpl->content->cat_dir;
				$GLOBALS['uri_segment']	=	$this->lmtpl->content->content_id."_";
				for($a=1;$a<=$annexPages;$a++){
					$GLOBALS['page']		=	$a;
					$contenturl				=	$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,$a,"dir");
					
					$this->lmtpl->save($catobj->cat_preview_tpl,$contenturl);
					if($a==1){
						$url	=	$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,0,"dir");
						@unlink($url);
						@copy($contenturl,$url);
					}
				}
			}
			else{
				$this->lmtpl->save($catobj->cat_preview_tpl,$this->html->url($channel_id,$this->lmtpl->content->cat_id,$this->lmtpl->content->content_id,0,0,"dir"));
			}
		}
		unset($GLOBALS['dir']);
		unset($GLOBALS['catid']);
	}
	//更新无分类的内容
	function UpdateNoneCategoryContent($channel_id,$content_id=0){
		//计算生成文件总数结束。
		$channel				=	$this->channel->getchannel($channel_id);	//站点数据
		$GLOBALS['channel_id']	=	$channel_id;
		$Tpl					=	$channel->channel_content_tpl;
		if($content_id>0){
			$cons[]					=	$this->html->conPrev($channel->model_value,$content_id,$channel_id);
			$cons[]					=	$this->html->conCurr($channel->model_value,$content_id,$channel_id);
			$cons[]					=	$this->html->conNext($channel->model_value,$content_id,$channel_id);
		}
		else{
			$cons					=	$this->content->get_content_list(array("channel_id"=>$channel_id,"model_value"=>$channel->model_value,"category_id"=>0,"keywords"=>false,"page"=>false,"showline"=>false));
		}
	

		$GLOBALS['dir']			=	$channel->channel_html_dir;
		for($page=0;$page<count($cons);$page++){
			if(!is_object($cons[$page])){	continue;	}
			$this->lmtpl->content			=	$cons[$page];	//取得当前id内容
			$this->lmtpl->content->keywords	=	$this->lmtpl->content->tags;
			$this->lmtpl->channel_id		=	$channel_id;
			$GLOBALS['content_id']			=	$this->lmtpl->content->content_id;
			$this->lmtpl->defineload($Tpl);
			//内容模板出现分页时
			if($this->lmtpl->pagesize){
				$counts		=	$this->content->getannexs($channel->model_value,$this->lmtpl->content->content_id)->count();
				$pagesize	=	$this->lmtpl->pagesize;
				$annexPages	=	$counts%$pagesize>=1?intval($counts/$pagesize)+1:intval($counts/$pagesize);
				$GLOBALS['sum']			=	$counts;
				$GLOBALS['pagesize']	=	$pagesize;
				$GLOBALS['dir']			=	$channel->channel_html_dir;
				$GLOBALS['uri_segment']	=	$this->lmtpl->content->content_id."_";
				for($a=1;$a<=$annexPages;$a++){
					$GLOBALS['page']		=	$a;
					$contenturl				=	$this->html->url($channel_id,0,$this->lmtpl->content->content_id,0,$a,"dir");
					
					$this->lmtpl->save($catobj->cat_preview_tpl,$contenturl);
					if($a==1){
						$url	=	$this->html->url($channel_id,0,$this->lmtpl->content->content_id,0,0,"dir");
						@unlink($url);
						@copy($contenturl,$url);
					}
				}
			}
			else{
				$this->lmtpl->save($Tpl,$this->html->url($channel_id,0,$this->lmtpl->content->content_id,0,0,"dir"));
			}
		}
		unset($GLOBALS['dir']);
	}
}