<?php
defined('BASEPATH') OR exit('No direct script access allowed');
defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/');

class Accountmain extends CI_Model {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function getAccount($user) {
		$sql = "SELECT fldid ID, email, username FROM tbl_usermaster WHERE (is_delete IS NULL OR is_delete = 0) AND fldid = ".$user;
		$q = $this->db->query($sql);		
		return ($q && ($r = $q->result()) && $r[0]) ? $this->ObjToArray($r[0]) : NULL;
	}
	
	public function getAccountByEmail($email) {
		$sql = "SELECT fldid ID, username FROM tbl_usermaster WHERE (is_delete IS NULL OR is_delete = 0) AND LOWER(email) = '".(strtolower(trim($email)))."'";
		$q = $this->db->query($sql);		
		return ($q && ($r = $q->result()) && $r[0]) ? $this->ObjToArray($r[0]) : NULL;
	}
	
	public function checkAccountExists($id) {
		$sql = "SELECT COUNT(1) CT FROM tbl_usermaster WHERE fldid = ".$id." AND (is_delete IS NULL OR is_delete = 0)";
		$q = $this->db->query($sql);
		
		return ($q && ($r = $q->result()) && $r[0] && $r[0]->CT) ? TRUE : FALSE;
	}
	
	public function checkEmailExists($email, $id = 0) {
		$sql = "SELECT COUNT(1) CT FROM tbl_usermaster WHERE LOWER(email) = '".(strtolower($email))."' AND (is_delete IS NULL OR is_delete = 0)";
		$sql .= ($id) ? " AND fldid != ".$id : NULL;
		$q = $this->db->query($sql);
		
		return ($q && ($r = $q->result()) && $r[0] && $r[0]->CT) ? TRUE : FALSE;
	}
	
	public function getOldPassword($id) {
		$sql = "SELECT password FROM tbl_usermaster WHERE fldid = ".$id." AND (is_delete IS NULL OR is_delete = 0)";
		$q = $this->db->query($sql);
		
		return ($q && ($r = $q->result()) && $r[0]) ? $r[0]->password : NULL;
	}
	
	public function deleteAccount($id) {
		$sql = "UPDATE tbl_usermaster SET is_delete = 1 WHERE fldid = ".$id;
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function saveAccount($data) {
		$sql = "SELECT COUNT(1) CT FROM tbl_usermaster WHERE fldid = ".$data['ID'];
		$q = $this->db->query($sql);
		
		if($ct = ($q && ($r = $q->result()) && $r[0] && $r[0]->CT) ? TRUE : FALSE) {
			$sql = "UPDATE tbl_usermaster
								SET
										username = '".(addslashes(trim($data['username'])))."',
										email = '".(addslashes(trim($data['email'])))."'
										".((isset($data['newpassword']) && trim($data['newpassword'])) ? ", password = '".(md5($data['newpassword']))."' " : NULL)."
								WHERE fldid = ".$data['ID'];
			return ($this->db->query($sql)) ? $data['ID'] : 0;
		} else {
			$sql = "INSERT INTO tbl_usermaster (
								email, password, username, role, created_dt
							) VALUES (
								'".(addslashes(trim($data['email'])))."',
								'".(md5(trim($data['password'])))."',
								'".(addslashes(trim($data['username'])))."',
								'User',
								NOW()
							)";
			return ($this->db->query($sql)) ? $this->db->insert_id() : 0;
		}
	}
	
	public function savePassword($user, $password) {
		$sql = "UPDATE tbl_usermaster SET password = '".(md5($password))."' WHERE fldid = ".$user;
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function saveResetPassword($user, $code) {		
		$sql = "INSERT INTO user_password_reset ( user, code, createdDT ) VALUES ( ".$user.", '".$code."', NOW() )";
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function getResetInfo($code) {
		$sql = "SELECT user, createdDT FROM user_password_reset WHERE code = '".$code."'";
		$q = $this->db->query($sql);
		$return = NULL;
		if($result = ($q && ($r = $q->result()) && $r[0]) ? $this->ObjToArray($r[0]) : NULL) {
			$return = $result;
			$return['elapsed'] = $this->getTimeElapsed($result['createdDT']);
		}
		return $return;
	}
	
	public function processReset($user, $code) {
		$sql = "UPDATE user_password_reset SET processed = 1 WHERE user = ".$user." AND code = '".$code."'";
		return ($this->db->query($sql)) ? TRUE : FALSE;
	}
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
	private function getTimeElapsed($datetime) {
		$now = new DateTime;
		$ago = new DateTime($datetime);
		$diff = $now->diff($ago);

		$diff->w = floor($diff->d / 7);
		$diff->d -= $diff->w * 7;

		$string = array(
				'y' => 'year',
				'm' => 'month',
				'w' => 'week',
				'd' => 'day',
				'h' => 'hour',
				'i' => 'minute',
				's' => 'second',
		);
		
		foreach ($string as $k => &$v) {
			$v = ($diff->$k) ? $diff->$k : 0;
		}
		
		return $string ? $string : NULL;
	}
		
	
}































