my-calendar-locations.php 26.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

function mc_update_location( $field, $data, $location ) {
	global $wpdb;
	$field  = sanitize_key( $field );
	$result = $wpdb->query( $wpdb->prepare( "UPDATE " . my_calendar_locations_table() . " SET $field = %d WHERE location_id=%d", $data, $location ) );

	return $result;
}


function mc_update_location_controls() {
	if ( isset( $_POST['mc_locations'] ) && $_POST['mc_locations'] == 'true' ) {
		$nonce = $_POST['_wpnonce'];
		if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) {
			wp_die( "Invalid nonce" );
		}
		$locations            = $_POST['mc_location_controls'];
		$mc_location_controls = array();
		foreach ( $locations as $key => $value ) {
			$mc_location_controls[ $key ] = mc_csv_to_array( $value[0] );
		}
		update_option( 'mc_location_controls', $mc_location_controls );
		echo "<div class='notice updated'><p>" . __( 'Location Controls Updated', 'my-calendar' ) . "</p></div>";
	}
}

function mc_mass_delete_locations() {
	global $wpdb;
	$mcdb = $wpdb;
	// mass delete locations
	if ( ! empty( $_POST['mass_edit'] ) && isset( $_POST['mass_delete'] ) ) {
		$nonce = $_REQUEST['_wpnonce'];
		if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) {
			die( "Security check failed" );
		}
		$locations = $_POST['mass_edit'];
		$i         = $total = 0;
		$deleted   = $ids = array();
		foreach ( $locations as $value ) {
			$total     = count( $locations );
			$ids[]     = (int) $value;
			$deleted[] = $value;
			$i ++;
		}
		$statement = implode( ',', $ids );
		$sql       = 'DELETE FROM ' . my_calendar_locations_table() . " WHERE location_id IN ($statement)";
		$result    = $mcdb->query( $sql );
		if ( $result !== 0 && $result !== false ) {
			mc_delete_cache();
			// argument: array of event IDs
			do_action( 'mc_mass_delete_locations', $deleted );
			$message = "<div class='updated'><p>" . sprintf( __( '%1$d locations deleted successfully out of %2$d selected', 'my-calendar' ), $i, $total ) . "</p></div>";
		} else {
			$message = "<div class='error'><p><strong>" . __( 'Error', 'my-calendar' ) . ":</strong>" . __( 'Your locations have not been deleted. Please investigate.', 'my-calendar' ) . "</p></div>";
		}
		echo $message;
	}
}

function mc_insert_location( $add ) {
	global $wpdb;
	$mcdb    = $wpdb;
	$add     = array_map( 'mc_kses_post', $add );			
	$formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%d', '%s', '%s', '%s' );
	$results = $mcdb->insert( my_calendar_locations_table(), $add, $formats );

	return $results;
}

function mc_modify_location( $update, $where ) {
	global $wpdb;
	$mcdb    = $wpdb;
	$update  = array_map( 'mc_kses_post', $update );		
	$formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%d', '%s', '%s', '%s' );
	$results = $mcdb->update( my_calendar_locations_table(), $update, $where, $formats, '%d' );

	return $results;
}

