<?php

class Admin_User_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}

	function loginUserCheck($userName, $currPassword)
	{
		$this->db->select ( '*' );
		$this->db->from ( 'tbl_admin_user' );
		$this->db->where ( 'user_name', $userName );
		$this->db->where ( 'user_pwd', md5 ( $currPassword ) );
		$this->db->where ( 'active_status', 1 );
		$resultSet = $this->db->get ();
		return $resultSet->row ();
	}

	function getAdminUserInfo($admin_user_id)
	{
		$this->db->select ( '*' );
		$this->db->from ( 'tbl_admin_user' );
		$this->db->where ( 'admin_user_id', $admin_user_id );
		$res = $this->db->get ();
		$result = $res->row ();
		return $result;
	}

	/**
	 * @author Shafiul Alam
	 * this function return the admin gateway information
     *
     * Modified By Mohammad Sajjad Hossain, added encryption
	 *
	 * @return unknown
	 */
	function getAdminGatewaySetup ()
	{
		$this->db->select('*');
		$this->db->from('tbl_admin_gateway_setup_custom');
		$this->db->where('admin_gateway_row_id ',1);
		$resultSet = $this->db->get();
		$row = $resultSet->row();


        if(is_object($row))
        {
            if(trim($row->paypal_api_user_name))
            {
                $row->paypal_api_user_name = ($row->paypal_api_user_name);
            }

            if(trim($row->paypal_api_key))
            {
                $row->paypal_api_password = ($row->paypal_api_key);
            }

            if(trim($row->paypal_api_signature))
            {
                $row->paypal_api_signature = ($row->paypal_api_signature);
            }

			$row->authorize_api_login_key = '';
			$row->authorize_api_transaction_key = '';
			$row->authorize_api_signature = '';

        }
        return $row;
	}

	/**
	 * @author Shafiul Alam
	 *	this function returns admin gateway row id exists or not
	 * @param unknown_type $admin_gateway_row_id
	 * @return unknown
	 */
	function adminGatewayRowIdExists ($admin_gateway_row_id)
	{
		$this->db->select('count(*) as cns');
		$this->db->from('tbl_admin_gateway_setup');
		$this->db->where('admin_gateway_row_id', $admin_gateway_row_id);
		$recordSet = $this->db->get();
		$row_count = $recordSet->row();
		if ($row_count->cns > 0)
			return true; else
			return false;
	}
	/**
	 * @author Shafiul Alam
	 * Save admin Gateway inforrmation
     * Modified By Mohammad Sajjad Hossain, added encryption
	 *
	 * @param unknown_type $gatewaySetupData
	 */

	function saveAdminGatewaySetup ($gatewaySetupData)
	{
		$admin_gateway_row_id=$gatewaySetupData['admin_gateway_row_id'];
		$adminGatewayRowId=$this->adminGatewayRowIdExists ($admin_gateway_row_id);

        if(trim($gatewaySetupData['paypal_api_user_name']))
        {
            $gatewaySetupData['paypal_api_user_name'] = encrypt($gatewaySetupData['paypal_api_user_name']);
        }

        if(trim($gatewaySetupData['paypal_api_password']))
        {
            $gatewaySetupData['paypal_api_password'] = encrypt($gatewaySetupData['paypal_api_password']);
        }

        if(trim($gatewaySetupData['paypal_api_signature']))
        {
            $gatewaySetupData['paypal_api_signature'] = encrypt($gatewaySetupData['paypal_api_signature']);
        }

        if(trim($gatewaySetupData['authorize_api_login_key']))
        {
            $gatewaySetupData['authorize_api_login_key'] = encrypt($gatewaySetupData['authorize_api_login_key']);
        }

        if(trim($gatewaySetupData['authorize_api_transaction_key']))
        {
            $gatewaySetupData['authorize_api_transaction_key'] = encrypt($gatewaySetupData['authorize_api_transaction_key']);
        }

		if($adminGatewayRowId)
		{
			$this->db->where('admin_gateway_row_id', $gatewaySetupData['admin_gateway_row_id']);
			$this->db->update('tbl_admin_gateway_setup', $gatewaySetupData);
		}
		else
		{
			$this->db->insert('tbl_admin_gateway_setup', $gatewaySetupData);
		}
	}

	/**
	 * @author Himel Nag Rana
	 * @abstract This function saves announcement settings
	 */
	function saveAnnouncements($data)
	{
		if(isset($data['announcement_row_id']))
		{
			$this->db->where('announcement_row_id',$data['announcement_row_id']);
			$this->db->update('tbl_admin_announcement',$data);
		}
		else
		{
			$this->db->insert('tbl_admin_announcement',$data);
		}
        $this->generateAnnouncementFront();
	}

	function deleteAnnouncement($data)
	{
		$this->db->set('active_status',0);
		$this->db->where('announcement_row_id',$data['announcement_row_id']);
		$this->db->where('admin_user_id',$data['admin_user_id']);
		if($this->db->update('tbl_admin_announcement'))
        {
            $this->generateAnnouncementFront();
        }
	}

    function generateAnnouncementFront()
    {
        $sql = 'SELECT *,
                    DATE_FORMAT(create_date, "'. $this->config->item('show_datetime_fmt').'") AS date_of_creation
                FROM tbl_admin_announcement
                WHERE check_if_today = 1
                AND active_status = 1
                ORDER BY create_date DESC';

        $res = $this->db->query($sql);
        $result = $res->result();
        $return = '';

        if($result)
        {
            foreach($result as $row)
            {
                $return .= '<table width="100%" align="center" cellspacing="0" cellpadding="4" bgcolor="#FFFFFF">
                                    <tr>
                                        <td>' . $row->date_of_creation . '</td>
                                    </tr>
                                    <tr>
                                        <td class="welcomeAnnounceTitleText">' . $row->announcement_title . '</td>
                                    </tr>
                                    <tr>
                                        <td>' . $row->announcement_short_notes.'... <a class="actionLinks" href="' . BASEURL . 'welcome/showAnnouncementDetails/' . $row->announcement_row_id . '.html">[more]</a></td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                    </tr>
                                </table>';

            }
        }
        else
        {
            $return = 'No Announcement';
        }

        write_file(ROOT_PATH . 'application/views/announcement/front_view.php', $return);
    }

	/**
	 * @author Himel Nag Rana
	 * @abstract This function retrieves announcement settings
	 */
	function getAnnouncementList($admin_user_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_admin_announcement');
		$this->db->where('admin_user_id',$admin_user_id);
		$this->db->where('active_status',1);

		$res = $this->db->get();
		$result = $res->result();
		return $result;
	}

	/**
	 * @author Himel Nag Rana
	 * @abstract This function retrieves announcement settings
	 */
	function changeShowStatus($announcement_row_id,$type)
	{
		//$type=1 => check the box i.e. show
		//$type=2 => uncheck the box i.e. don't show

		if($type==1)
		{
			$this->db->set('check_if_today','1');
			$this->db->where('announcement_row_id',$announcement_row_id);
			$this->db->update('tbl_admin_announcement');
		}
		elseif($type==2)
		{
			$this->db->set('check_if_today','0');
			$this->db->where('announcement_row_id',$announcement_row_id);
			$this->db->update('tbl_admin_announcement');
		}
	}

	/**
	 * @author Himel Nag Rana
	 * @abstract This Function shows announcements in welcome page
	 */
	function showAnnouncements()
	{
		$sql = 'SELECT *
                FROM tbl_admin_announcement
                WHERE check_if_today = 1
                ORDER BY create_date DESC';

		$res = $this->db->query($sql);
		$result = $res->result_array();
        return $result;
	}

    /**
	 * @author Himel Nag Rana
	 * @abstract This function retrieves details of an announcement
	 */
	function getAnnouncementDetails($announcement_row_id)
	{
        $sql = "SELECT  announcement_row_id,
                        admin_user_id,
                        announcement_title,
                        announcement_short_notes,
                        announcement_content,
                        check_if_today,
                        create_date,
                        DATE_FORMAT(create_date, '%M %d, %Y') AS created_on,
                        active_status
                FROM tbl_admin_announcement
                WHERE announcement_row_id = $announcement_row_id
                AND check_if_today = 1";

		$res = $this->db->query($sql);
		$result = $res->row_array();
		return $result;
	}

	/**
	* @author Md. Shafiul Alam
	* @abstract This function check existing username and password
	* @version PWC1.2
	*/
	function checkExistanceOfPasswordForUserId($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_merchant');
		$this->db->where('email_address',$data['merchant_email']);
		$this->db->where('merchant_row_id',$data['merchant_row_id']);

		$res = $this->db->get();
		$result = $res->row();
		$merchant_password = $result->user_password;
		if($merchant_password == $data['merchant_password'])
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	/**
	* @author Md. Shafiul Alam
	* @abstract This function update merchant username and password
	* @version PWC1.2
	*/
	function updateMerchantLoginInformation($data)
	{
		$this->db->set('user_password',$data['newPassword']);
		$this->db->where('merchant_row_id',$data['merchant_row_id']);
		$this->db->update('tbl_merchant');
	}

    function checkAdminStatus($admin_user_id)
    {
        $this->db->select('active_status');
        $this->db->from('tbl_admin_user');
        $this->db->where('admin_user_id',$admin_user_id);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result['active_status'];
    }
}
?>