<?php
	//error_reporting(1);
	require_once('files/nusoap/nusoap.php');
 	require_once('files/lib/DB.config.php');
	require_once('files/lib/DBManager.class.php');
	require_once('files/lib/TicketModel.class.php');
 	
	$NAMESPACE = 'http://networds.datatask.net/';
	DEFINE('BASE_URL','http://networds.datatask.net/');

	
	 
	$server = new soap_server;
	$server->configureWSDL('ticketsWSDL', $NAMESPACE);
	$server->wsdl->schemaTargetNamespace = $NAMESPACE;
	$ini = ini_set("soap.wsdl_cache_enabled", 0);
	$server->soap_defencoding = 'UTF-8';
	$server->decode_utf8 = false;
	
	//login('admin','admin');
	
	$server->wsdl->addComplexType(
	    'Admin',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' => array('name'=>'id'				,'type'=>'xsd:string'),
	        'name' => array('name'=>'name'			,'type'=>'xsd:string'),
	        'password' => array('name'=>'password'	,'type'=>'xsd:string'),
	        'email' => array('name'=>'email'		,'type'=>'xsd:string')
	    )
	);
	
	$server->wsdl->addComplexType('Admins',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Admin[]')
					),
					'tns:Admin'
				);	

	//status
	$server->wsdl->addComplexType(
	    'Status',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 	=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'nazwa' => array('name'=>'nazwa'			,'type'=>'xsd:string'),
	    )
	);
	
	$server->wsdl->addComplexType('Statusy',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Status[]')
					),
					'tns:Status'
				);	
	//end of status
	
	//Kategorie
	$server->wsdl->addComplexType(
	    'Kategoria',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 	=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'nazwa' => array('name'=>'nazwa'		,'type'=>'xsd:string')
	    )
	);
	
	
	
	$server->wsdl->addComplexType('Kategorie',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Kategoria[]')
					),
					'tns:Kategoria'
				);
	//end of kategorie
	
	//priorytety
	$server->wsdl->addComplexType(
	    'Priorytet',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 	=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'nazwa' => array('name'=>'nazwa'		,'type'=>'xsd:string')
	    )
	);
	
	
	
	$server->wsdl->addComplexType('Priorytety',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Priorytet[]')
					),
					'tns:Priorytet'
				);					
		
	//user
	$server->wsdl->addComplexType(
	    'User',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 		=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'id_konta'  => array('name'=>'id_konta'		,'type'=>'xsd:string'),
			'name' 		=> array('name'=>'name'			,'type'=>'xsd:string'),
			'password'  => array('name'=>'password'		,'type'=>'xsd:string'),
			'email'  	=> array('name'=>'email'		,'type'=>'xsd:string'),
			'user_type' => array('name'=>'user_type'	,'type'=>'xsd:string')
	    )
	);	
	

	
	//Zalacznik
	$server->wsdl->addComplexType(
	    'Zalacznik',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 			=> array('name'=>'id'			,'type'=>'xsd:int'),
	        'sciezka' 		=> array('name'=>'sciezka'		,'type'=>'xsd:string')
	    )
	);	
	
	$server->wsdl->addComplexType('Zalaczniki',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Zalacznik[]')
					),
					'tns:Zalacznik'
				);	
				
	$server->wsdl->addComplexType('ZalacznikiTab',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Zalaczniki[]')
					),
					'tns:Zalaczniki'
				);					
	//end of zalacznik
	
	
	//ticket
	$server->wsdl->addComplexType(
	    'Ticket',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 				=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'parent_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'kategoria_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'status_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'priorytet_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'nadawca_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'odbiorca_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'temat' 			=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'wiadomosc' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'data_wyslania' 	=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'data_odebranie' 	=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
	    )
	);
	
	$server->wsdl->addComplexType('Tickets',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:Tickets[]')
					),
					'tns:Tickets'
				);	
	//end of ticket
	
	
	//ticket extended
	$server->wsdl->addComplexType(
	    'TicketExtended',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 				=> array('name'=>'id'			,'type'=>'xsd:string'),
	        'parent_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'kategoria_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'status_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'priorytet_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'nadawca_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'odbiorca_id' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'temat' 			=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'wiadomosc' 		=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'data_wyslania' 	=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'data_odebranie' 	=> array('name'=>'nazwa'		,'type'=>'xsd:string'),
			'kategoria' 	=> array('name'=>'kategoria'		,'type'=>'xsd:string'),
			'status' 		=> array('name'=>'status'			,'type'=>'xsd:string'),
			'priorytet' 	=> array('name'=>'priorytet'		,'type'=>'xsd:string'),
			'zalaczniki' 	=> array('name'=>'zalaczniki'		,'type'=>'xsd:string'),
			'nadawca' 		=> array('name'=>'nadawca'			,'type'=>'xsd:string'),
			'odbiorca' 		=> array('name'=>'odbiorca'			,'type'=>'xsd:string'),
	    )
	);
	
	$server->wsdl->addComplexType('TicketsExtended',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:TicketExtended[]')
					),
					'tns:TicketExtended'
				);			
	//end of ticket extended
	
	$server->wsdl->addComplexType(
		'ShowTicketResponse',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'ticket' 				=> array('name'=>'ticket'			,'type'=>'tns:TicketExtended'),
			'owner_id' 				=> array('name'=>'owner_id'			,'type'=>'xsd:string'),
			'child_tickets'			=> array('name'=>'ticket'			,'type'=>'tns:TicketsExtended'),
			'zalaczniki'			=> array('name'=>'zalaczniki'		,'type'=>'tns:Zalaczniki')
	    )
	);	
	
	$server->wsdl->addComplexType(
		'AddResponseFormResponse',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'statusy' 					=> array('name'=>'statusy'					,'type'=>'tns:Statusy'),
			'kategorie' 				=> array('name'=>'kategorie'				,'type'=>'tns:Kategorie'),
			'priorytety'				=> array('name'=>'priorytety'				,'type'=>'tns:Priorytety'),
			'parent_ticket'				=> array('name'=>'parent_ticket'			,'type'=>'tns:TicketExtended'),
			'parent_ticket_zalaczniki'	=> array('name'=>'parent_ticket_zalaczniki'		,'type'=>'tns:Zalaczniki'),
			'child_tickets'				=> array('name'=>'child_tickets'			,'type'=>'tns:TicketsExtended'),
			'child_tickets_zalaczniki'	=> array('name'=>'child_tickets_zalaczniki'	,'type'=>'tns:ZalacznikiTab'),
			'can_response'				=> array('name'=>'can_response'				,'type'=>'xsd:boolean')
	    )
	);		
					
	//ticket list item
	$server->wsdl->addComplexType(
	    'ticket_list_item',
	    'complexType',
	    'struct',
	    'all',
	    '',
	    array(
	        'id' 				=> array('name'=>'id'					,'type'=>'xsd:string'),
	        'parent_id' 		=> array('name'=>'parent_id'			,'type'=>'xsd:string'),
	        'kategoria_id' 		=> array('name'=>'kategoria_id'			,'type'=>'xsd:string'),
	        'status_id' 		=> array('name'=>'status_id'			,'type'=>'xsd:string'),
			'priorytet_id' 		=> array('name'=>'priorytet_id'			,'type'=>'xsd:string'),
			'nadawca_id' 		=> array('name'=>'nadawca_id'			,'type'=>'xsd:string'),
			'odbiorca_id' 		=> array('name'=>'odbiorca_id'			,'type'=>'xsd:string'),
			'temat' 			=> array('name'=>'temat'				,'type'=>'xsd:string'),
			'wiadomosc' 		=> array('name'=>'wiadomosc'			,'type'=>'xsd:string'),
			'data_wyslania' 	=> array('name'=>'data_wyslania'		,'type'=>'xsd:string'),
			'data_odebrania' 	=> array('name'=>'data_odebrania'		,'type'=>'xsd:string'),
			'odebrano' 			=> array('name'=>'odebrano'				,'type'=>'xsd:string'),
			'kategoria' 		=> array('name'=>'kategoria'			,'type'=>'xsd:string'),
			'liczba_odpowiedzi' => array('name'=>'liczba_odpowiedzi'	,'type'=>'xsd:string'),
			'status' 			=> array('name'=>'status'				,'type'=>'xsd:string'),
			'priorytet' 		=> array('name'=>'priorytet'			,'type'=>'xsd:string'),
			'zalaczniki' 		=> array('name'=>'zalaczniki'			,'type'=>'xsd:string'),
			'nadawca' 			=> array('name'=>'nadawca'				,'type'=>'xsd:string'),
			'odbiorca' 			=> array('name'=>'odbiorca'				,'type'=>'xsd:string')
	    )
	);	
	
	$server->wsdl->addComplexType('ticket_list_items',
					'complexType',
					'array',
					'',
					'SOAP-ENC:Array',
					array(),
					array(
						array('ref'=>'SOAP-ENC:arrayType',
						'wsdl:arrayType'=>'tns:ticket_list_item[]')
					),
					'tns:ticket_list_item'
				);
	
	$server->register(
	    'hello_world',
	    array('name'=>'xsd:string'),
	    array('return'=>'xsd:string'),
	    $NAMESPACE);	
		
	$server->register(
	    'login',
	    array('name'=>'xsd:string','password'=>'xsd:string'),
	    array('return'=>'tns:User'),
	    $NAMESPACE);				
	
	$server->register(
		'getOdebrane',
	    array('user'=>'tns:User','monthCondition'=>'xsd:int'),
	    array('return'=>'tns:ticket_list_items'),
	    $NAMESPACE);
		
	$server->register(
		'getWyslane',
	    array('user'=>'tns:User','monthCondition'=>'xsd:int'),
	    array('return'=>'tns:ticket_list_items'),
	    $NAMESPACE);	
	
	$server->register(
		'showTicket',
		 array('user'=>'tns:User','ticketId'=>'xsd:int'),
		 array('return'=>'tns:ShowTicketResponse'),
		 $NAMESPACE);		
		
	$server->register(
		'addTicket',
		 array('user'=>'tns:User','ticket'=>'tns:Ticket','zalaczniki'=>'tns:Zalaczniki'),
		 array('return'=>'xsd:boolean'),
		 $NAMESPACE);
		 
	$server->register(
		'addResponse',
		 array('user'=>'tns:User','ticket'=>'tns:Ticket','zalaczniki'=>'tns:Zalaczniki'),
		 array('return'=>'xsd:boolean'),
		 $NAMESPACE);
		 
	$server->register(
		'addResponseForm',
		 array('user'=>'tns:User','$ticket_parent_id'=>'xsd:int'),
		 array('return'=>'tns:AddResponseFormResponse'),
		 $NAMESPACE);	
		 
	$server->register(
		'getMinutesToEndSession',
		 array('ticket_id'=>'xsd:int'),
		 array('return'=>'xsd:int'),
		 $NAMESPACE);	
		 
	$server->register(
		'resetSession',
		 array('ticket_id'=>'xsd:int'),
		 array('return'=>'xsd:boolean'),
		 $NAMESPACE);			 	 	 			
	    	    
	function hello_world($name)
	{
		
	    return 'Hello '.$name;
	}
	function getAdmins()
	{
		try{
				$mysqli = DBManager::getConnection();
				$sql    = 'SELECT * FROM admin';
				$result = $mysqli->query($sql);
					if($mysqli->error)
						throw new Exception($mysqli->error);
					$data;	
					    while ($row = $result->fetch_assoc()) {
						       $data[] = $row;
						    }					
						
						return $data;
			}
		catch(Exception $e)
			{
				//echo 'Wystąpił błąd: '.$e->getMessage().'<br />';
			}
		return FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function login($name,$password)
	{
			
		try
		{
			$mysqli = DBManager::getConnection();
			$sql    = 'SELECT a.id AS id,k.id AS id_konta,a.name AS name,a.password AS password ,a.email AS email, \'admin\' AS user_type
			           FROM konta k JOIN admin a ON k.origin_id=a.id
			           WHERE a.name=\''.$name.'\' AND a.password=md5(\''.$password.'\')';
					   
							   
			$result = $mysqli->query($sql);
			if($mysqli->error)
				throw new Exception($mysqli->error);
			if($result->num_rows == 1)
			{
				$user 				= $result->fetch_assoc();
				return $user;				
			}
			$result->close();
		}	
		catch(Exception $e)
		{
			//echo 'Error'.$e->getMessage().'<br />';							
		}	
		return FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function login2($name,$password)
	{
			
		try
		{
			$mysqli = DBManager::getConnection();
			$sql    = 'SELECT a.id AS id,k.id AS id_konta,a.name AS name,a.password AS password ,a.email AS email, \'admin\' AS user_type
			           FROM konta k JOIN admin a ON k.origin_id=a.id
			           WHERE a.name=\''.$name.'\' AND a.password=\''.$password.'\'';
					   
							   
			$result = $mysqli->query($sql);
			if($mysqli->error)
				throw new Exception($mysqli->error);
			if($result->num_rows == 1)
			{
				$user 				= $result->fetch_assoc();
				return $user;				
			}
			$result->close();
		}	
		catch(Exception $e)
		{
			//echo 'Error'.$e->getMessage().'<br />';							
		}	
		return FALSE;
	}	
	/*----------------------------------------------------------------------------------------------------------------*/
	function getOdebrane($user,$monthCondition=null)
	{
		if(login2($user['name'],$user['password']))
		{
			$ticketModel = new TicketModel();
			
			try
			{
				$ids = $ticketModel->get_received_list_of_ticket_id($user['id_konta'],$monthCondition);
				$data = null;
				
				for($i=0;$i<count($ids);$i++)
				{
					$data[] = $ticketModel->get_received_ticket_list_item((int)$ids[$i],$monthCondition);		
				}
				
				return $data;
			}
			catch(Exception $e)
			{
				//echo 'Error'.$e->getMessage().'<br />';							
			}	
			return FALSE;	
		}
		else
			return FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/			
	function getWyslane($user,$monthCondition=null)
	{
		if(login2($user['name'],$user['password']))
		{
			$ticketModel = new TicketModel();
			
			try		
			{
				$ids 	= $ticketModel->get_sended_list_of_ticket_id($user['id_konta'],$monthCondition);
				$data 	= null;
				
				for($i=0;$i<count($ids);$i++)
				{
					if($i<count($ids))
						$data[] = $ticketModel->get_received_ticket_list_item((int)$ids[$i],$monthCondition);		
				}
				return $data;			
			}
			catch(Exception $e)
			{
				//echo 'Error'.$e->getMessage().'<br />';							
			}	
			return FALSE;	
		}
		else
			return FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function showTicket($user,$ticketId)
	{
		$ticketModel = new TicketModel();
		if(login2($user['name'],$user['password']) && $user['user_type'] = 'admin' /*&& $ticketModel->does_user_have_permision_to_ticket($ticketId,$user['id_konta'])*/)
		{
			$data					=	null;
			$data['ticket']			= $ticketModel->get($ticketId);
			
			if($data['ticket']->data_odebrania == '')
				$ticketModel->set_data_odebrania($ticketId);
			
			$ticketModel->clear_ticket_owners_session();	//czyszczenie starych sesji odpowiedzi do ticketów
			$data['owner_id']		= $ticketModel->get_ticket_owner($ticketId);	
							
			if($data['ticket']->parent_id != '')
				{
					$data['ticket']	= $ticketModel->get($data['ticket']->parent_id);					
					$ticketId       = $data['ticket']->id;
				}
	
			$data['child_tickets'] = $ticketModel->get_child_tickets_by($ticketId);
			$data['zalaczniki']    = $ticketModel->get_attachment_list($ticketId);
			
			return $data;	
		}
		else
			echo FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function addTicket($user,$ticket,$zalaczniki)
	{
		//add($nadawca,$odbiorca,$kategoria,$status,$priorytet,$temat,$wiadomosc,$files,$parent_id=0)
		//print_r($ticket);
		if(login2($user['name'],$user['password']))
		{
			try
			{
				$ticketModel = new TicketModel();
				
				if(!$ticketModel->add($ticket['nadawca_id'],$ticket['odbiorca_id'],$ticket['kategoria_id'],$ticket['status_id'],$ticket['priorytet_id'],$ticket['temat'],$ticket['wiadomosc'],$zalaczniki))
					throw new Exception();
				
				
				//nadawca
				$status_name 		= $ticketModel->get_status_name_by_id($ticket['status_id']);
				$nadawca_email		= $ticketModel->get_user_email_by_account_id($ticket['nadawca_id']);
				$nadawca_type   	= $ticketModel->get_user_type_by_account_id($ticket['nadawca_id']);
				send_email($ticket_id,$status_name,$ticket['kategoria_id'],$nadawca_email,$nadawca_type); //do nadawcy
				
				//odbiorca					
				$odbiorca_email		= $ticketModel->get_user_email_by_account_id($ticket['odbiorca_id']);
				$odbiorca_type   	= $ticketModel->get_user_type_by_account_id($ticket['odbiorca_id']);
				send_email($ticket_id,$status_name,$ticket['kategoria_id'],$odbiorca_email,$odbiorca_type);					
				
			}
			catch(Exception $e)
			{
				print_r($e);				
			}
			return TRUE;
		}
		else
			return FALSE;
		
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function addResponse($user,$ticket,$zalaczniki)
	{
		
		if(login2($user['name'],$user['password']))
		{
			
			try
			{
							
				$ticketModel = new TicketModel();
				
				if(!$ticketModel->add($ticket['nadawca_id'],$ticket['odbiorca_id'],$ticket['kategoria_id'],
				        $ticket['status_id'],$ticket['priorytet_id'],$ticket['temat'],
				        $ticket['wiadomosc'],$zalaczniki,$ticket['parent_id']))		
					throw new Exception();
					
				//usuniecie sesji ticket ownera
				$ticketModel->delete_ticket_owner_session($parent_id);

				//wysylanie wiadomosci email
				//nadawca
				$status_name 		= $ticketModel->get_status_name_by_id($ticket['status_id']);
				$nadawca_email		= $ticketModel->get_user_email_by_account_id($ticket['nadawca_id']);
				$nadawca_type   	= $ticketModel->get_user_type_by_account_id($ticket['nadawca_id']);
				send_email($ticket['parent_id'],$status_name,$ticket['kategoria_id'],$nadawca_email,$nadawca_type); //do nadawcy
				
				//odbiorca					
				$odbiorca_email		= $ticketModel->get_user_email_by_account_id($ticket['odbiorca_id']);
				$odbiorca_type   	= $ticketModel->get_user_type_by_account_id($ticket['odbiorca_id']);
				send_email($ticket['parent_id'],$status_name,$ticket['kategoria_id'],$odbiorca_email,$odbiorca_type);			

			}
			catch(Exception $e)
			{
				return FALSE;		
			}
			return TRUE;	
		}
		else
			return FALSE;
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function addResponseForm($user,$ticket_parent_id)
	{
		if(login2($user['name'],$user['password']))
		{
			
			$ticketModel 				= new TicketModel();
			
			$data['statusy']			= $ticketModel->get_status_list();
			$data['kategorie']			= $ticketModel->get_category_list();
			$data['priorytety']			= $ticketModel->get_priority_list();
			
			$data['parent_ticket']		= $ticketModel->get($parent_id);
			$data['parent_zalaczniki']  = $ticketModel->get_attachment_list($parent_id);
			
			$data['child_tickets'] 		= $ticketModel->get_child_tickets_by($parent_id);
			
			$data['child_tickets_zalaczniki'] = null;
					
			if(count($data['child_tickets'] ))
			{		
				foreach($data['child_tickets'] as $key=>$value)
				{
					$data['child_tickets_zalaczniki'][$value->id] = $ticketModel->get_child_tickets_by($value->id);				
				}	
			}
			
			$data['can_response'] 		= false;		
			
			//ustawienie ownera - gdy admin
			if($user['type'] == 'admin')
			{
				$owner_id = $ticketModel->get_ticket_owner($parent_id);
				if(empty($owner_id))
				{
					$ticketModel->set_owner($data['user_account']['id'],$parent_id);
					$data['can_response'] = true;
				}
				else
				{
					if($user['id_konta'] == $owner_id)
					{
						$data['can_response'] = true;
						//update session
						$ticketModel->update_ticket_owner($parent_id);
					}
					else
					{
						$data['can_response'] = false;						
					}
				}
			}
			return $data;			
			
		}	
		else
			return FALSE;		
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function getMinutesToEndSession($ticketId)
	{
		if(login2($user['name'],$user['password']))
		{
			$ticketModel = new TicketModel();
			return $ticketModel->get_minutes_to_end_session($ticket_id);
		}
		else
			return -1;	
	}
	/*----------------------------------------------------------------------------------------------------------------*/
	function resetSession($ticketId)
	{
		if(login2($user['name'],$user['password']))
		{
			$ticketModel = new TicketModel();
			if($ticketModel->update_ticket_owner($ticket_id))
				return TRUE;
			else
				return FALSE;
		}
		else
			return FALSE;			
	}	
	/*----------------------------------------------------------------------------------------------------------------*/
	
	// Use the request to (try to) invoke the service
	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	$server->service($HTTP_RAW_POST_DATA);
	/*
	$us = login('admin','admin');
	
                $t['parent_id'] = "1";
                $t['kategoria_id'] = "1";
                $t['nadawca_id'] = "22";
                $t['odbiorca_id'] = "23";
                $t['priorytet_id'] = "1";
                $t['status_id'] = "1";
                $t['temat'] = "WStest";
                $t['wiadomosc'] = "wiadomosc testowa";

                
                $z['sciezka'] = "test/test.jpg";

                $zals[] = $z;
				
				echo '1';
				add_Response($us,$t,$z);				
				
				
	print_r(addResponseForm($us,1));*/
	//print_r(showTicket($us,6));
	//var_dump($us);
	//print_r(add(22,23,1,1,1,'XoX','Xcxc',null,0));
	//var_dump(getOdebrane());
	//logout();
	
	
	/*---------------------------------------------------------------------*/
	//PRIVATE FUNCTIONS:
	/*---------------------------------------------------------------------*/
	/*----------------------------------------------------------------------------------------------------------------*/
	function send_email($ticket_id,$ticket_status,$ticket_kategoria,$do,$user_type)
	{
		//$do 			- tablica maili do których zostanie wyslana wiadomosc 
		//$user_type	- nazwa okreslajaca nadawce [user | afiliant | admin]
	
		require_once 'files/swift-4.0.5/lib/swift_required.php';
		require_once('files/lib/SMTP.config.php');
		
		$ticketModel = new TicketModel();
		
		//filtr .. jesli admin wyslij na alias 
		if($user_type == 'admin')
			$do = $alias_emails[$ticket_kategoria];
		
		 $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER,SMTP_PORT)
		  ->setUsername(SMTP_USER)
		  ->setPassword(SMTP_PASSWORD);
		
		$mailer = Swift_Mailer::newInstance($transport);
		
		
		$user_controller = null;
		switch($user_type)
		{
			case 'user'  		: $user_controller = 'admin'; 		break;
			case 'afiliant'		: $user_controller = 'afiliant'; 	break;
			case 'admin'		: $user_controller = 'superadmin';	break;		
		}
		
			//kodowanie parametrow
			$user_controller_encoded = $ticketModel->encode($user_controller);
			$ticket_id_encoded 		 = $ticketModel->encode($ticket_id);
		
		
		//Create a message
		$body =  'Wiadomość w systemie networds została '.substr($ticket_status,0,strlen($ticket_status)-1).'a.'.'<br/>'; 
		//$body .= 'Nadawca: '.'<br/>';
		
		if($user_controller != null)
			$body .= 'Wiadomość jest dostępna pod adresem: <br/><a href="'.BASE_URL.'tickets/show/'.$ticket_id_encoded.'/'.$user_controller_encoded.'">'.BASE_URL.'tickets/show/'.$ticket_id_encoded.'/'.$user_controller_encoded.'</a><br/><br/>';
			 
		$body .= 'Email został automatycznie wygenerowany przez system Networds.';
		
	
		$message = Swift_Message::newInstance('Wiadomość Networds: '.$ticket_id)
		  ->setFrom(array(SMTP_USER => 'Networds System'))
		  ->setTo($do)
		  ->setBody($body, 'text/html', 'utf-8');
		  ;
		
		$headers = $message->getHeaders();
		$headers->addTextHeader('Reply-To',SMTP_USER);
		     
		//Send the message
		$result = $mailer->send($message);
		return $result; 		
	}	
?>