<?php
/**
 * Created by PhpStorm.
 * User: neosoft
 * Date: 22/11/14
 * Time: 10:53 AM
 */

class Currency extends CI_Controller {

    /**
     * Function index
     * default functionality for the user.
     * Controllers default method.
     *
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function index()
    {
        $this->load->view('welcome_message');
    }

    /**
     * Function currency_listing
     * Function to generate the list of currency
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function currency_listing()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectpath = base_url().'index.php/users/login';
            redirect($strRedirectpath);
        }

        #Load required models and libraries.
        $this->load->model('agents');
        #$this->load->library('pagination');
        #$this->load->library('Backend_pagination');
        $arrAgents = $this->agents->fnGetAllAgents('currency');
        $arrData['middle']   = 'currency/currency_listings';
        $arrData['arrData']   = $arrAgents;
        $this->load->view('admin/templates/template', $arrData);
    }


    /**
     * Function magazine_list
     * Listing of added magezines.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function add_currency()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectpath = base_url().'index.php/users/login';
            redirect($strRedirectpath);
        }

        # Load the required model.
        $this->load->model('agents');

        # set form fields rules.
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[100]|alpha|xss_clean');

        # Check whether the validation turns true.
        if ($this->form_validation->run() == TRUE)
        {
            $arrPostedData = $_POST;

            #get the values of all fields.
            $arrAgent = array();
            $arrAgent['name'] = $_POST['name'];

            # Save to the database.
            $intNewPromotionId = $this->agents->fnSavePromotion('currency', $arrAgent);

            $this->session->set_flashdata('success', 'New currency has been added successfully');
            redirect(base_url().'index.php/currency/currency_listing');
        } else {

            /** if validation errors found then redirect  **/
            $strValidationErrors = validation_errors();
            if ($strValidationErrors != "" ) {
                $this->session->set_flashdata('error', $strValidationErrors);
                $strRedirectpath = base_url().'index.php/currency/currency_listing/';
                redirect($strRedirectpath);
            } else {
                $arrData['middle']   = 'currency/add_currency';
                $strValidationErrors = validation_errors();
                $this->session->set_flashdata('error', $strValidationErrors);
                echo $this->load->view('currency/add_currency', $arrData, true);exit;
            }
        }
    }

    /**
     * Function magazine_list
     * Listting of added magezines.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function edit_currency()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectPath = base_url().'index.php/users/login';
            redirect($strRedirectPath);
        }

        # now check whether we receive the valid agent id.
        $intAgent = (int) base64_decode($_REQUEST['lang_id']);
        if ($intAgent == 0) {
            $strRedirectPath = base_url().'index.php/currency/currency_listing';
            redirect($strRedirectPath);
        }

        # Load the required model.
        $this->load->model('agents');

        # set form fields rules.
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[100]|alpha|xss_clean');

        # Check whether the validation turns true.
        if ($this->form_validation->run() == TRUE)
        {
            $arrPostedData = $_POST;

            #get the values of all fields.
            $arrAgent = array();
            $arrAgent['name'] = $_POST['name'];

            # Update to the database.
            $this->agents->fnUpdatePromotion('currency', $arrAgent, $intAgent);

            $this->session->set_flashdata('success', 'Currency information has been updated successfully');
            redirect(base_url().'index.php/currency/currency_listing');
        } else {

            /** if validation errors found then redirect  **/
            $strValidationErrors = validation_errors();
            if ($strValidationErrors != "" ) {
                $this->session->set_flashdata('error', $strValidationErrors);
                $strRedirectPath = base_url().'index.php/currency/currency_listing/';
                redirect($strRedirectPath);
            } else {
                $arrDetailedInformation = $this->agents->fnGetAllAgents('currency', $intAgent);
                $arrData['middle']   = 'currency/edit_currency';
                $arrData['arrData']   = $arrDetailedInformation[0];
                $arrData['strAgentId']   = $_REQUEST['lang_id'];
                $strValidationErrors = validation_errors();
                $this->session->set_flashdata('error', $strValidationErrors);
                echo $this->load->view('currency/edit_currency', $arrData, true);exit;
            }
        }
    }

    /**
     * Function fnCheckGivenLanguage
     * Function to check the given details
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function fnCheckGivenLanguage()
    {
        $strLanguageName = $_GET['currency_name'];
        # Load the required model.
        $this->load->model('agents');

        # delete requested subscription
        $arrResultSet = $this->agents->fnCheckGivenLanguage('currency', strtolower($strLanguageName));
        echo count($arrResultSet);
    }
} 