<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Index extends HC_Controller
{
	
	function __construct()
	{
            parent::__construct();
            $this->load_lang('adminsys/index');
            //$this -> load('admin'); 
            //$this -> admin -> online_admin();
            $this -> load_db();
            $this -> load_model('admin'); 
		
	}
	
	public function act_default()
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24 
		$this -> view -> display('index.tpl');
	}
	
	public function top()
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24 
		$this -> view ->  display('top.tpl');
	}
	
	
	public function menu()
	{
		//$this -> view ->   caching = true;
		//$this -> view ->   cache_lifetime = 86400 ;
		$typeval = $this -> input ->  req["type"];
		
		if($typeval !='' ){
			$menu_arr = $this -> get_menu($typeval);
		}else{
			$menu_arr = $this -> get_menu('web');
		}
		//var_dump( $menu_arr );
		
		$this -> view ->  assign('menu_arr',$menu_arr);
		$this -> view ->  display('menu.tpl');
	}
	
	function get_power( $re_arr = array() )
	{

		if( $_SESSION["admin"]["adm_user"] != EMPLOYEE_BASEUSER )
		{
				$pow_arr = $this -> admin_mod -> get_power_menu();

				foreach($re_arr as $rkey => $rval){
					if(!array_key_exists($rval['men_power_name'],$pow_arr)){
						unset($re_arr[$rkey]);
					}
					foreach((array)$rval['array'] as $arr_key => $arr_val){
						if(!in_array($arr_val['men_power_name'],(array)$pow_arr[$rval['men_power_name']]) ){

							unset($re_arr[$rkey]['array'][$arr_key]);
						}	
					}
					
					
				}

				return $re_arr;

		}else{
			return $re_arr;
		}
		
	}
	

	
	function get_menu( $type='' )
	{

		if($type!='')
		{
			$sql_query = "SELECT men_id, men_name, men_power_name, men_file, men_parent_id ,men_target_val   FROM ".$this -> db -> prefix."menu WHERE men_enabled = 1 and men_type ='".$type."' ORDER BY men_list , men_id  ";
			$menuInfo = $this -> db -> select_info($sql_query);

			$reArray = array();
			 foreach ($menuInfo as $row)
			 {
				if($row["men_parent_id"] == 0)
				{
					$reArray[$row["men_id"]]['men_id'] 			= $row["men_id"] ;
					$reArray[$row["men_id"]]['men_power_name'] 	= $row["men_power_name"] ;
					$reArray[$row["men_id"]]['men_name'] 		= $row["men_name"] ;
					$reArray[$row["men_id"]]['men_file'] 		= $row["men_file"];
					$reArray[$row["men_id"]]['men_parent_id'] 	= $row["men_parent_id"];

				}else{
					$reArray[$row["men_parent_id"]]['array'][] 	= $row;	
				}
			 }
			 
			return $this -> get_power( $reArray );
			
			
			
		}
		return false;
		
		
	}
	
	
	public function delimit()
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		$this -> view ->  display('delimit.tpl');
	}
	
	function main()
	{
		//count
		$count_info = array('order_total'=>0,'users_total'=>0,'product_total'=>0,'article_total'=>0,'picture_total'=>0,
							'order_today'=>0,'users_today'=>0,'product_today'=>0,'article_today'=>0,'picture_today'=>0,
							'order_yesterday'=>0,'users_yesterday'=>0,'product_yesterday'=>0,'article_yesterday'=>0,'picture_yesterday'=>0,
							'order_week'=>0,'users_week'=>0,'product_week'=>0,'article_week'=>0,'picture_week'=>0,
							'order_month'=>0,'users_month'=>0,'product_month'=>0,'article_month'=>0,'picture_month'=>0);
							
							
		$sql_total_order = "SELECT count(1) FROM ".$this -> db -> prefix."order WHERE 1 ";
		$count_info['order_total'] = $this -> db -> get_count($sql_total_order);
		
		$sql_total_user = "SELECT count(1) FROM ".$this -> db -> prefix."users WHERE 1 ";
		$count_info['users_total'] = $this -> db -> get_count($sql_total_user);
		
		$sql_total_product = "SELECT count(1) FROM ".$this -> db -> prefix."product WHERE 1 ";
		$count_info['product_total'] = $this -> db -> get_count($sql_total_product);
		
		$sql_total_article = "SELECT count(1) FROM ".$this -> db -> prefix."article WHERE 1 ";
		$count_info['article_total'] = $this -> db -> get_count($sql_total_article);
		
		$sql_total_picture = "SELECT count(1) FROM ".$this -> db -> prefix."picture WHERE 1 ";
		$count_info['picture_total'] = $this -> db -> get_count($sql_total_picture);
		
		//today_count
		$today_time = strtotime(date('Y-m-d'));
		$sql_today_order = "SELECT count(1) FROM ".$this -> db -> prefix."order WHERE ord_add_time >=  ".$today_time;
		$count_info['order_today'] = $this -> db -> get_count($sql_today_order);
		
		$sql_today_user = "SELECT count(1) FROM ".$this -> db -> prefix."users WHERE use_login_time >=  ".$today_time;
		$count_info['users_today'] = $this -> db -> get_count($sql_today_user);
		
		$sql_today_product = "SELECT count(1) FROM ".$this -> db -> prefix."product WHERE pro_add_time >=  ".$today_time;
		$count_info['product_today'] = $this -> db -> get_count($sql_today_product);
		
		$sql_today_article = "SELECT count(1) FROM ".$this -> db -> prefix."article WHERE art_add_time >=  ".$today_time;
		$count_info['article_today'] = $this -> db -> get_count($sql_today_article);
		
		$sql_today_picture = "SELECT count(1) FROM ".$this -> db -> prefix."picture WHERE pic_add_time >=  ".$today_time;
		$count_info['picture_today'] = $this -> db -> get_count($sql_today_picture);
		
		//yesterday_count
		$yesterday_time = strtotime(date("Y-m-d",strtotime("-1 day")));
		$sql_yesterday_order = "SELECT count(1) FROM ".$this -> db -> prefix."order WHERE ord_add_time >= ".$yesterday_time." and ord_add_time <  ".$today_time;
		$count_info['order_yesterday'] = $this -> db -> get_count($sql_yesterday_order);
		
		$sql_yesterday_user = "SELECT count(1) FROM ".$this -> db -> prefix."users WHERE use_login_time >= ".$yesterday_time." and use_login_time <  ".$today_time;
		$count_info['users_yesterday'] = $this -> db -> get_count($sql_yesterday_user);
		
		$sql_yesterday_product = "SELECT count(1) FROM ".$this -> db -> prefix."product WHERE pro_add_time >= ".$yesterday_time." and pro_add_time <  ".$today_time;
		$count_info['product_yesterday'] = $this -> db -> get_count($sql_yesterday_product);
		
		$sql_yesterday_article = "SELECT count(1) FROM ".$this -> db -> prefix."article WHERE art_add_time >= ".$yesterday_time." and art_add_time <  ".$today_time;
		$count_info['article_yesterday'] = $this -> db -> get_count($sql_today_article);
		
		$sql_yesterday_picture = "SELECT count(1) FROM ".$this -> db -> prefix."picture WHERE  pic_add_time >= ".$yesterday_time." and pic_add_time <  ".$today_time;
		$count_info['picture_yesterday'] = $this -> db -> get_count($sql_yesterday_picture);
		
		//week_count
		//echo date("Y-m-d",strtotime("-1 week Monday"));
		$week_time = strtotime(date("Y-m-d",strtotime("-1 week Monday")));
		$sql_week_order = "SELECT count(1) FROM ".$this -> db -> prefix."order WHERE ord_add_time >=  ".$week_time;
		$count_info['order_week'] = $this -> db -> get_count($sql_week_order);
		
		$sql_week_user = "SELECT count(1) FROM ".$this -> db -> prefix."users WHERE use_login_time >=  ".$week_time;
		$count_info['users_week'] = $this -> db -> get_count($sql_week_user);
		
		$sql_week_product = "SELECT count(1) FROM ".$this -> db -> prefix."product WHERE pro_add_time >=  ".$week_time;
		$count_info['product_week'] = $this -> db -> get_count($sql_week_product);
		
		$sql_week_article = "SELECT count(1) FROM ".$this -> db -> prefix."article WHERE art_add_time >=  ".$week_time;
		$count_info['article_week'] = $this -> db -> get_count($sql_week_article);
		
		$sql_week_picture = "SELECT count(1) FROM ".$this -> db -> prefix."picture WHERE pic_add_time >=  ".$week_time;
		$count_info['picture_week'] = $this -> db -> get_count($sql_week_picture);
		
		//month_count
		$month_time = strtotime(date('Y-m-d'));
		$sql_month_order = "SELECT count(1) FROM ".$this -> db -> prefix."order WHERE ord_add_time >=  ".$month_time;
		$count_info['order_month'] = $this -> db -> get_count($sql_month_order);
		
		$sql_month_user = "SELECT count(1) FROM ".$this -> db -> prefix."users WHERE use_login_time >=  ".$month_time;
		$count_info['users_month'] = $this -> db -> get_count($sql_month_user);
		
		$sql_month_product = "SELECT count(1) FROM ".$this -> db -> prefix."product WHERE pro_add_time >=  ".$month_time;
		$count_info['product_month'] = $this -> db -> get_count($sql_month_product);
		
		$sql_month_article = "SELECT count(1) FROM ".$this -> db -> prefix."article WHERE art_add_time >=  ".$month_time;
		$count_info['article_month'] = $this -> db -> get_count($sql_month_article);
		
		$sql_month_picture = "SELECT count(1) FROM ".$this -> db -> prefix."picture WHERE pic_add_time >=  ".$month_time;
		$count_info['picture_month'] = $this -> db -> get_count($sql_month_picture);
		
		//var_dump($count_info);
		$this -> view -> assign('count_info',$count_info);
		
		//login
		$sql_ip = "SELECT * FROM ".$this -> db -> prefix."adm_login_log WHERE 1 ORDER BY adl_time DESC LIMIT 0,2";
		$ip_info = $this -> db -> select_info($sql_ip);
		if(isset($ip_info[0]['adl_time'])){
			$ip_info[0]['adl_time'] = date("Y-m-d h:i:s",$ip_info[0]['adl_time']);
		}
		if(isset($ip_info[1]['adl_time'])){
			$ip_info[1]['adl_time'] = date("Y-m-d h:i:s",$ip_info[1]['adl_time']);
		}
		//var_dump($ip_info);
		$this -> view -> assign('ip_info',$ip_info);
		$this -> view -> assign('adm_user',$_SESSION["admin"]["adm_user"]);
		$this -> view -> display('main.tpl');
	}

}

 