function my_calendar_manage_locations() {
	global $wpdb;
	$mcdb = $wpdb;
	?>
	<div class="wrap jd-my-calendar">
	<?php my_calendar_check_db();
	// We do some checking to see what we're doing
	mc_update_location_controls();
	mc_mass_delete_locations();
	if ( ! empty( $_POST ) && ( ! isset( $_POST['mc_locations'] ) && ! isset( $_POST['mass_delete'] ) ) ) {
		$nonce = $_REQUEST['_wpnonce'];
		if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) {
			die( "Security check failed" );
		}
	}
	if ( isset( $_POST['mode'] ) && $_POST['mode'] == 'add' ) {
		$add     = array(
			'location_label'     => $_POST['location_label'],
			'location_street'    => $_POST['location_street'],
			'location_street2'   => $_POST['location_street2'],
			'location_city'      => $_POST['location_city'],
			'location_state'     => $_POST['location_state'],
			'location_postcode'  => $_POST['location_postcode'],
			'location_region'    => $_POST['location_region'],
			'location_country'   => $_POST['location_country'],
			'location_url'       => $_POST['location_url'],
			'location_longitude' => $_POST['location_longitude'],
			'location_latitude'  => $_POST['location_latitude'],
			'location_zoom'      => $_POST['location_zoom'],
			'location_phone'     => $_POST['location_phone'],
			'location_phone2'    => $_POST['location_phone2'],
			'location_access'    => isset( $_POST['location_access'] ) ? serialize( $_POST['location_access'] ) : ''
		);
		$results = mc_insert_location( $add );
		do_action( 'mc_save_location', $results, $add );
		if ( $results ) {
			echo "<div class=\"updated\"><p><strong>" . __( 'Location added successfully', 'my-calendar' ) . "</strong></p></div>";
		} else {
			echo "<div class=\"error\"><p><strong>" . __( 'Location could not be added to database', 'my-calendar' ) . "</strong></p></div>";
		}
	} else if ( isset( $_GET['location_id'] ) && $_GET['mode'] == 'delete' ) {
		$sql     = "DELETE FROM " . my_calendar_locations_table() . " WHERE location_id=" . (int) ( $_GET['location_id'] );
		$results = $mcdb->query( $sql );
		do_action( 'mc_delete_location', $results, (int) $_GET['location_id'] );
		if ( $results ) {
			echo "<div class=\"updated\"><p><strong>" . __( 'Location deleted successfully', 'my-calendar' ) . "</strong></p></div>";
		} else {
			echo "<div class=\"error\"><p><strong>" . __( 'Location could not be deleted', 'my-calendar' ) . "</strong></p></div>";
		}
	} else if ( isset( $_GET['mode'] ) && isset( $_GET['location_id'] ) && $_GET['mode'] == 'edit' && ! isset( $_POST['mode'] ) ) {
		$cur_loc = (int) $_GET['location_id'];
		mc_show_location_form( 'edit', $cur_loc );
	} else if ( isset( $_POST['location_id'] ) && isset( $_POST['location_label'] ) && $_POST['mode'] == 'edit' ) {
		$update  = array(
			'location_label'     => $_POST['location_label'],
			'location_street'    => $_POST['location_street'],
			'location_street2'   => $_POST['location_street2'],
			'location_city'      => $_POST['location_city'],
			'location_state'     => $_POST['location_state'],
			'location_postcode'  => $_POST['location_postcode'],
			'location_region'    => $_POST['location_region'],
			'location_country'   => $_POST['location_country'],
			'location_url'       => $_POST['location_url'],
			'location_longitude' => $_POST['location_longitude'],
			'location_latitude'  => $_POST['location_latitude'],
			'location_zoom'      => $_POST['location_zoom'],
			'location_phone'     => $_POST['location_phone'],
			'location_phone2'    => $_POST['location_phone2'],
			'location_access'    => isset( $_POST['location_access'] ) ? serialize( $_POST['location_access'] ) : ''
		);
				
		$where   = array(
			'location_id' => (int) $_POST['location_id']
		);
		$results = mc_modify_location( $update, $where );
				
		do_action( 'mc_modify_location', $where, $update );
		if ( $results === false ) {
			echo "<div class=\"error\"><p><strong>" . __( 'Location could not be edited.', 'my-calendar' ) . "</strong></p></div>";
		} else if ( $results == 0 ) {
			echo "<div class=\"updated error\"><p><strong>" . __( 'Location was not changed.', 'my-calendar' ) . "</strong></p></div>";
		} else {
			echo "<div class=\"updated\"><p><strong>" . __( 'Location edited successfully', 'my-calendar' ) . "</strong></p></div>";
		}
		$cur_loc = (int) $_POST['location_id'];
		mc_show_location_form( 'edit', $cur_loc );

	}

	if ( isset( $_GET['mode'] ) && $_GET['mode'] != 'edit' || isset( $_POST['mode'] ) && $_POST['mode'] != 'edit' || ! isset( $_GET['mode'] ) && ! isset( $_POST['mode'] ) ) {
		mc_show_location_form( 'add' );
	}
}

