Passed
Push — release-3.0.0 ( e8971e...d75c10 )
by Daniel
04:23
created

location::get_submitted_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\util was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	 * @return array
68
	 */
69
	public function get_field_value(array $data)
70
	{
71
		$fields = ['place', 'address', 'zoom', 'latitude', 'longitude', 'map_type'];
72
		$field_value = $this->ensure_is_array($data['field_value']);
73
		$field_value = array_pad(array_map('trim', $field_value), count($fields), '');
74
75
		return array_combine($fields, $field_value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_combine($fields, $field_value) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
76
	}
77
78
	/**
79
	 * @inheritdoc
80
	 */
81
	public function display_field(array $data, array $topic_data, $view_mode)
82
	{
83
		if (!$data['field_value'])
84
		{
85
			return '';
86
		}
87
88
		$callable = 'display_' . $data['field_props']['disp_type'];
89
		return $this->$callable($data, $view_mode);
90
	}
91
92
	/**
93
	 * @inheritdoc
94
	 * @return array
95
	 */
96
	public function get_submitted_value(array $data, $form_is_submitted = false)
97
	{
98
		if ($form_is_submitted)
99
		{
100
			return $this->request->variable($data['field_name'], array('' => ''), true);
101
		}
102
103
		return $this->get_field_value($data);
104
	}
105
106
	/**
107
	 * @inheritdoc
108
	 */
109
	public function show_form_field(array &$data)
110
	{
111
		$this->util->add_assets(array(
112
			'js'	=> array(
113
				100 => '@blitze_content/assets/fields/form.min.js',
114
				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',
115
			)
116
		));
117
118
		$data['show_input'] = true;
119
120
		return parent::show_form_field($data);
121
	}
122
123
	/**
124
	 * @param array $data
125
	 * @return string
126
	 */
127
	protected function display_address(array $data)
128
	{
129
		return $this->get_location_title($data['field_value']['place'], $data['field_value']['address']) . $data['field_value']['address'];
130
	}
131
132
	/**
133
	 * @param array $data
134
	 * @return string
135
	 */
136
	protected function display_coordinates(array $data)
137
	{
138
		return $this->get_location_title($data['field_value']['place'], $data['field_value']['address']) .
139
			$data['field_value']['latitude'] . ', ' . $data['field_value']['longitude'];
140
	}
141
142
	/**
143
	 * @param array $data
144
	 * @return string
145
	 */
146
	protected function display_active_map(array $data)
147
	{
148
		// do not include assets on preview page as form already handles this
149
		if (!$this->request->is_set_post('cp'))
150
		{
151
			$this->util->add_assets(array(
152
				'js'	=> array(
153
					'//maps.googleapis.com/maps/api/js?key=' . $this->google_api_key . '&callback=initMap&language=' . $this->language->get_used_language() . '" async defer charset="UTF-8',
154
					'@blitze_content/assets/fields/display.min.js',
155
				)
156
			));
157
		}
158
159
		$this->ptemplate->assign_vars($data);
160
		return $this->ptemplate->render_view('blitze/content', "fields/location.html", 'location_field');
161
	}
162
163
	/**
164
	 * @param array $data
165
	 * @return string
166
	 */
167
	protected function display_static_map(array $data)
168
	{
169
		$info = $data['field_value'];
170
		$settings = $data['field_props'];
171
		$map_types = $settings['options'] ?: ['roadmap'];
172
		$coordinates = $info['latitude'] . ',' . $info['longitude'];
173
174
		$params = array(
175
			'center'	=> $coordinates,
176
			'zoom'		=> $settings['map_zoom'] ?: $info['zoom'],
177
			'size'		=> (int) $settings['map_width'] . 'x' . (int) $settings['map_height'],
178
			'maptype'	=> $info['map_type'] ?: $map_types[0],
179
			'markers'	=> $coordinates,
180
			'key'		=> $this->google_api_key,
181
		);
182
183
		return '<img src="https://maps.googleapis.com/maps/api/staticmap?' . http_build_query($params) . '" alt="' . $info['address'] . '" title="' . $info['place'] . '" />';
184
	}
185
186
	/**
187
	 * @param string $place
188
	 * @param string $address
189
	 * @return string
190
	 */
191
	protected function get_location_title($place, $address)
192
	{
193
		return (strpos($address, $place) === false) ? '<strong>' . $place . '</strong><br />' : $address;
194
	}
195
}
196