<?php
class Mpartners extends Model
{
	function __construct()
	{
		parent::__construct();

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

	function isAdmin()
	{
		$access_id = $this->phpsession->get('access_id');

		$query = "SELECT p.weborder_access AS weborder_access, p.rm_access AS rm_access, auth.ctrID AS ctrID, p.is_admin AS is_admin
            FROM cl_hlusers.partner_authenticate auth
            LEFT JOIN cl_hlusers.partner_permissions p ON p.id = auth.permission_id
            WHERE auth.username = '{$access_id}' LIMIT 1";
		$access = $this->db->query($query)->row();

		return $access->is_admin == 1;
	}

	private function getDistributor()
	{
		$distID = 0;

		$distributor = $this->db->query("SELECT id FROM cl_hlusers.distributors WHERE ctrID = '{$this->ctrID}'")->row();

		if (!empty($distributor))
			$distID = $distributor->id;

		return $distID;
	}

	function insertUser($name, $email, $ctrID)
	{
		$distID 	= $this->getDistributor();
		$status     = '11';
		$userType   = 1;

		$user               = new stdClass();
		$user->strName      = $name;
		$user->nManagerID   = 36;
		$user->nSchoolID    = 0;
		$user->deleted      = 0;
		$user->old_db       = 'clusers_eng';
		$user->bNewsletter  = 'N';
		$user->strEMail     = $email;
		$user->strCurrency  = substr($ctrID, 0, 2) == '11' ? 'USD' : 'EUR';
		$user->nCtrID       = substr($ctrID, 0, 2);
		$user->nUserStatus  = $status;
		$user->nDistID      = $distID;
		$user->nUserType    = $userType;

		$this->db->insert("cl_hlusers.users", $user);
		$nUserID = $this->db->insert_id();

		$exploded_email = explode("@", $email);

		$phpass = new PasswordHash(10, true);
		$password = $phpass->HashPassword($exploded_email[0]);

		$jmlUser                = new stdClass();
		$jmlUser->name          = $name;
		$jmlUser->username      = $email;
		$jmlUser->email         = $email;
		$jmlUser->password      = $password;
		$jmlUser->registerDate  = date('Y-m-d');
		$jmlUser->nUserID       = $nUserID;

		$this->db->insert("www_archline_hu.jml_users", $jmlUser);
		$jmlID = $this->db->insert_id();

		$jmlGroup           = new stdClass();
		$jmlGroup->user_id  = $jmlID;
		$jmlGroup->group_id = 2;

		$this->db->insert("www_archline_hu.jml_user_usergroup_map", $jmlGroup);

		$history                = new stdClass();
		$history->nUserID       = $nUserID;
		$history->nTopicID      = 382;
		$history->dateEvent     = date('Y-m-d');
		$history->strPlace      = 'Weborder';
		$history->strInfo       = 'Weborderen regisztrált';
		$history->nManagerID    = 36;
		$history->insertDate    = date('Y-m-d H:i:s');

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

		$u_email                = new stdClass();
		$u_email->email         = $email;
		$u_email->nUserID       = $nUserID;
		$u_email->default       = 1;
		$u_email->active        = 1;
		$u_email->deleted       = 0;
		$u_email->newsletter    = 1;

		$this->db->insert("cl_hlusers.u_emails", $u_email);

		return $nUserID;
	}

	function lanToString($lanNum)
	{

		if ($lanNum == 0) {
			return '-';
		}
		if ($lanNum == 6) {
			return 10;
		}
		if ($lanNum == 7) {
			return 15;
		}
		if ($lanNum == 8) {
			return 20;
		}
		if ($lanNum == 9) {
			return 'N';
		}
		return $lanNum;
	}

	function get_mod_name($ID)
	{
		$this->keydb = $this->load->database('ujugyfelkezelo', true);
		$query = $this->keydb->select('mdtNameEng')->where('ID', $ID)->get('p_mtypes');

		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();
		}
	}
}
