Commit 4357cc25 authored by Benjamin Bellamy's avatar Benjamin Bellamy 💬
Browse files

feat(map): display geolocated episodes on a map page

parent 652fa365
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -781,6 +781,12 @@ $routes->group('@(:podcastName)', function ($routes): void {
$routes->get('/credits', 'CreditsController', [
    'as' => 'credits',
]);
$routes->get('/map', 'MapMarkerController', [
    'as' => 'map',
]);
$routes->get('/episodes-markers', 'MapMarkerController::getEpisodesMarkers', [
    'as' => 'episodes-markers',
]);
$routes->get('/pages/(:slug)', 'PageController/$1', [
    'as' => 'page',
]);
+4 −4
Original line number Diff line number Diff line
@@ -124,8 +124,8 @@ class PodcastImportController extends BaseController
            if (property_exists($nsPodcast, 'location') && $nsPodcast->location !== null) {
                $location = new Location(
                    (string) $nsPodcast->location,
                    (string) $nsPodcast->location->attributes()['geo'],
                    (string) $nsPodcast->location->attributes()['osm'],
                    $nsPodcast->location->attributes()['geo'] === null ? null : (string) $nsPodcast->location->attributes()['geo'],
                    $nsPodcast->location->attributes()['osm'] === null ? null : (string) $nsPodcast->location->attributes()['osm'],
                );
            }
            if (property_exists($nsPodcast, 'guid') && $nsPodcast->guid !== null) {
@@ -338,8 +338,8 @@ class PodcastImportController extends BaseController
            if (property_exists($nsPodcast, 'location') && $nsPodcast->location !== null) {
                $location = new Location(
                    (string) $nsPodcast->location,
                    (string) $nsPodcast->location->attributes()['geo'],
                    (string) $nsPodcast->location->attributes()['osm'],
                    $nsPodcast->location->attributes()['geo'] === null ? null : (string) $nsPodcast->location->attributes()['geo'],
                    $nsPodcast->location->attributes()['osm'] === null ? null : (string) $nsPodcast->location->attributes()['osm'],
                );
            }

+59 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace App\Controllers;

use App\Entities\Page;
use App\Models\EpisodeModel;
use CodeIgniter\HTTP\ResponseInterface;

class MapMarkerController extends BaseController
{
    public function index(): string
    {
        $locale = service('request')
            ->getLocale();
        $cacheName = "page_map_{$locale}";
        if (! ($found = cache($cacheName))) {
            $found = view('map', [], [
                'cache' => DECADE,
                'cache_name' => $cacheName,
            ]);
        }
        return $found;
    }

    public function getEpisodesMarkers(): ResponseInterface
    {
        $cacheName = 'episodes_markers';
        if (! ($found = cache($cacheName))) {
            $episodes = (new EpisodeModel())->where('location_geo is not', null)
                ->findAll();
            $found = [];
            foreach ($episodes as $episode) {
                $found[] = [
                    'latitude' => $episode->location->latitude,
                    'longitude' => $episode->location->longitude,
                    'location_name' => $episode->location->name,
                    'location_url' => $episode->location->url,
                    'episode_link' => $episode->link,
                    'podcast_link' => $episode->podcast->link,
                    'image_path' => $episode->image->thumbnail_url,
                    'podcast_title' => $episode->podcast->title,
                    'episode_title' => $episode->title,
                ];
            }
            // The page cache is set to a decade so it is deleted manually upon episode update
            cache()
                ->save($cacheName, $found, DECADE);
        }
        return $this->response->setJSON($found);
    }
}
+12 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ use Config\Services;
 * @property string $name
 * @property string|null $geo
 * @property string|null $osm
 * @property double|null $latitude
 * @property double|null $longitude
 */
class Location extends Entity
{
@@ -34,12 +36,21 @@ class Location extends Entity
    public function __construct(
        protected string $name,
        protected ?string $geo = null,
        protected ?string $osm = null
        protected ?string $osm = null,
    ) {
        $latitude = null;
        $longitude = null;
        if ($geo !== null) {
            $geoArray = explode(',', substr($geo, 4));
            $latitude = floatval($geoArray[0]);
            $longitude = floatval($geoArray[1]);
        }
        parent::__construct([
            'name' => $name,
            'geo' => $geo,
            'osm' => $osm,
            'latitude' => $latitude,
            'longitude' => $longitude,
        ]);
    }

+3 −0
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ if (! function_exists('render_page_links')) {
        $links .= anchor(route_to('credits'), lang('Person.credits'), [
            'class' => 'px-2 underline hover:no-underline',
        ]);
        $links .= anchor(route_to('map'), lang('Page.map'), [
            'class' => 'px-2 underline hover:no-underline',
        ]);
        foreach ($pages as $page) {
            $links .= anchor($page->link, $page->title, [
                'class' => 'px-2 underline hover:no-underline',
Loading