function mc_show_location_form( $view = 'add', $curID = '' ) {
	global $wpdb;
	$mcdb    = $wpdb;
	$cur_loc = false;
	if ( $curID != '' ) {
		$sql     = "SELECT * FROM " . my_calendar_locations_table() . " WHERE location_id=$curID";
		$cur_loc = $mcdb->get_row( $sql );
	}
	$has_data = ( empty( $cur_loc ) ) ? false : true;
	if ( $view == 'add' ) {
		?>
		<h1><?php _e( 'Add New Location', 'my-calendar' ); ?></h1>
	<?php } else { ?>
		<h1><?php _e( 'Edit Location', 'my-calendar' ); ?></h1>
	<?php } ?>
	<div class="postbox-container jcd-wide">
		<div class="metabox-holder">

			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
					<h2><?php _e( 'Location Editor', 'my-calendar' ); ?></h2>

					<div class="inside location_form">
						<form id="my-calendar" method="post"
						      action="<?php echo admin_url( "admin.php?page=my-calendar-locations" ); ?>">
							<div><input type="hidden" name="_wpnonce"
							            value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/></div>
							<?php if ( $view == 'add' ) { ?>
								<div>
									<input type="hidden" name="mode" value="add"/>
									<input type="hidden" name="location_id" value=""/>
								</div>
							<?php } else { ?>
								<div>
									<input type="hidden" name="mode" value="edit"/>
									<input type="hidden" name="location_id"
									       value="<?php echo $cur_loc->location_id ?>"/>
								</div>
							<?php
							}
							echo mc_locations_fields( $has_data, $cur_loc, 'location' );
							?>
							<p>
								<input type="submit" name="save" class="button-primary"
								       value="<?php if ( $view == 'edit' ) {
									       _e( 'Save Changes', 'my-calendar' );
								       } else {
									       _e( 'Add Location', 'my-calendar' );
								       } ?> &raquo;"/>
							</p>
						</form>
					</div>
				</div>
			</div>
			<?php if ( $view == 'edit' ) { ?>
				<p>
					<a href="<?php echo admin_url( "admin.php?page=my-calendar-locations" ); ?>"><?php _e( 'Add a New Location', 'my-calendar' ); ?> &raquo;</a>
				</p>
			<?php } ?>
			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
					<h2><?php _e( 'Manage Locations', 'my-calendar' ); ?></h2>

					<div class="inside">
						<?php mc_manage_locations(); ?>
					</div>
				</div>
			</div>
			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
					<h2><?php _e( 'Location Controls', 'my-calendar' ); ?></h2>

					<div class="inside">
						<?php mc_location_controls(); ?>
					</div>
				</div>
			</div>			
		</div>
	</div>
		<?php mc_show_sidebar(); ?>
	</div>

<?php
}

function mc_controlled_field( $this_field ) {
	$this_field = trim( $this_field );
	$controls   = get_option( 'mc_location_controls' );
	if ( ! is_array( $controls ) || empty( $controls ) ) {
		return false;
	}
	$controlled = array_keys( $controls );
	if ( in_array( 'event_' . $this_field, $controlled ) && ! empty( $controls[ 'event_' . $this_field ] ) ) {
		return true;
	} else {
		return false;
	}
}

function mc_location_controller( $fieldname, $selected, $context = 'location' ) {
	$field    = ( $context == 'location' ) ? 'location_' . $fieldname : 'event_' . $fieldname;
	$selected = esc_attr( trim( $selected ) );
	$options  = get_option( 'mc_location_controls' );
	$regions  = $options[ 'event_' . $fieldname ];
	$form     = "<select name='$field' id='e_$fieldname'>";
	if ( $selected == '' || in_array( $selected, array_keys( $regions ) ) ) {
		$form .= "<option value=''>". __( 'None', 'my-calendar' )."</option>\n";
	} else {
		$form .= "<option value='$selected'>$selected " . __( '(Not a controlled value)', 'my-calendar' ) . "</option>\n";
	}
	foreach ( $regions as $key => $value ) {
		$key       = esc_attr( trim( $key ) );
		$value    = esc_html( $value );
		$aselected = ( $selected == $key ) ? " selected='selected'" : '';
		$form .= "<option value='$key'$aselected>$value</option>\n";
	}
	$form .= "</select>";

	return $form;
}

