| 1 |
<?php |
| 2 |
/** |
| 3 |
* The template for Edit field MAP. |
| 4 |
* |
| 5 |
* This is the template that field layout for edit form, gps |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
?> |
| 13 |
<?php |
| 14 |
|
| 15 |
//wmvc_dump($field); |
| 16 |
|
| 17 |
/* |
| 18 |
* |
| 19 |
* Should be exists in name _lat |
| 20 |
* Should be exists same field for lng, example |
| 21 |
* gps_lat and gps_lng |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
if(isset($field->field)) |
| 26 |
{ |
| 27 |
$field_id = $field->field; |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
$field_id = 'field_'.$field->idfield; |
| 32 |
} |
| 33 |
|
| 34 |
if(!isset($field->hint))$field->hint = ''; |
| 35 |
|
| 36 |
$field_label = $field->field_label; |
| 37 |
|
| 38 |
$required = ''; |
| 39 |
if(isset($field->is_required) && $field->is_required == 1) |
| 40 |
$required = '*'; |
| 41 |
|
| 42 |
if(strpos($field_id, '_lat') === FALSE) return false; |
| 43 |
|
| 44 |
$field_id = str_replace('_lat', '', $field_id); |
| 45 |
|
| 46 |
?> |
| 47 |
|
| 48 |
<div class="wdk-field-edit <?php echo esc_attr($field->field_type); ?>" id="inputbox_map_<?php echo esc_attr($field_id); ?>"> |
| 49 |
<label for="<?php echo esc_attr($field_id); ?>"><?php echo esc_html($field_label).esc_html($required); ?></label> |
| 50 |
<div class="wdk-field-container"> |
| 51 |
<div class="inputbox_map" id="<?php echo esc_attr($field_id); ?>_map"></div> |
| 52 |
|
| 53 |
<input class="regular-text hidden" name="<?php echo esc_attr($field_id).'_lat'; ?>" type="text" id="<?php echo esc_attr($field_id).'_lat'; ?>" value="<?php echo esc_attr(wmvc_show_data($field_id.'_lat', $db_data, '')); ?>"> |
| 54 |
<p class="wdk-hint"> |
| 55 |
<?php echo esc_html($field->hint); ?> |
| 56 |
</p> |
| 57 |
</div> |
| 58 |
<?php |
| 59 |
wp_enqueue_style('leaflet'); |
| 60 |
wp_enqueue_script('leaflet'); |
| 61 |
|
| 62 |
wp_enqueue_style('wdk-notify'); |
| 63 |
wp_enqueue_script('wdk-notify'); |
| 64 |
?> |
| 65 |
<script> |
| 66 |
jQuery(document).ready(function($) { |
| 67 |
if(!$('#<?php echo esc_attr($field_id); ?>_lat').length || !$('#<?php echo esc_attr($field_id); ?>_lng').length){$('#inputbox_map_<?php echo esc_attr($field_id); ?>').hide()} |
| 68 |
let map,marker; |
| 69 |
map = L.map('<?php echo esc_attr($field_id); ?>_map', { |
| 70 |
center: [<?php echo wmvc_show_data($field_id.'_lat', $db_data, get_option('wdk_default_lat', 51.505)); ?>, <?php echo wmvc_show_data($field_id.'_lng', $db_data, get_option('wdk_default_lng', -0.09)); ?>], |
| 71 |
zoom: 4, |
| 72 |
}); |
| 73 |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
| 74 |
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' |
| 75 |
}).addTo(map); |
| 76 |
|
| 77 |
marker = L.marker( |
| 78 |
[<?php echo wmvc_show_data($field_id.'_lat', $db_data, get_option('wdk_default_lat', 51.505)); ?>, <?php echo wmvc_show_data($field_id.'_lng', $db_data, get_option('wdk_default_lng', -0.09)); ?>], |
| 79 |
{draggable: true} |
| 80 |
).addTo(map); |
| 81 |
|
| 82 |
marker.on('dragend', (event) => { |
| 83 |
let marker = event.target; |
| 84 |
let {lat,lng} = marker.getLatLng(); |
| 85 |
$('#<?php echo esc_attr($field_id); ?>_lat').val(lat); |
| 86 |
$('#<?php echo esc_attr($field_id); ?>_lng').val(lng); |
| 87 |
//retrieved the position |
| 88 |
}); |
| 89 |
}); |
| 90 |
</script> |
| 91 |
</div> |