Completed
Push — develop ( c77227...1859c0 )
by Daniel
14:00
created

location   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 172
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_name() 0 4 1
A get_default_props() 0 11 1
A get_field_value() 0 10 2
A show_form_field() 0 12 1
A display_field() 0 12 2
A display_address() 0 4 1
A display_coordinates() 0 5 1
A display_active_map() 0 16 2
A display_static_map() 0 18 4
A get_location_title() 0 4 2
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2017 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\form\field;
11
12
class location extends base
13
{
14
	/** @var \blitze\sitemaker\services\util */
15
	protected $util;
16
17
	/** @var string */
18
	protected $google_api_key;
19
20
	/** @var string */
21
	protected $session_id;
22
23
	/**
24
	 * Constructor
25
	 *
26
	 * @param \phpbb\language\language                  	$language       	Language object
27
	 * @param \phpbb\request\request_interface				$request			Request object
28
	 * @param \blitze\sitemaker\services\template			$ptemplate			Sitemaker template object
29
	 * @param \phpbb\config\config							$config				Config object
30
	 * @param \phpbb\user									$user				User object
31
	 * @param \blitze\sitemaker\services\util				$util       		Sitemaker utility object
32
	 */
33
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \phpbb\config\config $config, \phpbb\user $user, \blitze\sitemaker\services\util $util)
34
	{
35
		parent::__construct($language, $request, $ptemplate);
36
37
		$this->util = $util;
38
		$this->google_api_key = $config['google_api_key'];
39
		$this->session_id = $user->session_id;
40
	}
41
42
	/**
43
	 * @inheritdoc
44
	 */
45
	public function get_name()
46
	{
47
		return 'location';
48
	}
49
50
	/**
51
	 * @inheritdoc
52
	 */
53
	public function get_default_props()
54
	{
55
		return array(
56
			'disp_type'		=> 'address',
57
			'map_types'		=> '',
58
			'map_width'		=> '100%',
59
			'map_height'	=> 400,
60
			'map_zoom'		=> 0,
61
			'session_id'	=> $this->session_id,
62
		);
63
	}
64
65
	/**
66
	 * @inheritdoc
67
	 */
68
	public function get_field_value(array $data)
69
	{
70
		// form has been submitted so get value from request object
71
		if ($this->request->is_set_post('cp'))
72
		{
73
			return array_filter($this->request->variable($data['field_name'], array('' => ''), true));
74
		}
75
76
		return json_decode(htmlspecialchars_decode($data['field_value']), true);
77
	}
78
79
	/**
80
	 * @inheritdoc
81
	 */
82
	public function show_form_field($name, array &$data)
83
	{
84
		$this->util->add_assets(array(
85
			'js'	=> array(
86
				100 => '@blitze_content/assets/fields/form.min.js',
87
				101 => '//maps.googleapis.com/maps/api/js?key=' . $this->google_api_key . '&libraries=places&callback=initMap&language=' . $this->language->get_used_language() . '" async defer charset="UTF-8',
88
			)
89
		));
90
		$data['show_input'] = true;
91
92
		return parent::show_form_field($name, $data);
93
	}
94
95
	/**
96
	 * @inheritdoc
97
	 */
98
	public function display_field(array $data, $mode = '')
99
	{
100
		if (!$data['field_value'])
101
		{
102
			return '';
103
		}
104
105
		$data['field_value'] = $this->get_field_value($data);
106
		$callable = 'display_' . $data['field_props']['disp_type'];
107
108
		return $this->$callable($data);
109
	}
110
111
	/**
112
	 * @param array $data
113
	 * @return string
114
	 */
115
	protected function display_address(array $data)
116
	{
117
		return $this->get_location_title($data['field_value']['place'], $data['field_value']['address']) . $data['field_value']['address'];
118
	}
119
120
	/**
121
	 * @param array $data
122
	 * @return string
123
	 */
124
	protected function display_coordinates(array $data)
125
	{
126
		return $this->get_location_title($data['field_value']['place'], $data['field_value']['address']) .
127
			$data['field_value']['latitude'] . ', ' . $data['field_value']['longitude'];
128
	}
129
130
	/**
131
	 * @param array $data
132
	 * @return string
133
	 */
134
	protected function display_active_map(array $data)
135
	{
136
		// do not include assets on preview page as form already handles this
137
		if (!$this->request->is_set_post('cp'))
138
		{
139
			$this->util->add_assets(array(
140
				'js'	=> array(
141
					'//maps.googleapis.com/maps/api/js?key=' . $this->google_api_key . '&callback=initMap&language=' . $this->language->get_used_language() . '" async defer charset="UTF-8',
142
					'@blitze_content/assets/fields/display.min.js',
143
				)
144
			));
145
		}
146
147
		$this->ptemplate->assign_vars($data);
148
		return $this->ptemplate->render_view('blitze/content', "fields/location.html", 'location_field');
149
	}
150
151
	/**
152
	 * @param array $data
153
	 * @return string
154
	 */
155
	protected function display_static_map(array $data)
156
	{
157
		$info = $data['field_value'];
158
		$settings = $data['field_props'];
159
		$map_types = $settings['options'] ?: ['roadmap'];
160
		$coordinates = $info['latitude'] . ',' . $info['longitude'];
161
162
		$params = array(
163
			'center'	=> $coordinates,
164
			'zoom'		=> $settings['map_zoom'] ?: $info['zoom'],
165
			'size'		=> $settings['map_width'] . 'x' . $settings['map_height'],
166
			'maptype'	=> $info['map_type'] ?: $map_types[0],
167
			'markers'	=> $coordinates,
168
			'key'		=> $this->google_api_key,
169
		);
170
171
		return '<img src="https://maps.googleapis.com/maps/api/staticmap?' . http_build_query($params) . '" alt="' . $info['address'] . '" title="' . $info['place'] . '" />';
172
	}
173
174
	/**
175
	 * @param string $place
176
	 * @param string $address
177
	 * @return string
178
	 */
179
	protected function get_location_title($place, $address)
180
	{
181
		return (strpos($address, $place) === false) ? '<strong>' . $place . '</strong><br />' : '';
182
	}
183
}
184