<?php
namespace AdminBundle\Controller;
use PafBundle\Entity\PafAddress;
use PafBundle\Entity\PafAddressUs;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class AutocompleteController extends BaseController
{
public function getAction(Request $request)
{
$data = $request->query->all();
$main_location = $this->getParameter('main_location');
try {
$search = $data['search'] ? $data['search'] : '';
$sphinxService = $this->get('sphinx.service');
if ($main_location == 'US') {
$results = $sphinxService->autocompleteUS($search);
if (empty($results)) {
$mapboxService = $this->get('mapbox.service');
$results = $mapboxService->autocomplete($search);
}
} else {
$sphinxService = $this->get('sphinx.service');
$results = $sphinxService->autocomplete($search);
}
return new JsonResponse($results);
} catch (\Exception $e) {
return new JsonResponse($e->getMessage());
}
}
public function updateUsageAddressAction(Request $request)
{
$data = $request->request->all();
$main_location = $this->getParameter('main_location');
if ($main_location === 'UK') {
$pafManager = $this->get('doctrine.orm.paf_entity_manager');
$class = PafAddress::class;
} else {
$pafManager = $this->get('doctrine.orm.paf_us_entity_manager');
$class = PafAddressUs::class;
}
try {
$pafAddress = $pafManager
->getRepository($class, 'paf')
->findOneBy(['addressKey' => $data['addressKey']])
;
if ($pafAddress) {
$usage = 0;
if ($main_location === 'UK') {
if ($pafAddress->getUsage() !== null) {
$usage = $pafAddress->getUsage() + 1;
}
$pafAddress->setUsage($usage);
} else {
if ($pafAddress->getUsed() !== null) {
$usage = $pafAddress->getUsed() + 1;
}
$pafAddress->setUsed($usage);
}
$pafManager->persist($pafAddress);
$pafManager->flush();
}
return new JsonResponse('true');
} catch (\Exception $e) {
return new JsonResponse('false');
}
}
}