function mc_manage_locations() {
	global $wpdb;
	$mcdb = $wpdb;

	// pull the locations from the database
	$items_per_page = 50;
	$current        = empty( $_GET['paged'] ) ? 1 : intval( $_GET['paged'] );
	$locations      = $mcdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM " . my_calendar_locations_table() . " ORDER BY location_label ASC LIMIT " . ( ( $current - 1 ) * $items_per_page ) . ", " . $items_per_page );
	$found_rows     = $wpdb->get_col( "SELECT FOUND_ROWS();" );
	$items          = $found_rows[0];

	$num_pages = ceil( $items / $items_per_page );
	if ( $num_pages > 1 ) {
		$page_links = paginate_links( array(
			'base'      => add_query_arg( 'paged', '%#%' ),
			'format'    => '',
			'prev_text' => __( '&laquo; Previous<span class="screen-reader-text"> Locations</span>', 'my-calendar' ),
			'next_text' => __( 'Next<span class="screen-reader-text"> Locations</span> &raquo;', 'my-calendar' ),
			'total'     => $num_pages,
			'current'   => $current,
			'mid_size'  => 1
		) );
		printf( "<div class='tablenav'><div class='tablenav-pages'>%s</div></div>", $page_links );
	}

	if ( ! empty( $locations ) ) {
		?>
	<form action="<?php echo esc_url( add_query_arg( $_GET, admin_url( 'admin.php' ) ) ); ?>" method="post">
		<div><input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/></div>
		<div class='mc-actions'>
			<input type="submit" class="button-secondary delete" name="mass_delete"
			       value="<?php _e( 'Delete locations', 'my-calendar' ); ?>"/>
		</div>
		<table class="widefat page" id="my-calendar-admin-table">
			<thead>
			<tr>
				<th scope="col"><?php _e( 'ID', 'my-calendar' ) ?></th>
				<th scope="col"><?php _e( 'Location', 'my-calendar' ) ?></th>
				<th scope="col"><?php _e( 'Edit', 'my-calendar' ) ?></th>
				<th scope="col"><?php _e( 'Delete', 'my-calendar' ) ?></th>
			</tr>
			</thead>
			<?php
			$class = '';
			foreach ( $locations as $location ) {
				$class = ( $class == 'alternate' ) ? '' : 'alternate'; ?>
				<tr class="<?php echo $class; ?>">
					<th scope="row"><input type="checkbox" value="<?php echo $location->location_id; ?>"
					                       name="mass_edit[]" id="mc<?php echo $location->location_id; ?>"/> <label
							for="mc<?php echo $location->location_id; ?>"><?php echo $location->location_id; ?></label>
					</th>
					<td><?php echo mc_hcard( $location, 'true', 'false', 'location' ); ?></td>
					<td>
						<a href="<?php echo admin_url( "admin.php?page=my-calendar-locations&amp;mode=edit&amp;location_id=$location->location_id" ); ?>"
						   class='edit'><?php _e( 'Edit', 'my-calendar' ); ?></a></td>
					<td>
						<a href="<?php echo admin_url( "admin.php?page=my-calendar-locations&amp;mode=delete&amp;location_id=$location->location_id" ); ?>"
						   class="delete"
						   onclick="return confirm('<?php _e( 'Are you sure you want to delete this category?', 'my-calendar' ); ?>')"><?php _e( 'Delete', 'my-calendar' ); ?></a>
					</td>
				</tr>
			<?php } ?>
		</table>
		<p>
			<input type="submit" class="button-secondary delete" name="mass_delete"
			       value="<?php _e( 'Delete locations', 'my-calendar' ); ?>"/>
		</p>
		</form><?php
	} else {
		echo '<p>' . __( 'There are no locations in the database yet!', 'my-calendar' ) . '</p>';
	} ?>
	<p><em>
			<?php _e( 'Please note: editing or deleting locations stored for re-use will have no effect on any event previously scheduled at that location. The location database exists purely as a shorthand method to enter frequently used locations into event records.', 'my-calendar' ); ?>
		</em></p>
<?php
}

