resq/src/Controller/HomeController.php

88 lines
2.5 KiB
PHP

<?php
namespace App\Controller;
use Catalyst\MenuBundle\Annotation\Menu;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RiderTracker;
use App\Service\GISManagerInterface;
use App\Entity\Rider;
class HomeController extends Controller
{
/**
* @Menu(selected="home")
*/
public function index(EntityManagerInterface $em, RiderTracker $rider_tracker,
GISManagerInterface $gis_manager)
{
// get map
$params['map_js_file'] = $gis_manager->getJSInitFile();
return $this->render('home.html.twig', $params);
}
public function getRiderLocations(EntityManagerInterface $em, RiderTracker $rider_tracker)
{
// get all riders
$riders = $em->getRepository(Rider::class)->findAll();
$locations = [];
foreach ($riders as $rider)
{
// get location for each rider
$rider_id = $rider->getID();
$coordinates = $rider_tracker->getRiderLocation($rider_id);
$lat = $coordinates->getLatitude();
$long = $coordinates->getLongitude();
$jo = $rider->getActiveJobOrder();
if ($jo == null)
{
$has_jo = false;
$clat = 0;
$clong = 0;
$jo_data = [];
}
else
{
$has_jo = true;
$cust_loc = $jo->getCoordinates();
$clat = $cust_loc->getLatitude();
$clong = $cust_loc->getLongitude();
$jo_id = $jo->getID();
$jo_data = [
'id' => $jo_id,
'status' => $jo->getStatusText(),
'stype' => $jo->getServiceTypeName(),
'url' => $this->generateUrl('jo_all_form', ['id' => $jo_id]),
'plate' => $jo->getCustomerVehicle()->getPlateNumber(),
'cname' => $jo->getCustomer()->getNameDisplay(),
];
}
// use rider map label as key
$rider_map_label = $rider->getMapLabel();
$locations[$rider_id] = [
'label' => $rider->getMapLabel(),
'loc' => [$lat, $long],
'has_jo' => $has_jo,
'cust_loc' => [$clat, $clong],
'jo' => $jo_data,
];
}
return $this->json([
'riders' => $locations,
]);
}
}