/**
 * Get location array using arguments.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 *
 * @param null|array $args {
 *    Attributes of args.
 *
 *    @type string $what What do you want to query. Possible values: city, region, country. Default: 'city'.
 *    @type string $city_val City value.
 *    @type string $region_val Region value.
 *    @type string $country_val Country value.
 *    @type string $country_non_restricted Non restricted countries.
 *    @type string $region_non_restricted Non restricted regions.
 *    @type string $city_non_restricted Non restricted cities.
 *    @type bool $filter_by_non_restricted Filter by non restricted?.
 *    @type string $compare_operator Comparison operator.
 *    @type string $country_column_name Country column name.
 *    @type string $region_column_name Region column name.
 *    @type string $city_column_name City column name.
 *    @type bool $location_link_part Location link part.
 *    @type string $order_by Order by value.
 *    @type string $no_of_records No of records to return.
 *    @type string $spage Current page number.
 *    @type array $format {
 *        Attributes of format.
 *
 *        @type string $type Type. Default: 'list'.
 *        @type string $container_wrapper Container wrapper. Default: 'ul'.
 *        @type string $container_wrapper_attr Container wrapper attr.
 *        @type string $item_wrapper Item wrapper. Default: 'li'.
 *        @type string $item_wrapper_attr Item wrapper attr.
 *
 *    }
 *
 * }
 * @param bool $switcher Todo: describe this part.
 * @return array|mixed|string
 */
