Function Reference: geodir_add_location

Summary

Handles ‘add location’ form data.

Global Values

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

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None

Package

GeoDirectory_Location_Manager

Change Log

Since: 1.0.0

Source File

geodir_add_location() is located in geodir_location_manager/geodir_location_functions.php [Line: 1111]

Source Code

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
					country=%s,
					region=%s,
					city=%s,
					city_latitude=%s,
					city_longitude=%s,
					country_slug = %s,
					region_slug = %s,
					city_slug = %s,
					city_meta=%s,
					city_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
								WHERE 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();
	}

}