Completed
Push — development ( 2fadcd...18958b )
by
unknown
03:13
created

Map_Field::lat_lng_to_latlng()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 12
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
	 * {@inheritDoc}
16
	 */
17
	protected $default_value = array(
18
		Value_Set::VALUE_PROPERTY => '40.346544,-101.645507',
19
		'lat' => 40.346544,
20
		'lng' => -101.645507,
21
		'zoom' => 10,
22
		'address' => '',
23
	);
24
25
	/**
26
	 * Create a field from a certain type with the specified label.
27
	 *
28
	 * @param string $type  Field type
29
	 * @param string $name  Field name
30
	 * @param string $label Field label
31
	 */
32
	public function __construct( $type, $name, $label ) {
33
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_PROPERTIES, array( 'lat' => '', 'lng' => '', 'zoom' => '', 'address' => '' ) ) );
34
		parent::__construct( $type, $name, $label );
35
	}
36
37
	/**
38
	 * Enqueue scripts and styles in admin
39
	 * Called once per field type
40
	 */
41
	public static function admin_enqueue_scripts() {
42
		$api_key = apply_filters( 'carbon_fields_map_field_api_key', false );
43
		$url = apply_filters( 'carbon_fields_map_field_api_url', '//maps.googleapis.com/maps/api/js?' . ( $api_key ? 'key=' . $api_key : '' ), $api_key );
44
45
		wp_enqueue_script( 'carbon-google-maps', $url, array(), null );
46
	}
47
48
	/**
49
	 * Convert lat and lng to a comma-separated list
50
	 */
51
	protected function lat_lng_to_latlng( $lat, $lng ) {
52
		return ( ! empty( $lat ) && ! empty( $lng ) ) ? $lat . ',' . $lng : '';
53
	}
54
55
	/**
56
	 * Load the field value from an input array based on its name
57
	 *
58
	 * @param  array $input Array of field names and values.
59
	 * @return self  $this
60
	 */
61
	public function set_value_from_input( $input ) {
62
		if ( ! isset( $input[ $this->get_name() ] ) ) {
63
			$this->set_value( null );
64
			return $this;
65
		}
66
67
		$value_set = array(
68
			'lat' => '',
69
			'lng' => '',
70
			'zoom' => '',
71
			'address' => '',
72
		);
73
74
		foreach ( $value_set as $key => $v ) {
75
			if ( isset( $input[ $this->get_name() ][ $key ] ) ) {
76
				$value_set[ $key ] = $input[ $this->get_name() ][ $key ];
77
			}
78
		}
79
80
		$value_set['lat'] = (float) $value_set['lat'];
81
		$value_set['lng'] = (float) $value_set['lng'];
82
		$value_set['zoom'] = (int) $value_set['zoom'];
83
		$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] );
84
85
		$this->set_value( $value_set );
86
		return $this;
87
	}
88
89
	/**
90
	 * Returns an array that holds the field data, suitable for JSON representation.
91
	 *
92
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
93
	 * @return array
94
	 */
95
	public function to_json( $load ) {
96
		$field_data = parent::to_json( $load );
97
98
		$value_set = $this->get_value();
99
100
		$field_data = array_merge( $field_data, array(
101
			'value' => array(
102
				'lat' => floatval( $value_set['lat'] ),
103
				'lng' => floatval( $value_set['lng'] ),
104
				'zoom' => intval( $value_set['zoom'] ),
105
				'address' => $value_set['address'],
106
				'value' => $value_set[ Value_Set::VALUE_PROPERTY ],
107
			),
108
		) );
109
110
		return $field_data;
111
	}
112
113
	/**
114
	 * Set the coords and zoom of this field.
115
	 *
116
	 * @param  string $lat  Latitude
117
	 * @param  string $lng  Longitude
118
	 * @param  int    $zoom Zoom level
119
	 * @return $this
120
	 */
121 1
	public function set_position( $lat, $lng, $zoom ) {
122 1
		return $this->set_default_value( array_merge(
123 1
			$this->get_default_value(),
124
			array(
125 1
				Value_Set::VALUE_PROPERTY => $this->lat_lng_to_latlng( $lat, $lng ),
126 1
				'lat' => $lat,
127 1
				'lng' => $lng,
128 1
				'zoom' => $zoom,
129
			)
130
		) );
131
	}
132
}
133