function mc_location_controls() {
	?>
	<form method="post" action="<?php echo admin_url( "admin.php?page=my-calendar-locations" ); ?>">
	<div><input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/></div>
	<div><input type="hidden" name="mc_locations" value="true"/></div>
	<fieldset>
		<legend><?php _e( 'Control Input Options for Location Fields', 'my-calendar' ); ?></legend>
		<div id="mc-accordion">
			<?php
			// array of fields allowing input control.
			$location_fields      = array(
				'event_label',
				'event_city',
				'event_state',
				'event_country',
				'event_postcode',
				'event_region'
			);
			$mc_location_controls = get_option( 'mc_location_controls' );
			foreach ( $location_fields as $field ) {
				?>
				<h4><span class="dashicons" aria-hidden="true"> </span><?php echo ucfirst( str_replace( 'event_', '', $field ) ); ?></h4>
				<div>
					<label
						for="loc_values_<?php echo $field; ?>"><?php printf( __( 'Location Controls for %s', 'my-calendar' ), ucfirst( str_replace( 'event_', '', $field ) ) ); ?>
						(<?php _e( 'Value, Label (one per line)', 'my-calendar' ); ?>)</label><br/>
					<?php
					$locations = '';
					if ( is_array( $mc_location_controls ) && isset( $mc_location_controls[ $field ] ) ) {
						foreach ( $mc_location_controls[ $field ] as $key => $value ) {
							$locations .= stripslashes( "$key,$value" ) . "\n";
						}
					}
					?>
					<textarea name="mc_location_controls[<?php echo $field; ?>][]"
							  id="loc_values_<?php echo $field; ?>" cols="80"
							  rows="6"><?php echo trim( $locations ); ?></textarea>
				</div>
			<?php } ?>
		</div>
		<p><input type='submit' class='button secondary'
				  value='<?php _e( 'Save Location Controls', 'my-calendar' ); ?>'/></p>
	</fieldset>
	<?php
}

