Skip to content

Commit 0fe763c

Browse files
committed
Adds location-based search to calendars
1 parent b372e0a commit 0fe763c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Diff for: src/module/Phpug/src/Phpug/Api/v1/CalendarController.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
namespace Phpug\Api\v1;
3333

34+
use Phpug\ORM\Query\AST\Functions\DistanceFrom;
3435
use Zend\Mvc\Controller\AbstractActionController;
3536
use Sabre\VObject;
3637
use Zend\Json\Json;
@@ -64,10 +65,16 @@ public function listAction()
6465
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
6566
$result = $em->getRepository('Phpug\Entity\Cache')->findBy(array('type' => 'event'));
6667
$calendar = new VObject\Component\VCalendar();
68+
$affectedUGs = $this->findGroupsWithinRangeAndDistance();
6769
foreach ($result as $cal) {
6870
if (! $cal->getGRoup()) {
6971
continue;
7072
}
73+
74+
if ($affectedUGs && ! in_array($cal->getGroup()->getShortname(), $affectedUGs)) {
75+
continue;
76+
}
77+
7178
try {
7279
$ical = VObject\Reader::read($cal->getCache());
7380
foreach ($ical->children as $event) {
@@ -81,7 +88,7 @@ public function listAction()
8188
} catch(\Exception $e){}
8289

8390
}
84-
91+
8592
$viewModel = $this->getViewModel();
8693

8794
return $viewModel->setVariable('calendar', new \Phpug\Wrapper\SabreVCalendarWrapper($calendar));
@@ -114,4 +121,43 @@ protected function setAcceptHeaderAccordingToParameters()
114121

115122
return $this;
116123
}
124+
125+
protected function findGroupsWithinRangeAndDistance()
126+
{
127+
$lat = $this->params()->fromQuery('latitude', null);
128+
$lon = $this->params()->fromQuery('longitude', null);
129+
$distance = $this->params()->fromQuery('distance', null);
130+
$number = $this->params()->fromQuery('count', null);
131+
132+
if (! $lat || ! $lon) {
133+
return array();
134+
}
135+
/** @var \Doctrine\ORM\EntityManager $em */
136+
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
137+
DistanceFrom::setLatitudeField('latitude');
138+
DistanceFrom::setLongitudeField('longitude');
139+
DistanceFrom::setRadius(6367);
140+
$em->getConfiguration()->addCustomNumericFunction('DISTANCEFROM', 'Phpug\ORM\Query\AST\Functions\DistanceFrom');
141+
142+
$qs = 'SELECT p, DISTANCEFROM(' . (float) $lat . ',' . (float) $lon . ') AS distance FROM \Phpug\Entity\Usergroup p WHERE p.state = 1 ';
143+
144+
145+
if ($distance) {
146+
$qs .= ' AND DISTANCEFROM(' . (float) $lat . ',' . (float) $lon . ') <= ' . (float) $distance;
147+
}
148+
149+
$qs .= ' ORDER BY distance';
150+
151+
$query = $em->createQuery($qs);
152+
if ($number) {
153+
$query->setMaxResults($number);
154+
}
155+
156+
$res = array();
157+
foreach ($query->getResult() as $result) {
158+
$res[] = $result[0]->getShortname();
159+
}
160+
161+
return $res;
162+
}
117163
}

Diff for: src/module/Phpug/src/Phpug/Wrapper/SabreVCalendarWrapper.php

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function getEvents(\DateInterval $interval)
6262
$now = new \DateTime();
6363
$then = (new \DateTime())->add($interval);
6464
$this->object->expand($now, $then);
65+
$return = array();
6566
foreach ($this->object->children as $item) {
6667
if (! $item instanceof VEvent) {
6768
continue;

0 commit comments

Comments
 (0)