<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Link_Model extends CI_Model {

	public function __construct() {

	}

	public function get_link_details($linkId) {

		$this->db->where('ID', $linkId);
		$this->db->where('UserID', $this->session->userdata('ID'));
		$this->db->where('Status', 'Y');
		$this->db->limit(1);

		$q = $this->db->get('links');

		return $q->num_rows() > 0 ? $q->row() : false;
	}

	public function create_link($domain, $subdomain, $remark = null, $phoneNumber, $text, $landing_page) {

		// Check if phone number is malaysian phone number of not
		if(!preg_match('/^601\d{8,9}$/', $phoneNumber)) {
			$result['success'] = false;
			$result['reason'] = 'Phone number is not valid malaysian number!';
			return $result;
		}

		// Check if this subdomain already exist or not
		$this->db->where('Domain', $domain);
		$this->db->where('Subdomain', $subdomain);
		$this->db->where('Status','Y');
		$q = $this->db->get('links');

		if($q->num_rows() >= 1) {

			$result['success'] = false;
			$result['reason'] = 'Duplicate subdomain/domain already exist!';
			return $result;

		} else {

			$data = array(
				'UserID' => $this->session->userdata('ID'),
				'Domain' => $domain,
				'Subdomain' => $subdomain,
				'Text' => $text,
				'PhoneNumber' => $phoneNumber,
				'FullLink' => "http://".$subdomain.".".$domain.".com",
				'InsertBy' => $this->session->userdata('Username'),
				'InsertDate' => date('Y-m-d H:i:s'),
				'Status' => 'Y',
				'Remark' => isset($remark) ? $remark : null,
				'LandingPage' => $landing_page
			);

			$q = $this->db->insert('links', $data);

			$result['success'] = $q;
			return $result;
		}
		

	}

	public function edit_link($linkId, $domain, $subdomain, $remark = null, $phoneNumber, $text, $landing_page) {

			// Check if phone number is malaysian phone number of not
		if(!preg_match('/^601\d{8,9}$/', $phoneNumber)) {
			$result['success'] = false;
			$result['reason'] = 'Phone number is not valid malaysian number!';
			return $result;
		}

		// Check if this subdomain already exist or not
		$this->db->where('Domain', $domain);
		$this->db->where('Subdomain', $subdomain);
		$this->db->where('ID !=', $linkId);
		$this->db->where('Status','Y');
		$q = $this->db->get('links');

		if($q->num_rows() >= 1) {

			$result['success'] = false;
			$result['reason'] = 'Duplicate subdomain/domain already exist!';
			return $result;

		} else {

			$this->db->where('ID', $linkId);
			$this->db->where('UserID', $this->session->userdata('ID'));
			$this->db->where('InsertBy', $this->session->userdata('Username'));
			

			$data = array(
				'Domain' => $domain,
				'Subdomain' => $subdomain,
				'Text' => $text,
				'PhoneNumber' => $phoneNumber,
				'FullLink' => "http://".$subdomain.".".$domain.".com",
				'Remark' => isset($remark) ? $remark : null,
				'LandingPage' => $landing_page
			);

			$q = $this->db->update('links', $data);

			$result['success'] = $q;
			return $result;
		}
	}

	public function delete_link($linkId) {

		$this->db->where('ID', $linkId);
		$this->db->where('Status','Y');
		$q = $this->db->update('links', array('Status' => 'N'));

		return $q;

	}
}