<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Basic_Settings extends REF_Controller {
    public function __construct() {
        parent::__construct();
        if ((!empty($this->user_data['role'])) && ($this->user_data['role'] != 3)) {
            show_403_error();
        }
        $this->load->library('upload');
        $this->load->model('basic_settings_model', '', TRUE);
        
        if (!file_exists(DIR_COMPANY_LOGO)) {
            mkdir(DIR_COMPANY_LOGO, 0777, true);
        }
        
        if (!file_exists(DIR_FAVICON)) {
            mkdir(DIR_FAVICON, 0777, true);
        }
    }
    
    public function index() {
        $data = array();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $data['setting'] = $this->basic_settings_model->get_by_id();
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $data['setting']->id;
        $data['view_link'] = $actions['view'];
        $data['edit_link'] = $actions['index'];
        
        $this->render($data, 'basic_settings/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $settings = array(
            'temple_name' => $this->input->post('temple_name'),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_temple_name') ),
            'reg_no' => $this->input->post('reg_no'),
            'comp_email' => $this->input->post('comp_email'),
            'contact_name' => $this->input->post('contact_name'),
            'contact_no' => $this->input->post('contact_no'),
            'contact_email' => $this->input->post('contact_email'),
            'fax' => $this->input->post('fax'),
            'website_url' => $this->input->post('website_url'),
            'address' => $this->input->post('address'),
            'updated_on' => date('Y-m-d H:i:s'),
            'updated_by' => $this->user_data['user_id']
        );
        
        
        
        
        if ($this->basic_settings_model->update($id, $settings) > 0) {
            $userdata = array();
            foreach($this->user_data as $key => $value) {
                $userdata[$key] = $value;
            }
            
            $this->session->set_userdata(TBL_PREFIX . 'user_logged_in', $userdata);
            $this->session->set_flashdata('success', 'Basic Settings updated successfully');
            
            /* $actions = $this->actions();
            $service_menu_id = 1; //Basic Settings      
            add_login_details($args = array( 'page_type_id' => '2', 'service_menu_id' => $service_menu_id, 'menu_type' => '2', 'auto_id' => $id)); */
        }
        else 
            $this->session->set_flashdata('error', 'Basic Settings not updated!');
        
        redirect('basic_settings', 'refresh');
    }
    
    public function view() {
        $data = array();
        $data['setting'] = $this->basic_settings_model->get_by_id();
        
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $data['setting']->id;
        $data['view_link'] = $actions['view'];
        $data['edit_link'] = $actions['index'];
        
        $this->render($data, 'basic_settings/view');
    }
}
?>