xref: /webtrees/app/Statistics/Repository/PlaceRepository.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
218add1155SRico Sonntag
22c8db8a43SGreg Roachuse Fisharebest\Webtrees\Family;
238add1155SRico Sonntaguse Fisharebest\Webtrees\Gedcom;
248add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
25c8db8a43SGreg Roachuse Fisharebest\Webtrees\Individual;
26c8db8a43SGreg Roachuse Fisharebest\Webtrees\Location;
278add1155SRico Sonntaguse Fisharebest\Webtrees\Place;
288add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartDistribution;
29f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\Interfaces\IndividualRepositoryInterface;
308add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\PlaceRepositoryInterface;
3193ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CountryService;
328add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
338add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
348add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause;
358add1155SRico Sonntag
3671378461SGreg Roachuse function array_key_exists;
374c78e066SGreg Roachuse function arsort;
384c78e066SGreg Roachuse function end;
394c78e066SGreg Roachuse function explode;
404c78e066SGreg Roachuse function preg_match;
414c78e066SGreg Roachuse function trim;
424c78e066SGreg Roachuse function view;
4371378461SGreg Roach
448add1155SRico Sonntag/**
458add1155SRico Sonntag * A repository providing methods for place related statistics.
468add1155SRico Sonntag */
478add1155SRico Sonntagclass PlaceRepository implements PlaceRepositoryInterface
488add1155SRico Sonntag{
494c78e066SGreg Roach    private Tree $tree;
504c78e066SGreg Roach
514c78e066SGreg Roach    private CountryService $country_service;
528add1155SRico Sonntag
53f78da678SGreg Roach    private IndividualRepositoryInterface $individual_repository;
54f78da678SGreg Roach
558add1155SRico Sonntag    /**
568add1155SRico Sonntag     * @param Tree                          $tree
574c78e066SGreg Roach     * @param CountryService                $country_service
58f78da678SGreg Roach     * @param IndividualRepositoryInterface $individual_repository
598add1155SRico Sonntag     */
60f78da678SGreg Roach    public function __construct(
61f78da678SGreg Roach        Tree $tree,
62f78da678SGreg Roach        CountryService $country_service,
63f78da678SGreg Roach        IndividualRepositoryInterface $individual_repository
64f78da678SGreg Roach    ) {
658add1155SRico Sonntag        $this->tree                  = $tree;
664c78e066SGreg Roach        $this->country_service       = $country_service;
67f78da678SGreg Roach        $this->individual_repository = $individual_repository;
688add1155SRico Sonntag    }
698add1155SRico Sonntag
708add1155SRico Sonntag    /**
718add1155SRico Sonntag     * Places
728add1155SRico Sonntag     *
738add1155SRico Sonntag     * @param string $fact
748add1155SRico Sonntag     * @param string $what
758add1155SRico Sonntag     * @param bool   $country
768add1155SRico Sonntag     *
77ac701fbdSGreg Roach     * @return array<int|string,int>
788add1155SRico Sonntag     */
798add1155SRico Sonntag    private function queryFactPlaces(string $fact, string $what = 'ALL', bool $country = false): array
808add1155SRico Sonntag    {
818add1155SRico Sonntag        $rows = [];
828add1155SRico Sonntag
838add1155SRico Sonntag        if ($what === 'INDI') {
847aab89b4SGreg Roach            $rows = DB::table('individuals')
857aab89b4SGreg Roach                ->select(['i_gedcom as tree'])
867aab89b4SGreg Roach                ->where('i_file', '=', $this->tree->id())
877aab89b4SGreg Roach                ->where('i_gedcom', 'LIKE', "%\n2 PLAC %")
887aab89b4SGreg Roach                ->get()
897aab89b4SGreg Roach                ->all();
908add1155SRico Sonntag        } elseif ($what === 'FAM') {
917aab89b4SGreg Roach            $rows = DB::table('families')->select(['f_gedcom as tree'])
927aab89b4SGreg Roach                ->where('f_file', '=', $this->tree->id())
937aab89b4SGreg Roach                ->where('f_gedcom', 'LIKE', "%\n2 PLAC %")
947aab89b4SGreg Roach                ->get()
957aab89b4SGreg Roach                ->all();
968add1155SRico Sonntag        }
978add1155SRico Sonntag
988add1155SRico Sonntag        $placelist = [];
998add1155SRico Sonntag
1008add1155SRico Sonntag        foreach ($rows as $row) {
101d72b284aSGreg Roach            if (preg_match('/\n1 ' . $fact . '(?:\n[2-9].*)*\n2 PLAC (.+)/', $row->tree, $match)) {
1028add1155SRico Sonntag                if ($country) {
1038add1155SRico Sonntag                    $tmp   = explode(Gedcom::PLACE_SEPARATOR, $match[1]);
1048add1155SRico Sonntag                    $place = end($tmp);
1058add1155SRico Sonntag                } else {
1068add1155SRico Sonntag                    $place = $match[1];
1078add1155SRico Sonntag                }
1088add1155SRico Sonntag
1097aab89b4SGreg Roach                $placelist[$place] = ($placelist[$place] ?? 0) + 1;
1108add1155SRico Sonntag            }
1118add1155SRico Sonntag        }
1128add1155SRico Sonntag
1138add1155SRico Sonntag        return $placelist;
1148add1155SRico Sonntag    }
1158add1155SRico Sonntag
1168add1155SRico Sonntag    /**
1178add1155SRico Sonntag     * Query places.
1188add1155SRico Sonntag     *
1198add1155SRico Sonntag     * @param string $what
1208add1155SRico Sonntag     * @param string $fact
1218add1155SRico Sonntag     * @param int    $parent
1228add1155SRico Sonntag     * @param bool   $country
1238add1155SRico Sonntag     *
124f70bcff5SGreg Roach     * @return array<int|object>
1258add1155SRico Sonntag     */
1268add1155SRico Sonntag    public function statsPlaces(string $what = 'ALL', string $fact = '', int $parent = 0, bool $country = false): array
1278add1155SRico Sonntag    {
1288add1155SRico Sonntag        if ($fact) {
1298add1155SRico Sonntag            return $this->queryFactPlaces($fact, $what, $country);
1308add1155SRico Sonntag        }
1318add1155SRico Sonntag
1328add1155SRico Sonntag        $query = DB::table('places')
1330b5fd0a6SGreg Roach            ->join('placelinks', static function (JoinClause $join): void {
1348add1155SRico Sonntag                $join->on('pl_file', '=', 'p_file')
1358add1155SRico Sonntag                    ->on('pl_p_id', '=', 'p_id');
1368add1155SRico Sonntag            })
1378add1155SRico Sonntag            ->where('p_file', '=', $this->tree->id());
1388add1155SRico Sonntag
1398add1155SRico Sonntag        if ($parent > 0) {
1408add1155SRico Sonntag            // Used by placehierarchy map modules
1418add1155SRico Sonntag            $query->select(['p_place AS place'])
1428add1155SRico Sonntag                ->selectRaw('COUNT(*) AS tot')
1438add1155SRico Sonntag                ->where('p_id', '=', $parent)
1448add1155SRico Sonntag                ->groupBy(['place']);
1458add1155SRico Sonntag        } else {
1468add1155SRico Sonntag            $query->select(['p_place AS country'])
1478add1155SRico Sonntag                ->selectRaw('COUNT(*) AS tot')
1488add1155SRico Sonntag                ->where('p_parent_id', '=', 0)
1498add1155SRico Sonntag                ->groupBy(['country'])
1508add1155SRico Sonntag                ->orderByDesc('tot')
1518add1155SRico Sonntag                ->orderBy('country');
1528add1155SRico Sonntag        }
1538add1155SRico Sonntag
154c8db8a43SGreg Roach        if ($what === Individual::RECORD_TYPE) {
1550b5fd0a6SGreg Roach            $query->join('individuals', static function (JoinClause $join): void {
1568add1155SRico Sonntag                $join->on('pl_file', '=', 'i_file')
1578add1155SRico Sonntag                    ->on('pl_gid', '=', 'i_id');
1588add1155SRico Sonntag            });
159c8db8a43SGreg Roach        } elseif ($what === Family::RECORD_TYPE) {
1600b5fd0a6SGreg Roach            $query->join('families', static function (JoinClause $join): void {
1618add1155SRico Sonntag                $join->on('pl_file', '=', 'f_file')
1628add1155SRico Sonntag                    ->on('pl_gid', '=', 'f_id');
1638add1155SRico Sonntag            });
164c8db8a43SGreg Roach        } elseif ($what === Location::RECORD_TYPE) {
165c8db8a43SGreg Roach            $query->join('other', static function (JoinClause $join): void {
166c8db8a43SGreg Roach                $join->on('pl_file', '=', 'o_file')
167c8db8a43SGreg Roach                    ->on('pl_gid', '=', 'o_id');
168c8db8a43SGreg Roach            })
169c8db8a43SGreg Roach                ->where('o_type', '=', Location::RECORD_TYPE);
1708add1155SRico Sonntag        }
1718add1155SRico Sonntag
172936d28c8SRico Sonntag        return $query
173936d28c8SRico Sonntag            ->get()
174f70bcff5SGreg Roach            ->map(static function (object $entry) {
175936d28c8SRico Sonntag                // Map total value to integer
176936d28c8SRico Sonntag                $entry->tot = (int) $entry->tot;
177c8db8a43SGreg Roach
178936d28c8SRico Sonntag                return $entry;
179936d28c8SRico Sonntag            })
180936d28c8SRico Sonntag            ->all();
1818add1155SRico Sonntag    }
1828add1155SRico Sonntag
1838add1155SRico Sonntag    /**
1848add1155SRico Sonntag     * Get the top 10 places list.
1858add1155SRico Sonntag     *
186f78da678SGreg Roach     * @param array<int> $places
1878add1155SRico Sonntag     *
188f78da678SGreg Roach     * @return array<array<string,int|Place>>
1898add1155SRico Sonntag     */
1908add1155SRico Sonntag    private function getTop10Places(array $places): array
1918add1155SRico Sonntag    {
1928add1155SRico Sonntag        $top10 = [];
1938add1155SRico Sonntag        $i     = 0;
1948add1155SRico Sonntag
1958add1155SRico Sonntag        arsort($places);
1968add1155SRico Sonntag
1978add1155SRico Sonntag        foreach ($places as $place => $count) {
198ac701fbdSGreg Roach            $tmp     = new Place((string) $place, $this->tree);
1998add1155SRico Sonntag            $top10[] = [
2008add1155SRico Sonntag                'place' => $tmp,
2018add1155SRico Sonntag                'count' => $count,
2028add1155SRico Sonntag            ];
2038add1155SRico Sonntag
2048add1155SRico Sonntag            ++$i;
2058add1155SRico Sonntag
2068add1155SRico Sonntag            if ($i === 10) {
2078add1155SRico Sonntag                break;
2088add1155SRico Sonntag            }
2098add1155SRico Sonntag        }
2108add1155SRico Sonntag
2118add1155SRico Sonntag        return $top10;
2128add1155SRico Sonntag    }
2138add1155SRico Sonntag
2148add1155SRico Sonntag    /**
2158add1155SRico Sonntag     * Renders the top 10 places list.
2168add1155SRico Sonntag     *
217ac701fbdSGreg Roach     * @param array<int|string,int> $places
2188add1155SRico Sonntag     *
2198add1155SRico Sonntag     * @return string
2208add1155SRico Sonntag     */
2218add1155SRico Sonntag    private function renderTop10(array $places): string
2228add1155SRico Sonntag    {
2238add1155SRico Sonntag        $top10Records = $this->getTop10Places($places);
2248add1155SRico Sonntag
2258add1155SRico Sonntag        return view(
2268add1155SRico Sonntag            'statistics/other/top10-list',
2278add1155SRico Sonntag            [
2288add1155SRico Sonntag                'records' => $top10Records,
2298add1155SRico Sonntag            ]
2308add1155SRico Sonntag        );
2318add1155SRico Sonntag    }
2328add1155SRico Sonntag
2338add1155SRico Sonntag    /**
2348add1155SRico Sonntag     * A list of common birth places.
2358add1155SRico Sonntag     *
2368add1155SRico Sonntag     * @return string
2378add1155SRico Sonntag     */
2388add1155SRico Sonntag    public function commonBirthPlacesList(): string
2398add1155SRico Sonntag    {
2408add1155SRico Sonntag        $places = $this->queryFactPlaces('BIRT', 'INDI');
2418add1155SRico Sonntag        return $this->renderTop10($places);
2428add1155SRico Sonntag    }
2438add1155SRico Sonntag
2448add1155SRico Sonntag    /**
2458add1155SRico Sonntag     * A list of common death places.
2468add1155SRico Sonntag     *
2478add1155SRico Sonntag     * @return string
2488add1155SRico Sonntag     */
2498add1155SRico Sonntag    public function commonDeathPlacesList(): string
2508add1155SRico Sonntag    {
2518add1155SRico Sonntag        $places = $this->queryFactPlaces('DEAT', 'INDI');
2528add1155SRico Sonntag        return $this->renderTop10($places);
2538add1155SRico Sonntag    }
2548add1155SRico Sonntag
2558add1155SRico Sonntag    /**
2568add1155SRico Sonntag     * A list of common marriage places.
2578add1155SRico Sonntag     *
2588add1155SRico Sonntag     * @return string
2598add1155SRico Sonntag     */
2608add1155SRico Sonntag    public function commonMarriagePlacesList(): string
2618add1155SRico Sonntag    {
2628add1155SRico Sonntag        $places = $this->queryFactPlaces('MARR', 'FAM');
2638add1155SRico Sonntag        return $this->renderTop10($places);
2648add1155SRico Sonntag    }
2658add1155SRico Sonntag
2668add1155SRico Sonntag    /**
2678add1155SRico Sonntag     * A list of common countries.
2688add1155SRico Sonntag     *
2698add1155SRico Sonntag     * @return string
2708add1155SRico Sonntag     */
2718add1155SRico Sonntag    public function commonCountriesList(): string
2728add1155SRico Sonntag    {
2738add1155SRico Sonntag        $countries = $this->statsPlaces();
2748add1155SRico Sonntag
275320f6a24SGreg Roach        if ($countries === []) {
2768add1155SRico Sonntag            return '';
2778add1155SRico Sonntag        }
2788add1155SRico Sonntag
2798add1155SRico Sonntag        $top10 = [];
2808add1155SRico Sonntag        $i     = 1;
2818add1155SRico Sonntag
2828add1155SRico Sonntag        // Get the country names for each language
2838add1155SRico Sonntag        $country_names = [];
28490a2f718SGreg Roach        $old_language = I18N::languageTag();
28590a2f718SGreg Roach
2868add1155SRico Sonntag        foreach (I18N::activeLocales() as $locale) {
2878add1155SRico Sonntag            I18N::init($locale->languageTag());
28893ccd686SRico Sonntag            $all_countries = $this->country_service->getAllCountries();
2898add1155SRico Sonntag            foreach ($all_countries as $country_code => $country_name) {
2908add1155SRico Sonntag                $country_names[$country_name] = $country_code;
2918add1155SRico Sonntag            }
2928add1155SRico Sonntag        }
2938add1155SRico Sonntag
29490a2f718SGreg Roach        I18N::init($old_language);
2958add1155SRico Sonntag
2968add1155SRico Sonntag        $all_db_countries = [];
2978add1155SRico Sonntag        foreach ($countries as $place) {
2988add1155SRico Sonntag            $country = trim($place->country);
2996ccdf4f0SGreg Roach            if (array_key_exists($country, $country_names)) {
3008add1155SRico Sonntag                if (isset($all_db_countries[$country_names[$country]][$country])) {
3018add1155SRico Sonntag                    $all_db_countries[$country_names[$country]][$country] += (int) $place->tot;
3028add1155SRico Sonntag                } else {
3038add1155SRico Sonntag                    $all_db_countries[$country_names[$country]][$country] = (int) $place->tot;
3048add1155SRico Sonntag                }
3058add1155SRico Sonntag            }
3068add1155SRico Sonntag        }
3078add1155SRico Sonntag
3088add1155SRico Sonntag        // get all the user’s countries names
30993ccd686SRico Sonntag        $all_countries = $this->country_service->getAllCountries();
3108add1155SRico Sonntag
3118add1155SRico Sonntag        foreach ($all_db_countries as $country_code => $country) {
3128add1155SRico Sonntag            foreach ($country as $country_name => $tot) {
3138add1155SRico Sonntag                $tmp     = new Place($country_name, $this->tree);
3148add1155SRico Sonntag
3158add1155SRico Sonntag                $top10[] = [
3168add1155SRico Sonntag                    'place' => $tmp,
3178add1155SRico Sonntag                    'count' => $tot,
3188add1155SRico Sonntag                    'name'  => $all_countries[$country_code],
3198add1155SRico Sonntag                ];
3208add1155SRico Sonntag            }
3218add1155SRico Sonntag
3228add1155SRico Sonntag            if ($i++ === 10) {
3238add1155SRico Sonntag                break;
3248add1155SRico Sonntag            }
3258add1155SRico Sonntag        }
3268add1155SRico Sonntag
3278add1155SRico Sonntag        return view(
3288add1155SRico Sonntag            'statistics/other/top10-list',
3298add1155SRico Sonntag            [
3308add1155SRico Sonntag                'records' => $top10,
3318add1155SRico Sonntag            ]
3328add1155SRico Sonntag        );
3338add1155SRico Sonntag    }
3348add1155SRico Sonntag
3358add1155SRico Sonntag    /**
3368add1155SRico Sonntag     * Count total places.
3378add1155SRico Sonntag     *
3388add1155SRico Sonntag     * @return int
3398add1155SRico Sonntag     */
3408add1155SRico Sonntag    private function totalPlacesQuery(): int
3418add1155SRico Sonntag    {
3428add1155SRico Sonntag        return DB::table('places')
3438add1155SRico Sonntag            ->where('p_file', '=', $this->tree->id())
3448add1155SRico Sonntag            ->count();
3458add1155SRico Sonntag    }
3468add1155SRico Sonntag
3478add1155SRico Sonntag    /**
3488add1155SRico Sonntag     * Count total places.
3498add1155SRico Sonntag     *
3508add1155SRico Sonntag     * @return string
3518add1155SRico Sonntag     */
3528add1155SRico Sonntag    public function totalPlaces(): string
3538add1155SRico Sonntag    {
3548add1155SRico Sonntag        return I18N::number($this->totalPlacesQuery());
3558add1155SRico Sonntag    }
3568add1155SRico Sonntag
3578add1155SRico Sonntag    /**
3588add1155SRico Sonntag     * Create a chart showing where events occurred.
3598add1155SRico Sonntag     *
3608add1155SRico Sonntag     * @param string $chart_shows
3618add1155SRico Sonntag     * @param string $chart_type
3628add1155SRico Sonntag     * @param string $surname
3638add1155SRico Sonntag     *
3648add1155SRico Sonntag     * @return string
3658add1155SRico Sonntag     */
3668add1155SRico Sonntag    public function chartDistribution(
3678add1155SRico Sonntag        string $chart_shows = 'world',
3688add1155SRico Sonntag        string $chart_type = '',
3698add1155SRico Sonntag        string $surname = ''
3708add1155SRico Sonntag    ): string {
371f78da678SGreg Roach        return (new ChartDistribution($this->tree, $this->country_service, $this->individual_repository, $this))
37288de55fdSRico Sonntag            ->chartDistribution($chart_shows, $chart_type, $surname);
3738add1155SRico Sonntag    }
3748add1155SRico Sonntag}
375