app/Http/Controllers/AutocompleteController.php
private function placeSearch(Tree $tree, int $page, string $query, bool $create): array
    {
        $offset  = $page * self::RESULTS_PER_PAGE;
        $results = [];
        $found   = false;

        foreach ($this->search_service->searchPlaces($tree, $query) as $place) {
            $place_name = $place->gedcomName();
            if ($place_name === $query) {
                $found = true;
            }
            $results[] = [
                'id'    => $place_name,
                'text'  => $place_name,
                'title' => ' ',
            ];
        }

        // No place found? Use an external gazetteer
        if (empty($results) && $tree->getPreference('GEONAMES_ACCOUNT')) {
            $url =
                'http://api.geonames.org/searchJSON' .
                '?name_startsWith=' . urlencode($query) .
                '&lang=' . WT_LOCALE .
                '&fcode=CMTY&fcode=ADM4&fcode=PPL&fcode=PPLA&fcode=PPLC' .
                '&style=full' .
                '&username=' . $tree->getPreference('GEONAMES_ACCOUNT');
            // try to use curl when file_get_contents not allowed
            if (ini_get('allow_url_fopen')) {
                $json   = file_get_contents($url);
                $places = json_decode($json, true);
            } elseif (function_exists('curl_init')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $json   = curl_exec($ch);
                $places = json_decode($json, true);
                curl_close($ch);
            } else {
                $places = [];
            }
            if (isset($places['geonames']) && is_array($places['geonames'])) {
                foreach ($places['geonames'] as $k => $place) {
                    $place_name = $place['name'] . ', ' . $place['adminName2'] . ', ' . $place['adminName1'] . ', ' . $place['countryName'];
                    if ($place_name === $query) {
                        $found = true;
                    }
                    $results[] = [
                        'id'    => $place_name,
                        'text'  => $place_name,
                        'title' => ' ',
                    ];
                }
            }
        }

        // Include the query term in the results.  This allows the user to select a
        // place that doesn't already exist in the database.
        if (!$found && $create) {
            array_unshift($results, [
                'id'   => $query,
                'text' => $query,
            ]);
        }

        $more    = count($results) > $offset + self::RESULTS_PER_PAGE;
        $results = array_slice($results, $offset, self::RESULTS_PER_PAGE);

        return [
            'results'    => $results,
            'pagination' => [
                'more' => $more,
            ],
        ];
    }
private function placeSearch(Tree $tree, int $page, string $query, bool $create): array
    {
        $offset  = $page * self::RESULTS_PER_PAGE;
        $results = [];
        $found   = false;

        foreach ($this->search_service->searchPlaces($tree, $query) as $place) {
            $place_name = $place->gedcomName();
            if ($place_name === $query) {
                $found = true;
            }
            $results[] = [
                'id'    => $place_name,
                'text'  => $place_name,
                'title' => ' ',
            ];
        }

        // No place found? Use an external gazetteer
        if (empty($results) && $tree->getPreference('GEONAMES_ACCOUNT')) {
            $url =
                'http://api.geonames.org/searchJSON' .
                '?name_startsWith=' . urlencode($query) .
                '&lang=' . WT_LOCALE .
                '&fcode=CMTY&fcode=ADM4&fcode=PPL&fcode=PPLA&fcode=PPLC' .
                '&style=full' .
                '&username=' . $tree->getPreference('GEONAMES_ACCOUNT');
            // try to use curl when file_get_contents not allowed
            if (ini_get('allow_url_fopen')) {
                $json   = file_get_contents($url);
                $places = json_decode($json, true);
            } elseif (function_exists('curl_init')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $json   = curl_exec($ch);
                $places = json_decode($json, true);
                curl_close($ch);
            } else {
                $places = [];
            }
            if (isset($places['geonames']) && is_array($places['geonames'])) {
                foreach ($places['geonames'] as $k => $place) {
                    $place_name = $place['name'] . ', ' . $place['adminName2'] . ', ' . $place['adminName1'] . ', ' . $place['countryName'];
                    if ($place_name === $query) {
                        $found = true;
                    }
                    $results[] = [
                        'id'    => $place_name,
                        'text'  => $place_name,
                        'title' => ' ',
                    ];
                }
            }
        }

        // Include the query term in the results.  This allows the user to select a
        // place that doesn't already exist in the database.
        if (!$found && $create) {
            array_unshift($results, [
                'id'   => $query,
                'text' => $query,
            ]);
        }

        $more    = count($results) > $offset + self::RESULTS_PER_PAGE;
        $results = array_slice($results, $offset, self::RESULTS_PER_PAGE);

        return [
            'results'    => $results,
            'pagination' => [
                'more' => $more,
            ],
        ];
    }
private function placeSearch ( Tree $ tree , int $ page , string $ query , bool $ create ) : array { $ offset = $ page * self :: RESULTS_PER_PAGE ; $ results = [ ] ; $ found = false ; foreach ( $ this -> search_service -> searchPlaces ( $ tree , $ query ) as $ place ) { $ place_name = $ place -> gedcomName ( ) ; if ( $ place_name === $ query ) { $ found = true ; } $ results [ ] = [ 'id' => $ place_name , 'text' => $ place_name , 'title' => ' ' , ] ; } // No place found? Use an external gazetteer if ( empty ( $ results ) && $ tree -> getPreference ( 'GEONAMES_ACCOUNT' ) ) { $ url = 'http://api.geonames.org/searchJSON' . '?name_startsWith=' . urlencode ( $ query ) . '&lang=' . WT_LOCALE . '&fcode=CMTY&fcode=ADM4&fcode=PPL&fcode=PPLA&fcode=PPLC' . '&style=full' . '&username=' . $ tree -> getPreference ( 'GEONAMES_ACCOUNT' ) ; // try to use curl when file_get_contents not allowed if ( ini_get ( 'allow_url_fopen' ) ) { $ json = file_get_contents ( $ url ) ; $ places = json_decode ( $ json , true ) ; } elseif ( function_exists ( 'curl_init' ) ) { $ ch = curl_init ( ) ; curl_setopt ( $ ch , CURLOPT_URL , $ url ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 ) ; $ json = curl_exec ( $ ch ) ; $ places = json_decode ( $ json , true ) ; curl_close ( $ ch ) ; } else { $ places = [ ] ; } if ( isset ( $ places [ 'geonames' ] ) && is_array ( $ places [ 'geonames' ] ) ) { foreach ( $ places [ 'geonames' ] as $ k => $ place ) { $ place_name = $ place [ 'name' ] . ', ' . $ place [ 'adminName2' ] . ', ' . $ place [ 'adminName1' ] . ', ' . $ place [ 'countryName' ] ; if ( $ place_name === $ query ) { $ found = true ; } $ results [ ] = [ 'id' => $ place_name , 'text' => $ place_name , 'title' => ' ' , ] ; } } } // Include the query term in the results.  This allows the user to select a // place that doesn't already exist in the database. if ( ! $ found && $ create ) { array_unshift ( $ results , [ 'id' => $ query , 'text' => $ query , ] ) ; } $ more = count ( $ results ) > $ offset + self :: RESULTS_PER_PAGE ; $ results = array_slice ( $ results , $ offset , self :: RESULTS_PER_PAGE ) ; return [ 'results' => $ results , 'pagination' => [ 'more' => $ more , ] , ] ; }
Look up a place name.

@param Tree   $tree   Search this tree.
@param int    $page   Skip this number of pages.  Starts with zero.
@param string $query  Search terms.
@param bool   $create if true, include the query in the results so it can be created.

@return mixed[]
Look up a place name .