Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-10773: Helsinki near you - Services block #793

Merged
merged 11 commits into from
Dec 17, 2024
Merged
1 change: 1 addition & 0 deletions public/modules/custom/helfi_etusivu/helfi_etusivu.module
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ function helfi_etusivu_theme() : array {
'back_link_url' => NULL,
'coordinates' => NULL,
'title' => NULL,
'service_groups' => NULL,
],
'template' => 'helsinki-near-you-results-page',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\helfi_etusivu\Enum\InternalSearchLink;
use Drupal\helfi_etusivu\Enum\ServiceMapLink;
use Drupal\helfi_etusivu\Servicemap;
use Drupal\helfi_react_search\LinkedEvents;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -36,6 +38,7 @@ public function __construct(
* Returns a renderable array.
*
* @param \Symfony\Component\HttpFoundation\Request $request
*
* The request.
*
* @return array
Expand All @@ -49,12 +52,8 @@ public function content(Request $request) : array|RedirectResponse {
$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)
)
);
$address = Xss::filter($address);
$addressData = $this->getCoordinates(urldecode($address));

if (!$addressData) {
$this->messenger()->addError(
Expand All @@ -67,6 +66,8 @@ public function content(Request $request) : array|RedirectResponse {
return $this->redirect('helfi_etusivu.helsinki_near_you');
}

$addressName = $this->resolveTranslation($addressData['address_translations']);

return [
'#attached' => [
'drupalSettings' => [
Expand All @@ -92,13 +93,93 @@ public function content(Request $request) : array|RedirectResponse {
'#cache' => [
'contexts' => ['url.query_args:q'],
],
'#coordinates' => $addressData ? $addressData['coordinates'] : NULL,
'#coordinates' => $addressData['coordinates'],
'#theme' => 'helsinki_near_you_results_page',
'#title' => $this->t(
'Services, events and news near your address @address',
['@address' => $addressData ? $this->resolveTranslation($addressData['address_translations']) : ''],
['@address' => $addressName],
['context' => 'Helsinki near you']
),
'#service_groups' => [
[
'title' => $this->t('Health is key', [], ['context' => 'Helsinki near you']),
'service_links' => [
[
'link_label' => $this->t('Your own health station', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::HEALTH_STATIONS,
$addressName,
),
],
[
'link_label' => $this->t('Closest maternity and child health clinic', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::CHILD_HEALTH_STATIONS,
$addressName,
),
],
],
],
[
'title' => $this->t('Grow and learn', [], ['context' => 'Helsinki near you']),
'service_links' => [
[
'link_label' => $this->t('Schools close to you', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::SCHOOLS,
$addressName,
),
],
[
'link_label' => $this->t('Closest playgrounds and family houses', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::PLAYGROUNDS_FAMILY_HOUSES,
$addressName,
),
],
[
'link_label' => $this->t('Closest daycare centres', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::DAYCARES,
$addressName,
),
],
],
],
[
'title' => $this->t('Move around the city', [], ['context' => 'Helsinki near you']),
'service_links' => [
[
'link_label' => $this->t('Roadway ploughing schedule', [], ['context' => 'Helsinki near you']),
'link_url' => $this->getInternalSearchLink(
InternalSearchLink::PLOWING_SCHEDULES,
$addressName,
),
],
[
'link_label' => $this->t('Roadworks and events on map', [], ['context' => 'Helsinki near you']),
'link_url' => $this->servicemap->getLink(ServiceMapLink::ROADWORK_EVENTS, $addressName),
],
[
'link_label' => $this->t('City bike stations', [], ['context' => 'Helsinki near you']),
'link_url' => $this->servicemap->getLink(ServiceMapLink::CITYBIKE_STATIONS_STANDS, $addressName),
],
],
],
[
'title' => $this->t('The city is developing', [], ['context' => 'Helsinki near you']),
'service_links' => [
[
'link_label' => $this->t('Street and park development', [], ['context' => 'Helsinki near you']),
'link_url' => $this->servicemap->getLink(ServiceMapLink::STREET_PARK_PROJECTS, $addressName),
],
[
'link_label' => $this->t('Plans in process', [], ['context' => 'Helsinki near you']),
'link_url' => $this->servicemap->getLink(ServiceMapLink::PLANS_IN_PROCESS, $addressName),
],
],
],
],
];
}

Expand Down Expand Up @@ -171,4 +252,29 @@ protected function resolveTranslation(\stdClass $translations) : string {
return $translations->{"$langcode"} ?? $translations->fi;
}

/**
* Generate link to internal search with address param set.
*
* @param \Drupal\helfi_etusivu\Enum\InternalSearchLink $link
* Internal search link option.
* @param string $address
* Address param for the link.
*
* @return string
* The resulting link.
*/
protected function getInternalSearchLink(
InternalSearchLink $link,
string $address,
) : string {
$langcode = $this->languageManager()->getCurrentLanguage()->getId();
$query = ['address' => urlencode($address)];
$url = Url::fromUri(
$link->getLinkTranslations()[$langcode],
['query' => $query],
);

return $url->toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_etusivu\Enum;

/**
* Enum class for internal search links.
*
* For now this class is just a "stupid" enum class with a list of links.
* This should probably be reworked into a more robust solution in the future.
*/
enum InternalSearchLink {
case HEALTH_STATIONS;
case CHILD_HEALTH_STATIONS;
case SCHOOLS;
case PLAYGROUNDS_FAMILY_HOUSES;
case DAYCARES;
case PLOWING_SCHEDULES;

/**
* Return array of link translations.
*
* @return array
* Array of link translations.
*/
public function getLinkTranslations() : array {
return match ($this) {
InternalSearchLink::HEALTH_STATIONS => [
'fi' => 'https://www.hel.fi/fi/sosiaali-ja-terveyspalvelut/terveydenhoito/terveysasemat',
'sv' => 'https://www.hel.fi/sv/social-och-halsovardstjanster/halsovard/halsostationer',
'en' => 'https://www.hel.fi/en/health-and-social-services/health-care/health-stations',
],
InternalSearchLink::CHILD_HEALTH_STATIONS => [
'fi' => 'https://www.hel.fi/fi/sosiaali-ja-terveyspalvelut/lasten-ja-perheiden-palvelut/aitiys-ja-lastenneuvolat',
'sv' => 'https://www.hel.fi/sv/social-och-halsovardstjanster/tjanster-for-barn-och-familjer/modra-och-barnradgivningarna',
'en' => 'https://www.hel.fi/en/health-and-social-services/child-and-family-services/maternity-and-child-health-clinics',
],
InternalSearchLink::SCHOOLS => [
'fi' => 'https://www.hel.fi/fi/kasvatus-ja-koulutus/perusopetus/peruskoulut',
'sv' => 'https://www.hel.fi/sv/fostran-och-utbildning/grundlaggande-utbildning/grundskolor',
'en' => 'https://www.hel.fi/en/childhood-and-education/basic-education/comprehensive-schools',
],
InternalSearchLink::PLAYGROUNDS_FAMILY_HOUSES => [
'fi' => 'https://www.hel.fi/fi/kasvatus-ja-koulutus/leikkipuistot/leikkipuistot-ja-perhetalot',
'sv' => 'https://www.hel.fi/sv/fostran-och-utbildning/lekparker/sok-lekparker-och-familjehus',
'en' => 'https://www.hel.fi/en/childhood-and-education/playgrounds/find-playgrounds-and-family-houses',
],
InternalSearchLink::DAYCARES => [
'fi' => 'https://www.hel.fi/fi/kasvatus-ja-koulutus/varhaiskasvatus/varhaiskasvatus-paivakodissa/etsi-kunnallisia-paivakoteja',
'sv' => 'https://www.hel.fi/sv/fostran-och-utbildning/smabarnspedagogik/smabarnspedagogik-pa-daghem/sok-kommunala-daghem',
'en' => 'https://www.hel.fi/en/childhood-and-education/early-childhood-education/early-childhood-education-in-daycare-centres/search-municipal-daycare-centres',
],
InternalSearchLink::PLOWING_SCHEDULES => [
'fi' => 'https://www.hel.fi/fi/kaupunkiymparisto-ja-liikenne/kunnossapito/katujen-kunnossapito/katujen-talvikunnossapito',
'sv' => 'https://www.hel.fi/sv/stadsmiljo-och-trafik/underhall/gatuunderhall/vinterunderhall-av-gator',
'en' => 'https://www.hel.fi/en/urban-environment-and-traffic/general-maintenance/street-maintenance/winter-street-maintenance',
],
};
}

}
31 changes: 31 additions & 0 deletions public/modules/custom/helfi_etusivu/src/Enum/ServiceMapLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_etusivu\Enum;

/**
* Enum class for service map links.
*/
enum ServiceMapLink {
case ROADWORK_EVENTS;
case CITYBIKE_STATIONS_STANDS;
case STREET_PARK_PROJECTS;
case PLANS_IN_PROCESS;

/**
* Get link param for service map.
*
* @return string
* Link param.
*/
public function link(): string {
return match($this) {
ServiceMapLink::ROADWORK_EVENTS => 'eCBuut',
ServiceMapLink::CITYBIKE_STATIONS_STANDS => 'eCAduu',
ServiceMapLink::STREET_PARK_PROJECTS => 'eCBJGT',
ServiceMapLink::PLANS_IN_PROCESS => 'eCCv3K',
};
}

}
39 changes: 38 additions & 1 deletion public/modules/custom/helfi_etusivu/src/Servicemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Drupal\Component\Utility\Xss;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\helfi_etusivu\Enum\ServiceMapLink;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Log\LoggerInterface;
Expand All @@ -17,11 +19,17 @@
final class Servicemap {

/**
* API URL.
* API URL for querying data.
*
* @var string
*/
private const API_URL = 'https://api.hel.fi/servicemap/v2/search/';
/**
* Site url for redirecting users.
*
* @var string
*/
private const SITE_URL = 'https://kartta.hel.fi/';

/**
* Constructs a new instance.
Expand Down Expand Up @@ -82,4 +90,33 @@ public function query(string $address, int $page_size = 1) : array {
return $result->results;
}

/**
* Generate link to servicemap view with predefined data visible.
*
* @param \Drupal\helfi_etusivu\Enum\ServiceMapLink $link
* Service map link option.
* @param string $address
* Address param for the link.
*
* @return string
* The resulting link.
*/
public function getLink(ServiceMapLink $link, string $address) : string {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
$query = [
'link' => $link->link(),
'addresslocation' => Xss::filter($address),
'setlanguage' => $langcode,
];

$url = Url::fromUri(
self::SITE_URL,
[
'query' => $query,
],
);

return $url->toString();
}

}
60 changes: 60 additions & 0 deletions public/modules/custom/helfi_etusivu/translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,63 @@ msgstr "Tapahtumat lähelläsi"
msgctxt "Helsinki near you"
msgid "Browse events near you, sorted by their start time"
msgstr "Tutustu tapahtumiin tapahtumisajan mukaisessa järjestyksessä."

msgctxt "Helsinki near you"
msgid "Health is key"
msgstr "Hoida terveyttä"

msgctxt "Helsinki near you"
msgid "Your own health station"
msgstr "Oma terveysasema"

msgctxt "Helsinki near you"
msgid "Closest maternity and child health clinic"
msgstr "Lähin neuvola"

msgctxt "Helsinki near you"
msgid "Grow and learn"
msgstr "Kasva ja opi"

msgctxt "Helsinki near you"
msgid "Schools close to you"
msgstr "Lähikoulut"

msgctxt "Helsinki near you"
msgid "Closest playgrounds and family houses"
msgstr "Lähimmät leikkipuistot ja perhetalot"

msgctxt "Helsinki near you"
msgid "Closest daycare centres"
msgstr "Lähimmät päiväkodit"

msgctxt "Helsinki near you"
msgid "Move around the city"
msgstr "Kulje kaupungissa"

msgctxt "Helsinki near you"
msgid "Roadway ploughing schedule"
msgstr "Aurausaikataulu"

msgctxt "Helsinki near you"
msgid "Roadworks and events on map"
msgstr "Katutyöt ja tapahtumat kartalla"

msgctxt "Helsinki near you"
msgid "City bike stations"
msgstr "Kaupunkipyöräasemat ja pyörätelineet"

msgctxt "Helsinki near you"
msgid "The city is developing"
msgstr "Kaupunki kehittyy"

msgctxt "Helsinki near you"
msgid "Street and park development"
msgstr "Katu- ja puistohankkeet"

msgctxt "Helsinki near you"
msgid "Plans in process"
msgstr "Valmisteilla oleva kaavat"

msgctxt "Helsinki near you"
msgid "Please enter an address"
msgstr "Syötä osoite"
Loading
Loading