/**
 *	Save miltisite settings from the form
 *	@param string $settings Multisite settings in present time
 *	@return string New multisite settings
 */
function jcf_save_multisite_settings($new_value)
{
    $current_value = jcf_get_multisite_settings();
    $new_value = trim($new_value);
    if ($current_value) {
        $saved = update_site_option('jcf_multisite_setting', $new_value);
    } else {
        $saved = add_site_option('jcf_multisite_setting', $new_value);
    }
    if ($saved) {
        jcf_add_admin_notice('notice', __('<strong>MultiSite settings</strong> has been updated.', JCF_TEXTDOMAIN));
    }
    return $new_value;
}
/**
 * import fields callback
 */
function jcf_ajax_import_fields()
{
    if (!empty($_POST['action']) && $_POST['action'] == 'jcf_import_fields') {
        if (!empty($_FILES['import_data']['name'])) {
            $path_info = pathinfo($_FILES['import_data']['name']);
            if ($path_info['extension'] == 'json') {
                $uploaddir = get_home_path() . "wp-content/uploads/";
                $uploadfile = $uploaddir . basename($_FILES['import_data']['name']);
                if (is_readable($_FILES['import_data']['tmp_name'])) {
                    $post_types = jcf_get_settings_from_file($_FILES['import_data']['tmp_name']);
                    unlink($_FILES['import_data']['tmp_name']);
                    if (empty($post_types)) {
                        $notice = array('error', __('<strong>Import FAILED!</strong> File do not contain fields settings data..', JCF_TEXTDOMAIN));
                    }
                } else {
                    $notice = array('error', __('<strong>Import FAILED!</strong> Can\'t read uploaded file.', JCF_TEXTDOMAIN));
                }
            } else {
                $notice = array('error', __('<strong>Import FAILED!</strong> Please upload correct file format.', JCF_TEXTDOMAIN));
            }
        } else {
            $notice = array('error', __('<strong>Import FAILED!</strong> Import file is missing.', JCF_TEXTDOMAIN));
        }
    }
    if (!empty($notice)) {
        jcf_add_admin_notice($notice[0], $notice[1]);
    }
    header('Content-Type: text/html; charset=utf-8');
    include JCF_ROOT . '/templates/import.tpl.php';
    exit;
}
/**
 *	Function for update saving method
 *	@return string Return read method from file or database
 */
function jcf_update_read_settings()
{
    $current_value = jcf_get_read_settings();
    $new_value = $_POST['jcf_read_settings'];
    if (MULTISITE && ($_POST['jcf_multisite_setting'] != JCF_CONF_MS_NETWORK && $new_value == JCF_CONF_SOURCE_FS_GLOBAL)) {
        jcf_add_admin_notice('error', __('<strong>Settings storage update FAILED!</strong>. Your MultiSite Settings do not allow to set global storage in FileSystem', JCF_TEXTDOMAIN));
        return $current_value;
    } else {
        if (!empty($current_value)) {
            // if need to copy settings from db to FS
            if (!empty($_POST['jcf_keep_settings'])) {
                if (in_array($new_value, array(JCF_CONF_SOURCE_FS_GLOBAL, JCF_CONF_SOURCE_FS_THEME))) {
                    $file = jcf_get_settings_file_path($new_value);
                    if (jcf_clone_db_settings_to_fs($file, $new_value)) {
                        jcf_add_admin_notice('notice', __('<strong>Database settings has been imported</strong> to file system.', JCF_TEXTDOMAIN));
                        $saved = update_site_option('jcf_read_settings', $new_value);
                    } else {
                        jcf_add_admin_notice('error', __('<strong>Database settings import to file system FAILED!</strong>. Revert settings storage to Database.', JCF_TEXTDOMAIN));
                    }
                }
            } else {
                $saved = update_site_option('jcf_read_settings', $new_value);
            }
        } else {
            $saved = add_site_option('jcf_read_settings', $new_value);
        }
        if ($saved) {
            jcf_add_admin_notice('notice', __('<strong>Settings storage</strong> configurations has been updated.', JCF_TEXTDOMAIN));
        }
        return $saved ? $new_value : $current_value;
    }
}
/**
 *	Admin main page
 */
function jcf_admin_settings_page()
{
    $post_types = jcf_get_post_types('object');
    $jcf_read_settings = jcf_get_read_settings();
    $jcf_multisite_settings = jcf_get_multisite_settings();
    $jcf_tabs = !isset($_GET['tab']) ? 'fields' : $_GET['tab'];
    // edit page
    if (!empty($_GET['pt']) && isset($post_types[$_GET['pt']])) {
        jcf_admin_fields_page($post_types[$_GET['pt']]);
        return;
    }
    if (!empty($_POST['save_import'])) {
        $saved = jcf_admin_save_settings($_POST['import_data']);
        $notice = $saved ? array('notice', __('<strong>Import</strong> has been completed successfully!', JCF_TEXTDOMAIN)) : array('error', __('<strong>Import failed!</strong> Please check that your import file has right format.', JCF_TEXTDOMAIN));
        jcf_add_admin_notice($notice[0], $notice[1]);
    }
    if (!empty($_POST['jcf_update_settings'])) {
        if (MULTISITE) {
            $jcf_multisite_settings = jcf_save_multisite_settings($_POST['jcf_multisite_setting']);
        }
        $jcf_read_settings = jcf_update_read_settings();
    }
    // load template
    include JCF_ROOT . '/templates/settings_page.tpl.php';
}