Completed
Push — milestone/2_0/react-ui ( 2196cf...c9a76c )
by
unknown
02:52
created

Map_Field   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 138
ccs 0
cts 46
cp 0
rs 10
wmc 13
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A admin_enqueue_scripts() 0 6 2
A lat_lng_to_latlng() 0 3 3
B set_value_from_input() 0 26 4
B to_json() 0 25 2
A set_position() 0 7 1
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Value_Set\Value_Set;
6
7
/**
8
 * Google Maps with Address field class.
9
 * Allows to manually select a pin, or to position a pin based on a specified address.
10
 * Coords (lat, lng), address and zoom are saved in the database.
11
 */
12
class Map_Field extends Field {
13
	
14
	/**
15
	 * Whether to lazy load this field.
16
	 *
17
	 * @var bool
18
	 */
19
	protected $lazyload = true;
20
21
	/**
22
	 * Default latitude.
23
	 *
24
	 * @var float
25
	 */
26
	protected $default_lat = 40.346544;
27
28
	/**
29
	 * Default longitude.
30
	 *
31
	 * @var float
32
	 */
33
	protected $default_lng = -101.645507;
34
35
	/**
36
	 * Default zoom.
37
	 *
38
	 * @var int
39
	 */
40
	protected $default_zoom = 10;
41
42
	/**
43
	 * Create a field from a certain type with the specified label.
44
	 * 
45
	 * @param string $type  Field type
46
	 * @param string $name  Field name
47
	 * @param string $label Field label
48
	 */
49
	public function __construct( $type, $name, $label ) {
50
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_PROPERTIES, array( 'lat' => '', 'lng' => '', 'zoom' => '', 'address' => '' ) ) );
51
		parent::__construct( $type, $name, $label );
52
	}
53
54
	/**
55
	 * Enqueue scripts and styles in admin
56
	 * Called once per field type
57
	 */
58
	public static function admin_enqueue_scripts() {
59
		$api_key = apply_filters( 'carbon_fields_map_field_api_key', false );
60
		$url = apply_filters( 'carbon_fields_map_field_api_url', '//maps.googleapis.com/maps/api/js?' . ( $api_key ? 'key=' . $api_key : '' ), $api_key );
61
62
		wp_enqueue_script( 'carbon-google-maps', $url, array(), null );
63
	}
64
65
	/**
66
	 * Convert lat and lng to a comma-separated list
67
	 */
68
	protected function lat_lng_to_latlng( $lat, $lng ) {
69
		return ( ! empty( $lat ) && ! empty( $lng ) ) ? $lat . ',' . $lng : '';
70
	}
71
72
	/**
73
	 * Load the field value from an input array based on it's name
74
	 *
75
	 * @param array $input Array of field names and values.
76
	 */
77
	public function set_value_from_input( $input ) {
78
		if ( ! isset( $input[ $this->get_name() ] ) ) {
79
			$this->set_value( null );
80
			return;
81
		}
82
83
		$value_set = array(
84
			'lat' => '',
85
			'lng' => '',
86
			'zoom' => '',
87
			'address' => '',
88
		);
89
90
		foreach ( $value_set as $key => $v ) {
91
			if ( isset( $input[ $this->get_name() ][ $key ] ) ) {
92
				$value_set[ $key ] = $input[ $this->get_name() ][ $key ];
93
			}
94
		}
95
96
		$value_set['lat'] = (float) $value_set['lat'];
97
		$value_set['lng'] = (float) $value_set['lng'];
98
		$value_set['zoom'] = (int) $value_set['zoom'];
99
		$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] );
100
101
		$this->set_value( $value_set );
102
	}
103
104
	/**
105
	 * Returns an array that holds the field data, suitable for JSON representation.
106
	 *
107
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
108
	 * @return array
109
	 */
110
	public function to_json( $load ) {
111
		$field_data = parent::to_json( $load );
112
113
		$value_set = $this->get_value();
114
		if ( $value_set === null ) {
115
			$value_set = array(
116
				'lat' => floatval( $this->default_lat ),
117
				'lng' => floatval( $this->default_lng ),
118
				'zoom' => intval( $this->default_zoom ),
119
				'address' => '',
120
			);
121
			$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] );
122
		}
123
		$field_data = array_merge( $field_data, array(
124
			'value' => array(
125
				'lat' => floatval( $value_set['lat'] ),
126
				'lng' => floatval( $value_set['lng'] ),
127
				'zoom' => intval( $value_set['zoom'] ),
128
				'address' => $value_set['address'],
129
				'value' => $value_set[ Value_Set::VALUE_PROPERTY ],
130
			),
131
		) );
132
133
		return $field_data;
134
	}
135
136
	/**
137
	 * Set the coords and zoom of this field.
138
	 * @param string $lat  Latitude
139
	 * @param string $lng  Longitude
140
	 * @param int $zoom Zoom level
141
	 */
142
	public function set_position( $lat, $lng, $zoom ) {
143
		$this->default_lat = floatval( $lat );
144
		$this->default_lng = floatval( $lng );
145
		$this->default_zoom = $zoom;
146
147
		return $this;
148
	}
149
}
150