<?php
class data {
	private $obj;
	private $postion;
	private $title;
	private $keywords;
	private $description;
	private $channel;
	private $flink;
	private $namefield;
	function __construct($obj){
		$this->obj=$obj;
		$this->postion=array();
		$this->channel=new channelModel();
		$this->obj->assign("menu",$this->htmlRewrite($this->channel->getChannelList("where ishidden=0"),'menu'));
		$this->obj->assign("attchment_path", ATTCHMENT_PATH);
		$this->obj->assign("appPath", APP_PATH);
		$this->obj->assign("appName", APP_NAME);
		$this->obj->assign("stylePath", APP_STYLE);
		$this->obj->assign("url","http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
		$this->obj->register_function('elincms','data::get_index_show');
	}

	function index(){
		$this->flink_html();
		$this->obj->assign("keywords", APP_KEYWORDS);
		$this->obj->assign("description", APP_DESCRIPTION);
		$this->obj->assign("flink",$this->flink);
	}

	function channel($val){
		$model = $val['model'];
		$this->postion[]=array('channelid'=>$val['id'],'cateid'=>'','cnname'=>$val['cnname']);
		$this->obj->assign("channelname", $val['cnname']);
		$this->obj->assign("channelpic", $val['channelpic']);
		$this->title = $val['cnname']."-";
		$_REQUEST[channelid]=$val[id];
		if(!$model){
				//这里为出错处理，没有此栏目。
			}else{

				$html   = $this->main_content($val);
				$this->obj->assign("chid", $val[id]);
				$this->obj->assign("title", $this->title);
				$this->obj->assign("description", !empty($this->description) ? $this->description : APP_DESCRIPTION);
				$this->obj->assign("keywords", !empty($this->keywords) ? $this->keywords : APP_KEYWORDS);
				$this->obj->assign("postion", $this->htmlRewrite($this->postion,'postion'));

			}
		return $html.".html";
	}


	function main_content($val){
		$object = $val['model']."Model";
		$model  = $val['model'];
		$template = !empty($val['template']) ? $val['template'] : $val['model'];
		if($val['model']=='article' || $val['model']=='product' || $val['model']=='download'){
				$num = $val['model']=='article' || $val['model']=='download' ? ARTICLE_PAGE_SIZE : PRODUCT_PAGE_SIZE;
				$object_cate = new $object("_cate");
				$object		 = new $object();
				$cate  = $object_cate->get_cate(); //查找大类 参数ALL为全部分类
				if(!empty($cate)){				//这个为大类的第一子类
					foreach ( $cate as $k => $v){
						$cate[$k][sub]=$object_cate->get_cate($v[id]);
					}
				}
				$this->obj->assign('cate',$this->htmlRewrite($cate,'cate'));
				if(isset($_REQUEST[cateid])){
						$cate_info=$object_cate->find($_REQUEST[cateid]);
						if(!empty($cate_info[parentid])){
							$parent=$this->get_parent_postion($object_cate,$cate_info[parentid]);
							if($parent){
								krsort($parent);
								foreach($parent as $k=>$v){
									$this->postion[]=array('channelid'=>$v['channelid'],'cateid'=>$v['id'],'cnname'=>$v['catename']);
								}
							$this->postion[]=array('channelid'=>$cate_info['channelid'],'cateid'=>$cate_info['id'],'cnname'=>$cate_info['catename']);
							}
							if(count($parent)>=1){
								$son   =$this->get_son_postion($object_cate,$cate_info[id]);
								$this->obj->assign('soncate',$this->htmlRewrite($son,'cate'));
							}

						}else{
							$this->postion[]=array('channelid'=>$cate_info['channelid'],'cateid'=>$cate_info['id'],'cnname'=>$cate_info['catename']);
						}

						$this->title=$cate_info['catename']."-";
						$id = $object_cate->check_parentid($_REQUEST[cateid]);
						if(!empty($id)){
								$id  = implode(",",$id);
								$id .=",$_REQUEST[cateid]";
								$con = "cateid in (".$id.")";
								$where ="where $con and channelid=$_REQUEST[channelid]";
								$and = "and $con";
						}else{
								$con = "cateid = ".$_REQUEST[cateid]."";
								$where ="where $con and channelid=$_REQUEST[channelid]";
								$and = "and $con";
						}
				}else{
					$where ="where channelid=$val[id]";
				}


				if(isset($_REQUEST['id'])){
					$content = $object->find($_REQUEST['id']);
					$this->obj->assign("next",$this->htmlRewrite($object->find_next($_REQUEST['id']),'note'));
					$this->obj->assign("prev",$this->htmlRewrite($object->find_prev($_REQUEST['id']),'note'));
					if(!empty($content[bigpic])){
						$piclist=explode("|",$content[bigpic]);
						$picnum = count($piclist)>=3 ? 3 : count($piclist);
						$this->obj->assign("picnum",$picnum);
						$this->obj->assign("piclist",$piclist);
					}
					$this->postion[]=array('channelid'=>'','cateid'=>'','cnname'=>$content['title']);
					$this->title = $content['title']."-";
					$this->keywords=$content['keywords'];
					$template.="_info";
				}else{
						$total = count( $object->total($where));
						if(SEO_TYPE=='rewrite'){
							$this->page = new page($total,$num,'rewrite');
						}else{
							$this->page = new page($total,$num,'');
						}

						$content = $object->findAll("",$this->page->getOffset(),$num,"where channelid=$_REQUEST[channelid] $and ","id");
						if($content){
						$content=$this->clear_html($content);
						}
						$fpage =$this->htmlRewrite($this->page->fpage(),'page');
						$this->obj->assign("fpage",$fpage);

				}
				$this->obj->assign("content",$this->htmlRewrite($content,'content'));




		}else{
				$object = new $object();
				if(isset($_REQUEST[cateid])){
						$cate_info=$object->find($_REQUEST[cateid]);
						$this->namefield = $model."name";
						$this->postion[]=array('channelid'=>'','cateid'=>'','cnname'=>$cate_info[$this->namefield]);
						$this->title = $cate_info[$this->namefield]."-";
						$where ="where channelid=$_REQUEST[channelid] and id=$_REQUEST[cateid]";
				}else{
						$where ="where channelid=$_REQUEST[channelid]";
						$this->namefield = $model."name";
				}

				if($model == 'single' || $model == 'jobs'){
					$list=$object->findAll("", 0, 0,"where channelid=".$_REQUEST['channelid']."","id");
					$list=$this->htmlRewrite($list,'list');
					$content=$object->findAll("", 0,1,$where,"id");
					$this->obj->assign('content',$content[0]);
				}else{
						$total = count( $object->total($where));
						if(SEO_TYPE=='rewrite'){
							$this->page = new page($total,ARTICLE_PAGE_SIZE,'rewrite');
						}else{
							$this->page = new page($total,ARTICLE_PAGE_SIZE,'');
						}
						$list=$object->findAll("", $this->page->getOffset(), ARTICLE_PAGE_SIZE,$where,"id desc");
						$maxfloor= $this->page->getPageNum()==1 ? $total : ($total-($this->page->getPageNum()-1)*ARTICLE_PAGE_SIZE);
						$fpage =$this->page->fpage();
						$this->obj->assign("maxfloor",$maxfloor);
						$this->obj->assign("fpage",$this->htmlRewrite($fpage,'page'));
						$this->obj->assign("channelid",$_REQUEST['channelid']);
				}
				$this->obj->assign('list',$list);
		}
		return $template;

	}



		function get_parent_postion($object,$p){
			global $data;
			$cate=$object->find($p);
			$data[]=array('id'=>$cate[id],'channelid'=>$cate[channelid],'catename'=>$cate[catename]);
			if(!empty($cate[parentid])){
				$this->get_parent_postion($object,$cate[parentid]);
			}
			return $data;
		}
		function get_son_postion($object,$id){
			return $cate=$object->get_cate($id);

		}


		function clear_html($content){
				foreach($content as $key=>$val){

					$str=strip_tags($val[content]);

					$content[$key][content]=$this->replace($str);
				}
				return $content;

		}

		function replace($str){
			$str=str_replace("&quot;",'"',$str);
			$str=str_replace("&gt;",'>',$str);
			$str=str_replace("&lt;",'<',$str);
			$str=str_replace("\r\n",'',$str);
			$str=str_replace("\n",'',$str);
			return $str;
		}




	 function get_index_show ( $params ){
		extract($params);
		$object = $model."Model";
		$object	= new $object();
		$content = $object->findAll("",0,$num,"where channelid=$channelid","$sort , id");
		foreach($content as $key =>$val){
			if(SEO_TYPE=='rewrite'){
				$content[$key]['uri']=APP_PATH.'channel-'.$val['channelid'].'/info-'.$val['id'].".html";
			}elseif(SEO_TYPE=='active'){
				$content[$key]['uri']="index.php?a=channel&channelid=".$val['id']."";
			}

		}
		$this->assign("$model"._."$channelid",$content);
	}


		function flink_html(){
			$link_html=array();
			$object = new flinkModel();
			$link   = $object->findAll("", 0, 0,"","id");
			if(!empty($link)){
				foreach($link as $val){
					if($val[display]==0){
						$link_html[name].="<span><a href='".$val[webUrl]."'>".$val[webName]."</a></span>";
					}else{
						$link_html[logo]="<span><a href='".$val[webUrl]."'><img src = ".$val[webLogo]." border='0' width='88' height='31'/></a></span>";
					}

				}
			}
			$this->flink= $link_html;
		}


		//=================htmlRewrite函数=====================
		function htmlRewrite($arr,$type){

				switch ($type){
					case 'menu':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								foreach($arr as $key =>$val){
									$menu[$key]['cnname']=$val['cnname'];
									$menu[$key]['id']=$val['id'];
									$menu[$key]['uri']   =APP_PATH.'channel-'.$val['id']."/";
								}
							}else{
								foreach($arr as $key =>$val){
									$menu[$key]['cnname']=$val['cnname'];
									$menu[$key]['id']=$val['id'];
									$menu[$key]['uri']   ="index.php?a=channel&channelid=$val[id]";
								}
							}
						}else{
							//静态路径
						}
					}
					return $menu;
					break;
					case 'cate':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								foreach($arr as $key =>$val){
									$cate[$key]['catename']=$val['catename'];
									$cate[$key]['id']=$val['id'];
									$cate[$key]['sub']=!empty($val['sub']) ? $this->htmlRewrite($val['sub'],'cate') : "";
									$cate[$key]['uri']   =APP_PATH.'channel-'.$val['channelid'].'-'.$val['id']."/";
								}

							}else{
								foreach($arr as $key =>$val){
									$cate[$key]['catename']=$val['catename'];
									$cate[$key]['id']=$val['id'];
									$cate[$key]['sub']=!empty($val['sub']) ? $this->htmlRewrite($val['sub'],'cate') : "";
									$cate[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."&cateid=".$val['id']."";
								}

							}

						}else{
							//静态路径
						}

					}
					return $cate;
					break;
					case 'postion':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								foreach($arr as $key =>$val){
									$postion[$key]['catename']=$val['cnname'];
									if(!empty($val['channelid']) && !empty($val['cateid'])){
										$postion[$key]['uri']   =APP_PATH.'channel-'.$val['channelid'].'-'.$val['cateid']."/";
									}elseif(!empty($val['channelid'])){
										$postion[$key]['uri']   =APP_PATH.'channel-'.$val['channelid']."/";
									}else{
										$postion[$key]['uri']   ="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
									}

								}

							}else{
								foreach($arr as $key =>$val){
									$postion[$key]['catename']=$val['cnname'];
									if(!empty($val['channelid']) && !empty($val['cateid'])){
										$postion[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."&cateid=".$val['cateid']."";
									}elseif(!empty($val['channelid'])){
										$postion[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."";
									}else{
										$postion[$key]['uri']   ="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
									}

								}


							}

						}else{
							//静态路径
						}

					}
					return $postion;
					break;
					case 'list':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								foreach($arr as $key =>$val){
									$list[$key][$this->namefield]=$val[$this->namefield];
									if(!empty($val['channelid']) && !empty($val['id'])){
										$list[$key]['uri']   =APP_PATH.'channel-'.$val['channelid'].'-'.$val['id']."/";
									}elseif(!empty($val['channelid'])){
										$list[$key]['uri']   =APP_PATH.'channel-'.$val['channelid']."/";
									}else{
										$list[$key]['uri']   ="";
									}

								}

							}else{
								foreach($arr as $key =>$val){
									$list[$key][$this->namefield]=$val[$this->namefield];
									if(!empty($val['channelid']) && !empty($val['id'])){
										$list[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."&cateid=".$val['id']."";
									}elseif(!empty($val['channelid'])){
										$list[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."";
									}else{
										$list[$key]['uri']   ="";
									}

								}

							}

						}else{
							//静态路径
						}

					}
					return $list;
					break;
					case 'page':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								$page=preg_replace("/\&page=([1-9]*)/","page-$1.html",$arr);

							}else{
								$page=$arr;
							}

						}else{
							//静态路径
						}

					}
					return $page;
					break;
					case 'content':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								if(count($arr)==count($arr,1)){
										$content=$arr;
										if($content['fileurl']){
											$content['fileurl']=preg_match("/^http(.*)/",trim($arr['fileurl'])) ? $arr['fileurl'] : ATTCHMENT_PATH.$arr['fileurl'];
										}
								}else{
									foreach($arr as $key =>$val){
										if($val['fileurl']){
											$content[$key]['fileurl']=preg_match("/^http(.*)/",trim($val['fileurl'])) ? $val['fileurl'] : ATTCHMENT_PATH.$val['fileurl'];
										}
										$content[$key]['title']=$val['title'];
										$content[$key]['id']=$val['id'];
										$content[$key]['content']=$val['content'];
										$content[$key]['author']=$val['author'];
										$content[$key]['publish']=$val['publish'];
										$content[$key]['click']=$val['click'];
										$content[$key]['spic']=$val['spic'];
										$content[$key]['uri']   =APP_PATH.'channel-'.$val['channelid'].'/info-'.$val['id'].".html";
									}
								}

							}else{
								if(count($arr)==count($arr,1)){
										$content=$arr;
										if($content['fileurl']){
											$content['fileurl']=preg_match("/^http(.*)/",trim($arr['fileurl'])) ? $arr['fileurl'] : ATTCHMENT_PATH.$arr['fileurl'];
										}
								}else{
									foreach($arr as $key =>$val){
										if($val['fileurl']){
											$content[$key]['fileurl']=preg_match("/^http(.*)/",trim($val['fileurl'])) ? $val['fileurl'] : ATTCHMENT_PATH.$val['fileurl'];
										}
										$content[$key]['title']=$val['title'];
										$content[$key]['id']=$val['id'];
										$content[$key]['content']=$val['content'];
										$content[$key]['author']=$val['author'];
										$content[$key]['publish']=$val['publish'];
										$content[$key]['click']=$val['click'];
										$content[$key]['spic']=$val['spic'];
										$content[$key]['uri']   ="index.php?a=channel&channelid=".$val['channelid']."&id=".$val['id']."";
									}
								}

							}

						}else{
							//静态路径
						}


					}
					return $content;
					break;
					case 'note':
					{
						$rewrite = SEO_TYPE == 'rewrite' ? 1 : 0;
						if(SEO_TYPE!='html'){
							if($rewrite){
								$note['title']=$arr['title'];
								$note['uri']=APP_PATH.'channel-'.$arr['channelid'].'/info-'.$arr['id'].".html";
							}else{
								$note['title']=$arr['title'];
								$note['uri']="index.php?a=channel&channelid=".$arr['channelid']."&id=".$arr['id']."";
							}

						}else{
							//静态路径
						}

					}
					return $note;
					break;


				}
		}




}
?>
