<?php

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

class Admin_city_category extends back_end {

    function __construct() {
        parent::__construct();

        if (!$this->auth->is_logged_in()) {
            $this->session->set_flashdata('error_msg', 'يجب عليك تسجيل الدخول أولا');
            redirect('admin');
        }

        $this->lang->load('patient/patient');
        $this->template->metas('page_title', $this->lang->line('page_title_cat'));
        $this->load->model('city_category_model', 'city_category');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters("<span class='incorrect'>", "</span>");
    }

    public function index() {
        $this->overview();
    }

    /**
     *  create new new_category then redirect overview.
     */
    public function create() {
        $this->form_validation->set_rules('category_name', $this->lang->line('category_title'), 'trim|required|xss_clean|htmlspecialchars');
        $this->form_validation->set_rules('hide', "التفعيل", 'trim');
        
        if ($this->form_validation->run() == false) {
            $this->view('city_category_new');
        } else {
            $this->city_category->create();
            $this->session->set_flashdata('success_msg', $this->lang->line('success_msg'));
            redirect('patient/admin_city_category/overview/');
        }
    }

    /**
     *  display all categories of city in datagrid.
     */
    public function overview($row = 0) {
        
        $this->load->library('pagination');
        
        $config['base_url'] = base_url() . "patient/admin_city_category/overview";
        $config['total_rows'] = $this->city_category->getNumUsersAdmin();
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;

        $config['full_tag_open'] = '<div class="pages">';
        $config['full_tag_close'] = '</div>';

        $config['cur_tag_open'] = '<a href="javascript:void(0)" class="current">';
        $config['cur_tag_close'] = '</a>';
        
        $config['first_link'] = '&lt;&lt; الأولى';
        $config['last_link'] = 'الأخيرة &gt;&gt;';

        $this->pagination->initialize($config);
        
        $data['row'] = $row;
        $data['total_rows'] = $this->city_category->getNumUsersAdmin();
        $data['links'] = $this->pagination->create_links();
        
        $data['categories'] = $this->city_category->get_categories($row);
        $this->view('city_category_list', $data);
    }

    /**
     *  This function call operation function in city_category_model
      that's executes some operation according to choice.
     */
    public function operation() {
        $this->city_category->operation();
        redirect('patient/admin_city_category/overview');
    }

    /**
     * This function remove new_category then redirect to overview
     * @example id=1
     * @param integer $id
     */
    public function remove($id) {
        $new = $this->city_category->get_category_one($id);
        if ($this->city_category->delete($id)) {
            $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_del'));
            redirect_back();
        }
    }

    /**
     *  This function activate an new_category then redirect to overview
     * @example id=1
     * @param integer $id
     */
    public function activate($id) {
        if ($this->city_category->is_valid_category($id)) {
            $this->city_category->activate($id);
            $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_active_cat'));
            redirect('patient/admin_city_category/overview/');
        } else {
            redirect('patient/admin_city_category/overview/');
        }
    }

    /**
     * This function deactivate an new_category then redirect to overview
     * @example id=1
     * @param integer $id
     */
    public function deactivate($id) {
        if ($this->city_category->is_valid_category($id)) {
            $this->city_category->deactivate($id);
            $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_inactive_cat'));
            redirect('patient/admin_city_category/overview/');
        } else {
            redirect('patient/admin_city_category/overview/');
        }
    }

    /**
     * This function edit an new_category then redirect to overview
     * @example id=1
     * @param integer $id
     */
    public function edit($id) {
        $data['category'] = $this->city_category->get_category_one($id);
        $this->form_validation->set_rules('category_name', $this->lang->line('category_title'), 'trim|required|xss_clean|htmlspecialchars');
        $this->form_validation->set_rules('hide', "التفعيل", 'trim');
        //$this->form_validation->set_rules('lang', 'اللغة', 'trim|required|xss_clean|htmlspecialchars');
        if ($this->form_validation->run() == false) {
            $this->view('city_category_edit', $data);
        } else {
            $this->city_category->update($id);
            $this->session->set_flashdata('success_msg', $this->lang->line('success_msg_edit_cat'));
            redirect('patient/admin_city_category/overview/');
        }
    }

    /**
     * This function sort city_category then redirect to overview
     * @example direction ='up', $id =1
     * @param string $direction
     * @param integer $id
     */
    public function sort($direction = 'up', $id = FALSE) {
        if ($id == FALSE) {
            redirect('patient/admin_city_category/overview/');
        } else {
            if ($this->city_category->is_valid_page($id)) {
                switch ($direction) {
                    case 'up':
                        $this->city_category->sort($direction, $id);
                        break;
                    case 'down':
                        $this->city_category->sort($direction, $id);
                        break;
                    default:
                        break;
                }
                redirect('patient/admin_city_category/overview/');
            } else {
                redirect('patient/admin_city_category/overview/');
            }
        }
    }

}

/* End of file dashboard.php */
/* Location: ./system/application/modules/matchbox/controllers/dashboard.php */