public function near($lat, $lon, $distance = 100000)
{
$key = 'events' . $lat . $lon . $distance;
$cache = new Nette\Caching\Cache($this->context->cacheStorage);
$ids = $cache->load($key);
$ids = NULL;
if ($ids === NULL) {
/* Vytáhnu si všechny koordináty */
$res = dibi::select('id, lat, lon')->from('subject')->where('deleted = 0')->and('map_coords !=""')->fetchAll();
/* Zjistím vzdálenost jednotlivých ID od středu */
$locs = array();
foreach ($res as $r => $n) {
$dist = Geolocation::getDistance($lat, $lon, $n['lat'], $n['lon']);
if ($dist <= $distance && $dist > 1) {
$locs[] = array('id' => $n['id'], 'dist' => $dist);
}
}
/* Nestliže nic nenajdu, tak vyhodím chybu. */
if (count($locs) == 0) {
throw new DibiException('There is no places', 1);
}
foreach ($locs as $key => $row) {
$ids[$key] = $row['id'];
$distances[$key] = $row['dist'];
}
foreach ($locs as $key => $row) {
$aa[$row['id']] = $row['dist'];
}
array_multisort($distances, SORT_ASC, $ids, SORT_ASC, $locs);
$cache->save($key, $ids, array(Nette\Caching\Cache::TAGS => array("events", "event", "distance"), Nette\Caching\Cache::EXPIRATION => '4 hours'));
}
$this->where('subject_id', $ids);
return $this;
}