<?php
class Partners extends Controller
{
	var $access = array(
		'36' => 'archxp2009',
		'39' => 'ita456789x',
		'43' => 'ger123456y',
		'491' => 'ger2020x',
		'492' => 'ger2020y',
		'493' => 'ger2020z',
		'494' => 'ger2020w',
		'49' => 'gercad123x',
		'41' => 'ger123456y',
		'30' => 'grcad23456g',
		'42' => 'cz9876543',
		'34' => 'espcad5679e',
		'38' => 'crcad2009',
		'52' => 'mexcad2009',
		'82' => 'korcad123',
		'48' => 'polcad456789x',
		'81' => 'jap2009',
		'40' => 'rocad2009',
		'94' => 'ita456789x94',
		'84' => 'viecad2019',
		'61' => 'aucad123456x',
		'33' => '3RUqyUkz7QrVuAXGB3RdpEZvjL44py',
		'37' => 'test2019',
		'371' => 'test2019',
		'88' => 'tw56789',
		'71' => '3dlotus',
		'60' => 'acad2021',
		'86' => 'disti345',
		'51' => 'iber567',
		'11' => 'us2020',
		'55' => 'tCAD_2021',
		'490' => 'test2020',
		'48x' => 'polcad2021x',
		'65' => '5HyRF!x8',
		'90' => 'spex2022',
		'44' => 'seeit_2022',
		'431' => 'Ger567!zx',
		'495' => 'Breeze754_lkj',
		'Anonym' => 'Test_2022'
	);


	function __construct()
	{
		parent::__construct();

		header('location: https://partners.staging.cadline.hu/');

		if (!$this->_check_access() && $this->uri->segment(2) != 'login')
			redirect('partners/login');

		if ($this->_check_access() && $this->uri->segment(2) == 'login')
			redirect('partners');

		$username 	= $this->phpsession->get('access_id');
		$partner	= $this->db->query("SELECT ctrID FROM cl_hlusers.partner_authenticate WHERE username = '{$username}'")->row();

		$this->access_id = $partner->ctrID;

		if ($this->phpsession->get('access_id') == '431') {
			redirect('/resourcemanager/index');
		}

		$this->load->library('PasswordHash');
		$this->load->model('mpartners');
	}

