/**
 * Function to install all location manager related data and options.
 *
 * @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_activation_script()
{
    global $wpdb, $plugin_prefix;
    /**
     * Include any functions needed for upgrades.
     *
     * @since 1.3.6
     */
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    $is_set_default_location = geodir_get_default_location();
    $wpdb->hide_errors();
    // rename tables if we need to
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_locations'") > 0) {
        $wpdb->query("RENAME TABLE geodir_post_locations TO " . $wpdb->prefix . "geodir_post_locations");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_neighbourhood'") > 0) {
        $wpdb->query("RENAME TABLE geodir_post_neighbourhood TO " . $wpdb->prefix . "geodir_post_neighbourhood");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_countries'") > 0) {
        $wpdb->query("RENAME TABLE geodir_countries TO " . $wpdb->prefix . "geodir_countries");
    }
    if ($wpdb->query("SHOW TABLES LIKE 'geodir_location_seo'") > 0) {
        $wpdb->query("RENAME TABLE geodir_location_seo TO " . $wpdb->prefix . "geodir_location_seo");
    }
    $collate = '';
    if ($wpdb->has_cap('collation')) {
        if (!empty($wpdb->charset)) {
            $collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
        }
        if (!empty($wpdb->collate)) {
            $collate .= " COLLATE {$wpdb->collate}";
        }
    }
    if ($wpdb->get_var("SHOW TABLES LIKE '" . POST_LOCATION_TABLE . "'") != POST_LOCATION_TABLE) {
        $location_table = "CREATE TABLE IF NOT EXISTS " . POST_LOCATION_TABLE . " (\n\t\t\t\t\t`location_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`country` varchar(254) NOT NULL,\n\t\t\t\t\t`region` varchar(254) NOT NULL,\n\t\t\t\t\t`city` varchar(254) NOT NULL, \n\t\t\t\t\t`country_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`region_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`city_slug` varchar(254) NOT NULL,\n\t\t\t\t\t`city_latitude` varchar(254) NOT NULL,\n\t\t\t\t\t`city_longitude` varchar(254) NOT NULL,\n\t\t\t\t\t`is_default` ENUM( '0', '1' ) NOT NULL DEFAULT '0',\n\t\t\t\t\t`city_meta` VARCHAR( 254 ) NOT NULL,\n\t\t\t\t\t`city_desc` TEXT NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`location_id`)) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_locations'") > 0) {
            $wpdb->query("RENAME TABLE geodir_post_locations TO " . $wpdb->prefix . "geodir_post_locations");
        } else {
            dbDelta($location_table);
        }
    }
    $location_result = (array) geodir_get_default_location();
    // this function is there in core plugin location_functions.php file.
    $post_types = geodir_get_posttypes();
    // Fuction in core geodirectory plugin
    $location_info = geodir_add_new_location_via_adon($location_result);
    geodir_location_set_default($location_info->location_id);
    if (!empty($post_types)) {
        foreach ($post_types as $post_type) {
            $table = $plugin_prefix . $post_type . '_detail';
            $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET post_location_id=%d WHERE post_location_id=0", array($location_info->location_id)));
        }
    }
    if ($wpdb->get_var("SHOW TABLES LIKE '" . POST_NEIGHBOURHOOD_TABLE . "'") != POST_NEIGHBOURHOOD_TABLE) {
        $neighbourhood_table = "CREATE TABLE IF NOT EXISTS " . POST_NEIGHBOURHOOD_TABLE . " (\n\t\t\t\t\t`hood_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`hood_location_id` int(11) NOT NULL,\n\t\t\t\t\t`hood_name` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_latitude` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_longitude` varchar(254) NOT NULL,\n\t\t\t\t\t`hood_slug` varchar(254) NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`hood_id`)) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_post_neighbourhood'") > 0) {
            $wpdb->query("RENAME TABLE geodir_post_neighbourhood TO " . $wpdb->prefix . "geodir_post_neighbourhood");
        } else {
            dbDelta($neighbourhood_table);
        }
    }
    $address_extra_info = $wpdb->get_results("select id, extra_fields from " . GEODIR_CUSTOM_FIELDS_TABLE . " where field_type = 'address'");
    if (!empty($address_extra_info)) {
        foreach ($address_extra_info as $extra) {
            $fields = array();
            if ($extra->extra_fields != '') {
                $fields = unserialize($extra->extra_fields);
                if (!isset($fields['show_city'])) {
                    $fields['show_city'] = 1;
                }
                if (!isset($fields['city_lable'])) {
                    $fields['city_lable'] = __('City', GEODIRLOCATION_TEXTDOMAIN);
                }
                if (!isset($fields['show_region'])) {
                    $fields['show_region'] = 1;
                }
                if (!isset($fields['region_lable'])) {
                    $fields['region_lable'] = __('Region', GEODIRLOCATION_TEXTDOMAIN);
                }
                if (!isset($fields['show_country'])) {
                    $fields['show_country'] = 1;
                }
                if (!isset($fields['country_lable'])) {
                    $fields['country_lable'] = __('Country', GEODIRLOCATION_TEXTDOMAIN);
                }
                $wpdb->query($wpdb->prepare("UPDATE " . GEODIR_CUSTOM_FIELDS_TABLE . " SET extra_fields=%s WHERE id=%d", array(serialize($fields), $extra->id)));
            }
        }
    }
    //$post_types = geodir_get_posttypes();
    if (!empty($post_types)) {
        foreach ($post_types as $post_type) {
            $detail_table = $plugin_prefix . $post_type . '_detail';
            $meta_field_add = "VARCHAR( 30 ) NULL";
            geodir_add_column_if_not_exist($detail_table, "post_neighbourhood", $meta_field_add);
        }
    }
    // location seo table
    if ($wpdb->get_var("SHOW TABLES LIKE '" . LOCATION_SEO_TABLE . "'") != LOCATION_SEO_TABLE) {
        $location_table = "CREATE TABLE IF NOT EXISTS " . LOCATION_SEO_TABLE . " (\n\t\t\t\t\t`seo_id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `location_type` varchar(255) NOT NULL,\n\t\t\t\t\t  `country_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `region_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `city_slug` varchar(254) NOT NULL,\n\t\t\t\t\t  `seo_title` varchar(254) NOT NULL,\n\t\t\t\t\t  `seo_desc` text NOT NULL,\n\t\t\t\t\t  `date_created` datetime NOT NULL,\n\t\t\t\t\t  `date_updated` datetime NOT NULL,\n\t\t\t\t\t  PRIMARY KEY (`seo_id`)\n\t\t\t\t\t) {$collate} ";
        if ($wpdb->query("SHOW TABLES LIKE 'geodir_location_seo'") > 0) {
            $wpdb->query("RENAME TABLE geodir_location_seo TO " . $wpdb->prefix . "geodir_location_seo");
        } else {
            dbDelta($location_table);
        }
    }
    // location term count table
    if ($wpdb->get_var("SHOW TABLES LIKE '" . GEODIR_TERM_META . "'") != GEODIR_TERM_META) {
        $term_meta_table = "CREATE TABLE IF NOT EXISTS " . GEODIR_TERM_META . " (\n\t\t\t\t\t\tid int NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\tlocation_type varchar( 100 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tlocation_name varchar( 100 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tterm_count varchar( 5000 ) NULL DEFAULT NULL,\n\t\t\t\t\t\treview_count varchar( 5000 ) NULL DEFAULT NULL,\n\t\t\t\t\t\tPRIMARY KEY  (id)\n\t\t\t\t\t\t) {$collate} ";
        dbDelta($term_meta_table);
    }
}
/**
 * Handles 'add location' form data.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 */
