<?php
/*
@author  Dmitriy Kubarev
@link  http://www.simpleopencart.com
@link  http://www.opencart.com/index.php?route=extension/extension/info&extension_id=4811
*/  

class ControllerModuleSimple extends Controller {
    private $error = array(); 
    private $default_settings = '[encoded data, 16492 characters]';
    private $settings;
    private $store_settings;

    public function install() {
        $this->load->model('setting/setting');
        $this->load->model('setting/store');

        $stores = $this->model_setting_store->getStores();
        $stores[] = array('store_id' => 0, 'name' => 'default');

        $settings = unserialize(base64_decode($this->default_settings));

        foreach ($stores as $key => $value) {
            $this->model_setting_setting->editSetting('simple', $settings, $value['store_id']);
        }

        $this->create_tables();
    }

    private function create_tables() {
        $this->db->query('CREATE TABLE IF NOT EXISTS `simple_custom_data` (
                          `object_type` tinyint(4) NOT NULL,
                          `object_id` int(11) NOT NULL,
                          `customer_id` int(11) NOT NULL,
                          `data` text NOT NULL,
                          PRIMARY KEY (`object_type`,`object_id`,`customer_id`)
                        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;');
    }
      
    private function load_language($path) {
        $language = $this->language;
        if (isset($language) && method_exists($language, 'load')) {
            $this->language->load($path);
            unset($language);
            return;
        }

        $load = $this->load;
        if (isset($load) && method_exists($load, 'language')) {
            $this->load->language($path);
            unset($load);
            return;
        }
    }
    public function index() {
        $this->load_language('module/simple');
            
        $this->document->setTitle($this->language->get('heading_title'));
        
        $this->load->model('setting/setting');
        $this->load->model('setting/store');
        
        $this->data['stores'] = $this->model_setting_store->getStores();
        $this->data['stores'][] = array('store_id' => 0, 'name' => 'default');

        if (isset($this->request->get['store_id'])) {
            $this->data['store_id'] = $this->request->get['store_id'];
        } else {
            $this->data['store_id'] = 0;
        }

        $this->load->model('sale/customer_group');
        $this->data['groups'] = $this->model_sale_customer_group->getCustomerGroups();
        
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->create_tables();
            
            if (is_uploaded_file($this->request->files['import']['tmp_name'])) {
                $content = file_get_contents($this->request->files['import']['tmp_name']);
                $tmp = unserialize(base64_decode($content));
                if (is_array($tmp)) {
                    $this->request->post = $tmp;
                }
            }

            foreach ($this->data['groups'] as $group) {
             
                if (isset($this->request->post['simple_set_checkout_customer']['shipping'][$group['customer_group_id']]) && is_array($this->request->post['simple_set_checkout_customer']['shipping'][$group['customer_group_id']])) {
                    $tmp = array();
                    foreach ($this->request->post['simple_set_checkout_customer']['shipping'][$group['customer_group_id']] as $key => $value) {
                        $tmp[str_replace('_101_', '.', $key)] = $value;
                    }
                    $this->request->post['simple_set_checkout_customer']['shipping'][$group['customer_group_id']] = $tmp;
                }

                if (isset($this->request->post['simple_set_checkout_address']['shipping'][$group['customer_group_id']]) && is_array($this->request->post['simple_set_checkout_address']['shipping'][$group['customer_group_id']])) {
                    $tmp = array();
                    foreach ($this->request->post['simple_set_checkout_address']['shipping'][$group['customer_group_id']] as $key => $value) {
                        $tmp[str_replace('_101_', '.', $key)] = $value;
                    }
                    $this->request->post['simple_set_checkout_address']['shipping'][$group['customer_group_id']] = $tmp;
                }
            }

            $simple_common_template = $this->request->post['simple_common_template'];
            $simple_common_template = str_replace(' ', '', $simple_common_template);

            $find = array(
                '{cart}',
                '{shipping}',
                '{payment}',
                '{agreement}',
                '{help}',
                '{payment_form}'
            );  

            $replace = array(
                '{cart}'         => '',
                '{shipping}'     => '',
                '{payment}'      => '',
                '{agreement}'    => '',
                '{help}'         => '',
                '{payment_form}' => ''
            );  

            $simple_common_template = trim(str_replace($find, $replace, $simple_common_template));
            $simple_common_template = preg_replace('/\[\[[-_a-zA-Z0-9.]+\]\]/si', '', $simple_common_template);

            $find = array(
                    '{left_column}{/left_column}',
                    '{right_column}{/right_column}'
            );  

            $replace = array(
                '{left_column}{/left_column}'   => '',
                '{right_column}{/right_column}' => ''
            );
                
            $simple_common_template = trim(str_replace($find, $replace, $simple_common_template));

            if ($simple_common_template == '{customer}') {
                $this->request->post['simple_customer_two_column'] = true;
            } else {
                $this->request->post['simple_customer_two_column'] = false;
            }

            $this->model_setting_setting->editSetting('simple', $this->request->post, $this->data['store_id']);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('module/simple', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL'));
        }

            
        $this->data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }
      
        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_module'),
            'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('module/simple', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['button_save']    = $this->language->get('button_save');
        $this->data['button_cancel']  = $this->language->get('button_cancel');
        $this->data['button_restore'] = $this->language->get('button_restore');

        $this->data['action_without_store'] = $this->url->link('module/simple', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['action']               = $this->url->link('module/simple', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL');
        $this->data['backup_link']          = $this->url->link('module/simple/backup', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL');
        $this->data['restore_link']         = $this->url->link('module/simple/restore', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL');
        $this->data['header_save_link']     = $this->url->link('module/simple/header', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL');
        $this->data['footer_save_link']     = $this->url->link('module/simple/footer', 'token=' . $this->session->data['token'] . '&store_id=' . $this->data['store_id'], 'SSL');
        $this->data['cancel']               = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['token']                = $this->session->data['token'];
        
        $this->data['tab_checkout']                               = $this->language->get('tab_checkout');
        $this->data['tab_customer_fields']                        = $this->language->get('tab_customer_fields');
        $this->data['tab_registration']                           = $this->language->get('tab_registration');
        $this->data['tab_account_pages']                          = $this->language->get('tab_account_pages');
        $this->data['tab_backup']                                 = $this->language->get('tab_backup');
        $this->data['tab_headers']                                = $this->language->get('tab_headers');
        $this->data['tab_methods']                                = $this->language->get('tab_methods');
        $this->data['tab_template_helper']                        = $this->language->get('tab_template_helper');
        
        $this->data['text_yes']                                   = $this->language->get('text_yes');
        $this->data['text_no']                                    = $this->language->get('text_no');
        
        $this->data['text_module_links']                          = $this->language->get('text_module_links');
        $this->data['text_module_links_2']                        = $this->language->get('text_module_links_2');
        $this->data['entry_payment_module']                       = $this->language->get('entry_payment_module');
        $this->data['entry_shipping_modules']                     = $this->language->get('entry_shipping_modules');
        $this->data['entry_payment_modules']                      = $this->language->get('entry_payment_modules');
        $this->data['entry_shipping_module']                      = $this->language->get('entry_shipping_module');
        $this->data['text_select_shipping']                       = $this->language->get('text_select_shipping');
        $this->data['entry_template']                             = $this->language->get('entry_template');
        $this->data['entry_template_description']                 = $this->language->get('entry_template_description');
        $this->data['text_simplecheckout']                        = $this->language->get('text_simplecheckout');
        $this->data['text_agreement_block']                       = $this->language->get('text_agreement_block');
        $this->data['entry_agreement_id']                         = $this->language->get('entry_agreement_id');
        $this->data['entry_agreement_text']                       = $this->language->get('entry_agreement_text');
        $this->data['entry_agreement_checkbox']                   = $this->language->get('entry_agreement_checkbox');
        $this->data['entry_agreement_checkbox_init']              = $this->language->get('entry_agreement_checkbox_init');
        $this->data['text_shipping_block']                        = $this->language->get('text_shipping_block');
        $this->data['entry_shipping_title']                       = $this->language->get('text_shipping_title');
        $this->data['entry_shipping_address_empty']               = $this->language->get('entry_shipping_address_empty');
        $this->data['text_payment_block']                         = $this->language->get('text_payment_block');
        $this->data['entry_payment_address_empty']                = $this->language->get('entry_payment_address_empty');
        $this->data['text_customer_block']                        = $this->language->get('text_customer_block');
        $this->data['entry_customer_register']                    = $this->language->get('entry_customer_register');
        $this->data['text_user_choice']                           = $this->language->get('text_user_choice');
        $this->data['entry_customer_login']                       = $this->language->get('entry_customer_login');
        $this->data['entry_customer_type']                        = $this->language->get('entry_customer_type');
        $this->data['entry_customer_type_selection']              = $this->language->get('entry_customer_type_selection');
        $this->data['entry_customer_group_after_reg']             = $this->language->get('entry_customer_group_after_reg');
        $this->data['entry_customer_register_init']               = $this->language->get('entry_customer_register_init');
        $this->data['entry_customer_address_select']              = $this->language->get('entry_customer_address_select');
        $this->data['text_registration_page']                     = $this->language->get('text_registration_page');
        $this->data['entry_registration_agreement_id']            = $this->language->get('entry_registration_agreement_id');
        $this->data['entry_registration_agreement_checkbox']      = $this->language->get('entry_registration_agreement_checkbox');
        $this->data['entry_registration_agreement_checkbox_init'] = $this->language->get('entry_registration_agreement_checkbox_init');
        $this->data['entry_registration_captcha']                 = $this->language->get('entry_registration_captcha');
        $this->data['text_fields_default']                        = $this->language->get('text_fields_default');
        $this->data['entry_field_id']                             = $this->language->get('entry_field_id');
        $this->data['entry_field_label']                          = $this->language->get('entry_field_label');
        $this->data['entry_field_type']                           = $this->language->get('entry_field_type');
        $this->data['entry_field_init']                           = $this->language->get('entry_field_init');
        $this->data['entry_field_values']                         = $this->language->get('entry_field_values');
        $this->data['entry_field_validation']                     = $this->language->get('entry_field_validation');
        $this->data['entry_field_save_to']                        = $this->language->get('entry_field_save_to');
        $this->data['entry_shipping_address_full']                = $this->language->get('entry_shipping_address_full');
        $this->data['entry_payment_address_full']                 = $this->language->get('entry_payment_address_full');
        $this->data['entry_shipping_address_full_description']    = $this->language->get('entry_shipping_address_full_description');
        $this->data['entry_payment_address_full_description']     = $this->language->get('entry_payment_address_full_description');
        $this->data['text_validation_none']                       = $this->language->get('text_validation_none');
        $this->data['text_validation_length']                     = $this->language->get('text_validation_length');
        $this->data['text_validation_regexp']                     = $this->language->get('text_validation_regexp');
        $this->data['text_validation_values']                     = $this->language->get('text_validation_values');
        $this->data['text_validation_not_null']                   = $this->language->get('text_validation_not_null');
        $this->data['entry_field_validation_error']               = $this->language->get('entry_field_validation_error');
        $this->data['text_select']                                = $this->language->get('text_select');
        $this->data['entry_customer_subscribe']                   = $this->language->get('entry_customer_subscribe');
        $this->data['entry_customer_subscribe_init']              = $this->language->get('entry_customer_subscribe_init');
        $this->data['text_add_field']                             = $this->language->get('text_add_field');
        $this->data['button_add']                                 = $this->language->get('button_add');
        $this->data['button_delete']                              = $this->language->get('button_delete');
        $this->data['entry_customer_password_confirm']            = $this->language->get('entry_customer_password_confirm');
        $this->data['entry_geoip_init']                           = $this->language->get('entry_geoip_init');
        $this->data['entry_shipping_module']                      = $this->language->get('entry_shipping_module');
        $this->data['entry_customer_fields']                      = $this->language->get('entry_customer_fields');
        $this->data['entry_password_length']                      = $this->language->get('entry_password_length');
        $this->data['entry_customer_email_field']                 = $this->language->get('entry_customer_email_field');
        $this->data['text_hide']                                  = $this->language->get('text_hide');
        $this->data['text_show_not_required']                     = $this->language->get('text_show_not_required');
        $this->data['text_required']                              = $this->language->get('text_required');
        $this->data['text_validation_function']                   = $this->language->get('text_validation_function');
        $this->data['entry_payment_autoselect_first']             = $this->language->get('entry_payment_autoselect_first');
        $this->data['entry_shipping_autoselect_first']            = $this->language->get('entry_shipping_autoselect_first');
        $this->data['entry_jquery_masked_input_mask']             = $this->language->get('entry_jquery_masked_input_mask');
        $this->data['entry_city_autocomplete']                    = $this->language->get('entry_city_autocomplete');
        $this->data['entry_generate_password']                    = $this->language->get('entry_generate_password');
        $this->data['entry_placeholder']                          = $this->language->get('entry_placeholder');
        $this->data['entry_save_label']                           = $this->language->get('entry_save_label');
        $this->data['text_order_minmax']                          = $this->language->get('text_order_minmax');
        $this->data['entry_use_total']                            = $this->language->get('entry_use_total');
        $this->data['entry_min_amount']                           = $this->language->get('entry_min_amount');
        $this->data['entry_max_amount']                           = $this->language->get('entry_max_amount');
        $this->data['entry_min_quantity']                         = $this->language->get('entry_min_quantity');
        $this->data['entry_max_quantity']                         = $this->language->get('entry_max_quantity');
        $this->data['entry_min_weight']                           = $this->language->get('entry_min_weight');
        $this->data['entry_max_weight']                           = $this->language->get('entry_max_weight');
        $this->data['entry_payment_method']                       = $this->language->get('entry_payment_method');
        $this->data['text_shipping_address_block']                = $this->language->get('text_shipping_address_block');
        $this->data['entry_shipping_address_select']              = $this->language->get('entry_shipping_address_select');
        $this->data['entry_shipping_address_show']                = $this->language->get('entry_shipping_address_show');
        $this->data['entry_shipping_address_same_init']           = $this->language->get('entry_shipping_address_same_init');
        $this->data['entry_shipping_address_same_show']           = $this->language->get('entry_shipping_address_same_show');
        $this->data['entry_shipping_address_fields']              = $this->language->get('entry_shipping_address_fields');
        $this->data['entry_help_text']                            = $this->language->get('entry_help_text');
        $this->data['entry_help_id']                              = $this->language->get('entry_help_id');
        $this->data['text_help_block']                            = $this->language->get('text_help_block');
        $this->data['entry_hide']                                 = $this->language->get('entry_hide');
        $this->data['entry_hide_if_logged']                       = $this->language->get('entry_hide_if_logged');
        $this->data['entry_empty_email']                          = $this->language->get('entry_empty_email');
        $this->data['entry_show_weight']                          = $this->language->get('entry_show_weight');
        $this->data['entry_fields_for_reload']                    = $this->language->get('entry_fields_for_reload');
        $this->data['entry_use_cookies']                          = $this->language->get('entry_use_cookies');
        $this->data['entry_show_will_be_registerd']               = $this->language->get('entry_show_will_be_registerd');
        $this->data['entry_guest_checkout']                       = $this->language->get('entry_guest_checkout');
        $this->data['entry_field_object']                         = $this->language->get('entry_field_object');
        $this->data['text_account_info_page']                     = $this->language->get('text_account_info_page');
        $this->data['text_account_address_page']                  = $this->language->get('text_account_address_page');
        $this->data['text_restore']                               = $this->language->get('text_restore');             
        $this->data['text_checkout_asap_logged']                  = $this->language->get('text_checkout_asap_logged');             
        $this->data['text_checkout_asap_not_logged']              = $this->language->get('text_checkout_asap_not_logged');             
        $this->data['text_no_only_after_click']                   = $this->language->get('text_no_only_after_click');             
        $this->data['text_payment_options_for_groups']            = $this->language->get('text_payment_options_for_groups');             
        $this->data['text_shipping_options_for_groups']           = $this->language->get('text_shipping_options_for_groups');             
        $this->data['entry_googleapi']                            = $this->language->get('entry_googleapi');             
        $this->data['entry_googleapi_key']                        = $this->language->get('entry_googleapi_key');             
        $this->data['entry_googleapi_language']                   = $this->language->get('entry_googleapi_language');             
        $this->data['entry_confirm_email']                        = $this->language->get('entry_confirm_email');             
        $this->data['entry_field_place']                          = $this->language->get('entry_field_place');             
        $this->data['entry_field_object_field']                   = $this->language->get('entry_field_object_field');             
        $this->data['entry_steps']                                = $this->language->get('entry_steps');             
        $this->data['entry_steps_summary']                        = $this->language->get('entry_steps_summary');             
        $this->data['entry_comment_target']                       = $this->language->get('entry_comment_target');             
        $this->data['entry_minify']                               = $this->language->get('entry_minify');             
        $this->data['entry_stub_not_show']                        = $this->language->get('entry_stub_not_show');             
        $this->data['entry_stub_show_always']                     = $this->language->get('entry_stub_show_always');             
        $this->data['entry_stub_show_only_for_empty']             = $this->language->get('entry_stub_show_only_for_empty');             
        $this->data['entry_stub_use_description']                 = $this->language->get('entry_stub_use_description');             
        $this->data['text_stub_description']                      = $this->language->get('text_stub_description');             
        $this->data['text_template_1']                            = $this->language->get('text_template_1');             
        $this->data['text_template_2']                            = $this->language->get('text_template_2');             
        $this->data['text_template_3']                            = $this->language->get('text_template_3');             
        $this->data['text_template_4']                            = $this->language->get('text_template_4');             
        $this->data['text_help_move_field']                       = $this->language->get('text_help_move_field');             
        $this->data['entry_tag_for_headers']                      = $this->language->get('entry_tag_for_headers');             
        $this->data['entry_only_business_days']                   = $this->language->get('entry_only_business_days');             
        $this->data['entry_only_for_days']                        = $this->language->get('entry_only_for_days');             
        $this->data['entry_min_date']                             = $this->language->get('entry_min_date');             
        $this->data['entry_max_date']                             = $this->language->get('entry_max_date');             
        $this->data['entry_start_date']                           = $this->language->get('entry_start_date');             
        $this->data['entry_end_date']                             = $this->language->get('entry_end_date');             
        $this->data['text_drag_n_drop']                           = $this->language->get('text_drag_n_drop');             
        $this->data['entry_debug_mode']                           = $this->language->get('entry_debug_mode');             
        $this->data['entry_show_button_back']                     = $this->language->get('entry_show_button_back');             
        $this->data['entry_geoip_mode']                           = $this->language->get('entry_geoip_mode');             
        $this->data['text_examples']                              = $this->language->get('text_examples');             
        $this->data['text_example_two_columns']                   = $this->language->get('text_example_two_columns');             
        $this->data['text_example_one_column']                    = $this->language->get('text_example_one_column');             
        $this->data['text_example_combined']                      = $this->language->get('text_example_combined');             
        $this->data['text_example_three_columns']                 = $this->language->get('text_example_three_columns');             
        $this->data['text_geoip_mode_1']                          = $this->language->get('text_geoip_mode_1');             
        $this->data['text_geoip_mode_2']                          = $this->language->get('text_geoip_mode_2');             
        $this->data['text_geoip_mode_3']                          = $this->language->get('text_geoip_mode_3');             
        $this->data['text_example']                               = $this->language->get('text_example');             
        $this->data['text_example_for_links']                     = $this->language->get('text_example_for_links');             
        $this->data['entry_use_geo_selector']                     = $this->language->get('entry_use_geo_selector');             
        $this->data['entry_geo_selector_type']                    = $this->language->get('entry_geo_selector_type');             
        $this->data['text_geo_selector_type_1']                   = $this->language->get('text_geo_selector_type_1');             
        $this->data['text_geo_selector_type_2']                   = $this->language->get('text_geo_selector_type_2');             
        $this->data['text_geo_selector_type_3']                   = $this->language->get('text_geo_selector_type_3');             
        $this->data['text_help_geo_selector']                     = $this->language->get('text_help_geo_selector');             
        $this->data['entry_type_of_links']                        = $this->language->get('entry_type_of_links');             
        $this->data['text_payment_shipping']                      = $this->language->get('text_payment_shipping');             
        $this->data['text_shipping_payment']                      = $this->language->get('text_shipping_payment');             
         
        $this->init_field('simple_links', array());
        $this->init_field('simple_links_2', array());
        $this->init_field('simple_common_template', '{left_column}{cart}{customer}{/left_column}{right_column}{shipping}{payment}{agreement}{/right_column}');
        $this->init_field('simple_common_view_agreement_id');
        $this->init_field('simple_common_view_agreement_text');
        $this->init_field('simple_common_view_agreement_checkbox');
        $this->init_field('simple_common_view_agreement_checkbox_init');
        $this->init_field('simple_shipping_view_title');
        $this->init_field('simple_shipping_view_address_empty');
        $this->init_field('simple_payment_view_address_empty');
        $this->init_field('simple_payment_view_autoselect_first');
        $this->init_field('simple_shipping_view_autoselect_first');
        $this->init_field('simple_customer_action_register');
        $this->init_field('simple_customer_view_login');
        $this->init_field('simple_customer_view_customer_type');
        $this->init_field('simple_customer_view_customer_register_init');
        $this->init_field('simple_customer_view_address_select');
        $this->init_field('simple_registration_agreement_id');
        $this->init_field('simple_registration_agreement_checkbox');
        $this->init_field('simple_registration_agreement_checkbox_init');
        $this->init_field('simple_registration_captcha');
        $this->init_field('simple_shipping_view_address_full', array());
        $this->init_field('simple_payment_view_address_full', array());
        $this->init_field('simple_customer_action_subscribe');
        $this->init_field('simple_customer_view_customer_subscribe_init');
        $this->init_field('simple_registration_subscribe');
        $this->init_field('simple_registration_subscribe_init');
        $this->init_field('simple_customer_view_password_confirm');
        $this->init_field('simple_customer_view_password_length_min');
        $this->init_field('simple_customer_view_password_length_max');
        $this->init_field('simple_registration_password_confirm');
        $this->init_field('simple_registration_password_length_min');
        $this->init_field('simple_registration_password_length_max');
        $this->init_field('simple_registration_view_customer_type');

        $this->init_field('simple_set_checkout_customer');
        $this->init_field('simple_set_checkout_address');
        $this->init_field('simple_set_registration');
        $this->init_field('simple_set_account_info');
        $this->init_field('simple_set_account_address');

        $this->init_field('simple_customer_view_email');
        $this->init_field('simple_customer_view_email_confirm');
        $this->init_field('simple_customer_generate_password');
        $this->init_field('simple_registration_generate_password');
        $this->init_field('simple_registration_view_email_confirm');
        $this->init_field('simple_show_shipping_address');
        $this->init_field('simple_show_shipping_address_same_init', 1);
        $this->init_field('simple_show_shipping_address_same_show', 1);
        $this->init_field('simple_shipping_view_address_select');
        $this->init_field('simple_use_total');
        $this->init_field('simple_min_amount');
        $this->init_field('simple_max_amount');
        $this->init_field('simple_min_quantity');
        $this->init_field('simple_max_quantity');
        $this->init_field('simple_min_weight');
        $this->init_field('simple_max_weight');
        $this->init_field('simple_debug');
        $this->init_field('simple_common_view_help_id');
        $this->init_field('simple_common_view_help_text');
        $this->init_field('simple_joomla_path');
        $this->init_field('simple_joomla_route');
        $this->init_field('simple_shipping_methods_hide');
        $this->init_field('simple_payment_methods_hide');
        $this->init_field('simple_customer_hide_if_logged');
        $this->init_field('simple_address_hide_if_logged');
        $this->init_field('simple_empty_email');
        $this->init_field('simple_show_weight');
        $this->init_field('simple_set_for_reload');
        $this->init_field('simple_use_cookies');
        $this->init_field('simple_show_will_be_registered');
        $this->init_field('simple_disable_guest_checkout');
        $this->init_field('simple_checkout_asap_for_not_logged');
        $this->init_field('simple_checkout_asap_for_logged');
        $this->init_field('simple_group_shipping');
        $this->init_field('simple_group_payment');
        $this->init_field('simple_account_view_customer_type');
        $this->init_field('simple_headers', array());
        $this->init_field('simple_header_tag', 'h3');
        $this->init_field('simple_geoip_mode', '1');
        $this->init_field('simple_googleapi');
        $this->init_field('simple_googleapi_key');
        $this->init_field('simple_show_back');
        $this->init_field('simple_shipping_titles', array());
        $this->init_field('simple_payment_titles', array());
        $this->init_field('simple_type_of_selection_of_group', 'radio');
        $this->init_field('simple_customer_group_id_after_reg', $this->config->get('config_customer_group_id'));
        $this->init_field('simple_steps');
        $this->init_field('simple_steps_summary');
        $this->init_field('simple_comment_target');
        $this->init_field('simple_minify', 0);
        $this->init_field('simple_customer_use_geo_selector', 0);
        $this->init_field('simple_customer_geo_selector_type', 1);
        $this->init_field('simple_shipping_address_use_geo_selector', 0);
        $this->init_field('simple_shipping_address_geo_selector_type', 1);
        $this->init_field('simple_childs_payment_first', 0);

        // fix
        $this->data['tab_methods'] = $this->data['tab_methods'] == 'tab_methods' ? 'Stubs for methods' : $this->data['tab_methods'];

        $this->load->model('setting/extension');

        $fields_for_autoreloading_needed = false;
        
        $payment_extensions = $this->model_setting_extension->getInstalled('payment');
        $tmp = array();
        foreach ($payment_extensions as $extension) {
            if ($this->config->get($extension . '_status')) {
                $tmp[] = $extension;
            }
            if ($this->config->get($extension . '_geo_zone_id')) {
                $fields_for_autoreloading_needed = true;
            }
        }
        $payment_extensions = $tmp;
        
        $this->data['payment_extensions'] = array();
        
        $files = glob(DIR_APPLICATION . 'controller/payment/*.php');
    
        if ($files) {
            foreach ($files as $file) {
                $extension = basename($file, '.php');

                if (in_array($extension, $payment_extensions)) {
                    $this->load_language('payment/' . $extension);
                    $this->data['payment_extensions'][$extension] = $this->language->get('heading_title');
                }
            }
        }
      
        $shipping_extensions = $this->model_setting_extension->getInstalled('shipping');
        $tmp = array();
        foreach ($shipping_extensions as $extension) {
            if ($this->config->get($extension . '_status')) {
                $tmp[] = $extension;
            }
            if ($this->config->get($extension . '_geo_zone_id')) {
                $fields_for_autoreloading_needed = true;
            }
        }
        $shipping_extensions = $tmp;
        
        $this->data['shipping_extensions'] = array();
        
        $files = glob(DIR_APPLICATION . 'controller/shipping/*.php');
    
        if ($files) {
            foreach ($files as $file) {
                $extension = basename($file, '.php');

                if (in_array($extension, $shipping_extensions)) {
                    $this->load_language('shipping/' . $extension);
                    $this->data['shipping_extensions'][$extension] = $this->language->get('heading_title');
                }
            }
        }

        $this->data['shipping_extensions_2'] = $this->data['shipping_extensions'];

        foreach ($this->data['simple_links_2'] as $key => $value) {
            if (!isset($this->data['shipping_extensions_2'][$key])) {
                $tmp = explode('.', $key);
                $text = $key;
                if (isset($tmp[0]) && !empty($this->data['shipping_extensions'][$tmp[0]])) {
                    $text = $this->data['shipping_extensions'][$tmp[0]].' ('.$key.')';
                }
                $this->data['shipping_extensions_2'][$key] = $text;
            }
        }

        if (!$fields_for_autoreloading_needed) {
        //    $this->data['simple_set_for_reload'] = '';
        }
        
        $shipping_codes = array_keys($this->data['shipping_extensions']);
        
        $customer_group_id = $this->config->get('config_customer_group_id');

        $this->data['shipping_extensions_for_customer'] = array_diff(isset($this->data['simple_set_checkout_customer']['shipping'][$customer_group_id]) ? array_keys($this->data['simple_set_checkout_customer']['shipping'][$customer_group_id]) : array(), $shipping_codes);
        $this->data['shipping_extensions_for_shipping_address'] = array_diff(isset($this->data['simple_set_checkout_address']['shipping'][$customer_group_id]) ? array_keys($this->data['simple_set_checkout_address']['shipping'][$customer_group_id]) : array(), $shipping_codes);
        
        $this->load->model('localisation/language');
        $languages = $this->model_localisation_language->getLanguages();
        $this->data['languages'] = array();
        foreach ($languages as $language) {
            $language['code'] = str_replace('-', '_', strtolower($language['code']));
            $this->data['languages'][] = $language;
        }

        $current_language = strtolower($this->config->get('config_admin_language'));
        $this->data['current_language'] = $current_language;

        $this->load->model('catalog/information');
        $this->data['informations'] = $this->model_catalog_information->getInformations();
        
        $this->init_field('simple_fields_main', array());
        $this->init_field('simple_fields_custom', array());
        
        $this->data['simple_fields_all'] = $this->data['simple_fields_main'] + $this->data['simple_fields_custom'];

        $address_fields = array('main_firstname','main_lastname','main_company','main_company_id','main_tax_id','main_address_1','main_address_2','main_city','main_postcode','main_zone_id','main_country_id');
        $account_info_fields = array('main_email','main_firstname','main_lastname','main_fax','main_telephone');

        $this->data['simple_fields_for_checkout_customer'] = array();
        $this->data['simple_fields_for_checkout_shipping_address'] = array();
        $this->data['simple_fields_for_registration'] = array();
        $this->data['simple_fields_for_account_info'] = array();
        $this->data['simple_fields_for_account_address'] = array();

        foreach ($this->data['simple_fields_main'] as $settings) {
            $key = $settings['id'];
            $text = !empty($settings['label'][$current_language]) ? $settings['label'][$current_language] : $key;

            $this->data['simple_fields_for_checkout_customer'][$key] = $text;

            if (in_array($key, $address_fields)) {
                $this->data['simple_fields_for_checkout_shipping_address'][$key] = $text;
            }

            if (in_array($key, $address_fields) || in_array($key, $account_info_fields)) {
                $this->data['simple_fields_for_registration'][$key] = $text;
            }

            if (in_array($key, $account_info_fields)) {
                $this->data['simple_fields_for_account_info'][$key] = $text;
            }

            if (in_array($key, $address_fields)) {
                $this->data['simple_fields_for_account_address'][$key] = $text;
            }
        }

        $headers = array();
        foreach ($this->data['simple_headers'] as $settings) {
            $key = $settings['id'];
            $text = !empty($settings['label'][$current_language]) ? $settings['label'][$current_language] : $key;
            $headers[$key] = $text;
        }

        foreach ($this->data['simple_fields_custom'] as $settings) {
            $key = $settings['id'];
            $text = !empty($settings['label'][$current_language]) ? $settings['label'][$current_language] : $key;
            $this->data['simple_fields_for_checkout_customer'][$key] = $text;

            if ($settings['object_type'] == 'address' || $settings['object_type'] == 'order') {
                $this->data['simple_fields_for_checkout_shipping_address'][$key] = $text;
            }

            if ($settings['object_type'] == 'address' || $settings['object_type'] == 'customer') {
                $this->data['simple_fields_for_registration'][$key] = $text;
            }

            if ($settings['object_type'] == 'customer') {
                $this->data['simple_fields_for_account_info'][$key] = $text;
            }

            if ($settings['object_type'] == 'address') {
                $this->data['simple_fields_for_account_address'][$key] = $text;
            }
        }

        $this->data['simple_fields_for_registration'] += $headers;
        $this->data['simple_fields_for_checkout_customer'] += $headers + array('split_split' => '<-- '.$this->language->get('text_splitter').' -->');
        $this->data['simple_fields_for_checkout_shipping_address'] += $headers + array('split_split' => '<-- '.$this->language->get('text_splitter').' -->');
        $this->data['simple_fields_for_account_info'] += $headers;
        $this->data['simple_fields_for_account_address'] += $headers;

        foreach ($this->data['groups'] as $group) {
            if (empty($this->data['simple_set_checkout_customer']['group'][$group['customer_group_id']]) && !empty($this->data['simple_set_checkout_customer']['group'][1])) {
                $this->data['simple_set_checkout_customer']['group'][$group['customer_group_id']] = $this->data['simple_set_checkout_customer']['group'][1];
            }
            if (empty($this->data['simple_set_checkout_address']['group'][$group['customer_group_id']]) && !empty($this->data['simple_set_checkout_address']['group'][1])) {
                $this->data['simple_set_checkout_address']['group'][$group['customer_group_id']] = $this->data['simple_set_checkout_address']['group'][1];
            }
            if (empty($this->data['simple_set_registration']['group'][$group['customer_group_id']]) && !empty($this->data['simple_set_registration']['group'][1])) {
                $this->data['simple_set_registration']['group'][$group['customer_group_id']] = $this->data['simple_set_registration']['group'][1];
            }
            if (empty($this->data['simple_set_account_info']['group'][$group['customer_group_id']]) && !empty($this->data['simple_set_account_info']['group'][1])) {
                $this->data['simple_set_account_info']['group'][$group['customer_group_id']] = $this->data['simple_set_account_info']['group'][1];
            }
            if (empty($this->data['simple_set_account_address']['group'][$group['customer_group_id']]) && !empty($this->data['simple_set_account_address']['group'][1])) {
                $this->data['simple_set_account_address']['group'][$group['customer_group_id']] = $this->data['simple_set_account_address']['group'][1];
            }
        }

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }
        
        $this->load->model('localisation/country');
        $this->data['countries'] = $this->model_localisation_country->getCountries();
        
        $this->load->model('localisation/zone');
        $this->data['zones'] = isset($this->data['simple_fields_main']['main_country_id']['init']) ? $this->model_localisation_zone->getZonesByCountryId($this->data['simple_fields_main']['main_country_id']['init']) : array();
        
        $this->data['zone_action'] = $this->url->link('module/simple/zone', 'token=' . $this->session->data['token'], 'SSL');

        $header_content = '';
        $footer_content = '';
        if (file_exists(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl')) {
            $tpl = file_get_contents(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl');
            $f_b = utf8_strpos($tpl, '<form');
            $header_content = utf8_substr($tpl, 0, $f_b);
            $f_e = utf8_strpos($tpl, '</form>');
            $footer_content = utf8_substr($tpl, $f_e + 7);;
        }
        
        $this->data['theme_folder'] = DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/';
        $this->data['styles_path'] = DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/stylesheet/simple.css';
        $this->data['common_header_path'] = DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/common/simple_header.tpl';
        $this->data['common_header_content'] = $header_content;
        $this->data['common_footer_path'] = DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/common/simple_footer.tpl';
        $this->data['common_footer_content'] = $footer_content;

        $this->template = 'module/simple.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }
    
    private function init_field($field_name, $default_value = '') {
        if (empty($this->settings)) {
            $this->settings = $this->model_setting_setting->getSetting('simple',$this->data['store_id']);
            if (empty($this->settings)) {
                $this->settings = unserialize(base64_decode($this->default_settings));
            }
        }

        if (isset($this->request->post[$field_name])) {
            $this->data[$field_name] = $this->request->post[$field_name];
        } elseif (isset($this->settings[$field_name])) {
            if ($this->is_serialized($this->settings[$field_name])) {
                $this->settings[$field_name] = unserialize($this->settings[$field_name]);
            }
            $this->data[$field_name] = $this->settings[$field_name];
        }

        if (!isset($this->data[$field_name])) {
            $this->data[$field_name] = $default_value;
        }
    }

    private function get_setting($key) {
        if (empty($this->store_settings)) {
            $this->store_settings = $this->model_setting_setting->getSetting('config', $this->data['store_id']);
        }
        
        return isset($this->store_settings[$key]) ? $this->store_settings[$key] : null;
    }

    private function is_serialized( $data ) {
        // if it isn't a string, it isn't serialized
        if ( !is_string( $data ) )
            return false;
        $data = trim( $data );
        if ( 'N;' == $data )
            return true;
        if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
            return false;
        switch ( $badions[1] ) {
            case 'a' :
            case 'O' :
            case 's' :
                if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
                    return true;
                break;
            case 'b' :
            case 'i' :
            case 'd' :
                if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
                    return true;
                break;
        }
        return false;
    }
    
    public function zone() {
        $output = '<option value="">' . $this->language->get('text_select') . '</option>';

        $this->load->model('localisation/zone');

        $results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
        
        foreach ($results as $result) {
            $output .= '<option value="' . $result['zone_id'] . '">' . $result['name'] . '</option>';
        } 

        if (!$results) {
            $output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
        }

        $this->response->setOutput($output);
    }  

    private function validate() {
        if (!$this->user->hasPermission('modify', 'module/simple')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (empty($this->request->post['simple_common_template'])) {
            $this->error['warning'] = $this->language->get('error_exists');
            $this->error['error_simple_common_template'] = $this->data['error_simple_common_template'] = $this->language->get('error_simple_common_template');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }  
    }

    public function custom() {

        $this->load->model('tool/simplecustom');

        $this->data['action'] = $this->url->link('module/simple/custom', 'token=' . $this->session->data['token'] . '&set=' . $this->request->get['set'] . '&type=' . $this->request->get['type'] . '&id=' . $this->request->get['id'], 'SSL');

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->model_tool_simplecustom->updateData($this->request->get['type'], $this->request->get['id'], $this->request->get['set'], $this->request->post);
        }
        
        $this->data['button_save'] = $this->language->get('button_save');

        $this->data['custom'] = $this->model_tool_simplecustom->loadData($this->request->get['type'], $this->request->get['id'], $this->request->get['set']);
        $this->data['form_id'] = $this->request->get['type'].'_'.$this->request->get['set'].'_'.$this->request->get['id'];

        $this->template = 'module/simple_custom.tpl';
        $this->response->setOutput($this->render());
    }

    public function backup() {
        $this->load->model('setting/setting');
        $this->load->model('setting/store');

        if (isset($this->request->get['store_id'])) {
            $this->data['store_id'] = $this->request->get['store_id'];
        } else {
            $this->data['store_id'] = 0;
        }

        if (empty($this->settings)) {
            $this->settings = $this->model_setting_setting->getSetting('simple',$this->data['store_id']);
            if (empty($this->settings)) {
                $this->settings = unserialize(base64_decode($this->default_settings));
            }
        }

        $this->response->addheader('Pragma: public');
        $this->response->addheader('Expires: 0');
        $this->response->addheader('Content-Description: File Transfer');
        $this->response->addheader('Content-Type: application/octet-stream');
        $this->response->addheader('Content-Disposition: attachment; filename=' . 'simple_settings_for_store_' . $this->data['store_id'] . '.txt');
        $this->response->addheader('Content-Transfer-Encoding: binary');
        
        $this->response->setOutput(base64_encode(serialize($this->settings)));
    }

    public function header() {
        $this->load->model('setting/setting');
        $this->load->model('setting/store');

        if (isset($this->request->get['store_id'])) {
            $this->data['store_id'] = $this->request->get['store_id'];
        } else {
            $this->data['store_id'] = 0;
        }

        $header_content = '';
        if (file_exists(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl')) {
            $tpl = file_get_contents(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl');
            $f_b = utf8_strpos($tpl, '<form');
            $header_content = utf8_substr($tpl, 0, $f_b);
        }

        $this->response->addheader('Pragma: public');
        $this->response->addheader('Expires: 0');
        $this->response->addheader('Content-Description: File Transfer');
        $this->response->addheader('Content-Type: application/octet-stream');
        $this->response->addheader('Content-Disposition: attachment; filename=' . 'simple_header.tpl');
        $this->response->addheader('Content-Transfer-Encoding: binary');
        
        $this->response->setOutput($header_content);
    }

    public function footer() {
        $this->load->model('setting/setting');
        $this->load->model('setting/store');

        if (isset($this->request->get['store_id'])) {
            $this->data['store_id'] = $this->request->get['store_id'];
        } else {
            $this->data['store_id'] = 0;
        }

        $footer_content = '';
        if (file_exists(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl')) {
            $tpl = file_get_contents(DIR_CATALOG . 'view/theme/' . $this->get_setting('config_template') . '/template/account/forgotten.tpl');
            $f_e = utf8_strpos($tpl, '</form>');
            $footer_content = utf8_substr($tpl, $f_e + 7);
        }
        
        $this->response->addheader('Pragma: public');
        $this->response->addheader('Expires: 0');
        $this->response->addheader('Content-Description: File Transfer');
        $this->response->addheader('Content-Type: application/octet-stream');
        $this->response->addheader('Content-Disposition: attachment; filename=' . 'simple_footer.tpl');
        $this->response->addheader('Content-Transfer-Encoding: binary');
        
        $this->response->setOutput($footer_content);
    }
}
?>