function geodir_get_location_array($args = null, $switcher = false)
{
    global $wpdb;
    $defaults = array('what' => 'city', 'city_val' => '', 'region_val' => '', 'country_val' => '', 'country_non_restricted' => '', 'region_non_restricted' => '', 'city_non_restricted' => '', 'filter_by_non_restricted' => true, 'compare_operator' => 'like', 'country_column_name' => 'country', 'region_column_name' => 'region', 'city_column_name' => 'city', 'location_link_part' => true, 'order_by' => 'asc', 'no_of_records' => '', 'spage' => '', 'format' => array('type' => 'list', 'container_wrapper' => 'ul', 'container_wrapper_attr' => '', 'item_wrapper' => 'li', 'item_wrapper_attr' => ''));
    $location_args = wp_parse_args($args, $defaults);
    $search_query = '';
    $location_link_column = '';
    $location_default = geodir_get_default_location();
    if ($location_args['filter_by_non_restricted']) {
        // Non restricted countries
        if ($location_args['country_non_restricted'] == '') {
            if (get_option('geodir_enable_country') == 'default') {
                $country_non_retsricted = isset($location_default->country) ? $location_default->country : '';
                $location_args['country_non_restricted'] = $country_non_retsricted;
            } else {
                if (get_option('geodir_enable_country') == 'selected') {
                    $country_non_retsricted = get_option('geodir_selected_countries');
                    if (!empty($country_non_retsricted) && is_array($country_non_retsricted)) {
                        $country_non_retsricted = implode(',', $country_non_retsricted);
                    }
                    $location_args['country_non_restricted'] = $country_non_retsricted;
                }
            }
            $location_args['country_non_restricted'] = geodir_parse_location_list($location_args['country_non_restricted']);
        }
        //Non restricted Regions
        if ($location_args['region_non_restricted'] == '') {
            if (get_option('geodir_enable_region') == 'default') {
                $regoin_non_restricted = isset($location_default->region) ? $location_default->region : '';
                $location_args['region_non_restricted'] = $regoin_non_restricted;
            } else {
                if (get_option('geodir_enable_region') == 'selected') {
                    $regoin_non_restricted = get_option('geodir_selected_regions');
                    if (!empty($regoin_non_restricted) && is_array($regoin_non_restricted)) {
                        $regoin_non_restricted = implode(',', $regoin_non_restricted);
                    }
                    $location_args['region_non_restricted'] = $regoin_non_restricted;
                }
            }
            $location_args['region_non_restricted'] = geodir_parse_location_list($location_args['region_non_restricted']);
        }
        //Non restricted cities
        if ($location_args['city_non_restricted'] == '') {
            if (get_option('geodir_enable_city') == 'default') {
                $city_non_retsricted = isset($location_default->city) ? $location_default->city : '';
                $location_args['city_non_restricted'] = $city_non_retsricted;
            } else {
                if (get_option('geodir_enable_city') == 'selected') {
                    $city_non_restricted = get_option('geodir_selected_cities');
                    if (!empty($city_non_restricted) && is_array($city_non_restricted)) {
                        $city_non_restricted = implode(',', $city_non_restricted);
                    }
                    $location_args['city_non_restricted'] = $city_non_restricted;
                }
            }
            $location_args['city_non_restricted'] = geodir_parse_location_list($location_args['city_non_restricted']);
        }
    }
    if ($location_args['what'] == '') {
        $location_args['what'] = 'city';
    }
    if ($location_args['location_link_part']) {
        switch ($location_args['what']) {
            case 'country':
                if (get_option('permalink_structure') != '') {
                    $location_link_column = ", CONCAT_WS('/', country_slug) AS location_link ";
                } else {
                    $location_link_column = ", CONCAT_WS('&gd_country=', '', country_slug) AS location_link ";
                }
                break;
            case 'region':
                if (get_option('permalink_structure') != '') {
                    $location_link_column = ", CONCAT_WS('/', country_slug, region_slug) AS location_link ";
                } else {
                    $location_link_column = ", CONCAT_WS('&', CONCAT('&gd_country=', country_slug), CONCAT('gd_region=', region_slug) ) AS location_link ";
                }
                break;
            case 'city':
                if (get_option('permalink_structure') != '') {
                    $location_link_column = ", CONCAT_WS('/', country_slug, region_slug, city_slug) AS location_link ";
                } else {
                    $location_link_column = ", CONCAT_WS('&', CONCAT('&gd_country=', country_slug), CONCAT('gd_region=', region_slug) ,CONCAT('gd_city=' , city_slug)) AS location_link ";
                }
                /*else
                				{
                					if ( get_option('permalink_structure') != '' )
                						$location_link_column = " ,   city_slug as location_link ";
                					else
                						$location_link_column = " , CONCAT_WS('&gd_city=', '',city_slug) as location_link ";
                
                				}*/
                break;
                /*default:
                				if(get_option('geodir_show_location_url')=='all')
                				{
                					if ( get_option('permalink_structure') != '' )
                						$location_link_column = " , CONCAT_WS('/', country_slug, region_slug, city_slug) as location_link ";
                					else
                						$location_link_column = " , CONCAT_WS('&', CONCAT('&gd_country=' ,country_slug) ,CONCAT('gd_region=' , region_slug) ,CONCAT('gd_city=' , city_slug)) as location_link ";
                				}
                				else
                				{
                					if ( get_option('permalink_structure') != '' )
                						$location_link_column = " ,   city_slug as location_link ";
                					else
                						$location_link_column = " , CONCAT_WS('&gd_city=', '',city_slug) as location_link ";
                
                				}
                				break;*/
        }
    }
    switch ($location_args['compare_operator']) {
        case 'like':
            if (isset($location_args['country_val']) && $location_args['country_val'] != '') {
                //$search_query .= " AND lower(".$location_args['country_column_name'].") like  '". mb_strtolower( $location_args['country_val'] )."%' ";
                $countries_search_sql = geodir_countries_search_sql($location_args['country_val']);
                $countries_search_sql = $countries_search_sql != '' ? " OR FIND_IN_SET(country, '" . $countries_search_sql . "')" : '';
                $translated_country_val = sanitize_title(trim(wp_unslash($location_args['country_val'])));
                $search_query .= " AND ( lower(" . $location_args['country_column_name'] . ") like  '%" . mb_strtolower($location_args['country_val']) . "%' OR  lower(country_slug) LIKE '" . $translated_country_val . "%' " . $countries_search_sql . " ) ";
            }
            if (isset($location_args['region_val']) && $location_args['region_val'] != '') {
                $search_query .= " AND lower(" . $location_args['region_column_name'] . ") like  '%" . mb_strtolower($location_args['region_val']) . "%' ";
            }
            if (isset($location_args['city_val']) && $location_args['city_val'] != '') {
                $search_query .= " AND lower(" . $location_args['city_column_name'] . ") like  '%" . mb_strtolower($location_args['city_val']) . "%' ";
            }
            break;
        case 'in':
            if (isset($location_args['country_val']) && $location_args['country_val'] != '') {
                $location_args['country_val'] = geodir_parse_location_list($location_args['country_val']);
                $search_query .= " AND lower(" . $location_args['country_column_name'] . ") in({$location_args['country_val']}) ";
            }
            if (isset($location_args['region_val']) && $location_args['region_val'] != '') {
                $location_args['region_val'] = geodir_parse_location_list($location_args['region_val']);
                $search_query .= " AND lower(" . $location_args['region_column_name'] . ") in({$location_args['region_val']}) ";
            }
            if (isset($location_args['city_val']) && $location_args['city_val'] != '') {
                $location_args['city_val'] = geodir_parse_location_list($location_args['city_val']);
                $search_query .= " AND lower(" . $location_args['city_column_name'] . ") in({$location_args['city_val']}) ";
            }
            break;
        default:
            if (isset($location_args['country_val']) && $location_args['country_val'] != '') {
                //$search_query .= " AND lower(".$location_args['country_column_name'].") =  '". mb_strtolower($location_args['country_val'])."' ";
                $countries_search_sql = geodir_countries_search_sql($location_args['country_val']);
                $countries_search_sql = $countries_search_sql != '' ? " OR FIND_IN_SET(country, '" . $countries_search_sql . "')" : '';
                $translated_country_val = sanitize_title(trim(wp_unslash($location_args['country_val'])));
                $search_query .= " AND ( lower(" . $location_args['country_column_name'] . ") =  '" . mb_strtolower($location_args['country_val']) . "' OR  lower(country_slug) LIKE '" . $translated_country_val . "%' " . $countries_search_sql . " ) ";
            }
            if (isset($location_args['region_val']) && $location_args['region_val'] != '') {
                $search_query .= " AND lower(" . $location_args['region_column_name'] . ") =  '" . mb_strtolower($location_args['region_val']) . "' ";
            }
            if (isset($location_args['city_val']) && $location_args['city_val'] != '') {
                $search_query .= " AND lower(" . $location_args['city_column_name'] . ") =  '" . mb_strtolower($location_args['city_val']) . "' ";
            }
            break;
    }
    // end of switch
    if ($location_args['country_non_restricted'] != '') {
        $search_query .= " AND LOWER(country) IN ({$location_args['country_non_restricted']}) ";
    }
    if ($location_args['region_non_restricted'] != '') {
        if ($location_args['what'] == 'region' || $location_args['what'] == 'city') {
            $search_query .= " AND LOWER(region) IN ({$location_args['region_non_restricted']}) ";
        }
    }
    if ($location_args['city_non_restricted'] != '') {
        if ($location_args['what'] == 'city') {
            $search_query .= " AND LOWER(city) IN ({$location_args['city_non_restricted']}) ";
        }
    }
    //page
    if ($location_args['no_of_records']) {
        $spage = $location_args['no_of_records'] * $location_args['spage'];
    } else {
        $spage = "0";
    }
    // limit
    $limit = $location_args['no_of_records'] != '' ? ' LIMIT ' . $spage . ', ' . (int) $location_args['no_of_records'] . ' ' : '';
    // display all locations with same name also
    $search_field = $location_args['what'];
    if ($switcher) {
        $select = $search_field . $location_link_column;
        $group_by = $search_field;
        $order_by = $search_field;
        if ($search_field == 'city') {
            $select .= ', country, region, city, country_slug, region_slug, city_slug';
            $group_by = 'country, region, city';
            $order_by = 'city, region, country';
        } else {
            if ($search_field == 'region') {
                $select .= ', country, region, country_slug, region_slug';
                $group_by = 'country, region';
                $order_by = 'region, country';
            } else {
                if ($search_field == 'country') {
                    $select .= ', country, country_slug';
                    $group_by = 'country';
                    $order_by = 'country';
                }
            }
        }
        $main_location_query = "SELECT " . $select . " FROM " . POST_LOCATION_TABLE . " WHERE 1=1 " . $search_query . " GROUP BY " . $group_by . " ORDER BY " . $order_by . " " . $location_args['order_by'] . " " . $limit;
    } else {
        $main_location_query = "SELECT {$location_args['what']} {$location_link_column} FROM " . POST_LOCATION_TABLE . " WHERE 1=1 " . $search_query . " GROUP BY {$location_args['what']} ORDER BY {$location_args['what']} {$location_args['order_by']} {$limit}";
    }
    $locations = $wpdb->get_results($main_location_query);
    if ($switcher && !empty($locations)) {
        $new_locations = array();
        foreach ($locations as $location) {
            //print_r($location);
            //echo '###'.$search_field;
            $new_location = $location;
            $label = $location->{$search_field};
            if (($search_field == 'city' || $search_field == 'region') && (int) geodir_location_check_duplicate($search_field, $label) > 1) {
                if ($search_field == 'city') {
                    $label .= ', ' . $location->region;
                } else {
                    if ($search_field == 'region') {
                        $country_iso2 = geodir_location_get_iso2($location->country);
                        $country_iso2 = $country_iso2 != '' ? $country_iso2 : $location->country;
                        $label .= $country_iso2 != '' ? ', ' . $country_iso2 : '';
                    }
                }
            }
            $new_location->title = $location->{$search_field};
            $new_location->{$search_field} = $label;
            $new_location->label = $label;
            $new_locations[] = $new_location;
        }
        $locations = $new_locations;
    }
    $location_as_formated_list = "";
    if (!empty($location_args['format'])) {
        if ($location_args['format']['type'] == 'array') {
            return $locations;
        } elseif ($location_args['format']['type'] == 'jason') {
            return json_encode($locations);
        } else {
            $base_location_link = geodir_get_location_link('base');
            $container_wrapper = '';
            $container_wrapper_attr = '';
            $item_wrapper = '';
            $item_wrapper_attr = '';
            if (isset($location_args['format']['container_wrapper']) && !empty($location_args['format']['container_wrapper'])) {
                $container_wrapper = $location_args['format']['container_wrapper'];
            }
            if (isset($location_args['format']['container_wrapper_attr']) && !empty($location_args['format']['container_wrapper_attr'])) {
                $container_wrapper_attr = $location_args['format']['container_wrapper_attr'];
            }
            if (isset($location_args['format']['item_wrapper']) && !empty($location_args['format']['item_wrapper'])) {
                $item_wrapper = $location_args['format']['item_wrapper'];
            }
            if (isset($location_args['format']['item_wrapper_attr']) && !empty($location_args['format']['item_wrapper_attr'])) {
                $item_wrapper_attr = $location_args['format']['item_wrapper_attr'];
            }
            if (!empty($container_wrapper)) {
                $location_as_formated_list = "<" . $container_wrapper . " " . $container_wrapper_attr . " >";
            }
            if (!empty($locations)) {
                foreach ($locations as $location) {
                    if (!empty($item_wrapper)) {
                        $location_as_formated_list .= "<" . $item_wrapper . " " . $item_wrapper_attr . " >";
                    }
                    if (isset($location->location_link)) {
                        $location_as_formated_list .= "<a href='" . geodir_location_permalink_url($base_location_link . $location->location_link) . "' ><i class='fa fa-caret-right'></i> ";
                    }
                    $location_as_formated_list .= $location->{$location_args}['what'];
                    if (isset($location->location_link)) {
                        $location_as_formated_list .= "</a>";
                    }
                    if (!empty($item_wrapper)) {
                        $location_as_formated_list .= "</" . $item_wrapper . ">";
                    }
                }
            }
            return $location_as_formated_list;
        }
    }
    return $locations;
}
/**
 * Handles ajax request.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 */