function geodir_add_location()
{
    global $wpdb, $plugin_prefix;
    if (isset($_REQUEST['location_addedit_nonce']) && current_user_can('manage_options')) {
        if (!wp_verify_nonce($_REQUEST['location_addedit_nonce'], 'location_add_edit_nonce')) {
            return;
        }
        $gd_city = $_REQUEST['gd_city'];
        $gd_region = $_REQUEST['gd_region'];
        $gd_country = $_REQUEST['gd_country'];
        $gd_latitude = $_REQUEST['gd_latitude'];
        $gd_longitude = $_REQUEST['gd_longitude'];
        $city_meta = $_REQUEST['city_meta'];
        $city_desc = $_REQUEST['city_desc'];
        $id = $_REQUEST['update_city'];
        if ($id) {
            $duplicate = $wpdb->get_var($wpdb->prepare("select location_id from " . POST_LOCATION_TABLE . " WHERE city = %s AND region=%s AND country=%s AND location_id!=%d", array($gd_city, $gd_region, $gd_country, $id)));
        } else {
            $duplicate = $wpdb->get_var($wpdb->prepare("select location_id from " . POST_LOCATION_TABLE . " WHERE city = %s AND region=%s AND country=%s", array($gd_city, $gd_region, $gd_country)));
        }
        if ($duplicate != '') {
            $setid = '';
            if ($id) {
                $setid = '&id=' . $id;
            }
            $msg = GD_LOCATION_EXITS;
            $msg = urlencode($msg);
            $location = admin_url() . "admin.php?page=geodirectory&tab=managelocation_fields&subtab=geodir_location_addedit&location_error=" . $msg . $setid;
            wp_redirect($location);
            exit;
        }
        if ($_POST['location_ajax_action'] == 'location') {
            $country_slug = create_location_slug($gd_country);
            $region_slug = create_location_slug($gd_region);
            $city_slug = create_location_slug($gd_city);
            if ($id) {
                $old_location = geodir_get_location_by_id('', $id);
                $sql = $wpdb->prepare("UPDATE " . POST_LOCATION_TABLE . " SET\n\t\t\t\t\tcountry=%s,\n\t\t\t\t\tregion=%s,\n\t\t\t\t\tcity=%s,\n\t\t\t\t\tcity_latitude=%s,\n\t\t\t\t\tcity_longitude=%s,\n\t\t\t\t\tcountry_slug = %s,\n\t\t\t\t\tregion_slug = %s,\n\t\t\t\t\tcity_slug = %s,\n\t\t\t\t\tcity_meta=%s,\n\t\t\t\t\tcity_desc=%s WHERE location_id = %d", array($gd_country, $gd_region, $gd_city, $gd_latitude, $gd_longitude, $country_slug, $region_slug, $city_slug, $city_meta, $city_desc, $id));
                $wpdb->query($sql);
                $geodir_location = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . POST_LOCATION_TABLE . " WHERE is_default='1' AND location_id = %d", array($id)), "OBJECT");
                if (!empty($geodir_location)) {
                    update_option('geodir_default_location', $geodir_location);
                }
                // UPDATE DEFAULT LOCATION OPTION
                $msg = GD_LOCATION_UPDATED;
                //UPDATE AND DELETE LISTING
                $posttype = geodir_get_posttypes();
                if (isset($_REQUEST['listing_action']) && $_REQUEST['listing_action'] == 'delete') {
                    foreach ($posttype as $posttypeobj) {
                        /* do not update latitude and longitude otrherwise all listings will be spotted on one point on map
                        						if ($old_location->city_latitude != $gd_latitude || $old_location->city_longitude != $gd_longitude) {
                        
                        							$del_post_sql = $wpdb->get_results(
                        								$wpdb->prepare(
                        									"SELECT post_id from ".$plugin_prefix.$posttypeobj."_detail WHERE post_location_id = %d AND (post_city != %s OR post_region != %s)",
                        									array($id,$gd_city,$gd_region)
                        								)
                        							);
                        							if (!empty($del_post_sql)) {
                        								foreach ($del_post_sql as $del_post_info) {
                        									$postid = (int)$del_post_info->post_id;
                        									//wp_delete_post($postid); // update post location instead of delete post
                        									$sql = $wpdb->prepare(
                        										"UPDATE ".$plugin_prefix.$posttypeobj."_detail SET post_latitude=%s, post_longitude=%s WHERE post_location_id=%d AND post_id=%d",
                        										array( $gd_latitude, $gd_longitude, $id, $postid )
                        									);
                        									$wpdb->query($sql);
                        								}
                        							}
                        						}
                        						*/
                        $post_locations = '[' . $city_slug . '],[' . $region_slug . '],[' . $country_slug . ']';
                        // set all overall post location
                        $sql = $wpdb->prepare("UPDATE " . $plugin_prefix . $posttypeobj . "_detail SET post_city=%s, post_region=%s, post_country=%s, post_locations=%s\n\t\t\t\t\t\t\t\tWHERE post_location_id=%d AND ( post_city!=%s OR post_region!=%s OR post_country!=%s)", array($gd_city, $gd_region, $gd_country, $post_locations, $id, $gd_city, $gd_region, $gd_country));
                        $wpdb->query($sql);
                    }
                }
            } else {
                $location_info = array();
                $location_info['city'] = $gd_city;
                $location_info['region'] = $gd_region;
                $location_info['country'] = $gd_country;
                $location_info['country_slug'] = $country_slug;
                $location_info['region_slug'] = $region_slug;
                $location_info['city_slug'] = $city_slug;
                $location_info['city_latitude'] = $gd_latitude;
                $location_info['city_longitude'] = $gd_longitude;
                $location_info['is_default'] = 0;
                $location_info['city_meta'] = $city_meta;
                $location_info['city_desc'] = $city_desc;
                geodir_add_new_location_via_adon($location_info);
                $msg = GD_LOCATION_SAVED;
            }
            $msg = urlencode($msg);
            $location = admin_url() . "admin.php?page=geodirectory&tab=managelocation_fields&subtab=geodir_location_manager&location_success=" . $msg;
            wp_redirect($location);
            exit;
        }
    } else {
        wp_redirect(home_url() . '/?geodir_signup=true');
        exit;
    }
}