<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Design_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('design_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);
        }
        
        if (!file_exists(DIR_LOGIN_IMG)) {
            mkdir(DIR_LOGIN_IMG, 0777, true);
        }
        
        if (!file_exists(DIR_ADMIN_MENU_BG)) {
            mkdir(DIR_ADMIN_MENU_BG, 0777, true);
        }
        
        if (!file_exists(DIR_USER_MENU_BG)) {
            mkdir(DIR_USER_MENU_BG, 0777, true);
        }
        
        if (!file_exists(DIR_ADMIN_BG)) {
            mkdir(DIR_ADMIN_BG, 0777, true);
        }
        
        if (!file_exists(DIR_USER_BG)) {
            mkdir(DIR_USER_BG, 0777, true);
        }
    }
    
    public function font_family_list() {
        $font = array (
            'Verdana' => 'Verdana',
            'Geneva' => 'Geneva',
            'sans-serif' => '\'Source Sans Pro\',sans-serif',
            'Georgia' => 'Georgia',
            'Times New Roman' => 'Times New Roman',
            'Courier New' => 'Courier New',
            'Arial' => 'Arial',
            'Helvetica' => 'Helvetica',
            'Tahoma' => 'Tahoma',
            'Trebuchet MS' => 'Trebuchet MS',
            'Arial Black' => 'Arial Black',
            'Palatino Linotype' => 'Palatino Linotype',
            'Book Antiqua' => 'Book Antiqua',
            'Lucida Sans Unicode' => 'Lucida Sans Unicode',
            'Comic Sans MS' => 'Comic Sans MS',
            'Helvetica, sans-serif' => 'Helvetica, sans-serif',
            'Lucidatypewriter, monospace' => 'Lucidatypewriter, monospace',
            'Verdana, sans-serif' => 'Verdana, sans-serif',
            'Courier New, monospace' => 'Courier New, monospace',
            'Gill Sans Extrabold, sans-serif' => 'Gill Sans Extrabold, sans-serif'
        );
        return $font;
    }
    
    public function font_sizes() {
        $size = array(
            '6' => '6',
            '7' => '7',
            '8' => '8',
            '9' => '9',
            '10' => '10',
            '10.5' => '10.5',
            '11' => '11',
            '12' => '12',
            '13' => '13',
            '14' => '14',
            '15' => '15',
            '16' => '16',
            '18' => '18',
            '20' => '20',
            '22' => '22',
            '24' => '24',
            '26' => '26',
            '28' => '28',
            '32' => '32',
            '36' => '36',
            '40' => '40',
            '44' => '44',
            '48' => '48',
            '54' => '54',
            '60' => '60',
            '66' => '66',
            '72' => '72',
            '80' => '80',
            '88' => '88',
            '96' => '96'
        );
        
        return $size;     
    }
    
    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->design_settings_model->get_by_id();
        $data['fonts'] = $this->font_family_list();
        $data['sizes'] = $this->font_sizes();
        
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $data['setting']->id;
        $data['view_link'] = $actions['view'];
        $data['edit_link'] = $actions['index'];
        
        $this->render($data, 'design_settings/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
       
        $settings = array(
            'font_family' => $this->input->post('font_family'),
            'font_size' => $this->input->post('font_size'),
            'head_font_family' => $this->input->post('head_font_family'),
            'head_font_size' => $this->input->post('head_font_size'),
            'color1' => $this->input->post('color1'),
            'color2' => $this->input->post('color2'),
            'admin_menu_bg_color' => (!empty($this->input->post('admin_menu_bg_color'))) ? $this->input->post('admin_menu_bg_color') : '#444',
            'admin_menu_lbl_color' => (!empty($this->input->post('admin_menu_lbl_color'))) ? $this->input->post('admin_menu_lbl_color') : '#FFF',
            'admin_menu_select_color' => (!empty($this->input->post('admin_menu_select_color'))) ? $this->input->post('admin_menu_select_color') : '#0fd8d0',
            'user_menu_bg_color' => (!empty($this->input->post('user_menu_bg_color'))) ? $this->input->post('user_menu_bg_color') : '#444',
            'user_menu_lbl_color' => (!empty($this->input->post('user_menu_lbl_color'))) ? $this->input->post('user_menu_lbl_color') : '#FFF',
            'user_menu_select_color' => (!empty($this->input->post('user_menu_select_color'))) ? $this->input->post('user_menu_select_color') : '#0fd8d0',
            'keyboard_color' => (!empty($this->input->post('t_key_board'))) ? $this->input->post('t_key_board') : '#11bba8',
            'login_page_color' => (!empty($this->input->post('login_page_color'))) ? $this->input->post('login_page_color') : '#3c2a17',
            'archanai_title_color' => (!empty($this->input->post('title_color'))) ? $this->input->post('title_color') : '#3c2a17',
            'service_menu_color' => (!empty($this->input->post('service_menu_color'))) ? $this->input->post('service_menu_color') : '#3c2a17',
            'updated_on' => date('Y-m-d H:i:s'),
            'updated_by' => $this->user_data['user_id']
        );
        
        
        if (!empty($_FILES['logo']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_COMPANY_LOGO;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_COMPANY_LOGO)) {
                mkdir(DIR_COMPANY_LOGO, 0777, true);
            }
            
            if (!$this->upload->do_upload('logo')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                /* $width = 216;
                $height = 96;
                $this->resize_image(DIR_COMPANY_LOGO . $upload_data['file_name'],DIR_COMPANY_LOGO, $upload_data['file_name'], $width, $height); */
                $settings['logo'] = $upload_data['file_name'];
            }
        }
        
        if (!empty($_FILES['favicon']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_FAVICON;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_FAVICON)) {
                mkdir(DIR_FAVICON, 0777, true);
            }
            
            if (!$this->upload->do_upload('favicon')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['favicon'] = $upload_data['file_name'];
            }
        }
        
        if (!empty($_FILES['login_image']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_LOGIN_IMG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_LOGIN_IMG)) {
                mkdir(DIR_LOGIN_IMG, 0777, true);
            }
            
            if (!$this->upload->do_upload('login_image')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['login_image'] = $upload_data['file_name'];
            }
        }
        
        /*
         * Kiosk Scan
         */
        if( !empty( $_FILES['scan_login_image']['name'] ) ) :
            $config = array();
            $config['upload_path'] = DIR_LOGIN_IMG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            
            if( !$this->upload->do_upload('scan_login_image') ) :
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            else :
                $upload_data = $this->upload->data();
                $settings['scan_login_image'] = $upload_data['file_name'];
            endif;
        endif;
        
        if( !empty( $_FILES['scan_main_image']['name'] ) ) :
            $config = array();
            $config['upload_path'] = DIR_LOGIN_IMG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            
            if( !$this->upload->do_upload('scan_main_image') ) :
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            else :
                $upload_data = $this->upload->data();
                $settings['scan_main_image'] = $upload_data['file_name'];
            endif;
        endif;
        
        
        
        if (!empty($_FILES['admin_menu_bg']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_ADMIN_MENU_BG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_ADMIN_MENU_BG)) {
                mkdir(DIR_ADMIN_MENU_BG, 0777, true);
            }
            
            if (!$this->upload->do_upload('admin_menu_bg')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['admin_menu_bg'] = $upload_data['file_name'];
            }
        }
        
        if (!empty($_FILES['user_menu_bg']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_USER_MENU_BG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_USER_MENU_BG)) {
                mkdir(DIR_USER_MENU_BG, 0777, true);
            }
            
            if (!$this->upload->do_upload('user_menu_bg')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['user_menu_bg'] = $upload_data['file_name'];
            }
        }
        
        if (!empty($_FILES['admin_bg']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_ADMIN_BG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_ADMIN_BG)) {
                mkdir(DIR_ADMIN_BG, 0777, true);
            }
            
            if (!$this->upload->do_upload('admin_bg')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['admin_bg'] = $upload_data['file_name'];
            }
        }
        
        if (!empty($_FILES['user_bg']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_USER_BG;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_USER_BG)) {
                mkdir(DIR_USER_BG, 0777, true);
            }
            
            if (!$this->upload->do_upload('user_bg')) {
                $this->session->set_flashdata('error', $this->upload->display_errors());
                redirect('design_settings', 'refresh');
            }
            else {
                $upload_data = $this->upload->data();
                $settings['user_bg'] = $upload_data['file_name'];
            }
        }
        
        
        if ($this->design_settings_model->update($id, $settings) > 0) {
            $userdata = array();
            foreach($this->user_data as $key => $value) {
                $userdata[$key] = $value;
            }
            
            if (!empty($settings['logo'])) {
                $userdata['logo'] = $settings['logo'];
            }
            
            if (!empty($settings['favicon'])) {
                $userdata['favicon'] = $settings['favicon'];
            }
            
            if (!empty($settings['login_image'])) {
                $userdata['login_image'] = $settings['login_image'];
            }
            
            if (!empty($settings['admin_menu_bg'])) {
                $userdata['admin_menu_bg'] = $settings['admin_menu_bg'];
            }
            
            if (!empty($settings['user_menu_bg'])) {
                $userdata['user_menu_bg'] = $settings['user_menu_bg'];
            }
            
            if (!empty($settings['admin_bg'])) {
                $userdata['admin_bg'] = $settings['admin_bg'];
            }
            
            if (!empty($settings['user_bg'])) {
                $userdata['user_bg'] = $settings['user_bg'];
            }
            
            $this->session->set_userdata(TBL_PREFIX . 'user_logged_in', $userdata);
            $this->session->set_flashdata('success', 'Design Settings updated successfully');
            
            /* $actions = $this->actions();
            $service_menu_id = 3; //Design 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', 'Design Settings not updated!');
        
        redirect('design_settings', 'refresh');
    }
    
    public function view() {
        $data = array();
        $data['setting'] = $this->design_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, 'design_settings/view');
    }
    
     /* public function resize_image($source_file_path, $target_path, $file_name, $width, $height) {
        $target_file_path = $target_path . $file_name;
        $type = end(explode('.', $file_name));
        switch($type) {
            case 'jpg':
            case 'jpeg':
                $source_img = imagecreatefromjpeg($source_file_path);
            break;

            case 'png':
                $source_img = imagecreatefrompng($source_file_path);
            break;

            default:
            break;
        }

        $temp_img = imagecreatetruecolor($width, $height);
        list($source_width, $source_height) = getimagesize($source_file_path);
        imagecopyresized($temp_img, $source_img, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
        imagejpeg($temp_img, $target_file_path, 100);
        imagedestroy($temp_img);
        return $target_file_path;
    } */
}
?>