Skip to content

Instantly share code, notes, and snippets.

@kolyadin
Created March 17, 2015 09:39
Show Gist options
  • Save kolyadin/df7cf05c9f8f3c3a52df to your computer and use it in GitHub Desktop.
Save kolyadin/df7cf05c9f8f3c3a52df to your computer and use it in GitHub Desktop.
<?php
namespace Hills\MapBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller {
public function mapAction() {
/** @var \Doctrine\ORM\EntityManager $em */
$em = $this->getDoctrine()->getManager();
$cities = $em->getRepository('HillsMapBundle:City')->findAll();
return $this->render('HillsMapBundle:Site:map.html.twig', [
'cities' => $cities
]);
}
/**
* Динамическая генерация файла /map.json для раздела "карта объектов"
* Этот файл используется front end
*
*
* @param Request $request
* @return JsonResponse
*/
public function mapJsonAction(Request $request) {
$geobaza = $this->get('hills.helper.geobaza');
/** @var \Doctrine\ORM\EntityManager $em */
$em = $this->getDoctrine()->getManager();
$out = [];
$objects = $em->getRepository('HillsMapBundle:Object')->findAll();
$cities = $em->getRepository('HillsMapBundle:City')->findAll();
foreach ($objects as $object) {
$metro = [];
foreach ($object->getSubway() as $subway) {
$metro[] = $subway->getId();
}
$out['objects'][] = [
'id' => $object->getId(),
'name' => $object->getName(),
'type' => $object->getType(),
'address' => $object->getAddress(),
'phone' => $object->getPhone(),
'fax' => $object->getFax(),
'operationTime' => $object->getOperationTime(),
'info' => $object->getInfo(),
'latitude' => $object->getLatitude(),
'longitude' => $object->getLongitude(),
'city' => $object->getCity()->getId(),
'metro' => $metro
];
}
foreach ($cities as $city) {
$metro = [];
foreach ($city->getSubways() as $station) {
$metro[] = [
'id' => $station->getId(),
'name' => $station->getName(),
'latitude' => $station->getLatitude(),
'longitude' => $station->getLongitude(),
];
}
$cityElement = [
'id' => $city->getId(),
'name' => $city->getName(),
];
if ($city->getName() == 'Москва') {
$cityElement['default'] = 'true';
}
if (count($metro)) {
$cityElement['metro'] = $metro;
}
$out['city'][] = $cityElement;
}
$out['currentLocation'] = $geobaza->getLocation('85.202.128.27');
return new JsonResponse($out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment