-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHelsinkiNearYouResultsController.php
165 lines (145 loc) · 4.47 KB
/
HelsinkiNearYouResultsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Drupal\helfi_etusivu\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\helfi_etusivu\Servicemap;
use Drupal\helfi_react_search\LinkedEvents;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Helsinki near you controller.
*/
class HelsinkiNearYouResultsController extends ControllerBase {
/**
* Constructs a new instance.
*
* @param \Drupal\helfi_etusivu\Servicemap $servicemap
* The servicemap service.
* @param \Drupal\helfi_react_search\LinkedEvents $linkedEvents
* The linked events service.
*/
public function __construct(
protected readonly Servicemap $servicemap,
protected readonly LinkedEvents $linkedEvents,
) {
}
/**
* Returns a renderable array.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return array
* A renderable array.
*/
public function content(Request $request) : array|RedirectResponse {
$address = $request->query->get('q');
$return_url = Url::fromRoute('helfi_etusivu.helsinki_near_you');
if (!$address) {
$this->messenger()->addError($this->t('Please enter an address', [], ['context' => 'Helsinki near you']));
return $this->redirect('helfi_etusivu.helsinki_near_you');
}
$addressData = $this->getCoordinates(
Xss::filter(
urldecode($address)
)
);
if (!$addressData) {
$this->messenger()->addError(
$this->t(
'The address you input yielded no results. You may want to try a different address.',
[],
['context' => 'Helsinki near you']
)
);
return $this->redirect('helfi_etusivu.helsinki_near_you');
}
return [
'#attached' => [
'drupalSettings' => [
'helsinki_near_you' => [
'events_api_url' => $this->linkedEvents->getEventsRequest([
'dwithin_origin' => $addressData['coordinates'],
'dwithin_distance' => 1000,
]),
],
],
],
'#back_link_label' => $this->t('Edit address', [], ['context' => 'Helsinki near you']),
'#back_link_url' => $return_url,
'#cache' => [
'contexts' => ['url.query_args:q'],
],
'#coordinates' => $addressData ? $addressData['coordinates'] : NULL,
'#theme' => 'helsinki_near_you_results_page',
'#title' => $this->t(
'Services, events and news near your address @address',
['@address' => $addressData ? $this->resolveTranslation($addressData['address_translations']) : ''],
['context' => 'Helsinki near you']
),
];
}
/**
* Serves autocomplete suggestions for the search form.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The result as JSON.
*/
public function addressSuggestions(Request $request) : JsonResponse {
$q = $request->query->get('q');
$suggestions = [];
$results = $this->servicemap->query($q, 10);
foreach ($results as $result) {
$name = $this->resolveTranslation($result->name);
$suggestions[] = [
'label' => $name,
'value' => $name,
];
}
return new JsonResponse($suggestions);
}
/**
* Get coordinates from servicemap API.
*
* @param string $address
* The address.
*
* @return array
* The coordinates.
*/
protected function getCoordinates(string $address) : ?array {
$results = $this->servicemap->query($address);
if (
isset($results['0']->name) &&
isset($results['0']->location->coordinates)
) {
return [
'address_translations' => $results['0']->name,
'coordinates' => $results['0']->location->coordinates,
];
}
return NULL;
}
/**
* Resolves the translation string for given translation object.
*
* Returns the translation for the current language if it exists, otherwise
* returns the Finnish translation.
*
* @param \stdClass $translations
* The translations object.
*
* @return string
* The translated string.
*/
protected function resolveTranslation(\stdClass $translations) : string {
$langcode = $this->languageManager()->getCurrentLanguage()->getId();
return $translations->{"$langcode"} ?? $translations->fi;
}
}