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

class Category 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 category_listing
     * Function to generate the listing of category
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function category_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('category');
        $arrData['middle']   = 'category/category_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_category()
    {
        # 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('category', $arrAgent);

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

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

    /**
     * Function magazine_list
     * Listing of added magazines.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function edit_category()
    {
        # 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/agent/agent_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('category', $arrAgent, $intAgent);

            $this->session->set_flashdata('success', 'Category information has been added successfully');
            redirect(base_url().'index.php/category/category_listing');
        } else {

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

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

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