<?php
if (!defined('ROOT')) exit('Can\'t Access !'); 
abstract class module_class{
	
    function __construct($module,$action){
		$this->module = $module;
		$this->action = $action;
		$this->mysql = new mysql_class();
		$this->pre = config::get('tablepre');
		$this->pagesize = $this->module=='admin' ? config::get("adminpagesize") : config::get("pagesize");
		$this->purviews = purviews();
		$this->execute();
        $this->checkuser();
    }
	
	function checkuser(){
		$this->user = new user_class($this);
		$this->member = $this->user->checkuser();
		if(is_array($this->member)){
		  if($this->user->purview()) $this->user->admin();
		}
		return true;
	}
	
	function execute(){//所有页面执行前执行的
		$this->mysql->update("{$this->pre}payorder",array('checked'=>2),"addtime<=".(time()-172800)." and checked=0");
		$this->topabout = $this->mysql->getarr("select * from {$this->pre}about order by id desc limit 0,5");
	    $query = $this->mysql->query("select * from {$this->pre}shopcart where uid='{$this->member['uid']}' order by id desc");
	    while($rs=$this->mysql->assoc($query)){
		   $shop = $this->mysql->select_one("select * from {$this->pre}shop where id='{$rs[shopid]}'");
           $this->moneyall+=$shop['money'];
		   $this->carts++;
	    }
		$this->moneyall = $this->moneyall ? $this->moneyall : 0;
		$this->carts = $this->carts ? $this->carts : 0;
	    return true;	
	}
	
	
	function getnews($classid,$num,$length='',$time=''){
		$where = $classid=='0' ? "" : "typeid='$classid'";
	    $query = $this->mysql->query("select * from {$this->pre}news where $where order by addtime desc limit 0,$num");
	    while($rs=$this->mysql->assoc($query)){
		    $rs['url'] = rewrite::request("?mod=news&act=show&id=".$rs['id']);
		    $rs['addtime'] = formattime($rs['addtime'],$time);
		    $rs['title'] = $length=='' ? $rs['title'] : msubstr($rs['title'],0,$length);
		    $news[] = $rs;
	    }
		return $news;
	}
	
	function getshoptype($num){
	    $query = $this->mysql->query("select * from {$this->pre}shoptype order by typeorder asc limit 0,$num");
	    while($rs=$this->mysql->assoc($query)){
			$rs['url'] = rewrite::request("?mod=shop&act=main&typeid=".$rs['id']);
		    $type[] = $rs;
	    }
		return $type;
	}
	
	function getshop($checked,$num,$typeid='0'){
		$where = "where id>0";
		if($checked) $where .= " and checked='$checked'";
		if($typeid) $where .= " and typeid='$typeid'";
	    $query = $this->mysql->query("select * from {$this->pre}shop $where order by id desc limit 0,$num");
	    while($rs=$this->mysql->assoc($query)){
		    $rs['addtime'] = formattime($rs['addtime']);
            $pics = array_filter(explode('|',$rs['pics']));
			$rs['pic'] = $pics[0];
			$rs['url'] = rewrite::request("?mod=shop&act=show&id=".$rs['id']);
		    $shop[] = $rs;
	    }
		return $shop;
	}
	
	function up_money($uid,$money,$action,$message,$type=''){
	   $user = $this->user->sql($uid);
	   $money = formatnum($money);
	   if(is_array($user)&&$money>0){
	     $arr['money'] = $action=='+' ? $user['money']+$money : $user['money']-$money;
         $this->mysql->update("{$this->pre}user",$arr,"uid='{$user[uid]}'");
	     $log['uid'] = $user['uid'];
	     $log['addtime'] = time();
	     $log['content'] = $message;
	     $log['lognum'] = $action.$money;
         $this->mysql->insert("{$this->pre}log",$log);
		 if($type){
		       $reslog['uid'] = $uid;
		       $reslog['addtime'] = time();
		       $reslog['content'] = $message;
		       $reslog['lognum'] = $money;
			   $reslog['type'] = $type;
               $this->mysql->insert("{$this->pre}reslog",$reslog);
		 }
		 
	   }
	   return $id;
	}
	
