<?php
// +----------------------------------------------------------------------
// ×î±ã½ÝµÄphp¿ò¼Ü
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.ky53.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.ky53.net )
// +----------------------------------------------------------------------

/**
 
 * @author   qq:974005652(zhx626@126.com) byÀÏÍçÍ¯
 * @version  1.0
 +------------------------------------------------------------------------------
 */
 
class Action {
   function __construct(){ 
   global $aindex,$conf;
          if(empty($this->cache)){
		  if(!empty($conf['memcache_on'])){		 
		  $this->cache=new Memcache();
		  $this->cache->connect($conf['memcache_host'], $conf['memcache_port']);
		  }else{
		   $this->cache=new Cache();
		   }
		  }
		 
		  if(!empty($conf['DEFAULT_M_PATH'])){
		  $modelclass=$aindex.'Model';
		  $this->model=new  $modelclass();
		  
		  }
		  if(!empty($conf['MYCLASS_PATH'])){
		  $mydatapath=APP_PATH.'/'.$conf['MYCLASS_PATH'].'/*';
		     foreach(glob($mydatapath) as $key){
			 require $key;
			 $myclasspatharr=explode('/',$key);
			 $myclasspatharrstr=$myclasspatharr[count($myclasspatharr)-1];
			 $myclassname= substr($myclasspatharrstr,0,strpos($myclasspatharrstr,'.'));
		     $this->$myclassname=new $myclassname();
		      }
	      }
		 
		  
     } 	
	 
	public function load($file){
	global $conf;
	 if(@file_exists(APP_PATH."/".$conf[DEFAULT_M_PATH]."/$file.class.php"))@require APP_PATH."/".$conf[DEFAULT_M_PATH]."/$file.class.php";
	 if(!@class_exists($file.'Model'))error(2,$file.'Model');
	 $filemodel='model_'.$file;
	 $filemodelclass=$file.'Model';
	 $this->$filemodel=new $filemodelclass();
	}
	public function display(){
	global $conf,$lable,$list,$datalist,$volist,$data;
	
	$file=getfunname(1);
	if(getfunname(2)){
	 $clas=getfunname(2);
	 }else{
	 $clas=getfunname(0);
	 }
	 
	
	$temp=new temp();
	
	$tpl=$conf['DEFAULT_TPL'];
	$data=$this->data;
	
	ob_start();	
	
	$temp->read(APP_PATH."/{$conf['DEFAULT_TPL_PATH']}/{$tpl}/".str_replace('Action','',$clas)."/".$file.".htm");
		
	$content=ob_get_contents();
	ob_end_clean();	
	$content=make_html($content,$conf['PATH_KEY']);
	
	
	 
	
	//ob_start();
		//ob_implicit_flush();
		//echo $conf[PATH_KEY];
		global $geturl;
		if($conf['PATH_KEY']==4){ 
		
		if(!file_exists($conf['DEFAULT_HTML_PATH']))mkdir($conf['DEFAULT_HTML_PATH']);
		if(!file_exists($conf['DEFAULT_HTML_PATH'].'/'.str_replace('Action','',$clas)))mkdir($conf['DEFAULT_HTML_PATH'].'/'.str_replace('Action','',$clas));
		$fstr=$conf['DEFAULT_HTML_PATH'].'/'.str_replace('Action','',$clas).'/'.$file.'';
		
		if(!file_exists($fstr))mkdir($fstr);
	   
	   
		if($geturl){
		 $urlarr=explode("/",$geturl);
		$fstr1=$conf['DEFAULT_HTML_PATH'].'/';
		 foreach($urlarr as $key){
		 if($key){
		
		$fstr1.=$key;		
		if(!file_exists($fstr1))mkdir($fstr1);
		$fstr1.='/';
		}
		}
		$fstr=$conf['DEFAULT_HTML_PATH'].$geturl;
		}
		
	$file_pointer = fopen($fstr.'/index.htm', 'w');	
	
	
		
     fwrite($file_pointer, $content); 
     fclose($file_pointer);
	 
	if($conf['URL_ROUTER_ON']==true){$this->jump(__ROOT__.'/'.$fstr.'/');}else{
	   if(!empty($conf['CACHE_ON'])){
	   $cache=$this->cache->get('cache_'.urlencode($geturl));
	   if(!$cache){$this->cache->set('cache_'.urlencode($geturl),$content);$cache=$content;}
	   }else{$cache=$content;}
	   echo $cache;}
	  }else{
	  if(!empty($conf['CACHE_ON'])){
	  $cache=$this->cache->get('cache_'.urlencode($geturl));
	  if(!$cache){$this->cache->set('cache_'.urlencode($geturl),$content);$cache=$content;}
	  }else{$cache=$content;}
	  echo $cache;
	  	
	}
	//flush();	
	}
	public function lable($name,$txt){
	global $lable;
	
	$lable[$name]=$txt;	
	
	}
	public function volist($name,$txt,$name1='',$txt1=''){
	global $volist,$list;
	
	$volist[$name]=$txt;	
	$list[$name1]=$txt1;
	}
	public function error($err,$url='')
	{
	 if($url){
	   $str="<script>alert('$err');window.location.href='$url';</script>";
	 }else{
	   $str="<script>alert('$err'); history.go(-1);</script>";
	 }
	 echo $str;
	}
	public function jump($url)
	{
	 $str="<script>window.location.href='$url';</script>";
	  echo $str;
	}
	public function datalist($name,$txt){
	global $datalist;
	
	$datalist[$name]=$txt;	
	
	}
	public function getform($name){
   $this->form=$name;
   $this->class=getfunname(0);
   
   }
	
}
?>