<?php

class ThemeMakersThemeModel {

//for ajax
    public static function change_options() {
        //$_REQUEST = array_map('stripslashes', $_REQUEST);
        $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)) {


                    if (!empty($data['import_settings'])) {
                        //import theme settings
                        $import_settings = ThemeMakersHelper::decode_array($data['import_settings']);

                        if (!empty($import_settings)) {
                            foreach ($import_settings as $option => $value) {
                                if (substr_count($option, THEMEMAKERS_THEME_PREFIX) > 0) {
                                    update_option($option, $value);
                                }
                            }
                        }

                        _e('Settings have been imported.', THEMEMAKERS_THEME_FOLDER_NAME);
                        exit;
                    }


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

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

                        //*****

                        if ($option == "sidebars") {
                            include_once THEMEMAKERS_THEME_PATH . "/admin/entities/entity_custom_sidebars.php";
                            $sidebar = new Thememakers_Entity_Custom_Sidebars();
                            unset($newvalue[0]);
                            $sidebar->save_sidebars($newvalue);
                            continue;
                        }
                        if ($option == "seo_group") {
                            include_once THEMEMAKERS_THEME_PATH . "/admin/entities/entity_seo_group.php";
                            $seo_group = new Thememakers_Entity_SEO_Group();
                            $seo_group->save_groups($newvalue);
                            continue;
                        }
                        if ($option == "contact_form") {
                            include_once THEMEMAKERS_THEME_PATH . "/admin/entities/entity_contact_form.php";
                            //exclude "-" symb
                            if (!empty($newvalue)) {
                                foreach ($newvalue as $key => $form) {
                                    if (!isset($newvalue[$key]['title'])) {
                                        unset($newvalue[$key]);
                                    }

                                    if (empty($newvalue[$key]['title'])) {
                                        unset($newvalue[$key]);
                                    }

                                    /*
                                      $newvalue[$key]['title'] = str_replace(" ", "_", $form['title']);
                                      if (!empty($form['inputs'])) {
                                      foreach ($form['inputs'] as $key2 => $input) {
                                      $newvalue[$key]['inputs'][$key2]['label'] = str_replace("-", "_", $input['label']);
                                      }
                                      }
                                     */
                                }
                            }

                            Thememakers_Entity_Contact_Form::save($newvalue);
                            continue;
                        }

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

                        update_option(THEMEMAKERS_THEME_PREFIX . $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;
                        }

                        update_option(THEMEMAKERS_THEME_PREFIX . $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;
    }

}

