Function Reference: geodir_get_location_array

Summary

Get location array using arguments.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None

Package

GeoDirectory_Location_Manager

Parameters

$args
(null|array) (required) {
Attributes of args. @type string $what What do you want to query. Possible values: city, region, country. @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. @type string $container_wrapper Container wrapper. @type string $container_wrapper_attr Container wrapper attr. @type string $item_wrapper Item wrapper. @type string $item_wrapper_attr Item wrapper attr. }

}.

Default: : ‘li’
$switcher
(bool) (required) Todo: describe this part.

Default: None

Return Values

(array|mixed|string)

    Change Log

    Since: 1.0.0

    Source File

    geodir_get_location_array() is located in geodir_location_manager/geodir_location_functions.php [Line: 82]

    Source Code

    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('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;
    			/*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 .= " ";
    					}
    
    					$location_as_formated_list .= $location->$location_args['what'] ;
    
    					if(isset($location->location_link))
    					{
    						$location_as_formated_list .= "";
    					}
    
    					if(!empty($item_wrapper))
    						$location_as_formated_list .="";
    				}
    			}
    
    			return $location_as_formated_list ;
    		}
    	}
    	return $locations ;
    }