/**
 * @param $tab_name
 */
function geodir_location_default_option_form($tab_name)
{
    switch ($tab_name) {
        case 'geodir_location_setting':
            geodir_admin_fields(geodir_location_default_options());
            ?>
			
			<p class="submit">
				
			<input name="save" class="button-primary" type="submit" value="<?php 
            _e('Save changes', GEODIRLOCATION_TEXTDOMAIN);
            ?>
" />
			<input type="hidden" name="location_ajax_action" value="settings">
			</p>
			</div>
			
			<?php 
            break;
    }
    // end of switch
}
/**
 * Function to delete all the location adons related data and option on plugin deletion.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 */
function geodir_location_uninstall()
{
    if (!isset($_REQUEST['verify-delete-adon'])) {
        $plugins = isset($_REQUEST['checked']) ? (array) $_REQUEST['checked'] : array();
        //$_POST = from the plugin form; $_GET = from the FTP details screen.
        wp_enqueue_script('jquery');
        require_once ABSPATH . 'wp-admin/admin-header.php';
        printf('<h2>%s</h2>', __('Warning!!', GEODIRLOCATION_TEXTDOMAIN));
        printf('%s<br/><strong>%s</strong><br /><br />%s <a href="http://wpgeodirectory.com">%s</a>.', __('You are about to delete a Geodirectory Adon which has important option and custom data associated to it.', GEODIRLOCATION_TEXTDOMAIN), __('Deleting this and activating another version, will be treated as a new installation of plugin, so all the data will be lost.', GEODIRLOCATION_TEXTDOMAIN), __('If you have any problem in upgrading the plugin please contact Geodirectroy', GEODIRLOCATION_TEXTDOMAIN), __('support', GEODIRLOCATION_TEXTDOMAIN));
        ?>
<br /><br />
		<form method="post" action="<?php 
        echo esc_url($_SERVER['REQUEST_URI']);
        ?>
" style="display:inline;">
						<input type="hidden" name="verify-delete" value="1" />
						<input type="hidden" name="action" value="delete-selected" />
						<input type="hidden" name="verify-delete-adon" value="1" />
						<?php 
        foreach ((array) $plugins as $plugin) {
            echo '<input type="hidden" name="checked[]" value="' . esc_attr($plugin) . '" />';
        }
        ?>
						<?php 
        wp_nonce_field('bulk-plugins');
        ?>
						<?php 
        submit_button(__('Delete plugin files only', GEODIRLOCATION_TEXTDOMAIN), 'button', 'submit', false);
        ?>
					</form>
					<form method="post" action="<?php 
        echo esc_url($_SERVER['REQUEST_URI']);
        ?>
" style="display:inline;">
						<input type="hidden" name="verify-delete" value="1" />
						<input type="hidden" name="action" value="delete-selected" />
                        <input type="hidden" name="verify-delete-adon" value="1" />
						<input type="hidden" name="verify-delete-adon-data" value="1" />
						<?php 
        foreach ((array) $plugins as $plugin) {
            echo '<input type="hidden" name="checked[]" value="' . esc_attr($plugin) . '" />';
        }
        ?>
						<?php 
        wp_nonce_field('bulk-plugins');
        ?>
						<?php 
        submit_button(__('Delete both plugin files and data', GEODIRLOCATION_TEXTDOMAIN), 'button', 'submit', false);
        ?>
					</form>
					
	<?php 
        require_once ABSPATH . 'wp-admin/admin-footer.php';
        exit;
    }
    if (isset($_REQUEST['verify-delete-adon-data'])) {
        global $wpdb, $plugin_prefix;
        /*delete_option('geodir_show_changelocation_nave', '');
        		delete_option('location_multicity', '');
        		delete_option('location_everywhere', '');
        		delete_option('location_neighbourhoods', '');*/
        /* --- delete location options --- */
        $location_option = geodir_location_default_options();
        if (!empty($location_option)) {
            foreach ($location_option as $value) {
                if (isset($value['id']) && $value['id'] != '') {
                    delete_option($value['id'], '');
                }
            }
        }
        unset($_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
        $default_location = geodir_get_default_location();
        $post_types = geodir_get_posttypes();
        if (!empty($post_types)) {
            foreach ($post_types as $post_type) {
                $table = $plugin_prefix . $post_type . '_detail';
                $del_post_sql = $wpdb->get_results($wpdb->prepare("SELECT post_id from " . $table . " where post_location_id != %d", array($default_location->location_id)));
                if (!empty($del_post_sql)) {
                    foreach ($del_post_sql as $del_post_info) {
                        $postid = $del_post_info->post_id;
                        wp_delete_post($postid);
                    }
                }
                $wpdb->query("UPDATE " . $table . " SET post_location_id='0'");
                if ($wpdb->get_var("SHOW COLUMNS FROM " . $table . " WHERE field = 'post_neighbourhood'")) {
                    $wpdb->query("ALTER TABLE " . $table . " DROP post_neighbourhood");
                }
            }
        }
        $wpdb->query("DROP TABLE " . $plugin_prefix . "post_locations");
        $wpdb->query("DROP TABLE " . $plugin_prefix . "post_neighbourhood");
        $wpdb->query("DROP TABLE IF EXISTS " . $plugin_prefix . "location_seo");
        $default_location->location_id = 0;
        update_option('geodir_default_location', $default_location);
    }
}
/**
 * Handles location form data.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 */
function geodir_location_form_submit_handler()
{
    if (isset($_REQUEST['geodir_location_merge']) && $_REQUEST['geodir_location_merge'] == 'merge') {
        include_once 'geodir_merge_field.php';
        exit;
    }
    if (isset($_REQUEST['location_ajax_action'])) {
        switch ($_REQUEST['location_ajax_action']) {
            case 'settings':
                geodir_update_options(geodir_location_default_options());
                $msg = GD_LOCATION_SETTINGS_SAVED;
                $msg = urlencode($msg);
                $location = admin_url() . "admin.php?page=geodirectory&tab=managelocation_fields&subtab=geodir_location_setting&location_success=" . $msg;
                wp_redirect($location);
                exit;
                break;
            case 'location':
                geodir_add_location();
                break;
            case 'add_hood':
                geodir_add_neighbourhood();
                break;
            case 'set_default':
                geodir_set_default();
                break;
            case 'merge':
                geodir_merge_location();
                break;
            case 'delete':
                geodir_delete_location();
                break;
            case 'delete_hood':
                geodir_delete_hood();
                break;
            case 'merge_cities':
                include_once 'geodir_merge_field.php';
                exit;
                break;
            case 'set_region_on_map':
                geodir_get_region_on_map();
                break;
            case 'geodir_set_location_seo':
                geodir_get_location_seo_settings();
                break;
            case 'geodir_save_cat_location':
                geodir_save_cat_location();
                break;
            case 'geodir_change_cat_location':
                geodir_change_cat_location();
                break;
        }
    }
}