function geodir_location_ajax_handler()
{
    if (isset($_REQUEST['gd_loc_ajax_action']) && $_REQUEST['gd_loc_ajax_action'] != '') {
        switch ($_REQUEST['gd_loc_ajax_action']) {
            case 'get_location':
                if (isset($_REQUEST['gd_which_location']) && $_REQUEST['gd_which_location'] != '') {
                    $city_val = '';
                    $region_val = '';
                    $country_val = '';
                    if (isset($_REQUEST['gd_city_val']) && $_REQUEST['gd_city_val'] != '') {
                        $city_val = $_REQUEST['gd_city_val'];
                    }
                    if (isset($_REQUEST['gd_region_val']) && $_REQUEST['gd_region_val'] != '') {
                        $region_val = $_REQUEST['gd_region_val'];
                    }
                    if (isset($_REQUEST['gd_country_val']) && $_REQUEST['gd_country_val'] != '') {
                        $country_val = $_REQUEST['gd_country_val'];
                    }
                    if (isset($_REQUEST['spage']) && $_REQUEST['spage'] != '') {
                        $spage = $_REQUEST['spage'];
                    } else {
                        $spage = '';
                    }
                    if (isset($_REQUEST['lscroll']) && $_REQUEST['lscroll'] != '') {
                        $no_of_records = '5';
                    } else {
                        $no_of_records = '';
                    }
                    $location_args = array('what' => $_REQUEST['gd_which_location'], 'city_val' => $city_val, 'region_val' => $region_val, 'country_val' => $country_val, 'compare_operator' => 'like', 'country_column_name' => 'country', 'region_column_name' => 'region', 'city_column_name' => 'city', 'location_link_part' => true, 'order_by' => ' asc ', 'no_of_records' => $no_of_records, 'format' => array('type' => 'array'), 'spage' => $spage);
                    $location_array = geodir_get_location_array($location_args);
                    if (isset($_REQUEST['gd_formated_for']) && $_REQUEST['gd_formated_for'] == 'location_switcher') {
                        $base_location_link = geodir_get_location_link('base');
                        if (!empty($location_array)) {
                            if ($_REQUEST['gd_which_location'] == 'city') {
                                $arrow_html = '';
                            } else {
                                $arrow_html = '<span class="geodir_loc_arrow"><a href="javascript:void(0);">&nbsp;</a></span>';
                            }
                            foreach ($location_array as $location_item) {
                                $location_name = $_REQUEST['gd_which_location'] == 'country' ? __($location_item->{$_REQUEST}['gd_which_location'], GEODIRECTORY_TEXTDOMAIN) : $location_item->{$_REQUEST}['gd_which_location'];
                                echo "<li class=\"geodir_loc_clearfix\"><a href='" . geodir_location_permalink_url($base_location_link . $location_item->location_link) . "' >" . $location_name . "</a>{$arrow_html}</li>";
                            }
                        } else {
                            echo _e("No Results", GEODIRLOCATION_TEXTDOMAIN);
                        }
                    } else {
                        print_r($location_array);
                    }
                    exit;
                }
                break;
            case 'fill_location':
                $gd_which_location = isset($_REQUEST['gd_which_location']) ? trim($_REQUEST['gd_which_location']) : '';
                $term = isset($_REQUEST['term']) ? trim($_REQUEST['term']) : '';
                $base_location = geodir_get_location_link('base');
                $current_location_array;
                $selected = '';
                $country_val = '';
                $region_val = '';
                $city_val = '';
                $country_val = geodir_get_current_location(array('what' => 'country', 'echo' => false));
                $region_val = geodir_get_current_location(array('what' => 'region', 'echo' => false));
                $city_val = geodir_get_current_location(array('what' => 'city', 'echo' => false));
                $item_set_selected = false;
                if (isset($_REQUEST['spage']) && $_REQUEST['spage'] != '') {
                    $spage = $_REQUEST['spage'];
                } else {
                    $spage = '';
                }
                if (isset($_REQUEST['lscroll']) && $_REQUEST['lscroll'] != '') {
                    $no_of_records = '5';
                } else {
                    $no_of_records = '';
                }
                $location_switcher_list_mode = get_option('geodir_location_switcher_list_mode');
                if (empty($location_switcher_list_mode)) {
                    $location_switcher_list_mode = 'drill';
                }
                if ($location_switcher_list_mode == 'drill') {
                    $args = array('what' => $gd_which_location, 'country_val' => strtolower($gd_which_location) == 'region' || strtolower($gd_which_location) == 'city' ? $country_val : '', 'region_val' => strtolower($gd_which_location) == 'city' ? $region_val : '', 'echo' => false, 'no_of_records' => $no_of_records, 'format' => array('type' => 'array'), 'spage' => $spage);
                } else {
                    $args = array('what' => $gd_which_location, 'echo' => false, 'no_of_records' => $no_of_records, 'format' => array('type' => 'array'), 'spage' => $spage);
                }
                if ($term != '') {
                    if ($gd_which_location == 'city') {
                        $args['city_val'] = $term;
                    }
                    if ($gd_which_location == 'region') {
                        $args['region_val'] = $term;
                    }
                    if ($gd_which_location == 'country') {
                        $args['country_val'] = $term;
                    }
                } else {
                    if ($gd_which_location == 'country' && $country_val != '') {
                        $args_current_location = array('what' => $gd_which_location, 'country_val' => $country_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
                        $current_location_array = geodir_get_location_array($args_current_location, true);
                    }
                    if ($gd_which_location == 'region' && $region_val != '') {
                        $args_current_location = array('what' => $gd_which_location, 'country_val' => $country_val, 'region_val' => $region_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
                        $current_location_array = geodir_get_location_array($args_current_location, true);
                    }
                    if ($gd_which_location == 'city' && $city_val != '') {
                        $args_current_location = array('what' => $gd_which_location, 'country_val' => $country_val, 'region_val' => $region_val, 'city_val' => $city_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
                        $current_location_array = geodir_get_location_array($args_current_location, true);
                    }
                    // if not searching then set to get exact matches
                    $args['compare_operator'] = 'in';
                }
                $location_array = geodir_get_location_array($args, true);
                // get country val in case of country search to get selected option
                if (get_option('geodir_everywhere_in_' . $gd_which_location . '_dropdown') && !isset($_REQUEST['lscroll'])) {
                    echo '<option value="' . $base_location . '">' . __('Everywhere', GEODIRLOCATION_TEXTDOMAIN) . '</option>';
                }
                $selected = '';
                $loc_echo = '';
                if (!empty($location_array)) {
                    foreach ($location_array as $locations) {
                        $selected = '';
                        $with_parent = isset($locations->label) ? true : false;
                        switch ($gd_which_location) {
                            case 'country':
                                if (strtolower($country_val) == strtolower($locations->country)) {
                                    $selected = 'selected="selected"';
                                }
                                $locations->country = __($locations->country, GEODIRECTORY_TEXTDOMAIN);
                                break;
                            case 'region':
                                $country_iso2 = geodir_location_get_iso2($country_val);
                                $country_iso2 = $country_iso2 != '' ? $country_iso2 : $country_val;
                                $with_parent = $with_parent && strtolower($region_val . ', ' . $country_iso2) == strtolower($locations->label) ? true : false;
                                if (strtolower($region_val) == strtolower($locations->region) || $with_parent) {
                                    $selected = 'selected="selected"';
                                }
                                break;
                            case 'city':
                                $with_parent = $with_parent && strtolower($city_val . ', ' . $region_val) == strtolower($locations->label) ? true : false;
                                if (strtolower($city_val) == strtolower($locations->city) || $with_parent) {
                                    $selected = 'selected="selected"';
                                }
                                break;
                        }
                        echo '<option value="' . geodir_location_permalink_url($base_location . $locations->location_link) . '" ' . $selected . '>' . ucwords($locations->{$gd_which_location}) . '</option>';
                        if (!$item_set_selected && $selected != '') {
                            $item_set_selected = true;
                        }
                    }
                }
                if (!empty($current_location_array) && !$item_set_selected && !isset($_REQUEST['lscroll'])) {
                    foreach ($current_location_array as $current_location) {
                        $selected = '';
                        $with_parent = isset($current_location->label) ? true : false;
                        switch ($gd_which_location) {
                            case 'country':
                                if (strtolower($country_val) == strtolower($current_location->country)) {
                                    $selected = 'selected="selected"';
                                }
                                $current_location->country = __($current_location->country, GEODIRECTORY_TEXTDOMAIN);
                                break;
                            case 'region':
                                $country_iso2 = geodir_location_get_iso2($country_val);
                                $country_iso2 = $country_iso2 != '' ? $country_iso2 : $country_val;
                                $with_parent = $with_parent && strtolower($region_val . ', ' . $country_iso2) == strtolower($current_location->label) ? true : false;
                                if (strtolower($region_val) == strtolower($current_location->region) || $with_parent) {
                                    $selected = 'selected="selected"';
                                }
                                break;
                            case 'city':
                                $with_parent = $with_parent && strtolower($city_val . ', ' . $region_val) == strtolower($current_location->label) ? true : false;
                                if (strtolower($city_val) == strtolower($current_location->city) || $with_parent) {
                                    $selected = 'selected="selected"';
                                }
                                break;
                        }
                        echo '<option value="' . geodir_location_permalink_url($base_location . $current_location->location_link) . '" ' . $selected . '>' . ucwords($current_location->{$gd_which_location}) . '</option>';
                    }
                }
                exit;
                break;
            case 'fill_location_on_add_listing':
                $selected = '';
                $country_val = isset($_REQUEST['country_val']) ? $_REQUEST['country_val'] : '';
                $region_val = isset($_REQUEST['region_val']) ? $_REQUEST['region_val'] : '';
                $city_val = isset($_REQUEST['city_val']) ? $_REQUEST['city_val'] : '';
                $compare_operator = isset($_REQUEST['compare_operator']) ? $_REQUEST['compare_operator'] : '=';
                if (isset($_REQUEST['term']) && $_REQUEST['term'] != '') {
                    if ($_REQUEST['gd_which_location'] == 'region') {
                        $region_val = $_REQUEST['term'];
                        $city_val = '';
                    } else {
                        if ($_REQUEST['gd_which_location'] == 'city') {
                            $city_val = $_REQUEST['term'];
                        }
                    }
                }
                if ($_REQUEST['gd_which_location'] != 'neighbourhood') {
                    $args = array('what' => $_REQUEST['gd_which_location'], 'country_val' => strtolower($_REQUEST['gd_which_location']) == 'region' || strtolower($_REQUEST['gd_which_location']) == 'city' ? $country_val : '', 'region_val' => $region_val, 'city_val' => $city_val, 'echo' => false, 'compare_operator' => $compare_operator, 'format' => array('type' => 'array'));
                    $location_array = geodir_get_location_array($args);
                } else {
                    geodir_get_neighbourhoods_dl($city_val);
                    exit;
                }
                // get country val in case of country search to get selected option
                if ($_REQUEST['gd_which_location'] == 'region') {
                    echo '<option  value="" >' . __('Select Region', GEODIRLOCATION_TEXTDOMAIN) . '</option>';
                } else {
                    echo '<option  value="" >' . __('Select City', GEODIRLOCATION_TEXTDOMAIN) . '</option>';
                }
                if (!empty($location_array)) {
                    foreach ($location_array as $locations) {
                        $selected = '';
                        switch ($_REQUEST['gd_which_location']) {
                            case 'country':
                                if (strtolower($country_val) == strtolower($locations->country)) {
                                    $selected = " selected='selected' ";
                                }
                                break;
                            case 'region':
                                if (strtolower($region_val) == strtolower($locations->region)) {
                                    $selected = " selected='selected' ";
                                }
                                break;
                            case 'city':
                                if (strtolower($city_val) == strtolower($locations->city)) {
                                    $selected = " selected='selected' ";
                                }
                                break;
                        }
                        echo '<option ' . $selected . ' value="' . ucwords($locations->{$_REQUEST}['gd_which_location']) . '" >' . ucwords($locations->{$_REQUEST}['gd_which_location']) . '</option>';
                    }
                } else {
                    if (isset($_REQUEST['term']) && $_REQUEST['term'] != '') {
                        echo '<option  value="' . $_REQUEST['term'] . '" >' . $_REQUEST['term'] . '</option>';
                    }
                }
                exit;
                break;
        }
    }
}
/**
 *
 * @global object $wpdb WordPress Database object.
 *
 * @param null $args
 * @return string
 */
function geodir_location_tab_switcher($args = null)
{
    $switcher = !empty($args) && isset($args['addSearchTermOnNorecord']) ? true : false;
    if (get_option('geodir_enable_country') != 'default' || get_option('geodir_enable_region') != 'default' || get_option('geodir_enable_city') != 'default') {
        $defaults = array('echo' => true, 'addSearchTermOnNorecord' => 0, 'autoredirect' => false);
        $args = wp_parse_args($args, $defaults);
        global $wpdb;
        // Options
        $echo = $args['echo'];
        $addSearchTermOnNorecord = $args['addSearchTermOnNorecord'];
        $autoredirect = $args['autoredirect'];
        $output = '';
        $selected = '';
        $location_list = '';
        $country_div = '';
        $region_div = '';
        $city_div = '';
        $onchange = '';
        $what_is_current_location = geodir_what_is_current_location();
        $what_is_current_location_div = $what_is_current_location . '_div';
        if ($what_is_current_location != '') {
            ${$what_is_current_location_div} = 'gd-tab-active';
        } else {
            $what_is_current_location = apply_filters('geodir_location_switcher_default_tab', 'city');
            $what_is_current_location_div = $what_is_current_location . '_div';
            ${$what_is_current_location_div} = 'gd-tab-active';
        }
        $location_value = '';
        if ($autoredirect === '0') {
        } else {
            $location_value = geodir_get_location_link('base');
            $onchange = ' onchange="window.location.href=this.value" ';
            $autoredirect = '1';
        }
        $base_location = geodir_get_location_link('base');
        $current_location_array = array();
        $selected = '';
        $country_val = '';
        $region_val = '';
        $city_val = '';
        $country_val = geodir_get_current_location(array('what' => 'country', 'echo' => false));
        $region_val = geodir_get_current_location(array('what' => 'region', 'echo' => false));
        $city_val = geodir_get_current_location(array('what' => 'city', 'echo' => false));
        $item_set_selected = false;
        $output .= '<div class="geodir_location_tab_container" >';
        $output .= '<dl class="geodir_location_tabs_head">';
        if (get_option('geodir_enable_country') != 'default') {
            $output .= '<dd data-location="country" class="geodir_location_tabs ' . $country_div . '" ><a href="javascript:void(0)">' . __('Country', GEODIRLOCATION_TEXTDOMAIN) . '</a></dd>';
        }
        if (get_option('geodir_enable_region') != 'default') {
            $output .= '<dd data-location="region" class="geodir_location_tabs ' . $region_div . '" ><a href="javascript:void(0)">' . __('Region', GEODIRLOCATION_TEXTDOMAIN) . '</a></dd>';
        }
        if (get_option('geodir_enable_city') != 'default') {
            $output .= '<dd data-location="city" class="geodir_location_tabs ' . $city_div . ' " ><a href="javascript:void(0)">' . __('City', GEODIRLOCATION_TEXTDOMAIN) . '</a></dd>';
        }
        $output .= '</dl>';
        $output .= '<input type="hidden" class="selected_location" value="city" /><div style="clear:both;"></div>';
        $output .= '<div class="geodir_location_sugestion">';
        $output .= '<select class="geodir_location_switcher_chosen" name="gd_location" data-placeholder="' . __('Please wait..&hellip;', GEODIRLOCATION_TEXTDOMAIN) . '" data-addSearchTermOnNorecord="' . $addSearchTermOnNorecord . '" data-autoredirect="' . $autoredirect . '" ' . $onchange . ' data-showeverywhere="1" >';
        $location_switcher_list_mode = get_option('geodir_location_switcher_list_mode');
        if (empty($location_switcher_list_mode)) {
            $location_switcher_list_mode = 'drill';
        }
        if ($location_switcher_list_mode == 'drill') {
            $args = array('what' => $what_is_current_location, 'country_val' => strtolower($what_is_current_location) == 'region' || strtolower($what_is_current_location) == 'city' ? $country_val : '', 'region_val' => strtolower($what_is_current_location) == 'city' ? $region_val : '', 'echo' => false, 'no_of_records' => '5', 'format' => array('type' => 'array'));
        } else {
            $args = array('what' => $what_is_current_location, 'echo' => false, 'no_of_records' => '5', 'format' => array('type' => 'array'));
        }
        if ($what_is_current_location == 'country' && $country_val != '') {
            $args_current_location = array('what' => $what_is_current_location, 'country_val' => $country_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
            $current_location_array = geodir_get_location_array($args_current_location, $switcher);
        }
        if ($what_is_current_location == 'region' && $region_val != '') {
            $args_current_location = array('what' => $what_is_current_location, 'country_val' => $country_val, 'region_val' => $region_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
            $current_location_array = geodir_get_location_array($args_current_location, $switcher);
        }
        if ($what_is_current_location == 'city' && $city_val != '') {
            $args_current_location = array('what' => $what_is_current_location, 'country_val' => $country_val, 'region_val' => $region_val, 'city_val' => $city_val, 'compare_operator' => '=', 'no_of_records' => '1', 'echo' => false, 'format' => array('type' => 'array'));
            $current_location_array = geodir_get_location_array($args_current_location, $switcher);
        }
        $location_array = geodir_get_location_array($args, $switcher);
        // get country val in case of country search to get selected option
        if (get_option('geodir_everywhere_in_' . $what_is_current_location . '_dropdown')) {
            $output .= '<option value="' . $base_location . '">' . __('Everywhere', GEODIRLOCATION_TEXTDOMAIN) . '</option>';
        }
        $selected = '';
        if (!empty($location_array)) {
            foreach ($location_array as $locations) {
                $selected = '';
                $with_parent = isset($locations->label) ? true : false;
                switch ($what_is_current_location) {
                    case 'country':
                        if (strtolower($country_val) == strtolower($locations->country)) {
                            $selected = 'selected="selected"';
                        }
                        $locations->country = __($locations->country, GEODIRECTORY_TEXTDOMAIN);
                        break;
                    case 'region':
                        $country_iso2 = geodir_location_get_iso2($country_val);
                        $country_iso2 = $country_iso2 != '' ? $country_iso2 : $country_val;
                        $with_parent = $with_parent && strtolower($region_val . ', ' . $country_iso2) == strtolower($locations->label) ? true : false;
                        if (strtolower($region_val) == strtolower($locations->region) || $with_parent) {
                            $selected = 'selected="selected"';
                        }
                        break;
                    case 'city':
                        $with_parent = $with_parent && strtolower($city_val . ', ' . $region_val) == strtolower($locations->label) ? true : false;
                        if (strtolower($city_val) == strtolower($locations->city) || $with_parent) {
                            $selected = 'selected="selected"';
                        }
                        break;
                }
                $output .= '<option value="' . geodir_location_permalink_url($base_location . $locations->location_link) . '" ' . $selected . '>' . ucwords($locations->{$what_is_current_location}) . '</option>';
                if (!$item_set_selected && $selected != '') {
                    $item_set_selected = true;
                }
            }
        }
        if (!empty($current_location_array) && !$item_set_selected) {
            foreach ($current_location_array as $current_location) {
                $selected = '';
                $with_parent = isset($current_location->label) ? true : false;
                switch ($what_is_current_location) {
                    case 'country':
                        if (strtolower($country_val) == strtolower($current_location->country)) {
                            $selected = 'selected="selected"';
                        }
                        $current_location->country = __($current_location->country, GEODIRECTORY_TEXTDOMAIN);
                        break;
                    case 'region':
                        $country_iso2 = geodir_location_get_iso2($country_val);
                        $country_iso2 = $country_iso2 != '' ? $country_iso2 : $country_val;
                        $with_parent = $with_parent && strtolower($region_val . ', ' . $country_iso2) == strtolower($current_location->label) ? true : false;
                        if (strtolower($region_val) == strtolower($current_location->region) || $with_parent) {
                            $selected = 'selected="selected"';
                        }
                        break;
                    case 'city':
                        $with_parent = $with_parent && strtolower($city_val . ', ' . $region_val) == strtolower($current_location->label) ? true : false;
                        if (strtolower($city_val) == strtolower($current_location->city) || $with_parent) {
                            $selected = 'selected="selected"';
                        }
                        break;
                }
                $output .= '<option value="' . geodir_location_permalink_url($base_location . $current_location->location_link) . '" ' . $selected . '>' . ucwords($current_location->{$what_is_current_location}) . '</option>';
            }
        }
        $output .= '</select>';
        $output .= "</div>";
        $output .= '</div>';
        if ($echo) {
            echo $output;
        } else {
            return $output;
        }
    }
}