102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Ramcar\BaseController;
|
|
use App\Access\Generator;
|
|
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
|
|
use App\Entity\Outlet;
|
|
use GuzzleHttp\Client as GuzzleClient;
|
|
|
|
class TestController extends BaseController
|
|
{
|
|
public function index(Generator $acl_gen)
|
|
{
|
|
$params = $this->initParameters('home');
|
|
|
|
$acl_data = $acl_gen->getACL();
|
|
error_log(print_r($acl_data, true));
|
|
|
|
return $this->render('home.html.twig', $params);
|
|
}
|
|
|
|
public function testIsGranted()
|
|
{
|
|
$params = $this->initParameters('home');
|
|
|
|
error_log(print_r($this->isGranted('dashboard.menu'), true));
|
|
|
|
return $this->render('home.html.twig', $params);
|
|
}
|
|
|
|
public function gmap()
|
|
{
|
|
$params = $this->initParameters('home');
|
|
|
|
return $this->render('test/map.html.twig', $params);
|
|
}
|
|
|
|
protected function mapGetDistances(Point $point, $outlets)
|
|
{
|
|
$client = new GuzzleClient();
|
|
|
|
// origins
|
|
$origins_value = $point->getLatitude() . ',' . $point->getLongitude();
|
|
|
|
// destinations
|
|
$dests = [];
|
|
foreach ($outlets as $outlet)
|
|
{
|
|
$coord = $outlet->getCoordinates();
|
|
$dests[] = $coord->getLatitude() . ',' . $point->getLongitude();
|
|
}
|
|
$dests_value = implode('|', $dests);
|
|
|
|
// google maps base url api
|
|
$maps_url = 'https://maps.googleapis.com/maps/api/distancematrix/json';
|
|
|
|
// parameters
|
|
$gmaps_params = [
|
|
'origins' => $origins_value,
|
|
'destinations' => $dests_value,
|
|
];
|
|
|
|
error_log(print_r($gmaps_params, true));
|
|
|
|
// query google maps api
|
|
$res = $client->request('GET', $maps_url, ['query' => $gmaps_params]);
|
|
error_log($res->getBody());
|
|
|
|
return $res;
|
|
}
|
|
|
|
public function distance()
|
|
{
|
|
$params = $this->initParameters('home');
|
|
|
|
// $point = new Point(121.0495453, 14.6042567);
|
|
$point = new Point(120.343692, 16.048560);
|
|
|
|
// test if we can get the top 5 closest outlets to a point
|
|
$em = $this->getDoctrine()->getManager();
|
|
$query = $em->createQuery('SELECT o, st_distance(o.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Outlet o ORDER BY dist')
|
|
->setParameter('lat', $point->getLatitude())
|
|
->setParameter('lng', $point->getLongitude())
|
|
->setMaxResults(5);
|
|
error_log($query->getSql());
|
|
$result = $query->getResult();
|
|
|
|
$outlets = [];
|
|
foreach ($result as $row)
|
|
{
|
|
error_log($row[0]->getName() . ' - ' . $row['dist']);
|
|
$outlets[] = $row[0];
|
|
}
|
|
|
|
$this->mapGetDistances($point, $outlets);
|
|
|
|
return $this->render('home.html.twig', $params);
|
|
}
|
|
}
|