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

class User extends CI_Model {

    /**
     *  Models default constructor.
     */
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	/**
	 * Function get company list
	 * Get list of all available companies.
	 * @params: $strUserName: Table name
	 * @params: $strPassword: Name
	 * @author	Himanshu Charegaonkar
	 * @access	public
	 * return result array
	 */
    function fnCheckLogin($strUserName, $strPassword)
    {
    	$this->db->select(array('id', 'access_role', 'first_name', 'last_name'));
    	$query = $this->db->get_where('user', array('username' => $strUserName, 'password' => $strPassword));
        return $query->result_array();
    }

    /**
     * function fnGetAccessRoles
     * Function to get all access roles
     * @return mixed
     * @author Himanshu Charegaonkar
     * @access Public
     */
    function fnGetAccessRoles($strSearchKey = "")
    {
        if ($strSearchKey == "") {
            $query = $this->db->get('access_roles');
        } else {
            $query = $this->db->get_where('access_roles', array('id' => $strSearchKey));
        }

        return $query->result_array();
    }

    /**
     * function fnGetAccessRoles
     * Function to get all access roles
     * @return mixed
     * @author Himanshu Charegaonkar
     * @access Public
     */
    function fnCheckAccessName($strSearchKey = "")
    {
        if ($strSearchKey == "") {
            $query = $this->db->get('access_roles');
        } else {
            $query = $this->db->get_where('access_roles', array('name' => $strSearchKey));
        }

        return $query->result_array();
    }

    /**
     * Function fnSaveUser
     * Function to save the Subscriber
     * @param $arrDetails: Array - Parameters to insert query
     * @return mixed
     * @author    Himanshu Charegaonkar
     * @access    public
     */
    function fnSaveUser ($arrDetails)
    {
        $this->db->insert('user', $arrDetails);
        return $this->db->insert_id();
    }

    /**
     * Function fnGetAllUsers
     * Function to get user information along with its access role name.
     * @return mixed
     * @access Public
     * @author Himanshu Charegaonkar
     */
    function fnGetAllUsers()
    {
        $query = " SELECT U.id, U.first_name, U.last_name, A.name as role
                  FROM user as U
                  JOIN  access_roles as A on U.access_role = A.id ";
        return $this->db->query($query)->result_array();
    }

    /**
     * Function get company list
     * Get list of all available companies.
     * @params: $strUserName: Table name
     * @params: $strPassword: Name
     * @author	Himanshu Charegaonkar
     * @access	public
     * return result array
     */
    function fnCheckDetails($strFieldName = "", $strValue = "", $strSelectedRole = "")
    {
        $this->db->select(array('id', 'access_role', 'first_name', 'last_name', 'email', 'designation'));
        if ($strFieldName != "" && $strValue != "") {
            $query = $this->db->get_where('user', array($strFieldName => $strValue));

        } else {
            if ($strSelectedRole == "") {
                $query = $this->db->get('user');
            } else {
                $query = $this->db->get_where('user', array('access_role' => $strSelectedRole));
            }

        }

        return $query->result_array();
    }

    /**
     * Function fnGetAllMethods
     * Function to get all methods in the system
     * @return mixed
     */
    function  fnGetAllMethods($intFlag = 0)
    {
        $query = "SELECT id, module_name, method_name, url_part, user_level_id, category, action_id, comment
                  FROM `manage_permissions` ORDER BY action_id ASC  ";

        $arrResultSet = $this->db->query($query)->result_array();
        if ($intFlag == 0){
            return  $arrResultSet;
        } else {
            $arrInfo = array();
            foreach($arrResultSet  as $result) {
                $arrInfo[] = $result['url_part'];
            }
            return $arrInfo;
        }

    }

    /**
     * Function fnUpdateUsersAccess
     * Function to Update users access.
     * @param $intId - Integer - Id.
     * @param $strNewAccess - String - string of ids of new access.
     * @return mixed
     */
    function fnUpdateUsersAccess($intId, $strNewAccess)
    {
        $this->db->where('level_id', $intId);
        return $this->db->update('access_role_management', $strNewAccess);
    }

    /**
     * Function fnUpdateAccess
     * Function to update the details of the given access
     * @param $intAccessId
     * @param $arrRecords
     * @return mixed
     * @author Himanshu Charegaonkar
     * @access Public
     */
    function fnUpdateAccess($intAccessId, $arrRecords)
    {
        $this->db->where('id', $intAccessId);
        return $this->db->update('access_roles', $arrRecords);
    }

    /**
     * Function fnGetPreviousAccess
     * Function to get the previous assigned access roles.
     * @param $role_id
     * @return mixed
     */
    function fnGetPreviousAccess($role_id)
    {
        $query = "SELECT alloted_access
                  FROM access_role_management
                  WHERE level_id =".$role_id;

        return $this->db->query($query)->result_array();
    }

    /**
     * Function fnCheckAccessIsAllowed
     * function to check the whether access to particular url is allowed or not.
     * @param $intLevelId
     * @param $strUrlToCheck
     * @return mixed
     */
    function fnCheckAccessIsAllowed($intLevelId, $strUrlToCheck)
    {
        $strQuery = "SELECT *
                      FROM `access_role_management` AS UA
                      JOIN manage_permissions AS MP ON FIND_IN_SET( MP.id, UA.alloted_access )
                      WHERE UA.level_id = $intLevelId
                      AND url_part = '".$strUrlToCheck."'
                      GROUP BY MP.id ";
        return $this->db->query($strQuery)->result_array();
    }

    /**
     * Function fnUpdateMagezine
     * Function update magazine details.
     * @param $arrResultData: Array - Array of subscriber information which has to update
     * @param $intId: integer - Id
     * @return mixed
     * @author    Himanshu Charegaonkar
     * @access    public
     */

    function fnUpdateUser($arrResultData, $intId)
    {
        $this->db->where('id', $intId);
        return $this->db->update('user', $arrResultData);
    }

    /**
     * Function to delete the access level.
     * @param $intTransactionId
     * @return mixed
     * @author    Himanshu Charegaonkar
     * @access    public
     */
    function fnDeleteAccessLevel($intTransactionId)
    {
        $this->db->where(array('id' => $intTransactionId));
        return $this->db->delete('access_roles');
    }

    /**
     * Function fnCheckForEmail
     * Function to check whether the given email address has been in database or not.
     * @param $strSearchTerm -string - Search term
     * @return mixed
     * @author Himanshu Charegaonkar
     * @access public
     */
    function fnCheckForEmail($strSearchTerm)
    {
        $strQuery = "SELECT id, first_name, last_name
                    FROM  user
                    WHERE email = '".$strSearchTerm."'";
        return $this->db->query($strQuery)->result_array();
    }


}
