<?php
class Partners extends Controller
{
	var $access = array(
		'36'=>'archxp2009',
		'39'=>'ita456789x',
		'43'=>'ger123456y',
		'49'=>'ger123456y',
		'41'=>'ger123456y',
		'30'=>'grcad23456g',
		'42'=>'cz9876543',
		'34'=>'espcad5679e',
		'38'=>'crcad2009',
		'52'=>'mexcad2009',
		'82'=>'korcad123',
		'48'=>'polcad456789x',
		'81'=>'jap2009',
		'40'=>'rocad2009',
		
	);
	
	
	function __construct()
	{
		parent::__construct();
		
		if(!$this->_check_access() && $this->uri->segment(2)!='login')
		{
			redirect('partners/login');
		}
				
		$this->access_id = $this->phpsession->get('access_id');
	
		$this->load->model('mpartners');
	}
	
	function login()
	{
		$pass = $this->input->post('pass');
		$user = $this->input->post('user');
		
		if($pass)
		{
			if(array_key_exists($user,$this->access) && $this->access[$user]==$pass)
			{
				$this->phpsession->save('bugtracker_access',true);
				$this->phpsession->save('access_id',$user);
				
				redirect('partners');
			}
		}
		
		$data['csik'] = array('home','Bugtracker Login');
		$data['tartalom'] = 'partners/login';
		$this->load->view('site',$data);
	}
	
	function logout()
	{
		$this->phpsession->clear('bugtracker_access');
		$this->phpsession->clear('access_id');
		
		redirect('support');
	}
	
	
	function index()
	{
		
		
		
		$data['csik']=array('home','Partners');
		$data['tartalom'] = 'partners/index';
		$this->load->view('site',$data);
		
		
	}
		
	function bugtracker()
	{
		$this->bugdb = $this->load->database('bugtracker',true);
		
		$project = $this->input->post('project');
		$id = $this->input->post('id');
		
		$projects = $this->bugdb->select('id, name')->order_by('name','asc')->get('mantis_project_table')->result();
		$data['dropdown'] = $this->_project_dropdown_array($projects);
		
		
		if(!$id)
		{
			if(!$project || $project == 0)
			{
				$sorok = $this->bugdb->from('mantis_bug_table')->join('mantis_bug_text_table','mantis_bug_table.id=mantis_bug_text_table.id')->orderby('mantis_bug_table.id','desc')->limit(1000)->get();
			}
			else
			{
				$sorok = $this->bugdb->from('mantis_bug_table')->join('mantis_bug_text_table','mantis_bug_table.id=mantis_bug_text_table.id')->where('project_id',$project)->orderby('mantis_bug_table.id','desc')->limit(100)->get();
			}
			
			
			if($sorok->num_rows()!=0)
			{
				$data['sorok'] = $sorok->result();
			}
			else
			{
				$data['sorok'] = false;
			}
		}
		else
		{
			if(is_numeric($id))
			{
				$sorok = $this->bugdb->from('mantis_bug_table')->join('mantis_bug_text_table','mantis_bug_table.id=mantis_bug_text_table.id')->where('mantis_bug_table.id',$id)->get();
				
				if($sorok->num_rows()!=1)
				{
					show_error('Bug not found. Bug ID was: '.$id);
				}
				else
				{
					$data['sorok'] = $sorok->result();
				}
			}
			else
			{
				
				$id = mysql_real_escape_string($id);
				
				
				$sorok = $this->bugdb->from('mantis_bug_table')->join('mantis_bug_text_table','mantis_bug_table.id=mantis_bug_text_table.id')->where("mantis_bug_table.summary LIKE '%$id%'")->order_by('mantis_bug_table.id','desc')->get();
				
				if($sorok->num_rows()==0)
				{
					$data['sorok'] = false;
				}
				else
				{
					$data['sorok'] = $sorok->result();
				}
			}
		}
		
		
		$data['id'] = $id;
		$data['project'] = $project;
		$data['csik']=array('home','partners','Bugtracker');
		$data['tartalom'] = 'partners/filter';
		$this->load->view('site',$data);
		
	}
	
	
	function keys()
	{
		$this->keydb = $this->load->database('hardlock',true);
		$search = $this->input->post('search');
		
		$empty = $this->input->post('empty');
		$disabled = $this->input->post('disabled');

		$from = $this->input->post('from');
		$to = $this->input->post('to');
		
		if((!isset($from) || $from=='') && !$this->phpsession->get('from'))
		{
		    $from = 0;
		    $this->phpsession->save('from',0);
		}
		elseif(isset($from) && $from!='')
		{
			$this->phpsession->save('from',$from);
		}
		elseif(!$from && $this->phpsession->get('from'))
		{
			$from = $this->phpsession->get('from');
		}

		if((!isset($to) || $to=='') && !$this->phpsession->get('to'))
		{
		    $to = 999999;
		    $this->phpsession->save('to',999999);
		}
		elseif(isset($to) && $to!='')
		{
			$this->phpsession->save('to',$to);
		}
		elseif(!$to && $this->phpsession->get('to'))
		{
			$to = $this->phpsession->get('to');
		}
		
		$limit = $this->input->post('limit');
		$page = $this->uri->segment(3);
		$pages = -1;

		if(!$limit && !$this->phpsession->get('limit'))
		{
    		$this->phpsession->save('limit',10);
		    $limit = 10;
  		}
		elseif($limit==0 && !$this->phpsession->get('limit'))
		{
		    $this->phpsession->save('limit',10);
		    $limit = 10;
  		}
  		elseif(!$limit && $this->phpsession->get('limit'))
  		{
            $limit = $this->phpsession->get('limit');
  		}
  		else
  		{
            $this->phpsession->save('limit',$limit);
  		}
		    
		if(!$page)
		    $page = 1;

		$min = $page==1 ? 0 : ($page - 1) * $limit;
		$data['max'] = $page==1 ? $limit : $page * $limit;
		$data['min'] = $min + 1;

		if(!$search)
		{
			if($empty)
			{
				$result = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where('hlStat',0)->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit(99999)->get();
				
				$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where('hlStat',0)->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit($limit,$min)->get();
				
			}
			elseif($disabled)
			{
				$result = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where('hlStat',2)->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit(99999)->get();
				
				$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where('hlStat',2)->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit($limit,$min)->get();
			}
			else
			{
				$result = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit(99999)->get();
				
				$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='".$this->access_id."'")->where("hlNum >= '".$from."' AND hlNum <= '".$to."'")->limit($limit,$min)->get();
			}
			
		
			if($result->num_rows()==0)
			{
				$data['sorok'] = false;
			}
			else
			{
                if($result->num_rows() % $limit>0)
					$pages = ($result->num_rows() + ($limit - ($result->num_rows() % $limit))) / $limit;
				else
					$pages = $result->num_rows() / $limit;
					
				if($result->num_rows()<=$limit)
				    $pages = -1;

				$data['sorok'] = $result_limit->result();
				$data['found'] = $result->num_rows();
			}
		}
		else
		{
			if(($search))
			{
				
				$result = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlTime','desc')->where("hlNum like '%$search%'")->where('hlCtrID',$this->access_id)->get();
				
				if($result->num_rows()==0)
				{
					$data['sorok'] = false;
				}
				else
				{
					$data['sorok'] = $result->result();
					$data['found'] = $result->num_rows();
				}
			}
			else
			{
				
				$result = $this->keydb->from('cl_fo_hardlock')->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlTime','desc')->where("hlCtrID=".$this->access_id." and ( strCompany like '%$search%' or strName like '%$search%' or hlUser like '%$search%')")->get();
				
				if($result->num_rows()==0)
				{
					$data['sorok'] = false;
				}
				else
				{
					$data['sorok'] = $result->result();
					$data['found'] = $result->num_rows();
				}
				
			}
		}
		
		if(isset($data['found']))
			$data['max'] > $data['found'] ? $data['max'] = $data['found'] : $data['max'] ;

		$data['tartalom']='partners/keyfilter';
		$data['csik'] = array('home','partners','Keys');
		$data['limit'] = $this->phpsession->get('limit');
		$data['page'] = $page;
		$data['pages'] = $pages;
		$data['from'] = $from;
		$data['to'] = $to;
		
		$this->load->view('site',$data);
		
	}
	