	function givemoney($uid){
	   $twotime = untime(formattime(time(),'Y-m-d 00:00'));
	   $onetime = $twotime-(3600*24);
	   $moneytime = $twotime+(3600*24);
	   $time = formattime(time(),'Ymd');
	   $user = $this->user->sql($uid);
	   $restemp = $this->mysql->select_one("select * from {$this->pre}restemp where addtime='{$time}'");
	   $mymoney = "0";
	   $query = $this->mysql->query("select * from {$this->pre}res where checked='1' and restime<'{$moneytime}' and uid='{$uid}'");
	   $f = 0;
	   while($rs=$this->mysql->assoc($query)){
		 $money = $rs['money']-$rs['resmoney']>=$restemp['onemoney'] ? $restemp['onemoney'] : $rs['money']-$rs['resmoney'];
         $mymoney += $money;
         $u['restime'] = $moneytime+1;
		 $u['resmoney'] = $rs['resmoney']+$money;
		 if($u['resmoney']>=$rs['money']){
		   $u['checked'] = '2';
		   $one = $this->mysql->select_one("select * from {$this->pre}res where checked='0' order by id asc limit 1");
		   $this->mysql->query("update {$this->pre}res set checked='1',restime='{$moneytime}' where id='{$one['id']}'");
		 }
		 $this->mysql->update("{$this->pre}res",$u,"id='{$rs['id']}'");
		 $f++;
	   } 
       if($mymoney>0) $this->up_money($uid,$mymoney,"+","个分红权分红",'1');
	   $re['res'] = $restemp['res'].",".$uid;
	   $this->mysql->update("{$this->pre}restemp",$re,"id='{$restemp['id']}'");
	   return false;
	}
	function getcompany($typeid,$num,$ishot='0'){
		$where = 'where id>0';
		$where .= $typeid=='0' ? "" : " and typeid='$typeid'";
		$where .= $ishot=='0' ? "" : " and ishot='1'";
	    $query = $this->mysql->query("select * from {$this->pre}company $where order by addtime desc limit 0,$num");
	    while($rs=$this->mysql->assoc($query)){
		    $rs['url'] = rewrite::request("?mod=company&act=show&id=".$rs['id']);
		    $rs['addtime'] = formattime($rs['addtime']);
			$rs['pic'] = pic($rs['content'],$rs['pics']);
		    $company[] = $rs;
	    }
		return $company;
	}


	function getads($adsname,$op=''){
	    $ads = $this->mysql->select_one("select * from {$this->pre}ads where adsname='$adsname'");
        if($ads['adstype']==1){
		   $ad = '<a href="'.$ads['adslink'].'" target="_blank" '.$op.'><img src="'.$ads['adspic'].'"  width="'.$ads['width'].'"  height="'.$ads['height'].'" /></a>';		   	
		}elseif($ads['adstype']==2){
		   $ad = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'.$ads['width'].'" height="'.$ads['height'].'">';
		   $ad .= '<param name="movie" value="'.$ads['adspic'].'" />';
		   $ad .= '<param name="quality" value="high" />';
		   $ad .= '<embed src="'.$ads['adspic'].'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'.$ads['width'].'" height="'.$ads['height'].'"></embed>';
		   $ad .= '</object>';
		}
		return $ad;

	}
	
	function getfocus($num){
	    $ads = $this->mysql->getarr("select * from {$this->pre}focus limit 0,".$num);
		return $ads;
	}
	
	
	function pageclass($count,$pagesize=''){
		$this->pagesize = $this->module=='admin' ? config::get("adminpagesize") : config::get("pagesize");
		if($pagesize!='') $this->pagesize = $pagesize;
		$this->page = new page_class($this->pagesize,0,$count);
	    $this->pageid = $this->page->page;
		$this->showpage = $this->page->showpage();
		return true;
	}
	

	function message($urlkey,$message){
       $this->message = language($message);
       $thisurl = Purl($urlkey); 
       $this->jcode = $thisurl=='-1' ? "javascript:history.go(-1);" : "javascript:location.href='$thisurl';";
       $this->scode = $thisurl=='-1' ? "history.go(-1)" : "location.href='$thisurl'";	
	   $this->openmessage = 1;
	   $this->template();
	   return true;
	}
	function javascript($urlkey,$message){
       $message = language($message);
       $url = Purl($urlkey);
	   $javascript = "<script language=\"javascript\">";
	   $javascript .= "alert('{$message}');";
	   if($url=='-1'){
	     $javascript .= "parent.location.reload();";  
	   }else{
		 $javascript .= "parent.location.href='{$url}';";  
	   }
	   $javascript .= "</script>";
	   echo $javascript;
	   exit;
	}
	
	
	function template(){
		$module = $this->module;
		$action = $this->action;
		$template_dir = $module=='admin' ? 'admin' : config::get("template_dir");
		$template_dir = $module=='member' ? $template_dir.'/member' : $template_dir;
		$this->tempdir = config::get('siteurl')."template/{$template_dir}/";
		$this->appdir = config::get('siteurl')."app/";
		if($this->openmessage){
		   $action = 'message';
		   $module = $module=='admin' ? 'admin' : 'index';
		   if($this->module=='member'){
			   $module='member';
		   }
		}
		include template($module.'_'.$action,$template_dir);
		exit();
	}
	
}
?>