	function login()
	{
		$password = $this->input->post('pass');
		$username = $this->input->post('user');

		$user = $this->db->query("SELECT username, password FROM cl_hlusers.partner_authenticate WHERE username = '{$username}'")->row();
		$phpass = new PasswordHash(10, true);
		$ok     = $phpass->CheckPassword($password, $user->password);

		if ($ok) {
			$this->phpsession->save('bugtracker_access', true);
			$this->phpsession->save('access_id', $username);

			redirect('partners');
		}

		$data['noindex'] = true;
		$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('/partners/login');
	}


	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()
	{
		if (UJUGYFELKEZELO === true)
			$this->keydb = $this->load->database('ujugyfelkezelo', true);
		else
			$this->keydb = $this->load->database('clusers', 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;

		$hidden = array(-1);

		// megszunt 2018-01-25
		/*
    $tmp=$this->keydb->from('hardlock')->where("hlDealer IN ('MAIER','STOCKINGER')")->limit(99999)->get();
		if($tmp->num_rows()>0) { foreach ($tmp->result() as $k=>$row) { $hidden[]=$row->hlNum; } }

    $tmp=$this->keydb->from('h_programs')->where("prDealer IN ('MAIER','STOCKINGER')")->limit(99999)->get();
		if($tmp->num_rows()>0) { foreach ($tmp->result() as $k=>$row) { $hidden[]=$row->prHlNum; } }

    $hidden=array_unique($hidden);
*/

		$x_hlNums = array('490210', '490211', '490212', '490213', '490214', '490221', '490233');
		$x_hlNums = implode(", ", $x_hlNums);

		if (!$search) {
			if ($empty) {
				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlNum in ({$x_hlNums})")->where('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();
					else
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->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('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();

				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlNum in ({$x_hlNums})")->where('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
					else
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
				} else
					$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users', 'cl_fo_hardlock.hlUser=cl_users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 0)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
			} elseif ($disabled) {
				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlNum in ({$x_hlNums})")->where('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();
					else
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->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('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();

				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlNum in ({$x_hlNums})")->where('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
					else
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
				} else
					$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users', 'cl_fo_hardlock.hlUser=cl_users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where('hlStat', 2)->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
			} else {
				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlNum in ({$x_hlNums})")->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();
					else
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'desc')->where("hlCtrID ='" . $this->access_id . "'")->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->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 . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit(99999)->get();

				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlNum in ({$x_hlNums})")->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
					else
						$result_limit = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->limit($limit, $min)->get();
				} else
					$result_limit = $this->keydb->from('cl_fo_hardlock')->join('cl_users', 'cl_fo_hardlock.hlUser=cl_users.nUserID', 'left')->orderby('hlNum', 'asc')->where("hlCtrID ='" . $this->access_id . "'")->where("hlNum >= '" . $from . "' AND hlNum <= '" . $to . "'")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->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)) {
				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlTime', 'desc')->where("hlNum like '%$search%'")->where("hlNum in ({$x_hlNums})")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->get();
					else
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlTime', 'desc')->where("hlNum like '%$search%'")->where('hlCtrID', $this->access_id)->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->get();
				} else
					$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)->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->get();

				if ($result->num_rows() == 0) {
					$data['sorok'] = false;
				} else {
					$data['sorok'] = $result->result();
					$data['found'] = $result->num_rows();
				}
			} else {
				if (UJUGYFELKEZELO === true) {
					if ($this->access_id == '490')
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlTime', 'desc')->where("hlNum in ({$x_hlNums}) and ( strCompany like '%$search%' or strName like '%$search%' or hlUser like '%$search%')")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->get();
					else
						$result = $this->keydb->from('hardlock')->join('users', 'hardlock.hlUser=users.nUserID', 'left')->orderby('hlTime', 'desc')->where("hlCtrID=" . $this->access_id . " and ( strCompany like '%$search%' or strName like '%$search%' or hlUser like '%$search%')")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->get();
				} 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%')")->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->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()
	{
		if (UJUGYFELKEZELO === true)
			$this->keydb = $this->load->database('ujugyfelkezelo', true);
		else
			$this->keydb = $this->load->database('clusers', true);
		$data['access_id'] = $this->phpsession->get('access_id');

		$hidden = array(-1);

		// megszunt 2018-01-25
		/*
    $tmp=$this->keydb->from('hardlock')->where("hlDealer IN ('MAIER','STOCKINGER')")->limit(99999)->get();
		if($tmp->num_rows()>0) { foreach ($tmp->result() as $k=>$row) { $hidden[]=$row->hlNum; } }

    $tmp=$this->keydb->from('h_programs')->where("prDealer IN ('MAIER','STOCKINGER')")->limit(99999)->get();
		if($tmp->num_rows()>0) { foreach ($tmp->result() as $k=>$row) { $hidden[]=$row->prHlNum; } }

    $hidden=array_unique($hidden);
*/
		$key_id = $this->uri->segment(3);
		if (!$key_id) {
			redirect('partners/keys');
		} else {
			if (UJUGYFELKEZELO === true)
				$key = $this->keydb->where('hlNum', $key_id)->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->join('users', 'hardlock.hlUser=users.nUserID', 'left')->get('hardlock');
			else
				$key = $this->keydb->where('hlNum', $key_id)->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->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 != substr($this->access_id, 0, 2) && !$this->mpartners->isAdmin()) {
				show_error("Access denied to key no. " . $key_id);
			}

			//////////////////////////////////////////////////////

			if (UJUGYFELKEZELO === true)
				$programs = $this->keydb->where('prHlNum', $key_id)->join('p_types', 'h_programs.prTypeID=p_types.typeCode')->join('p_versions', 'h_programs.prVerID=p_versions.verID', 'left')->order_by("prID")->get('h_programs');
			else
				$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')->order_by("prID")->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 = array();
				$programs2 = $this->keydb->where('prHlNum', $key_id)->get('h_programs')->result();

				foreach ($programs2 as $program) {
					$prmodules = $this->keydb->where('mdPrID', $program->prID)->get('p_modules')->result();
					$modules = array_merge($prmodules, $modules);
				}

				//print_r($modules->result());
				//print $this->keydb->last_query();
			} else {
				$modules = false;
			}


			//////////////////////////////////////////////////////

			if (UJUGYFELKEZELO === true)
				$history = $this->keydb->where('hiHlNum', $key_id)->join('h_events', 'h_events.evid=h_history.hiTypeID', 'left')->get('h_history');
			else
				$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();
			}

			//////////////////////////////////////////////////////
			$softwareKeyArray = array(95, 99);

			$data['csik'] = array('home', 'partners', 'keys');
			$data['tartalom'] = 'partners/key';
			$data['key'] = $key;
			$data['programs'] = $programs;
			$data['modules'] = $modules;
			$data['history'] = $history;
			$data['isSoftwareKey'] = (in_array(substr($key_id, 0, 2), $softwareKeyArray)) || ($programs[0]->prTypeID == '4');

			$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;
	}

	function changeKeyUser()
	{
		$name = $this->input->post('user_name');
		$email = $this->input->post('user_email');
		$hlNum = $this->input->post('hlNum');
		$nUserID = $this->input->post('nUserID');
		$isFloating = (int)$this->input->post('isFloating') ? (int)$this->input->post('isFloating') : 0;

		$this->keydb = $this->load->database('ujugyfelkezelo', true);
		$hidden = array(-1);

		$key = $this->keydb->where('hlNum', $hlNum)->where("hardlock.hlNum NOT IN (" . implode(",", $hidden) . ")")->join('users', 'hardlock.hlUser=users.nUserID', 'left')->get('hardlock')->row();
		if ($key->hlCtrID != substr($this->access_id, 0, 2) && !$this->mpartners->isAdmin()) {
			redirect('partners/key/' . $hlNum);
			return;
		}

		$newNuserID = '';
		$needUserUpdate = false;

		$u_email = $this->db->query("SELECT nUserID FROM cl_hlusers.u_emails WHERE email = '{$email}'")->row();
		if (!empty($u_email) && $email != '') {
			$newNuserID = $u_email->nUserID;
			$needUserUpdate = true;
		} else {
			if ($email != '') {
				$jml_user = $this->db->query("SELECT nUserID FROM www_archline_hu.jml_users WHERE email = '{$email}' OR username = '{$email}'")->row();

				if (!empty($jml_user))
					$newNuserID = $jml_user->nUserID;
				else
					$newNuserID = $this->mpartners->insertUser($name, $email, $this->access_id);

				$needUserUpdate = true;
			}
		}

		if ($needUserUpdate) {
			$updateQuery = "UPDATE cl_hlusers.hardlock
				SET hlUser = '{$newNuserID}', isFloating = {$isFloating}
				WHERE hlNum = '{$hlNum}'";
		} else {
			$updateQuery = "UPDATE cl_hlusers.hardlock
				SET isFloating = {$isFloating}
				WHERE hlNum = '{$hlNum}'";
		}

		$this->db->query($updateQuery);

		if ($newNuserID != $nUserID) {
			$info = "Régi Profil: <a href='https://crm.cadline.hu/en/users/show/{$nUserID}' target='_blank'>{$nUserID}</a> <br/> 
				Új Profil: <a href='https://crm.cadline.hu/en/users/show/{$newNuserID}' target='_blank'>{$newNuserID}</a>";

			$history			= new stdClass();
			$history->hiHlNum	= $hlNum;
			$history->hiDate   	= date('Y-m-d');
			$history->hiManID  	= 36;
			$history->hiTypeID	= 28;
			$history->hiTime    = date('Y-m-d H:i:s');
			$history->hiInfo  	= $info;

			$this->db->insert("cl_hlusers.h_history", $history);
		}

		if ($isFloating != $key->isFloating) {
			$infoFloating = $isFloating == 0 ? 'Weborder: Floating kikapcsolva' : 'Weborder: Floating bekapcsolva';
			$flType = $isFloating == 0 ? 30 : 29;

			$historyFl				= new stdClass();
			$historyFl->hiHlNum		= $hlNum;
			$historyFl->hiDate		= date('Y-m-d');
			$historyFl->hiManID  	= 36;
			$historyFl->hiTypeID	= $flType;
			$historyFl->hiTime    	= date('Y-m-d H:i:s');
			$historyFl->hiInfo  	= $infoFloating;

			$this->db->insert("cl_hlusers.h_history", $historyFl);
		}

		$to      = 'office@cadline.hu';
		$subject = 'Profil Módositás - ' . $this->access_id;
		$headers = "Content-Type: text/html; charset=utf-8\r\n";

		$message = "A <b>{$hlNum}<b> kulcs módosításra került: <br/><br/>";
		$message .= "Régi Profil: <a href='https://crm.cadline.hu/en/users/show/{$nUserID}' target='_blank'>{$nUserID}</a> <br/>";
		$message .= "Új Profil: <a href='https://crm.cadline.hu/en/users/show/{$newNuserID}' target='_blank'>{$newNuserID}</a>";

		if (substr($this->access_id, 0, 2) != 37)
			mail($to, $subject, $message, $headers);

		redirect('partners/key/' . $hlNum);
		return;
	}

	function documentation()
	{
		$data = array();

		if ($this->access_id != '37' && $this->access_id != '36')
			show_404();
		else
			$this->load->view('partners/documentation.php', $data);
	}
}
