<?php

class Afiliant extends Controller {
	
	var $user_logged = false;

	function Afiliant()
	{
		parent::Controller();
		$this->load->library('session');	
		$this->load->helper(array('form', 'url','html'));
		$this->load->model('baza','',TRUE);
		$this->user_logged = $this->_check_if_logged();
		
	}
	
	function _check_if_logged()
	{	
		if($this->uri->segment('2')!='' and $this->uri->segment('2')!='index')
		{
			if(empty($this->session->userdata['id']) and empty($this->session->userdata['pass']))
			{				
				$this->session->sess_destroy();
				redirect('/afiliant', 'location');
			}
			else
			{			
				$data_login = $this->baza->check_id_and_password_afiliant($this->session->userdata['id'],$this->session->userdata['pass']); 
											
				if(!empty($data_login))
				{	
					return true;						
				}
				else 
				{					
					$this->session->sess_destroy();
					redirect('/afiliant', 'location');			
				}
			}
		}		
	}
		
	function index($ticket_id = null)
	{
		$data['user']  = $this->session->userdata('login');
		if(empty($this->session->userdata['id']) and empty($this->session->userdata['pass']))
		{				
			$this->session->sess_destroy();			
		}
		else
		{			
			$data_login = $this->baza->check_id_and_password_afiliant($this->session->userdata['id'],$this->session->userdata['pass']); 
										
			if(!empty($data_login))
			{	
				redirect('/afiliant/start', 'location');						
			}
			else 
			{					
				$this->session->sess_destroy();
				redirect('/afiliant', 'location');			
			}
		}
		
			////////		
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<span style="color:red;">', '<br /></span>');
		$fields['login_form'] = 'login_form';
		$fields['pass'] = 'pass';
		$this->validation->set_fields($fields);
		
		$login_form = $this->validation->login_form;
		$pass_form = $this->validation->pass;
		$rules['login_form'] = "callback_login_form_check|required";
		$rules['pass'] = "callback_pass_check[$login_form]|required";
		$this->validation->set_rules($rules);
			
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('/layout/home');
		}
		else
		{	
			$afiliant = $this->baza->afiliant_check_user_pass(md5($pass_form),$login_form);
			//print_r($afiliant); 								
			$this->session->sess_destroy();
			$this->session->set_userdata('id',$afiliant[0]['id']);
			$this->session->set_userdata('login',($afiliant[0]['imie'].' '.$afiliant[0]['nazwisko']));
			$this->session->set_userdata('pass',$afiliant[0]['pass']);
			
			if($ticket_id != null)
				redirect('/tickets/show/'.$ticket_id,'location');
			else	
				redirect('/afiliant/start','location');
		}
	}
	
	function start()
	{
		$data['user']  = $this->session->userdata('login');
		$this->load->view('/layout/start_page',$data);	
	}
	
	function clients()
	{
		$data['user'] 				= $this->session->userdata('login');
		$dane['afiliant_users']		= $this->baza->get_affiliant_users($this->session->userdata['id']);		
		$data['content']  			= $this->load->view('/afiliant/clients',$dane,true);
		
		$this->load->view('/layout/default',$data);	
	}
	
	function profit_from_clients($type,$okres=1,$rok=2007)
	{
		$data['user']  = $this->session->userdata('login');
		$prowizja = $this->baza->get_prowizje($this->session->userdata['id']);

		$clients['prowizja'] = $prowizja[0]['procent'];
		
		if($type==1)
		{ //tygodniowe
			if($this->uri->segment(4)!=null)
			{				
				$clients['clients'] = $this->baza->get_affiliant_users_tydzien($this->session->userdata['id'],$okres,$rok);	
			}			
		}
		elseif($type==2)
		{//miesieczne
			if($this->uri->segment(4)!=null)
			{				
				$clients['clients'] = $this->baza->get_affiliant_users_miesiac($this->session->userdata['id'],$okres,$rok);	
			}	
		}
		elseif($type==3)
		{//roczne
			$clients['clients'] = $this->baza->get_affiliant_users_rok($this->session->userdata['id'],$rok);
		}
		
		$clients['type'] = $type;
						
		$data['content']  = $this->load->view('/afiliant/clients_table',$clients,true);
		$this->load->view('/layout/default',$data);	
	}
		
	
	function add_user_to_afiliant()
	{
		$data['user']  = $this->session->userdata('login');
		
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			$valid_form = null;
			
			if($user_data!=null)	
			{
				
				$data_afiliant['afilianci'] = $this->baza->superadmin_all_afiliants();
				$data['content']  = $this->load->view('/superadmin/add_user_to_afiliant',$data_afiliant,true);						
				$this->load->view('/layout/default',$data);				
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
		
	function get_affiliant_users($id=null)
	{
		$data['user']  = $this->session->userdata('login');
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			$valid_form = null;
			
			if($user_data!=null)	
			{
				if($id!=null)
				{
					$data['users'] = $this->baza->get_affiliant_users($id);
					$this->load->view('superadmin/users_table',$data);	
				}					
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function get_free_users_for_aff($id=null)
	{
		$data['user']  = $this->session->userdata('login');
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			$valid_form = null;
			
			if($user_data!=null)	
			{
				if($id!=null)
				{
					$data['users'] = $this->baza->get_free_users_for_aff($id);
					$this->load->view('superadmin/add_user_to_aff_table',$data);	
				}					
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	
	function del_affiliant_user($id=null)
	{
		$data['user']  = $this->session->userdata('login');
		
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			$valid_form = null;
			
			if($user_data!=null)	
			{
				if($id!=null)
				{
					$this->baza->del_user_from_aff($id);						
				}					
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');
		}
	}
	
	function update_affiliant_user($id=null,$affiliant)
	{	
		$data['user']  = $this->session->userdata('login');	
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			$valid_form = null;
			
			if($user_data!=null)	
			{
				if($id!=null)
				{
					$this->baza->update_user_from_aff($id,$affiliant);						
				}					
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');
		}
	}
			
	function change_region($domain_id)
	{
		$data['user']  = $this->session->userdata('login');
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				if(!empty($_POST['google_region']))
					$this->baza->superadmin_set_region($domain_id, $_POST['google_region']);				
								
				redirect('/superadmin/domain','location');	
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function domain()
	{
		$data['user']  = $this->session->userdata('login');
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$data['user']  = $user_data[0]['name'];
				$data['domains'] = $this->baza->superadmin_get_domains($user_data[0]['name'], $this->session->userdata['di_sa']);				
				$this->config->load('google_regions');
				$data['regions'] = $this->config->item('google_regions');
				$this->load->view('/layout/domain_page',$data);
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function show_domain_stat($id=null)
	{
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$data['user']  = $user_data[0]['name'];
				
				if($id!=null)
				{
					if($user_data[0]['name']!='admin')
					{
						$this->_check_domain_owner($this->session->userdata['di_sa'], $id);	
					}
					
					$data['keywords'] = $this->baza->get_domain($id);
					$data['pozycja'] = $this->baza->get_domain_key_position($id);
					$this->load->view('/layout/show_domain_page',$data);
				}
				else{
					$this->load->view('blad',$data);
				}
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function show_domain_chart($id=null)
	{
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$data['user']  = $user_data[0]['name'];
				
				if($id!=null)
				{	
					if($user_data[0]['name']!='admin')
					{
						$this->_check_domain_owner($this->session->userdata['di_sa'], $id);	
					}
					
					$data['keywords'] = $this->baza->get_keyword_position($id);
					//$domain_name = $this->baza->superadmin_get_doamin_name($id);	
					$data['domain_id']=$id;						
					$this->load->view('/layout/show_chart_domain_page',$data);
				}
				else{
					$this->load->view('blad',$data);
				}
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function show_domain_keyword_activity($id = null, $elem_start = 0)
	{
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$this->load->model('bazapg','','pg');
				$data['user']  = $user_data[0]['name'];
				$data['stats'] = $this->bazapg->get_stats_pagination($id,$elem_start,50);
				$this->load->library('pagination');
				$number_of_rows = $this->bazapg->count_stats($id);
				if(empty($number_of_rows[0]['ile'])) $number_of_rows[0]['ile']=0;
				
				$config['base_url'] = "/superadmin/show_domain_keyword_activity/$id/";
				$config['url_extention']= '';
				$config['total_rows'] = $number_of_rows[0]['ile'];
				$config['per_page'] = '50';
				$config['num_links'] = '9';
				$config['uri_segment'] = 4;
				$config['first_link'] = '<<';
				$config['last_link'] = '>>';
				
				$this->pagination->initialize($config);
				$data['paginacja'] = $this->pagination->create_links();					
				$data['domain_id']=$id;						
				$this->load->view('/layout/show_domain_search_term',$data);
	
			}
			else
			{
				redirect('/admin/logout','location');	
			}
		}		
		else
		{
			redirect('/admin/','location');		
		}
	}
	
	function show_domain_statcounter($id = null, $type = 1)
	{
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$data['user']  = $user_data[0]['name'];
				
				if($id!=null)
				{	
					if($user_data[0]['name']!='admin')
					{
						$this->_check_domain_owner($this->session->userdata['di_sa'], $id);	
					}
		
					switch($type)
					{
					    case 1: {
					    			// "Dzienne";
									if($this->uri->segment('1')=='superadmin') 
										$link = 'superadmin';
									else		
										$link = 'admin'; 
										
									$data['name_of_type'] = "Dzienne"; 
									$prefs = array (
           					    					'show_next_prev'  => TRUE,
               										'next_prev_url'   => '/'.$link.'/show_domain_statcounter/'.$id.'/'.$type.'',
							    					'month_type'   => 'long',
							    					'day_type'     => 'short'
             										);
								
								$prefs['template'] = '
			
												   {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}
												
												   {heading_row_start}<tr>{/heading_row_start}
												
												   {heading_previous_cell}<th><a href="{previous_url}/01">&lt;&lt;</a></th>{/heading_previous_cell}
												   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
												   {heading_next_cell}<th><a href="{next_url}/01">&gt;&gt;</a></th>{/heading_next_cell}
												
												   {heading_row_end}</tr>{/heading_row_end}
												
												   {week_row_start}<tr>{/week_row_start}
												   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
												   {week_row_end}</tr>{/week_row_end}
												
												   {cal_row_start}<tr>{/cal_row_start}
												   {cal_cell_start}<td>{/cal_cell_start}
												
												   {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
												   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}
												
												   {cal_cell_no_content}{day}{/cal_cell_no_content}
												   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}
												
												   {cal_cell_blank}&nbsp;{/cal_cell_blank}
												
												   {cal_cell_end}</td>{/cal_cell_end}
												   {cal_row_end}</tr>{/cal_row_end}
												
												   {table_close}</table>{/table_close}';
											
								   $this->load->library('calendar', $prefs);
									
									break;
								}
					    case 2: {
					    			// "Tygodniowe"; 
									$data['name_of_type'] = "Tygodniowe";
									//include_once('files/dateclass.php');
									break;
								}
					    case 3: {
					    			// "Miesieczne";
									$data['name_of_type'] = "Miesieczne";
									break;
								}
					    case 4: {
					    			// "Roczne";
									$data['name_of_type'] = "Roczne";
									break;
								}
					}
					
					
					//$this->load->model('bazapg','','pg');
					//$stats = $this->bazapg->get_stats($id);
					
					$data['domain_id']=$id;
					//$data['stats'] = $stats;
					$data['type'] = $type;						
					$this->load->view('/layout/show_stats_chart_domain_page',$data);
				}
				else{
					$this->load->view('blad',$data);
				}
			}
			else
			{
				redirect('/superadmin/logout','location');	
			}		
		}
		else
		{
			redirect('/admin/','location');		
		}
	}
	
	function profile()
	{
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$data['user']  = $user_data[0]['name'];
		
				if(isset($_POST['id']))
				{
					if($_POST['id']!='')
					{
						$this->baza->superadmin_update_user_data($_POST['id'],$_POST['company_name'],$_POST['contact_name'],$_POST['tax_id'],$_POST['website'],$_POST['email'],$_POST['city'],$_POST['post_code'],$_POST['street'],$_POST['b_no'],$_POST['a_no']);						
						$data['user_data'] = $this->baza->superadmin_get_user_data($this->session->userdata['di_sa']);
						$this->load->view('/layout/profile_page',$data);
					}
					else
					{
						$data['user_data'] = $this->baza->superadmin_get_user_data($this->session->userdata['di_sa']);
						$this->load->view('/layout/profile_page',$data);
					}
				}
				else
				{
					$data['user_data'] = $this->baza->superadmin_get_user_data($this->session->userdata['di_sa']);
					$this->load->view('/layout/profile_page',$data);
				}	
				
			}
			else
			{
				redirect('/superadmin/logout','refresh');	
			}		
		}
		else
		{
			redirect('/admin/','refresh');		
		}
	}
	
	function login_form_check($str)
	{
		$user=$this->baza->afiliant_user($str); 
		
		if ($user==null or $str==null)
		{
				$this->validation->set_message('login_form_check', 'Błędny login');
				return FALSE;
		}
		else
		{
				return TRUE;
		}
	}

	function pass_check($pass,$login)
	{
		if($pass==null or $login==null)
		{
			$password = null; 	
		}
		else	
		{
			$haslo = md5($pass);
			$password = $this->baza->afiliant_check_user_pass($haslo, $login); 	
		}
		
		if ($password == null)
		{
			$this->validation->set_message('pass_check', 'Błędne hasło');
			return FALSE;
		}
		else
		{	
			return TRUE;
		}
	}
	
	function change_pass()
	{	
		if(isset($this->session->userdata['user_sa_zalogowany']) and isset($this->session->userdata['di_sa']))
		{	
			$user_data = $this->baza->superadmin_get_user_login($this->session->userdata['user_sa_zalogowany'], $this->session->userdata['di_sa']);
			
			if($user_data!=null)	
			{
				$user  = $user_data[0]['name'];
				$data['user']=	$user_data[0]['name'];
				$this->load->helper(array('form', 'url'));
				$this->load->library('validation');
				$this->validation->set_error_delimiters('<span style="color:red;">', '<br /></span>');
				
				$fields['old_pass'] = 'old_pass';
				$fields['new_pass'] = 'new_pass';
				$fields['new_pass_repeat'] = 'new_pass_repeat';
				$this->validation->set_fields($fields);
				
				$new_pass = $this->validation->new_pass;
				$new_pass_repeat = $this->validation->new_pass_repeat;
				
				$rules['old_pass'] = "callback_old_pass_check[$user]|required";
				$rules['new_pass'] = "callback_new_pass_check[$new_pass_repeat]|required";
				$rules['new_pass_repeat'] = "callback_new_pass_repeat_check[$new_pass]|required";
				$this->validation->set_rules($rules);
				
				if ($this->validation->run() == FALSE)
				{
					$this->load->view('/layout/change_pass_page',$data);
				}
				else
				{		
					if($new_pass!=null)
					{
						$haslo = md5($new_pass);
	
						if($this->baza->superadmin_user_new_pass($user,$haslo))
						{			
							$data['nowe_haslo'] = $new_pass;
							redirect('/superadmin/logout','refresh');
							
							
						}else $this->load->view('blad',$data);
					}else $this->load->view('blad',$data);
				}
			}
			else
			{
				redirect('/superadmin/logout','refresh');
			}	
		}
		else
		{
			redirect('/superadmin/','refresh');
		}
	}
	
	
	function old_pass_check($str,$user)
	{
		$password_baza=$this->baza->superadmin_user($user); 
		$pass =  md5($str);
		
		if($password_baza==null)
		{
			$password_baza[0]['password']='1';
		}
		
		if ($password_baza[0]['password']!=$pass or $pass==null)
		{
				$this->validation->set_message('old_pass_check', 'Błędne hasło');
				return FALSE;
		}
		else
		{
				return TRUE;
		}
	}
	
	function new_pass_check($str,$pass_repeat)
	{	
		if ($str!=$pass_repeat or $str==null or $pass_repeat==null )
		{
				$this->validation->set_message('new_pass_check', 'Hasło nowe i hasło nowe powtórzopne muszą być takie same');
				return FALSE;
		}
		else
		{
				return TRUE;
		}
	}
	
	function new_pass_repeat_check($str,$new_pass)
	{	
		if ($str!=$new_pass or $new_pass==null or $str=null)
		{
				$this->validation->set_message('new_pass_repeat_check', 'Hasło nowe i hasło nowe powtórzopne muszą być takie same');
				return FALSE;
		}
		else
		{
				return TRUE;
		}
	}
	
	function logout()
	{
		$this->session->sess_destroy();
		redirect('','refresh');
	}
	
	function forgotten_pass()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<span style="color:red;">', '<br /></span>');
		
		$fields['email'] = 'email';
		$this->validation->set_fields($fields);
		
		$rules['email'] = "callback_email_check|required";
		$this->validation->set_rules($rules);
		$email = $this->validation->email;
		
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('layout/forgotten_pass_page');
		}
		else
		{	
			$pass = substr ( md5 ( uniqid ( rand(), true ) ), 0, 5 );
			$haslo = md5($pass);

			if($email!=null)
			{
				//zmienna email zawiera login a nie jak nazwawskazuje email 
				$is_user = $this->baza->user_get_email($email);
				
				if($is_user!=null)
				{
					if($this->baza->superadmin_user_new_pass($email,$haslo))
					{	
						$tresc = 'Twoje nowe hasło to:<br /><br />
									'.$pass.'<br /><br />
									<a href="http://admin.networds.mariusz.jrchosting.com/superadmin" >Zaloguj się</a>
							';	
							
							if($this->send_mail('Przypomnienie hasła', $tresc, $is_user[0]['email'])) 
							{
								$this->load->view('/new_pass_created');
							}	
					}else $this->load->view('/blad');
				}else	$this->load->view('/blad');
			}else $this->load->view('/blad');
		}
	}
	
	function email_check($str)
	{
		$user=$this->baza->superadmin_user($str); 
		
		if ($user==null or $str==null)
		{
				$this->validation->set_message('email_check', 'Błędny login');
				return FALSE;
		}
		else
		{
				return TRUE;
		}
	}
	
	function send_mail($title=null, $content_email=null, $email=null)
	{			
		require_once "swift_mailer/Swift.php";
		require_once "swift_mailer/Swift/Connection/SMTP.php";
		require_once "swift_mailer/Swift/Authenticator/LOGIN.php";

		$conn =& new Swift_Connection_SMTP("ubieranki.jrchosting.com");
		$conn->setUsername("mailer@ubieranki.org");
		$conn->setPassword("mailer0987");
		$swift =& new Swift($conn);
		$message =& new Swift_Message($title, $content_email, 'text/html');
		return $swift->send($message, $email, "mailer@ubieranki.org");
		
	}
}
?>