<?php

class ThemeMakersThemeModel {

	
    public static $options, $app_options;

    public static function register() {
        self::$options = get_option(THEMEMAKERS_THEME_PREFIX . 'theme_options');
        self::$app_options = get_option(THEMEMAKERS_THEME_PREFIX . 'theme_app_options');
    }

	
    public static function get_option($option, $prefix = THEMEMAKERS_THEME_PREFIX) {
			
		return get_option(THEMEMAKERS_THEME_PREFIX . $option);
    }

    public static function update_option($option, $data, $prefix = THEMEMAKERS_THEME_PREFIX) {
			
		return update_option(THEMEMAKERS_THEME_PREFIX . $option, $data);
	}
	  

    //ajax
    public static function change_options() {

        $action_type = $_REQUEST['type'];
        $data = array();
        parse_str($_REQUEST['values'], $data);
        $data = ThememakersHelper::db_quotes_shield($data);

        switch ($action_type) {
            case 'save':
                if (!empty($data)) {

                    foreach ($data as $option => $newvalue) {

                        if ($option == "sliders") {
                            unset($newvalue["__GROUP_INDEX__"]);
                            unset($newvalue["__SLIDE_INDEX__"]);
                            self::update_option($option, $newvalue);
                            continue;
                        }

                        //*****

                        if ($option == "sidebars") {
                            $sidebar = new Thememakers_Entity_Custom_Sidebars();
                            unset($newvalue[0]);
                            $sidebar->save_sidebars($newvalue);
                            continue;
                        }
                        if ($option == "seo_group") {
                            $seo_group = new Thememakers_Entity_SEO_Group();
                            $seo_group->save_groups($newvalue);
                            continue;
                        }
                        if ($option == "contact_form") {
                            if (!empty($newvalue)) {
                                foreach ($newvalue as $key => $form) {
                                    if (!isset($newvalue[$key]['title'])) {
                                        unset($newvalue[$key]);
                                    }

                                    if (empty($newvalue[$key]['title'])) {
                                        unset($newvalue[$key]);
                                    }
                                }
                            }
                            Thememakers_Entity_Contact_Form::save($newvalue);
                            continue;
                        }

                        if ($option == "slides") {
                            unset($newvalue["__INDEX__"]);
                            unset($newvalue["__GROUP_INDEX__"]);
                            unset($newvalue["__SLIDE_INDEX__"]);
                            self::update_option($option, $newvalue);
                            continue;
                        }

                        if (is_array($newvalue)) {
                            self::update_option($option, $newvalue);
                        } else {
                            $newvalue = stripcslashes($newvalue);
                            $newvalue = str_replace('\"', '"', $newvalue);
                            $newvalue = str_replace("\'", "'", $newvalue);
                            self::update_option($option, $newvalue);
                        }
                    }
                }
                _e('Options have been updated.', THEMEMAKERS_THEME_FOLDER_NAME);
                break;


            case 'reset':
                if (!empty($data)) {
                    foreach ($data as $option => $newvalue) {
                        if ($option == "sidebars") {
                            continue;
                        }
                        if ($option == "contact_form") {
                            continue;
                        }

                        if ($option == "slides") {
                            continue;
                        }

                        self::update_option($option, $newvalue);
                    }
                }
                _e('Options have been reset.', THEMEMAKERS_THEME_FOLDER_NAME);
                break;


            default:
                break;
        }


        //**** CSS REGENERATION
        $custom_css1 = ThemeMakersThemeView::draw_free_page(THEMEMAKERS_THEME_PATH . '/admin/views/custom_css1.php');
        $custom_css2 = ThemeMakersThemeView::draw_free_page(THEMEMAKERS_THEME_PATH . '/admin/views/custom_css2.php');
        $handle = fopen(THEMEMAKERS_THEME_PATH . '/css/custom1.css', 'w');
        fwrite($handle, $custom_css1);
        fclose($handle);
        $handle = fopen(THEMEMAKERS_THEME_PATH . '/css/custom2.css', 'w');
        fwrite($handle, $custom_css2);
        fclose($handle);
        exit;
		
		
		
		
		
		
		
    }

}