	function key()
	{
		$this->keydb = $this->load->database('hardlock',true);
		
		$key_id = $this->uri->segment(3);
		if(!$key_id)
		{
			redirect('partners/keys');
		}
		else
		{
			$key = $this->keydb->where('hlNum',$key_id)->join('cl_users','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->get('cl_fo_hardlock');
			if($key->num_rows()==0)
			{
				show_404();
			}
			else
			{
				$key = $key->row();
			}
			
			if($key->hlCtrID!=$this->access_id)
			{
				show_error("Access denied to key no. ".$key_id);
			}
			
			//////////////////////////////////////////////////////
			
			$programs = $this->keydb->where('prHlNum',$key_id)->join('cl_fo_types','cl_fo_programs.prTypeID=cl_fo_types.typeCode')->join('cl_fo_versions','cl_fo_programs.prVerID=cl_fo_versions.verID','left')->get('cl_fo_programs');
            
			if($programs->num_rows()==0)
			{
				$programs = false;
				$lastprID = false;
			}
			else
			{
                $lastprID = $programs->row_array($programs->num_rows()-1);
				$programs = $programs->result();
			}
			
			//////////////////////////////////////////////////////

			if($lastprID)
			{
                $modules = $this->keydb->where('mdPrHlNum',$key_id)->get('cl_fo_modules');
			}
			else
			{
				$modules = false;
			}
			
			if(!$modules || $modules->num_rows()==0)
			{
				$modules = false;
			}
			else
			{
				$modules = $modules->result();
			}
			
			//////////////////////////////////////////////////////
			
			$history = $this->keydb->where('hiHlNum',$key_id)->join('cl_fo_events','cl_fo_events.evid=cl_fo_history.hiTypeID','left')->get('cl_fo_history');
			if($history->num_rows()==0)
			{
				$history = false;
			}
			else
			{
				$history = $history->result();
			}
			
			//////////////////////////////////////////////////////
			
			$data['csik']=array('home','partners','keys');			
			$data['tartalom'] = 'partners/key';
			$data['key'] = $key;
			$data['programs'] = $programs;
			$data['modules'] = $modules;
			$data['history'] = $history;
			
			$this->load->view('site_key', $data);
		}
	}
	
	
	function _check_access()
	{
		if($this->phpsession->get('bugtracker_access'))
		{
			return true;
		}
		
		return false;
	}
	
	function _project_dropdown_array($projects)
	{
		$tomb[0] = 'Please Choose a Project';
		
		foreach($projects as $p)
		{
			$tomb[$p->id] = $p->name;
		}
		
		return $tomb;
	}
}