<?php

class Dashboard extends Controller 
{

	function __construct()
	{
		parent::__construct();
		
		$this->load->library('session');
		
		$this->load->config('analytics');
		$this->load->library('carabiner');
		$this->load->helper('url');
		$this->load->helper('date');
		$this->load->helper('text');
		$this->load->helper('form');
		
		//echo $this->session->userdata('uzytkownik_id');
		//echo $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_login;
	}
	
	
	function index()
	{
		$this->carabiner->js('shared/frameworks/mootools-1.2.1-core-yc.js');
		$this->carabiner->js('shared/frameworks/mootools-1.2-more.js');
		$this->carabiner->js('shared/plugins/swfobject.js');
		
		$this->carabiner->css('shared/utils/css-reset.css');
		$this->carabiner->css('shared/utils/clearfix.css');

		$this->carabiner->css('backend/grid/960.css');

		$this->carabiner->css('backend/global.css');
		$this->carabiner->css('backend/core.css');

		$this->carabiner->css('backend/text.css');
		$this->carabiner->css('backend/dashboard.css');
		
		
		$this->load->view('templates/base/template', array("module" => "dashboard", "view" => "browse"));
	}
	
	function clear_cache()
	{
		$mydir = BASEPATH.'cache/dashboard/'; 
		$d = dir($mydir); 
		while($entry = $d->read()) { 
		 if ($entry!= "." && $entry!= "..") { 
		 	@unlink(BASEPATH.'cache/dashboard/'.$entry); 
		 } 
		} 
		$d->close(); 
	}
	
	function statistics()
	{	
		$this->cache('table-','table_data','no_table_data');
	}
	
	function xml_data()
	{
		$this->cache('xml-','xml_data','empty_data');
	}
	
	function cache($filename, $view, $noresults = 'empty_data')
	{	
		$this->load->library('analytics');
		$use_cache = TRUE;

		$year = $this->input->post('year');
		$month = $this->input->post('month');
		$profile = $this->input->post('profile');
		$cprofile = str_replace(':', '', $profile);
		
		$filepath = BASEPATH . 'cache/dashboard/'.$cprofile.'-'.$filename.$year.'-'.$month.'___'.date('Y-n-d').EXT;
		$created = substr($filepath , strlen(BASEPATH. 'cache/dashboard/'.$filename),strlen($filepath));
		$created = substr($filepath, strpos($filepath,'___')+3,-4);
		// als de huidige maand gelijk is aan de ingegeven maand
		if($use_cache)
		{
			if($month == date('n') && $year == date('Y'))
			{
				// als de created date gelijk is aan vandaag
				if($created == date('Y-n-d'))
				{
					if(file_exists($filepath))
					{
						echo file_get_contents($filepath);
						exit;
					}
				}
			}
			else
			{
				$days = days_in_month($month, $year);
				// controle of de file wel de laatste dag bevat als we niet in deze maand zitten
				foreach (range(1, $days) as $number)
				{
					$filepath = BASEPATH .
					 'cache/dashboard/'.$cprofile.'-'.$filename.$year.'-'.$month.'___'.$year.'-'.$month.'-'.$number.EXT;
				    if(file_exists($filepath))
					{

						if($number == $days)
						{			
							echo file_get_contents($filepath);
							exit;
						}
						else
						{						
							@unlink($filepath);
						}
					}
				}
			}
		}
		
		$this->analytics->login($this->config->item('username'), $this->config->item('password')); // change
		$this->analytics->setProfileById($profile); // change
		$this->analytics->setMonth($month, $year);
		
		$data = array(
			'visitors' => $this->analytics->getVisitors(),
			'pageviews' => $this->analytics->getPageviews(),
			'visitsperhour' => $this->analytics->getVisitsPerHour(),
			'browsers' => $this->analytics->getBrowsers(),
			'referrers' => $this->analytics->getReferrers(),
			'searchwords' => $this->analytics->getSearchWords(),
			'screenresolutions' => $this->analytics->getScreenResolution(),
			'os' => $this->analytics->getOperatingSystem(),
			'month' => $month,
			'year' => $year
			);

		
		echo $cache = count($data['referrers']) ? 
			$this->load->view('modules/dashboard/'.$view,$data, TRUE) : $this->load->view('modules/dashboard/'. $noresults ,null, TRUE);
		
		
		// cleanup
		$max = days_in_month(date('n'), date('Y'));
		foreach (range(1, $max) as $number) {
		    @unlink( BASEPATH . 'cache/dashboard/'.$cprofile.'-'.$filename.$year.'-'.$month.'___'.date('Y-n-').$number.EXT);
		}
		
		// write
		//$handle = fopen($filepath,"x+");
		//fwrite($handle,$cache);
		//fclose($handle);
	}

	function analytics_profiles()
	{

		$this->load->library('analytics');
		$this->analytics->login($this->config->item('username'), $this->config->item('password')); // change
		//echo $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_ga_name;
		$aProfiles = $this->analytics->getProfileList($this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_ga_name);
		$counter = 1;
		$str = '';
		foreach($aProfiles as $value => $key)
		{	
			//if($key == 'www.webseb.pl'){
				$selected = 0;
				$comma = $counter == count($aProfiles) ? '' : '|';
				$str .= $value.','.$key.','.$selected.$comma;
				$counter++;
			//}
		}
		
		echo $str.'';
	}
	
}

?>