function mc_locations_fields( $has_data, $data, $context = 'location' ) {
	$return = '<div class="mc-locations">';
	if ( current_user_can( 'mc_edit_locations' ) && $context == 'event' ) {
		$return .= '<p class="checkboxes"><input type="checkbox" value="on" name="mc_copy_location" id="mc_copy_location" /> <label for="mc_copy_location">' . __( 'Copy this location into the locations table', 'my-calendar' ) . '</label></p>';
	}
	$return .= '
	<p class="checkbox">
	<label for="e_label">' . __( 'Name of Location (e.g. <em>Joe\'s Bar and Grill</em>)', 'my-calendar' ) . '</label>';
	$cur_label = ( ! empty( $data ) ) ? ( stripslashes( $data->{$context . '_label'} ) ) : '';
	if ( mc_controlled_field( 'label' ) ) {
		$return .= mc_location_controller( 'label', $cur_label, $context );
	} else {
		$return .= '<input type="text" id="e_label" name="' . $context . '_label" size="40" value="' . esc_attr( $cur_label ) . '" />';
	}
	$street_address  = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_street'} ) ) : '';
	$street_address2 = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_street2'} ) ) : '';
	$return .= '
	</p>
	<div class="locations-container">
	<div class="location-primary">
	<fieldset>
	<legend>' . __( 'Location Address', 'my-calendar' ) . '</legend>
	<p>
	<label for="e_street">' . __( 'Street Address', 'my-calendar' ) . '</label> <input type="text" id="e_street" name="' . $context . '_street" size="40" value="' . $street_address . '" />
	</p>
	<p>
	<label for="e_street2">' . __( 'Street Address (2)', 'my-calendar' ) . '</label> <input type="text" id="e_street2" name="' . $context . '_street2" size="40" value="' . $street_address2 . '" />
	</p>		
	<p>
	<label for="e_city">' . __( 'City', 'my-calendar' ) . '</label> ';
	$cur_city = ( ! empty( $data ) ) ? ( stripslashes( $data->{$context . '_city'} ) ) : '';
	if ( mc_controlled_field( 'city' ) ) {
		$return .= mc_location_controller( 'city', $cur_city, $context );
	} else {
		$return .= '<input type="text" id="e_city" name="' . $context . '_city" size="40" value="' . esc_attr( $cur_city ) . '" />';
	}
	$return .= "</p>
	<p>";
	$return .= '<label for="e_state">' . __( 'State/Province', 'my-calendar' ) . '</label> ';
	$cur_state = ( ! empty( $data ) ) ? ( stripslashes( $data->{$context . '_state'} ) ) : '';
	if ( mc_controlled_field( 'state' ) ) {
		$return .= mc_location_controller( 'state', $cur_state, $context );
	} else {
		$return .= '<input type="text" id="e_state" name="' . $context . '_state" size="10" value="' . esc_attr( $cur_state ) . '" />';
	}
	$return .= '</p>
	<p>
	<label for="e_postcode">' . __( 'Postal Code', 'my-calendar' ) . '</label> ';
	$cur_postcode = ( ! empty( $data ) ) ? ( stripslashes( $data->{$context . '_postcode'} ) ) : '';
	if ( mc_controlled_field( 'postcode' ) ) {
		$return .= mc_location_controller( 'postcode', $cur_postcode, $context );
	} else {
		$return .= '<input type="text" id="e_postcode" name="' . $context . '_postcode" size="40" value="' . esc_attr( $cur_postcode ) . '" />';
	}
	$return .= "</p>
	<p>";
	$return .= '<label for="e_region">' . __( 'Region', 'my-calendar' ) . '</label> ';
	$cur_region = ( ! empty( $data ) ) ? ( stripslashes( $data->{$context . '_region'} ) ) : '';
	if ( mc_controlled_field( 'region' ) ) {
		$return .= mc_location_controller( 'region', $cur_region, $context );
	} else {
		$return .= '<input type="text" id="e_region" name="' . $context . '_region" size="40" value="' . esc_attr( $cur_region ) . '" />';
	}
	$return .= '</p>
	<p>		
	<label for="e_country">' . __( 'Country', 'my-calendar' ) . '</label> ';
	$cur_country = ( $has_data ) ? ( stripslashes( $data->{$context . '_country'} ) ) : '';
	if ( mc_controlled_field( 'country' ) ) {
		$return .= mc_location_controller( 'country', $cur_country, $context );
	} else {
		$return .= '<input type="text" id="e_country" name="' . $context . '_country" size="10" value="' . esc_attr( $cur_country ) . '" />';
	}
	$zoom         = ( $has_data ) ? $data->{$context . '_zoom'} : '16';
	$event_phone  = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_phone'} ) ) : '';
	$event_phone2 = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_phone2'} ) ) : '';
	$event_url    = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_url'} ) ) : '';
	$event_lat    = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_latitude'} ) ) : '';
	$event_lon    = ( $has_data ) ? esc_attr( stripslashes( $data->{$context . '_longitude'} ) ) : '';
	$return .= '</p>
	<p>
	<label for="e_zoom">' . __( 'Initial Zoom', 'my-calendar' ) . '</label>
		<select name="' . $context . '_zoom" id="e_zoom">
			<option value="16"' . jd_option_selected( $zoom, '16', 'option' ) . '>' . __( 'Neighborhood', 'my-calendar' ) . '</option>
			<option value="14"' . jd_option_selected( $zoom, '14', 'option' ) . '>' . __( 'Small City', 'my-calendar' ) . '</option>
			<option value="12"' . jd_option_selected( $zoom, '12', 'option' ) . '>' . __( 'Large City', 'my-calendar' ) . '</option>
			<option value="10"' . jd_option_selected( $zoom, '10', 'option' ) . '>' . __( 'Greater Metro Area', 'my-calendar' ) . '</option>
			<option value="8"' . jd_option_selected( $zoom, '8', 'option' ) . '>' . __( 'State', 'my-calendar' ) . '</option>
			<option value="6"' . jd_option_selected( $zoom, '6', 'option' ) . '>' . __( 'Region', 'my-calendar' ) . '</option>
		</select>
	</p>
	</fieldset>
	<fieldset>
	<legend>' . __( 'GPS Coordinates (optional)', 'my-calendar' ) . '</legend>
	<p>
	' . __( 'If you supply GPS coordinates for your location, they will be used in place of any other address information to provide your map link.', 'my-calendar' ) . '
	</p>
	<p>
	<label for="e_latitude">' . __( 'Latitude', 'my-calendar' ) . '</label> <input type="text" id="e_latitude" name="' . $context . '_latitude" size="10" value="' . $event_lat . '" /> <label for="e_longitude">' . __( 'Longitude', 'my-calendar' ) . '</label> <input type="text" id="e_longitude" name="' . $context . '_longitude" size="10" value="' . $event_lon . '" />
	</p>			
	</fieldset>
	</div>
	<div class="location-secondary">
	<fieldset>
	<legend>' . __( 'Location Contact Information', 'my-calendar' ) . '</legend>
	<p>
	<label for="e_phone">' . __( 'Phone', 'my-calendar' ) . '</label> <input type="text" id="e_phone" name="' . $context . '_phone" size="32" value="' . $event_phone . '" />
	</p>
	<p>
	<label for="e_phone2">' . __( 'Secondary Phone', 'my-calendar' ) . '</label> <input type="text" id="e_phone2" name="' . $context . '_phone2" size="32" value="' . $event_phone2 . '" />
	</p>	
	<p>
	<label for="e_url">' . __( 'Location URL', 'my-calendar' ) . '</label> <input type="text" id="e_url" name="' . $context . '_url" size="40" value="' . $event_url . '" />
	</p>
	</fieldset>
	<fieldset>
	<legend>' . __( 'Location Accessibility', 'my-calendar' ) . '</legend>
	<ul class="accessibility-features checkboxes">';
	$access      = apply_filters( 'mc_venue_accessibility', mc_location_access() );
	$access_list = '';
	if ( $has_data ) {
		if ( $context == 'location' ) {
			$location_access = unserialize( $data->{$context . '_access'} );
		} else {
			if ( property_exists( $data, 'event_location' ) ) {
				$event_location = $data->event_location;
			} else {
				$event_location = false;
			}
			$location_access = unserialize( mc_location_data( 'location_access', $event_location ) );
		}
	} else {
		$location_access = array();
	}
	foreach ( $access as $k => $a ) {
		$id      = "loc_access_$k";
		$label   = $a;
		$checked = '';
		if ( is_array( $location_access ) ) {
			$checked = ( in_array( $a, $location_access ) || in_array( $k, $location_access ) ) ? " checked='checked'" : '';
		}
		$item = sprintf( '<li><input type="checkbox" id="%1$s" name="' . $context . '_access[]" value="%4$s" class="checkbox" %2$s /> <label for="%1$s">%3$s</label></li>', esc_attr( $id ), $checked, esc_html( $label ), esc_attr( $a ) );
		$access_list .= $item;
	}
	$return .= $access_list;
	$return .= '</ul>
	</fieldset></div>
	</div>
	</div>';

	return $return;
}

