Help With Sorting Subpages and Geo Plugin

Dear lovely Kirby-Lovers,

maybe straight forward → I am beginner to php and kirby helps me making my first steps.

Nun bin ich an eine Stelle geraten an der ich nicht mehr weiterkomme, darum schildere ich mein Problem mal hier, in der Hoffnung, dass mir der Kirby-Schwarm helfen kann.

Actually I am stuck at point and want to share my problem, hoping that the Kirby-Hive might help me get this thing to work.

I am using the GEO Plugin for a Job Market.

The page structure is →

  • Jobbörse (id=‘jobs’)
    • Standort-Niederlassung (spielt keine Rolle, soll nur erklären, warum ich auf die grandChildren zugreifen muss)
      • Job-Page (id=‘job-single’)

Every Job-Page has its own →joblatlng() field.

You can type the name of your city into the search field and it gets located by the GEO Plugin in the controller of the job market (jobs)

$data = get();
$searchgeo = geo::locate($data['geosearch']);

Using the radius filter only the matching grad child pages (job-single) will be displayed.

This looks as followed:

$jobresults = $page->grandChildren()->visible()->filterBy('joblatlng', 'radius', [
    'lat'    => $searchgeo->lat(),
    'lng'    => $searchgeo->lng(),
    'radius' => 30
]);

And this totally works fine until now.

That’s where we get to my problem, because now I want to sort this $jobresults Array with the result of the niceDistance-Method.
This Method calculates a “nice distance” between two points. In this case the localized point from the search bar and the →joblatlng() from every page inside the $jobresults array.

This is how i got it to work once, but it has a flaw:

function getGeoDistance($job)  {
  $data = get();
  $searchgeo = geo::locate($data['geosearch']);
  $job->geoDistance = $job->joblatlng()->niceDistance($searchgeo);

  return $job;
}

$jobresults = $page->grandChildren()->visible()->filterBy('joblatlng', 'radius', [
    'lat'    => $searchgeo->lat(),
    'lng'    => $searchgeo->lng(),
    'radius' => 30
]);->map('getGeoDistance')->sortBy('geoDistance', 'asc');

I try to map the “geoDistance()” filed manually to the the Job-Pages from $jobresults array to use the sortBy method with the callback.

The problem here is that the search will be slowly as hell, because the geo::locate function will trigger for each subpage inside the $jobresults array and that takes time. So I got the idea to save the once located value to an variable and give it to the function, but that doesn’t work.

That looks as followed:

$data = get();
$searchgeo = geo::locate($data['geosearch']);

function getGeoDistance($job)  {
  global $searchgeo
  $job->geoDistance = $job->joblatlng()->niceDistance($searchgeo);

  return $job;
}

$jobresults = $page->grandChildren()->visible()->filterBy('joblatlng', 'radius', [
    'lat'    => $searchgeo->lat(),
    'lng'    => $searchgeo->lng(),
    'radius' => 30
])->map('getGeoDistance')->sortBy('geoDistance', 'asc');

My next idea was to sort this Array manually afterwards:

$jobresults = $page->grandChildren()->visible()->filterBy('joblatlng', 'radius', [
    'lat'    => $searchgeo->lat(),
    'lng'    => $searchgeo->lng(),
    'radius' => 30
])

But I got no Idea how this will work. Google told me some tricks, but I was not able to reproduce it correctly.

What do you think?

Thank you very much for your help and your time! <3

Kindest regards
Mo