function mc_location_access() {
	$location_access = apply_filters( 'mc_location_access_choices', array(
			'1'  => __( 'Accessible Entrance', 'my-calendar' ),
			'2'  => __( 'Accessible Parking Designated', 'my-calendar' ),
			'3'  => __( 'Accessible Restrooms', 'my-calendar' ),
			'4'  => __( 'Accessible Seating', 'my-calendar' ),
			'5'  => __( 'Accessible Transportation Available', 'my-calendar' ),
			'6'  => __( 'Wheelchair Accessible', 'my-calendar' ),
			'7'  => __( 'Courtesy Wheelchairs', 'my-calendar' ),
			'8'  => __( 'Bariatric Seating Available', 'my-calendar' ),
			'9'  => __( 'Elevator to all public areas', 'my-calendar' ),
			'10' => __( 'Braille Signage', 'my-calendar' ),
			'11' => __( 'Fragrance-Free Policy', 'my-calendar' ),
			'12' => __( 'Other', 'my-calendar' )
		) 
	);
	
	return $location_access;
}

// get a specific field with an location ID
function mc_location_data( $field, $id ) {
	if ( $id ) {
		global $wpdb;
		$mcdb = $wpdb;
		if ( get_option( 'mc_remote' ) == 'true' && function_exists( 'mc_remote_db' ) ) {
			$mcdb = mc_remote_db();
		}
		$field  = esc_sql( $field );
		$sql    = $wpdb->prepare( "SELECT $field FROM " . my_calendar_locations_table() . " WHERE location_id = %d", $id );
		$result = $mcdb->get_var( $sql );

		return $result;
	}
}